Skip to content

Commit

Permalink
Verify that roomHello is requesting a supported version.
Browse files Browse the repository at this point in the history
  • Loading branch information
curfman committed Feb 17, 2016
1 parent cfffe9e commit c4eeea0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ type ArgError struct {
}

func (e ArgError) Error() string { return fmt.Sprintf("ARG.ERROR: %s", e.message) }

// VersionError describes a mismatch between requested and supported versions.
type VersionError struct {
message string
}

func (e VersionError) Error() string { return fmt.Sprintf("VERSION.ERROR: %s", e.message) }
2 changes: 1 addition & 1 deletion roomgoodbye.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Handles the "good-bye" request that is received each time
// a player leaves our room.
func handleGoodbye(conn *websocket.Conn, req *GameonRequest, room string) error {
func handleGoodbye(conn *websocket.Conn, req *GoodbyeMessage, room string) error {
locus := "GOODBYE"
checkpoint(locus, fmt.Sprintf("room=%s userid=%s username=%s\n",
MyRooms[room], req.UserId, req.Username))
Expand Down
32 changes: 27 additions & 5 deletions roomhello.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ type HelloResponse struct {
// Handles the "hello" request that is received each time
// a player enters our room. The incoming request is a string
// with the format <room>,<json>, where the JSON string contains
// the userid and username of the player entering our room. For
// example: "Room 3100",{"username": "ebullient","userId": "github:808713"}
// the version, userid and username of the player entering our room.
// For example:
// roomHello,43a4d07399ea23d648568c6d2d000b65,
// {"version": 1,"username": "DevUser","userId": "dummy.DevUser"}
//
// Return an error if a problem occurs, otherwise return nil.
func handleHello(conn *websocket.Conn, req *GameonRequest, room string) (e error) {
checkpoint("HELLO", fmt.Sprintf("room=%s userid=%s username=%s\n",
config.roomName, req.UserId, req.Username))
func handleHello(conn *websocket.Conn, req *HelloMessage, room string) (e error) {
locus := "HELLO"
checkpoint(locus, fmt.Sprintf("room=%s version=%d userid=%s username=%s\n",
MyRooms[room], req.Version, req.UserId, req.Username))

if versionSupported(req.Version) {
checkpoint(locus, fmt.Sprintf("VERSION=%d is supported.", req.Version))
} else {
checkpoint(locus, fmt.Sprintf("VERSION=%d is NOT supported.", req.Version))
conn.Close()
checkpoint(locus, "CONN.CLOSED")
e = VersionError{fmt.Sprintf("Version %d is not supported by this room.", req.Version)}
return
}

// Announce to the room and the player that the player has entered
// the room. Ignore errors for this.
Expand Down Expand Up @@ -75,3 +88,12 @@ func handleHello(conn *websocket.Conn, req *GameonRequest, room string) (e error
e = sendPlayerResp(conn, req.UserId, j)
return
}

func versionSupported(v int) bool {
for _, v := range SupportedVersions {
if v == v {
return true
}
}
return false
}
58 changes: 43 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ const (
lookAround = "look"
)

// roomHello,43a4d07399ea23d648568c6d2d000b65,
// {"version": 1,"username": "DevUser","userId": "dummy.DevUser"}
type HelloMessage struct {
Version int `json:"version,omitempty"`
UserId string `json:"userId,omitempty"`
Username string `json:"username,omitempty"`
}

// roomGoodbye,43a4d07399ea23d648568c6d2d000b65,
// {"username": "DevUser","userId": "dummy.DevUser"}
type GoodbyeMessage struct {
UserId string `json:"userId,omitempty"`
Username string `json:"username,omitempty"`
}

// room,43a4d07399ea23d648568c6d2d000b65,
// {"username":"DevUser","userId":"dummy.DevUser","content":"/examine book"}
type GameonRequest struct {
UserId string `json:"userId,omitempty"`
Username string `json:"username,omitempty"`
Expand Down Expand Up @@ -68,31 +85,42 @@ func roomHandler(w http.ResponseWriter, r *http.Request) {
checkpoint(locus, fmt.Sprintf("PARSE.ERROR err=%s", err.Error()))
continue
}
if config.debug {
fmt.Printf("PARSED cmd=%s\n", cmd)
fmt.Printf("PARSED room=%s\n", room)
fmt.Printf("PARSED JSON=%s\n", j)
}

var req GameonRequest
err = json.Unmarshal([]byte(j), &req)
if err != nil {
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR err=%s", err.Error()))
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR Offending JSON=%s", j))
continue
}
if config.debug {
fmt.Printf("roomHandler cmd='%s'\n", cmd)
fmt.Printf("roomHandler room='%s'\n", room)
fmt.Printf("roomHandler json='%s'\n", j)
checkpoint(locus, fmt.Sprintf("PARSE cmd=%s", cmd))
checkpoint(locus, fmt.Sprintf("PARSE room=%s", room))
checkpoint(locus, fmt.Sprintf("PARSE json=%s", j))
}

switch cmd {
case "roomHello":
var req HelloMessage
err = json.Unmarshal([]byte(j), &req)
if err != nil {
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR err=%s", err.Error()))
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR Offending JSON=%s", j))
continue
}
err = handleHello(conn, &req, room)

case "roomGoodbye":
var req GoodbyeMessage
err = json.Unmarshal([]byte(j), &req)
if err != nil {
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR err=%s", err.Error()))
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR Offending JSON=%s", j))
continue
}
err = handleGoodbye(conn, &req, room)

case "room":
var req GameonRequest
err = json.Unmarshal([]byte(j), &req)
if err != nil {
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR err=%s", err.Error()))
checkpoint(locus, fmt.Sprintf("JSON.UNMARSHALL.ERROR Offending JSON=%s", j))
continue
}
err = handleRoom(conn, &req, room)
default:
err = handleInvalidMessage(conn, payload)
Expand Down

0 comments on commit c4eeea0

Please sign in to comment.