diff rlgwebd.js @ 32:c75fc4b1d13d

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.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 05 Jun 2012 14:51:42 -0700
parents 7dd6becf9ce9
children 57f4b36ef06b
line wrap: on
line diff
--- 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);