comparison rlgwebd.js @ 132:823e878e5840

Logins don't time out anymore. No more login timestamps either.
author John "Elwin" Edwards <elwin@sdf.org>
date Wed, 12 Sep 2012 15:48:41 -0700
parents 3e3824711791
children dcd07c1d846a
comparison
equal deleted inserted replaced
131:f8c89e1ed33a 132:823e878e5840
465 var lkey = wsReq.resourceURL.query["key"]; 465 var lkey = wsReq.resourceURL.query["key"];
466 if (!(lkey in logins)) { 466 if (!(lkey in logins)) {
467 wsReq.reject(404, errorcodes[1]); 467 wsReq.reject(404, errorcodes[1]);
468 return; 468 return;
469 } 469 }
470 logins[lkey].ts = new Date();
471 var pname = logins[lkey].name; 470 var pname = logins[lkey].name;
472 var dims = [wsReq.resourceURL.query.h, wsReq.resourceURL.query.w]; 471 var dims = [wsReq.resourceURL.query.h, wsReq.resourceURL.query.w];
473 function progcallback(err, fname) { 472 function progcallback(err, fname) {
474 if (fname) { 473 if (fname) {
475 wsReq.reject(404, errorcodes[4]); 474 wsReq.reject(404, errorcodes[4]);
640 tslog("Reaping session %s", session.sessid); 639 tslog("Reaping session %s", session.sessid);
641 /* Dissociate it with its login name. */ 640 /* Dissociate it with its login name. */
642 var sn = logins[session.key].sessions.indexOf(session.sessid); 641 var sn = logins[session.key].sessions.indexOf(session.sessid);
643 if (sn >= 0) { 642 if (sn >= 0) {
644 logins[session.key].sessions.splice(sn, 1); 643 logins[session.key].sessions.splice(sn, 1);
645 if (now - logins[session.key].ts > playtimeout)
646 logins[session.key].ts = new Date(now - playtimeout);
647 } 644 }
648 /* Shut it down. */ 645 /* Shut it down. */
649 session.close(); 646 session.close();
650 } 647 }
651 }); 648 });
652 } 649 }
653 for (var sessid in sessions) { 650 for (var sessid in sessions) {
654 reapcheck(sessions[sessid]); 651 reapcheck(sessions[sessid]);
655 } 652 }
656 /* HELPME this is about as clever as I can code, so I can't tell whether
657 * there are any bugs. */
658 for (var lkey in logins) { 653 for (var lkey in logins) {
659 if (logins[lkey].sessions.length == 0) { 654 if (logins[lkey].sessions.length > 0) {
660 /* A login with no current games can be killed for inactivity. */ 655 /* Check for games that have terminated normally, and remove them. */
661 if (now - logins[lkey].ts > playtimeout * 4) {
662 tslog("Login for %s (key %s) timed out", logins[lkey].name, lkey);
663 delete logins[lkey];
664 }
665 }
666 else {
667 /* Check for games that have terminated normally, and update
668 * the timestamp. */
669 var expired = []; 656 var expired = [];
670 var targarray = logins[lkey].sessions; 657 var targarray = logins[lkey].sessions;
671 /* Let's not find out what happens if you modify an array 658 /* Let's not find out what happens if you modify an array
672 * you're iterating through. */ 659 * you're iterating through. */
673 for (var i = 0; i < targarray.length; i++) { 660 for (var i = 0; i < targarray.length; i++) {
674 if (!(targarray[i] in sessions)) 661 if (!(targarray[i] in sessions))
675 expired.push(targarray[i]); 662 expired.push(targarray[i]);
676 } 663 }
677 if (expired.length > 0) { 664 if (expired.length > 0) {
678 logins[lkey].ts = new Date(now);
679 for (var j = 0; j < expired.length; j++) { 665 for (var j = 0; j < expired.length; j++) {
680 targarray.splice(targarray.indexOf(expired[j]), 1); 666 targarray.splice(targarray.indexOf(expired[j]), 1);
681 } 667 }
682 } 668 }
683 } 669 }
744 } 730 }
745 var lkey = String(formdata["key"]); 731 var lkey = String(formdata["key"]);
746 if (!(lkey in logins)) { 732 if (!(lkey in logins)) {
747 sendError(res, 1, null); 733 sendError(res, 1, null);
748 return; 734 return;
749 }
750 else {
751 logins[lkey].ts = new Date();
752 } 735 }
753 var username = logins[lkey].name; 736 var username = logins[lkey].name;
754 var gname = formdata["game"]; 737 var gname = formdata["game"];
755 // If dims are not given or invalid, the constructor will handle it. 738 // If dims are not given or invalid, the constructor will handle it.
756 var dims = [formdata["h"], formdata["w"]]; 739 var dims = [formdata["h"], formdata["w"]];
908 function stopgame(res, formdata) { 891 function stopgame(res, formdata) {
909 if (!("key" in formdata) || !(formdata["key"] in logins)) { 892 if (!("key" in formdata) || !(formdata["key"] in logins)) {
910 sendError(res, 1); 893 sendError(res, 1);
911 return; 894 return;
912 } 895 }
913 /* Update timestamp, because this is an authenticated action. */
914 logins[formdata["key"]].ts = new Date();
915 var pname = logins[formdata["key"]].name; 896 var pname = logins[formdata["key"]].name;
916 if (!("g" in formdata) || !(formdata["g"] in games)) { 897 if (!("g" in formdata) || !(formdata["g"] in games)) {
917 sendError(res, 2, "No such game."); 898 sendError(res, 2, "No such game.");
918 return; 899 return;
919 } 900 }