# HG changeset patch # User John "Elwin" Edwards # Date 1338933102 25200 # Node ID c75fc4b1d13dfa9d22250335f838a8e03c236000 # Parent 7dd6becf9ce9d15ad7688ebbe1fb54313987b2fb rlgwebd.js: add a status interface. The RLG-Web server now makes its status and the list of current games available to a GET on /status. diff -r 7dd6becf9ce9 -r c75fc4b1d13d rlgwebd.js --- a/rlgwebd.js Tue Jun 05 09:31:49 2012 -0700 +++ b/rlgwebd.js Tue Jun 05 14:51:42 2012 -0700 @@ -54,10 +54,14 @@ */ function TermSession(game, user, files, dims) { /* First make sure starting the game will work. */ - if (!(game in games)) { + if (game in games) { + this.game = games[game]; + } + else { // TODO: throw an exception instead return null; } + this.player = user; /* This order seems to best avoid race conditions... */ this.alive = false; this.sessid = randkey(); @@ -86,7 +90,7 @@ childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); /* TODO handle tty-opening errors */ /* TODO make argument-finding into a method */ - args = [games[game].path, "-n", user.toString()]; + args = [this.game.path, "-n", user.toString()]; this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv}); var ss = this; this.alive = true; @@ -558,6 +562,21 @@ } } +function statusmsg(req, res) { + var reply = {"s": allowlogin, "g": []}; + for (var sessid in sessions) { + if (sessions[sessid].alive) { + var gamedesc = {}; + gamedesc["p"] = sessions[sessid].player; + gamedesc["g"] = sessions[sessid].game.name; + reply["g"].push(gamedesc); + } + } + res.writeHead(200, { "Content-Type": "application/json" }); + res.write(JSON.stringify(reply)); + res.end(); +} + var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", "Login failed", "Already playing", "Game launch failed", "Server shutting down" ]; @@ -637,10 +656,8 @@ } readFeed(res, cterm); } - /* Default page, create a new term */ - /* FIXME New term not created anymore, is a special case still needed? */ - else if (target == '/') { - serveStatic(req, res, "/"); + else if (target == '/status') { + statusmsg(req, res); } else /* Go look for it in the filesystem */ serveStatic(req, res, target);