From d0e3b1da2fab5997ce474c08375aa50a6f835237 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Mon, 4 Jun 2012 14:21:41 -0700 Subject: [PATCH] rlgwebd.js: read commands from the console The RLG-Web server can now be controlled with commands sent to stdin. Currently, the only one implemented is "quit". Some improvements to the shutdown process were also made. --- rlgterm.js | 2 +- rlgwebd.js | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/rlgterm.js b/rlgterm.js index 7664add..ce703ba 100644 --- a/rlgterm.js +++ b/rlgterm.js @@ -132,7 +132,7 @@ function processMsg(msg) { if (!msgDict.t) return null; else if (msgDict.t == "E") { - if (msgDict.c == 1) { + if (msgDict.c == 1 || msgDict.c == 6) { logout(); } debug(1, "Server error: " + msgDict.s); diff --git a/rlgwebd.js b/rlgwebd.js index 648d1f6..81f0187 100755 --- a/rlgwebd.js +++ b/rlgwebd.js @@ -15,6 +15,7 @@ var dropToGID = 501; var serveStaticRoot = "/var/www/"; // inside the chroot var passwdfile = "/dgldir/dgl-login"; var sessions = {}; +var allowlogin = true; var games = { "rogue3": { @@ -198,8 +199,9 @@ function TermSession(game, user, files, dims) { ss.remove(); }; this.remove = function () { - delete sessions[ss.sessid]; - tslog("Session %s removed.", this.sessid); + var id = ss.sessid; + delete sessions[id]; + tslog("Session %s removed.", id); }; } @@ -305,6 +307,10 @@ function auth(username, password) { } function login(req, res, formdata) { + if (!allowlogin) { + sendError(res, 6, null); + return; + } if (!("game" in formdata)) { sendError(res, 2, "No game specified."); return; @@ -521,8 +527,14 @@ function readFeed(res, term) { if (result == null) { if (term.alive) reply.t = "n"; - else - reply.t = "q"; + else { + if (allowlogin) + reply.t = "q"; + else { + sendError(res, 6, null); + return; + } + } } else { reply.t = "d"; @@ -539,7 +551,8 @@ function readFeed(res, term) { } 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" ]; function sendError(res, ecode, msg) { res.writeHead(200, { "Content-Type": "text/plain" }); @@ -554,7 +567,7 @@ function sendError(res, ecode, msg) { res.end(); } -function handler(req, res) { +function webHandler(req, res) { /* default headers for the response */ var resheaders = {'Content-Type': 'text/html'}; /* The request body will be added to this as it arrives. */ @@ -635,6 +648,26 @@ function handler(req, res) { } +function shutdown () { + httpServer.close(); + httpServer.removeAllListeners('request'); + process.stdin.removeAllListeners('data'); + tslog("Shutting down..."); + process.exit(); +} + +function conHandler(chunk) { + var msg = chunk.toString().split('\n')[0]; + if (msg == "quit") { + allowlogin = false; + tslog("Disconnecting..."); + for (var sessid in sessions) { + sessions[sessid].close(); + } + setTimeout(shutdown, 10000); + } +} + process.on("exit", function () { for (var sessid in sessions) { if (sessions[sessid].alive) @@ -675,5 +708,8 @@ catch (err) { process.exit(1); } -http.createServer(handler).listen(8080, "127.0.0.1"); +process.stdin.on('data', conHandler); +process.stdin.resume(); +var httpServer = http.createServer(webHandler); +httpServer.listen(8080, "127.0.0.1"); tslog('rlgwebd running at http://127.0.0.1:8080/');