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.
This commit is contained in:
John "Elwin" Edwards 2012-06-04 14:21:41 -07:00
parent 8599ec081d
commit d0e3b1da2f
2 changed files with 44 additions and 8 deletions

View file

@ -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);

View file

@ -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/');