Begin support for watching dgamelaunch games.

watcher.c is a subprocess which uses inotify to watch the inprogress
directories.  A C program is used because node's fs.watch() can't tell
the difference between creation and deletion.
This commit is contained in:
John "Elwin" Edwards 2014-04-01 11:23:25 -07:00
parent e855ba2f58
commit f605244ae6
2 changed files with 136 additions and 0 deletions

View file

@ -944,6 +944,38 @@ function findClient(formdata, playersOnly) {
return null;
}
function startProgressWatcher() {
var watchdirs = [];
for (var gname in games) {
watchdirs.push(path.join("/dgldir/inprogress", gname));
}
var subproc = child_process.spawn("/bin/watcher", watchdirs);
subproc.stdout.setEncoding('utf8');
subproc.stdout.on('data', function (chunk) {
var fname = chunk.slice(2, -1);
var filere = /.*\/([^\/]*)\/([^\/:]*):(node:)?(.*)/;
var matchresult = fname.match(filere);
if (!matchresult || matchresult[3])
return;
gname = matchresult[1];
pname = matchresult[2];
if (chunk[0] == "E") {
tslog("DGL: %s is playing %s: %s", pname, gname, fname)
}
else if (chunk[0] == "C") {
tslog("DGL: %s started playing %s: %s", pname, gname, fname)
}
else if (chunk[0] == "D") {
tslog("DGL: %s finished playing %s: %s", pname, gname, fname)
}
else {
tslog("Watcher says: %s", chunk)
}
});
subproc.stdout.resume();
return subproc;
}
function serveStatic(req, res, fname) {
var nname = path.normalize(fname);
if (nname == "" || nname == "/")
@ -1374,6 +1406,7 @@ function conHandler(chunk) {
for (var sessid in sessions) {
sessions[sessid].close();
}
progressWatcher.stdin.end("\n");
setTimeout(shutdown, 2000);
}
}
@ -1396,6 +1429,7 @@ if (process.getuid() != 0) {
var httpServer; // declare here so shutdown() can find it
var wsServer;
var progressWatcher;
/* This could be nonblocking, but nothing else can start yet anyway. */
if (fs.existsSync(ctlsocket)) {
@ -1433,6 +1467,7 @@ ctlServer.listen(ctlsocket, function () {
wsServer = new WebSocketServer({"httpServer": httpServer});
wsServer.on("request", wsHandler);
tslog('WebSockets are online');
progressWatcher = startProgressWatcher();
setInterval(pushStatus, 4000);
});