Selaa lähdekoodia

Added CLI arguments

Ronald Peterson 10 vuotta sitten
vanhempi
commit
4875b4d934
5 muutettua tiedostoa jossa 43 lisäystä ja 11 poistoa
  1. 4 1
      board/board.go
  2. 12 0
      flag/flag.go
  3. 3 3
      games/gameserver.go
  4. 22 5
      main.go
  5. 2 2
      stomp/stomp.go

+ 4 - 1
board/board.go

@@ -9,6 +9,9 @@ import (
 	"math/rand"
 	"net/http"
 	"time"
+	
+	
+	"battlecamp-go-server/flag"
 )
 
 var setMask [4]byte = [4]byte{0xFC, 0xF3, 0xCF, 0x3F}
@@ -122,7 +125,7 @@ func worker(workChan chan work, responseChan chan response) {
 			partialHeight = regionHeight
 		}
 
-		requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", w.width, w.height, partialWidth, partialHeight, w.x, w.y)
+		requestUrl := fmt.Sprintf("http://"+ *flag.BoardGenUrl + "/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", w.width, w.height, partialWidth, partialHeight, w.x, w.y)
 		log.Printf("Requested generation of tile with url: %v\n", requestUrl)
 
 		resp, err := http.Get(requestUrl)

+ 12 - 0
flag/flag.go

@@ -0,0 +1,12 @@
+package flag
+
+import "flag"
+
+var StompUrl, BoardGenUrl *string
+var Port *int
+
+func CreateFlags() {
+	Port = flag.Int("port", 8080, "The port of the battlecamp-go-server")
+	BoardGenUrl = flag.String("boardGen", "localhost:8081", "The url for the board region generator")
+	StompUrl = flag.String("stompUrl", "localhost:61613", "The url for active mq stomp")
+}

+ 3 - 3
games/gameserver.go

@@ -65,9 +65,9 @@ type stompGameStart struct {
 }
 
 func (games GameServer) AddGame(x, y int, stompConnection *extStomp.Conn) *Game {
-	game2 := NewGame(x, y)
+	game := NewGame(x, y)
 	addGame := addGameChan{
-		game: game2,
+		game: game,
 		id: make(chan int64),
 	}
 	
@@ -80,7 +80,7 @@ func (games GameServer) AddGame(x, y int, stompConnection *extStomp.Conn) *Game
 	}
 	b, _ := json.Marshal(stompGameStart)
 	stompConnection.Send("/topic/go-battlecamp.game", "application/json;charset=utf-8", b, extStomp.SendOpt.NoContentLength)
-	return game2
+	return game
 }
 
 func (games GameServer) ListGames() []*Game {

+ 22 - 5
main.go

@@ -2,15 +2,16 @@
 package main
 
 import (
+	"flag"
 	"fmt"
 	"log"
-	"math/rand"
 	"net/http"
 	"os"
-	"time"
+	"strconv"
 
 	extStomp "github.com/go-stomp/stomp"
 
+	bcFlag "battlecamp-go-server/flag"
 	"battlecamp-go-server/games"
 	"battlecamp-go-server/stomp"
 )
@@ -20,6 +21,17 @@ var stompConnection *extStomp.Conn
 
 func main() {
 	fmt.Println("Game server version 0.1")
+	
+	initLogging()
+
+	initCliFlags()
+
+	initStompConnection()
+
+	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*bcFlag.Port), newUrlRouter()))
+}
+
+func initLogging() {
 	logFile, err := os.Create("server.log")
 
 	if err == nil {
@@ -28,9 +40,14 @@ func main() {
 		log.Println("ERROR: Cannot open log file, using console.")
 		log.Printf("%v=n", err)
 	}
+}
+
+func initCliFlags() {
+	bcFlag.CreateFlags()
+	flag.Parse()
+}
 
-	rand.Seed(time.Now().UnixNano())
-	stompConnection = stomp.DailStomp()
+func initStompConnection() {
+	stompConnection = stomp.DailStomp(*bcFlag.StompUrl)
 	defer stompConnection.Disconnect()
-	log.Fatal(http.ListenAndServe(":8080", newUrlRouter()))
 }

+ 2 - 2
stomp/stomp.go

@@ -4,7 +4,7 @@ import (
 	"github.com/go-stomp/stomp"
 )
 
-func DailStomp() *stomp.Conn {
-	conn, _ := stomp.Dial("tcp", "localhost:61613")
+func DailStomp(url string) *stomp.Conn {
+	conn, _ := stomp.Dial("tcp", url)
 	return conn
 }