Parcourir la source

refactored stomp connection

Ronald Peterson il y a 10 ans
Parent
commit
170d69ee6c
5 fichiers modifiés avec 48 ajouts et 48 suppressions
  1. 13 15
      games/game.go
  2. 9 11
      games/gameserver.go
  3. 1 11
      main.go
  4. 19 4
      stomp/stomp.go
  5. 6 7
      urlrouter.go

+ 13 - 15
games/game.go

@@ -3,25 +3,24 @@ package games
 import (
 	"battlecamp-go-server/board"
 	"battlecamp-go-server/player"
-	"encoding/json"
+	"battlecamp-go-server/stomp"
 	"log"
 	"math/rand"
 	"os"
 	"runtime/pprof"
 	"sync"
 	"time"
-
-	"github.com/go-stomp/stomp"
 )
 
 type Game struct {
-	Id        int64            `json:"id"`
-	StartTime int64            `json:"startTime"`
-	EndTime   int64            `json:"endTime"`
-	Board     *board.Board     `json:"board"`
-	Players   []*player.Player `json:"-"`
-	mutex     sync.Mutex
-	Winner    *player.Player `json:"winner,omitempty"`
+	Id         int64            `json:"id"`
+	StartTime  int64            `json:"startTime"`
+	EndTime    int64            `json:"endTime"`
+	Board      *board.Board     `json:"board"`
+	Players    []*player.Player `json:"-"`
+	mutex      sync.Mutex
+	Winner     *player.Player `json:"winner,omitempty"`
+	gameServer *GameServer
 }
 
 func NewGame(cols, rows int) *Game {
@@ -78,7 +77,7 @@ type stompGameEnd struct {
 	Payload string `json:"payload"`
 }
 
-func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
+func (g *Game) Move(p *player.Player, direction string) bool {
 	if !(direction == "N" || direction == "W" || direction == "S" || direction == "E") {
 		log.Printf("Illigal direction %v", direction)
 		return false
@@ -111,8 +110,7 @@ func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
 		Players: g.Players,
 	}
 
-	b, _ := json.Marshal(up)
-	sc.Send("/topic/go-battlecamp.update", "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
+	stomp.SendJson("update", up)
 
 	if g.isWinner(p) {
 		g.EndTime = time.Now().Unix()
@@ -122,8 +120,8 @@ func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
 			GameId:  g.Id,
 			Payload: g.Winner.Id,
 		}
-		c, _ := json.Marshal(ge)
-		sc.Send("/topic/go-battlecamp.game", "application/json;charset=utf-8", c, stomp.SendOpt.NoContentLength)
+
+		stomp.SendJson("game", ge)
 	}
 
 	return true

+ 9 - 11
games/gameserver.go

@@ -1,9 +1,7 @@
 package games
 
 import (
-	"encoding/json"
-
-	extStomp "github.com/go-stomp/stomp"
+	"battlecamp-go-server/stomp"
 )
 
 type GameServer struct {
@@ -14,7 +12,7 @@ type GameServer struct {
 
 type addGameChan struct {
 	game *Game
-	id chan int64
+	id   chan int64
 }
 
 type gamesChan chan []*Game
@@ -64,22 +62,22 @@ type stompGameStart struct {
 	GameId int64  `json:"gameId"`
 }
 
-func (games GameServer) AddGame(x, y int, stompConnection *extStomp.Conn) *Game {
+func (games GameServer) AddGame(x, y int) *Game {
 	game := NewGame(x, y)
 	addGame := addGameChan{
 		game: game,
-		id: make(chan int64),
+		id:   make(chan int64),
 	}
-	
+
 	games.addGameChan <- addGame
-	gameId := <- addGame.id
-	
+	gameId := <-addGame.id
+
 	stompGameStart := stompGameStart{
 		Type:   "GAME_START",
 		GameId: gameId,
 	}
-	b, _ := json.Marshal(stompGameStart)
-	stompConnection.Send("/topic/go-battlecamp.game", "application/json;charset=utf-8", b, extStomp.SendOpt.NoContentLength)
+
+	stomp.SendJson("game", stompGameStart)
 	return game
 }
 

+ 1 - 11
main.go

@@ -9,15 +9,12 @@ import (
 	"os"
 	"strconv"
 
-	extStomp "github.com/go-stomp/stomp"
-
 	bcFlag "battlecamp-go-server/flag"
 	"battlecamp-go-server/games"
-	"battlecamp-go-server/stomp"
 )
 
 var currentGames games.GameServer = games.New()
-var stompConnection *extStomp.Conn
+
 
 func main() {
 	fmt.Println("Game server version 0.1")
@@ -26,8 +23,6 @@ func main() {
 
 	initCliFlags()
 
-	//initStompConnection()
-
 	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*bcFlag.Port), newUrlRouter()))
 }
 
@@ -46,8 +41,3 @@ func initCliFlags() {
 	bcFlag.CreateFlags()
 	flag.Parse()
 }
-
-func initStompConnection() {
-	stompConnection = stomp.DailStomp(*bcFlag.StompUrl)
-	defer stompConnection.Disconnect()
-}

+ 19 - 4
stomp/stomp.go

@@ -1,10 +1,25 @@
 package stomp
 
 import (
+	"encoding/json"
+	"log"
+
+	"battlecamp-go-server/flag"
+
 	"github.com/go-stomp/stomp"
 )
 
-func DailStomp(url string) *stomp.Conn {
-	conn, _ := stomp.Dial("tcp", url)
-	return conn
-}
+var stompConnection *stomp.Conn
+
+func getStompConnection() *stomp.Conn {
+	if stompConnection == nil {
+		stompConnection, _ = stomp.Dial("tcp", *flag.StompUrl)
+	}
+	return stompConnection
+}
+
+func SendJson(topic string, v interface{}) {
+	b, _ := json.Marshal(v)
+	log.Printf("Sending stomp to topic %v\n", topic)
+	getStompConnection().Send("/topic/go-battlecamp."+topic, "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
+}

+ 6 - 7
urlrouter.go

@@ -3,12 +3,12 @@ package main
 import (
 	"battlecamp-go-server/games"
 	"battlecamp-go-server/player"
+	"battlecamp-go-server/stomp"
 	"encoding/json"
 	"log"
 	"net/http"
 	"strconv"
-
-	"github.com/go-stomp/stomp"
+	
 	"github.com/julienschmidt/httprouter"
 )
 
@@ -65,9 +65,9 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	if xString != "" && yString != "" {
 		x, _ := strconv.Atoi(xString)
 		y, _ := strconv.Atoi(yString)
-		game = currentGames.AddGame(x, y, stompConnection)
+		game = currentGames.AddGame(x, y)
 	} else {
-		game = currentGames.AddGame(56, 35, stompConnection)
+		game = currentGames.AddGame(56, 35)
 	}
 
 	w.WriteHeader(http.StatusCreated)
@@ -89,8 +89,7 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	e := json.NewEncoder(w)
 	e.Encode(&p)
 
-	b, _ := json.Marshal(p)
-	stompConnection.Send("/topic/go-battlecamp.update", "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
+	stomp.SendJson("update", p)
 
 	log.Printf("Player %v joined game %v", p.Id, game.Id)
 }
@@ -129,7 +128,7 @@ func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	direction := ps.ByName("direction")
 
-	result := g.Move(p, direction, stompConnection)
+	result := g.Move(p, direction)
 
 	if !result {
 		http.Error(w, "Bad request", http.StatusBadRequest)