2014-04-23 14:28:13 -07:00
|
|
|
package server
|
2013-08-28 23:46:12 -07:00
|
|
|
|
|
|
|
import (
|
2013-09-01 23:00:09 -07:00
|
|
|
"encoding/json"
|
2013-11-13 23:45:02 -08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-11-06 21:12:10 -08:00
|
|
|
"io/ioutil"
|
2013-09-28 23:00:29 -07:00
|
|
|
"log"
|
2013-08-28 23:46:12 -07:00
|
|
|
"net/http"
|
2014-03-08 15:29:08 -08:00
|
|
|
"os"
|
2013-09-28 23:00:29 -07:00
|
|
|
"runtime/pprof"
|
2013-09-08 09:32:24 -07:00
|
|
|
"strings"
|
2014-03-29 01:57:29 -07:00
|
|
|
"sync"
|
2013-08-28 23:46:12 -07:00
|
|
|
)
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// JsonHandler is a function type that allows setting the Content-Type
|
|
|
|
// appropriately for views destined to serve JSON
|
2013-08-28 23:46:12 -07:00
|
|
|
type JsonHandler func(http.ResponseWriter, *http.Request)
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// ServeHTTP is JsonHandler's http.Handler implementation.
|
2013-08-28 23:46:12 -07:00
|
|
|
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
h(w, req)
|
|
|
|
}
|
2013-09-01 23:00:09 -07:00
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// Controller is the shepherd of a collection of games. The main package in
|
2014-04-23 14:28:13 -07:00
|
|
|
// server simply populates one of these and starts an http server.
|
2014-03-18 23:38:06 -07:00
|
|
|
type Controller struct {
|
|
|
|
Idg *IdGenerator
|
|
|
|
Conf Config
|
|
|
|
Games MapLock
|
|
|
|
Memprofile string
|
|
|
|
Profile string
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// NewController takes a populated Config, and some parameters to determine
|
|
|
|
// what sorts of profiling to deal with and returns a freshly populated
|
|
|
|
// Controller.
|
2014-03-31 22:54:30 -07:00
|
|
|
func NewController(conf Config, mprof, pprof string) *Controller {
|
|
|
|
idg := NewIdGenerator()
|
|
|
|
return &Controller{
|
|
|
|
Idg: idg,
|
|
|
|
Conf: conf,
|
|
|
|
Games: MapLock{
|
|
|
|
M: make(map[string]*Game),
|
|
|
|
},
|
|
|
|
Memprofile: mprof,
|
|
|
|
Profile: pprof,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// TODO Eventually this thing will have a select loop for dealing with game
|
|
|
|
// access in a more lock-free manner?
|
2014-03-31 22:54:30 -07:00
|
|
|
func (c *Controller) Run() {
|
|
|
|
c.Idg.Run()
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// StartGame is the http route responsible for responding to requests to start
|
|
|
|
// games under this controller. Creates a default Config object, and populates
|
|
|
|
// it according to data POSTed.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) {
|
2013-10-14 00:17:12 -07:00
|
|
|
log.Println("asked to create a game")
|
2013-11-06 21:12:10 -08:00
|
|
|
|
2014-03-18 23:38:06 -07:00
|
|
|
requested_game_name := c.Idg.Hash()
|
2014-04-26 14:56:12 -07:00
|
|
|
width, height := float64(c.Conf.Width), float64(c.Conf.Height)
|
2014-04-26 08:56:40 -07:00
|
|
|
obstacleCount := 0
|
2014-03-18 23:38:06 -07:00
|
|
|
maxPoints := c.Conf.MaxPoints
|
2014-01-15 22:16:06 -08:00
|
|
|
mode := "deathmatch"
|
2013-11-06 21:12:10 -08:00
|
|
|
|
|
|
|
// here we determine if we are going to run with defaults or pick them off
|
|
|
|
// a posted json blob
|
|
|
|
if req.Method == "POST" {
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
2014-04-14 00:39:42 -07:00
|
|
|
log.Printf("unable to read request body: %v", err)
|
2013-11-06 21:12:10 -08:00
|
|
|
}
|
|
|
|
req.Body.Close()
|
2013-11-13 20:38:57 -08:00
|
|
|
cfg := struct {
|
2013-11-29 00:10:49 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Config
|
2013-11-06 21:12:10 -08:00
|
|
|
}{}
|
2013-11-13 20:38:57 -08:00
|
|
|
err = json.Unmarshal(body, &cfg)
|
2013-11-06 21:12:10 -08:00
|
|
|
if err != nil {
|
|
|
|
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-11-13 20:38:57 -08:00
|
|
|
requested_game_name = cfg.Name
|
2014-04-26 14:56:12 -07:00
|
|
|
width = float64(cfg.Width)
|
|
|
|
height = float64(cfg.Height)
|
2014-04-26 08:56:40 -07:00
|
|
|
obstacleCount = cfg.ObstacleCount
|
2013-11-29 00:10:49 -08:00
|
|
|
maxPoints = cfg.MaxPoints
|
2014-01-15 22:16:06 -08:00
|
|
|
mode = cfg.Mode
|
2013-11-06 21:12:10 -08:00
|
|
|
}
|
|
|
|
|
2014-03-29 02:00:09 -07:00
|
|
|
g := c.Games.Get(requested_game_name)
|
2013-11-06 21:12:10 -08:00
|
|
|
if g == nil {
|
|
|
|
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
|
2014-03-29 01:24:20 -07:00
|
|
|
var err error
|
2014-04-26 08:56:40 -07:00
|
|
|
g, err = NewGame(requested_game_name, width, height, obstacleCount, c.Conf.Tick, maxPoints, mode)
|
2014-03-03 21:57:39 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("problem creating game: %s: %s", requested_game_name, err)
|
|
|
|
b, _ := json.Marshal(NewFailure("game creation failure"))
|
|
|
|
http.Error(w, string(b), http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2013-11-06 21:12:10 -08:00
|
|
|
go g.run()
|
2014-03-29 02:00:09 -07:00
|
|
|
c.Games.Add(g)
|
2013-11-06 21:12:10 -08:00
|
|
|
} else {
|
2013-11-29 00:10:49 -08:00
|
|
|
log.Printf("Game '%s' already exists: %p", requested_game_name, g)
|
|
|
|
b, _ := json.Marshal(NewFailure("game already exists"))
|
|
|
|
http.Error(w, string(b), http.StatusConflict)
|
|
|
|
return
|
2013-10-18 23:55:52 -07:00
|
|
|
}
|
2013-09-01 23:00:09 -07:00
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
game_json := struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
2013-10-18 23:55:52 -07:00
|
|
|
Id: g.id,
|
2013-09-08 09:32:24 -07:00
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
if err := json.NewEncoder(w).Encode(game_json); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-09-08 09:32:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// ListGames makes a reasonable JSON response based on the games currently
|
|
|
|
// being run.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) ListGames(w http.ResponseWriter, req *http.Request) {
|
2013-10-14 00:17:12 -07:00
|
|
|
log.Println("games list requested")
|
2014-03-18 23:38:06 -07:00
|
|
|
c.Games.RLock()
|
|
|
|
defer c.Games.RUnlock()
|
2013-09-26 21:51:57 -07:00
|
|
|
type pout struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Id string `json:"id"`
|
|
|
|
}
|
2013-09-07 19:12:46 -07:00
|
|
|
type gl struct {
|
2013-09-26 21:51:57 -07:00
|
|
|
Id string `json:"id"`
|
|
|
|
Players []pout `json:"players"`
|
2013-09-07 19:12:46 -07:00
|
|
|
}
|
2013-09-05 23:31:24 -07:00
|
|
|
ids := make([]gl, 0)
|
2014-03-18 23:38:06 -07:00
|
|
|
for id, g := range c.Games.M {
|
2013-09-26 21:51:57 -07:00
|
|
|
players := make([]pout, 0)
|
2013-11-08 21:25:42 -08:00
|
|
|
// TODO - players instead of robots?
|
2013-11-09 21:52:03 -08:00
|
|
|
for p := range g.players {
|
2013-11-08 21:25:42 -08:00
|
|
|
for _, r := range p.Robots {
|
|
|
|
players = append(players, pout{
|
|
|
|
Name: r.Name,
|
|
|
|
Id: r.Id,
|
|
|
|
})
|
|
|
|
}
|
2013-09-07 19:12:46 -07:00
|
|
|
}
|
2013-09-05 23:31:24 -07:00
|
|
|
ids = append(ids, gl{
|
2013-09-07 19:12:46 -07:00
|
|
|
Id: id,
|
|
|
|
Players: players,
|
|
|
|
})
|
2013-09-01 23:00:09 -07:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(ids); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// GameStats provides an control mechanism to query for the stats of a single
|
|
|
|
// game
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) GameStats(w http.ResponseWriter, req *http.Request) {
|
2013-11-15 08:54:52 -08:00
|
|
|
// TODO: wrap this up in something similar to the JsonHandler to verify the
|
|
|
|
// url? Look at gorilla routing?
|
2014-03-18 23:38:06 -07:00
|
|
|
key, err := c.getGameId(req.URL.Path)
|
2013-11-13 23:45:02 -08:00
|
|
|
if err != nil {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
2013-11-13 23:45:02 -08:00
|
|
|
}
|
|
|
|
log.Printf("requested stats for game: %s", key)
|
2014-03-18 23:38:06 -07:00
|
|
|
c.Games.RLock()
|
|
|
|
g, ok := c.Games.M[key]
|
|
|
|
c.Games.RUnlock()
|
2013-11-13 23:45:02 -08:00
|
|
|
if !ok {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure("game not found"))
|
|
|
|
http.Error(w, string(b), http.StatusNotFound)
|
2013-09-28 12:59:04 -07:00
|
|
|
return
|
|
|
|
}
|
2014-01-16 00:02:59 -08:00
|
|
|
g.stats.RLock()
|
|
|
|
defer g.stats.RUnlock()
|
2014-01-16 00:13:02 -08:00
|
|
|
if err := json.NewEncoder(w).Encode(g.stats.PlayerStats); err != nil {
|
2013-11-13 23:45:02 -08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// BW provides a route to query for current bandwidth utilization for a single
|
|
|
|
// game.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) BW(w http.ResponseWriter, req *http.Request) {
|
2014-03-03 21:57:39 -08:00
|
|
|
// TODO: wrap this up in something similar to the JsonHandler to verify the
|
|
|
|
// url? Look at gorilla routing?
|
2014-03-18 23:38:06 -07:00
|
|
|
key, err := c.getGameId(req.URL.Path)
|
2014-03-03 21:57:39 -08:00
|
|
|
if err != nil {
|
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("requested bandwidth for game: %s", key)
|
2014-03-18 23:38:06 -07:00
|
|
|
c.Games.RLock()
|
|
|
|
g, ok := c.Games.M[key]
|
|
|
|
c.Games.RUnlock()
|
2014-03-03 21:57:39 -08:00
|
|
|
if !ok {
|
|
|
|
b, _ := json.Marshal(NewFailure("game not found"))
|
|
|
|
http.Error(w, string(b), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := map[string][]float64{
|
|
|
|
"tx": <-g.bw.Tx,
|
|
|
|
"rx": <-g.bw.Rx,
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(s); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// StopGame is the only mechanism to decrease the number of running games in
|
|
|
|
// a Controller
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) {
|
|
|
|
key, err := c.getGameId(req.URL.Path)
|
2013-11-13 23:45:02 -08:00
|
|
|
if err != nil {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
2013-11-13 23:45:02 -08:00
|
|
|
}
|
2014-03-18 23:38:06 -07:00
|
|
|
c.Games.Lock()
|
|
|
|
g, ok := c.Games.M[key]
|
|
|
|
defer c.Games.Unlock()
|
2013-09-28 12:59:04 -07:00
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
2013-11-13 23:45:02 -08:00
|
|
|
g.kill <- true
|
2014-03-29 01:12:55 -07:00
|
|
|
delete(c.Games.M, key)
|
2013-11-13 23:45:02 -08:00
|
|
|
message := struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}{
|
|
|
|
Ok: true,
|
|
|
|
Message: fmt.Sprintf("Successfully stopped game: %s", key),
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(message); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2014-03-29 01:12:55 -07:00
|
|
|
log.Printf("returning from StopGame")
|
2013-09-28 12:59:04 -07:00
|
|
|
}
|
2013-09-28 23:00:29 -07:00
|
|
|
|
2014-04-23 14:28:13 -07:00
|
|
|
// KillServer is my favorite method of all the methods in server: it shuts
|
2014-04-14 00:26:41 -07:00
|
|
|
// things down respecting profiling requests.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if c.Profile != "" {
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Print("trying to stop cpu profile")
|
2013-09-28 23:00:29 -07:00
|
|
|
pprof.StopCPUProfile()
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Print("stopped cpu profile")
|
2013-09-28 23:00:29 -07:00
|
|
|
}
|
2014-03-18 23:38:06 -07:00
|
|
|
if c.Memprofile != "" {
|
2014-03-08 15:29:08 -08:00
|
|
|
log.Print("trying to dump memory profile")
|
2014-03-18 23:38:06 -07:00
|
|
|
f, err := os.Create(c.Memprofile)
|
2014-03-08 15:29:08 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
f.Close()
|
|
|
|
log.Print("stopped memory profile dump")
|
|
|
|
}
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Fatal("shit got fucked up")
|
2013-09-28 23:00:29 -07:00
|
|
|
}
|
2013-11-07 22:05:20 -08:00
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// Index is the function for handling all traffic not officially in the API. It
|
|
|
|
// just lets people know that this is a hackerbots server running at
|
|
|
|
// a particular version.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) Index(w http.ResponseWriter, req *http.Request) {
|
2013-11-07 22:05:20 -08:00
|
|
|
log.Println("version requested")
|
|
|
|
version := struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}{
|
|
|
|
Version: "0.1.2",
|
|
|
|
Name: "Hackerbots",
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(version); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 23:45:02 -08:00
|
|
|
|
2014-04-14 00:26:41 -07:00
|
|
|
// getGameId trims the gameid off of the url. This is hokey, and makes me miss
|
|
|
|
// django regex-specified routes.
|
2014-03-18 23:38:06 -07:00
|
|
|
func (c *Controller) getGameId(path string) (string, error) {
|
2013-11-13 23:45:02 -08:00
|
|
|
var err error
|
|
|
|
trimmed := strings.Trim(path, "/")
|
|
|
|
fullPath := strings.Split(trimmed, "/")
|
|
|
|
if len(fullPath) != 3 {
|
|
|
|
return "", errors.New("improperly formed url")
|
|
|
|
}
|
|
|
|
key := fullPath[2]
|
|
|
|
return key, err
|
|
|
|
}
|
2014-03-29 01:57:29 -07:00
|
|
|
|
2014-03-29 02:00:09 -07:00
|
|
|
// MapLock is simply a map and a RWMutex
|
2014-04-14 00:26:41 -07:00
|
|
|
// TODO: obviate the need for this in Controller.Run
|
2014-03-29 01:57:29 -07:00
|
|
|
type MapLock struct {
|
|
|
|
M map[string]*Game
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// get is a function that returns a game if found, and creates one if
|
|
|
|
// not found and force is true. In order to get a hash (rather than use
|
|
|
|
// the string you pass) send "" for id.
|
2014-03-29 02:00:09 -07:00
|
|
|
func (ml *MapLock) Get(id string) *Game {
|
2014-03-29 01:57:29 -07:00
|
|
|
ml.Lock()
|
|
|
|
g, _ := ml.M[id]
|
|
|
|
ml.Unlock()
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2014-03-29 02:00:09 -07:00
|
|
|
// add is used to insert a new game into this MapLock
|
|
|
|
func (ml *MapLock) Add(g *Game) {
|
2014-03-29 01:57:29 -07:00
|
|
|
ml.Lock()
|
|
|
|
ml.M[g.id] = g
|
|
|
|
ml.Unlock()
|
|
|
|
}
|