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.
This commit is contained in:
John "Elwin" Edwards 2012-06-05 14:51:42 -07:00
parent 37e6447094
commit e0bbee0242

View file

@ -54,10 +54,14 @@ var games = {
*/ */
function TermSession(game, user, files, dims) { function TermSession(game, user, files, dims) {
/* First make sure starting the game will work. */ /* 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 // TODO: throw an exception instead
return null; return null;
} }
this.player = user;
/* This order seems to best avoid race conditions... */ /* This order seems to best avoid race conditions... */
this.alive = false; this.alive = false;
this.sessid = randkey(); this.sessid = randkey();
@ -86,7 +90,7 @@ function TermSession(game, user, files, dims) {
childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
/* TODO handle tty-opening errors */ /* TODO handle tty-opening errors */
/* TODO make argument-finding into a method */ /* 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}); this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv});
var ss = this; var ss = this;
this.alive = true; this.alive = true;
@ -558,6 +562,21 @@ function readFeed(res, term) {
} }
} }
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", var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
"Login failed", "Already playing", "Game launch failed", "Login failed", "Already playing", "Game launch failed",
"Server shutting down" ]; "Server shutting down" ];
@ -637,10 +656,8 @@ function webHandler(req, res) {
} }
readFeed(res, cterm); readFeed(res, cterm);
} }
/* Default page, create a new term */ else if (target == '/status') {
/* FIXME New term not created anymore, is a special case still needed? */ statusmsg(req, res);
else if (target == '/') {
serveStatic(req, res, "/");
} }
else /* Go look for it in the filesystem */ else /* Go look for it in the filesystem */
serveStatic(req, res, target); serveStatic(req, res, target);