added bw calcs

unfortunately there is an issue with how these are calculated, and they don't
decrease as things quiet down. Well, the bad numbers are their for your
consumption, and should Just Work TM when appropriate changes in
bitbucket.org/smcquay/bandwith land. And they will. But I'm tired now.
This commit is contained in:
sm
2014-03-03 22:56:26 -08:00
parent ecc70606a9
commit 6fd4138740
5 changed files with 73 additions and 15 deletions
+34 -1
View File
@@ -57,7 +57,13 @@ func startGame(w http.ResponseWriter, req *http.Request) {
g := games.get(requested_game_name)
if g == nil {
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
g = NewGame(requested_game_name, width, height, obstacles, conf.Tick, maxPoints, mode)
g, err := NewGame(requested_game_name, width, height, obstacles, conf.Tick, maxPoints, mode)
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
}
go g.run()
games.add(g)
} else {
@@ -136,6 +142,33 @@ func gameStats(w http.ResponseWriter, req *http.Request) {
}
}
func bw(w http.ResponseWriter, req *http.Request) {
// TODO: wrap this up in something similar to the JsonHandler to verify the
// url? Look at gorilla routing?
key, err := getGameId(req.URL.Path)
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)
games.RLock()
g, ok := games.m[key]
games.RUnlock()
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)
}
}
func stopGame(w http.ResponseWriter, req *http.Request) {
key, err := getGameId(req.URL.Path)
if err != nil {