Added encoding negociation to protocol

This commit is contained in:
Stephen McQuay 2014-03-10 00:04:45 -07:00
parent ed39916f17
commit 21705cc16e
2 changed files with 24 additions and 3 deletions

View File

@ -1,8 +1,9 @@
package main package main
import ( import (
"bitbucket.org/hackerbots/vector"
"fmt" "fmt"
"bitbucket.org/hackerbots/vector"
) )
type ClientConfig struct { type ClientConfig struct {
@ -22,6 +23,7 @@ type GameParam struct {
MaxPoints int `json:"max_points"` MaxPoints int `json:"max_points"`
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"` Type string `json:"type"`
Encoding string `json:"encoding"`
} }
type StatsRequest struct { type StatsRequest struct {

View File

@ -1,14 +1,15 @@
package main package main
import ( import (
"bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket"
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math" "math"
"math/rand" "math/rand"
"sync" "sync"
"bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket"
) )
const maxSpeed = 100 const maxSpeed = 100
@ -86,6 +87,22 @@ func (r *robot) negociate() (err error) {
if err != nil { if err != nil {
return err return err
} }
encodings := []string{}
err = websocket.JSON.Receive(r.ws, &encodings)
log.Printf("%+v", encodings)
desiredEncoding := "json"
for _, encoding := range encodings {
if "gob" == encoding {
desiredEncoding = encoding
break
}
}
err = websocket.JSON.Send(r.ws, desiredEncoding)
if err != nil {
return err
}
err = websocket.JSON.Receive(r.ws, &r.game) err = websocket.JSON.Receive(r.ws, &r.game)
if r.game.Type != "gameparam" { if r.game.Type != "gameparam" {
return errors.New("didn't receive a good gameparam") return errors.New("didn't receive a good gameparam")
@ -93,6 +110,8 @@ func (r *robot) negociate() (err error) {
if *verbose { if *verbose {
log.Printf("%s: game parameters: %+v", r.name, r.game) log.Printf("%s: game parameters: %+v", r.name, r.game)
} }
// TODO: do something with r.game.Protocol
log.Printf("game params: %+v", r.game)
conf := ClientConfig{ conf := ClientConfig{
ID: r.game.Name, ID: r.game.Name,