comparison rlgwebd.js @ 155:245a2959f504

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.
author John "Elwin" Edwards
date Tue, 01 Apr 2014 11:23:25 -0700
parents bac9c3b01692
children 127f9e256d02
comparison
equal deleted inserted replaced
154:b5a1430d0f71 155:245a2959f504
942 } 942 }
943 } 943 }
944 return null; 944 return null;
945 } 945 }
946 946
947 function startProgressWatcher() {
948 var watchdirs = [];
949 for (var gname in games) {
950 watchdirs.push(path.join("/dgldir/inprogress", gname));
951 }
952 var subproc = child_process.spawn("/bin/watcher", watchdirs);
953 subproc.stdout.setEncoding('utf8');
954 subproc.stdout.on('data', function (chunk) {
955 var fname = chunk.slice(2, -1);
956 var filere = /.*\/([^\/]*)\/([^\/:]*):(node:)?(.*)/;
957 var matchresult = fname.match(filere);
958 if (!matchresult || matchresult[3])
959 return;
960 gname = matchresult[1];
961 pname = matchresult[2];
962 if (chunk[0] == "E") {
963 tslog("DGL: %s is playing %s: %s", pname, gname, fname)
964 }
965 else if (chunk[0] == "C") {
966 tslog("DGL: %s started playing %s: %s", pname, gname, fname)
967 }
968 else if (chunk[0] == "D") {
969 tslog("DGL: %s finished playing %s: %s", pname, gname, fname)
970 }
971 else {
972 tslog("Watcher says: %s", chunk)
973 }
974 });
975 subproc.stdout.resume();
976 return subproc;
977 }
978
947 function serveStatic(req, res, fname) { 979 function serveStatic(req, res, fname) {
948 var nname = path.normalize(fname); 980 var nname = path.normalize(fname);
949 if (nname == "" || nname == "/") 981 if (nname == "" || nname == "/")
950 nname = "index.html"; 982 nname = "index.html";
951 if (nname.match(/\/$/)) 983 if (nname.match(/\/$/))
1372 allowlogin = false; 1404 allowlogin = false;
1373 tslog("Disconnecting..."); 1405 tslog("Disconnecting...");
1374 for (var sessid in sessions) { 1406 for (var sessid in sessions) {
1375 sessions[sessid].close(); 1407 sessions[sessid].close();
1376 } 1408 }
1409 progressWatcher.stdin.end("\n");
1377 setTimeout(shutdown, 2000); 1410 setTimeout(shutdown, 2000);
1378 } 1411 }
1379 } 1412 }
1380 1413
1381 process.on("exit", function () { 1414 process.on("exit", function () {
1394 process.exit(1); 1427 process.exit(1);
1395 } 1428 }
1396 1429
1397 var httpServer; // declare here so shutdown() can find it 1430 var httpServer; // declare here so shutdown() can find it
1398 var wsServer; 1431 var wsServer;
1432 var progressWatcher;
1399 1433
1400 /* This could be nonblocking, but nothing else can start yet anyway. */ 1434 /* This could be nonblocking, but nothing else can start yet anyway. */
1401 if (fs.existsSync(ctlsocket)) { 1435 if (fs.existsSync(ctlsocket)) {
1402 fs.unlinkSync(ctlsocket); 1436 fs.unlinkSync(ctlsocket);
1403 } 1437 }
1431 tslog('rlgwebd running on port %d', httpPort); 1465 tslog('rlgwebd running on port %d', httpPort);
1432 setInterval(reaper, playtimeout / 4); 1466 setInterval(reaper, playtimeout / 4);
1433 wsServer = new WebSocketServer({"httpServer": httpServer}); 1467 wsServer = new WebSocketServer({"httpServer": httpServer});
1434 wsServer.on("request", wsHandler); 1468 wsServer.on("request", wsHandler);
1435 tslog('WebSockets are online'); 1469 tslog('WebSockets are online');
1470 progressWatcher = startProgressWatcher();
1436 setInterval(pushStatus, 4000); 1471 setInterval(pushStatus, 4000);
1437 }); 1472 });
1438 1473