comparison rlgwebd.js @ 103:f30495f7ede8

RLG-Web server: refactor statusmsg() to work with WebSockets. Separate the game-listing and http-responding components of statusmsg() so the listing function can be reused for WebSockets.
author John "Elwin" Edwards <elwin@sdf.org>
date Fri, 13 Jul 2012 09:17:05 -0700
parents f34a286c51bd
children 7d444ba4739e
comparison
equal deleted inserted replaced
102:f34a286c51bd 103:f30495f7ede8
870 res.writeHead(200, { "Content-Type": "application/json" }); 870 res.writeHead(200, { "Content-Type": "application/json" });
871 res.write(JSON.stringify(msgs)); 871 res.write(JSON.stringify(msgs));
872 res.end(); 872 res.end();
873 } 873 }
874 874
875 function statusmsg(req, res) { 875 function getStatus(callback) {
876 var now = new Date(); 876 var now = new Date();
877 var reply = {"s": allowlogin, "g": []}; 877 var statusinfo = {"s": allowlogin, "g": []};
878 function respond() {
879 res.writeHead(200, { "Content-Type": "application/json" });
880 if (req.method != 'HEAD')
881 res.write(JSON.stringify(reply));
882 res.end();
883 }
884 function idleset(i, idletime) { 878 function idleset(i, idletime) {
885 if (i >= 0 && i < reply.g.length) { 879 if (i >= 0 && i < statusinfo.g.length) {
886 reply.g[i].i = idletime; 880 statusinfo.g[i].i = idletime;
887 } 881 }
888 for (var j = 0; j < reply.g.length; j++) { 882 for (var j = 0; j < statusinfo.g.length; j++) {
889 if (!("i" in reply.g[j])) 883 if (!("i" in statusinfo.g[j]))
890 return; 884 return;
891 } 885 }
892 respond(); 886 callback(statusinfo);
893 } 887 }
894 for (var sessid in sessions) { 888 for (var sessid in sessions) {
895 var gamedesc = {}; 889 var gamedesc = {};
896 gamedesc["n"] = sessid; 890 gamedesc["n"] = sessid;
897 gamedesc["p"] = sessions[sessid].pname; 891 gamedesc["p"] = sessions[sessid].pname;
898 gamedesc["g"] = sessions[sessid].game.uname; 892 gamedesc["g"] = sessions[sessid].game.uname;
899 reply["g"].push(gamedesc); 893 statusinfo["g"].push(gamedesc);
900 } 894 }
901 if (reply.g.length == 0) { 895 if (statusinfo.g.length == 0) {
902 respond(); 896 callback(statusinfo);
903 return; 897 return;
904 } 898 }
905 function makecallback(i) { 899 function makecallback(i) {
906 return function (err, stats) { 900 return function (err, stats) {
907 if (err) 901 if (err)
908 idleset(i, null); 902 idleset(i, null);
909 else 903 else
910 idleset(i, now - stats.mtime); 904 idleset(i, now - stats.mtime);
911 } 905 }
912 } 906 }
913 for (var i = 0; i < reply.g.length; i++) { 907 for (var i = 0; i < statusinfo.g.length; i++) {
914 if (reply.g[i].n in sessions) { 908 if (statusinfo.g[i].n in sessions) {
915 fs.fstat(sessions[reply.g[i].n].record.fd, makecallback(i)); 909 fs.fstat(sessions[statusinfo.g[i].n].record.fd, makecallback(i));
916 } 910 }
917 else { 911 else {
918 idleset(i, null); 912 idleset(i, null);
919 } 913 }
920 } 914 }
915 }
916
917 function statusmsg(req, res) {
918 function respond(info) {
919 res.writeHead(200, { "Content-Type": "application/json" });
920 if (req.method != 'HEAD')
921 res.write(JSON.stringify(info));
922 res.end();
923 }
924 getStatus(respond);
921 } 925 }
922 926
923 function pstatusmsg(req, res) { 927 function pstatusmsg(req, res) {
924 if (req.method == 'HEAD') { 928 if (req.method == 'HEAD') {
925 res.writeHead(200, { "Content-Type": "application/json" }); 929 res.writeHead(200, { "Content-Type": "application/json" });