Mercurial > hg > rlgwebd
comparison rlgwebd.js @ 32:c75fc4b1d13d
rlgwebd.js: add a status interface.
The RLG-Web server now makes its status and the list of current games
available to a GET on /status.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Tue, 05 Jun 2012 14:51:42 -0700 |
parents | 7dd6becf9ce9 |
children | 57f4b36ef06b |
comparison
equal
deleted
inserted
replaced
31:7dd6becf9ce9 | 32:c75fc4b1d13d |
---|---|
52 * adds itself to the sessions dict. It currently assumes the user has | 52 * adds itself to the sessions dict. It currently assumes the user has |
53 * been authenticated. | 53 * been authenticated. |
54 */ | 54 */ |
55 function TermSession(game, user, files, dims) { | 55 function TermSession(game, user, files, dims) { |
56 /* First make sure starting the game will work. */ | 56 /* First make sure starting the game will work. */ |
57 if (!(game in games)) { | 57 if (game in games) { |
58 this.game = games[game]; | |
59 } | |
60 else { | |
58 // TODO: throw an exception instead | 61 // TODO: throw an exception instead |
59 return null; | 62 return null; |
60 } | 63 } |
64 this.player = user; | |
61 /* This order seems to best avoid race conditions... */ | 65 /* This order seems to best avoid race conditions... */ |
62 this.alive = false; | 66 this.alive = false; |
63 this.sessid = randkey(); | 67 this.sessid = randkey(); |
64 while (this.sessid in sessions) { | 68 while (this.sessid in sessions) { |
65 this.sessid = randkey(); | 69 this.sessid = randkey(); |
84 childenv[key] = process.env[key]; | 88 childenv[key] = process.env[key]; |
85 } | 89 } |
86 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); | 90 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); |
87 /* TODO handle tty-opening errors */ | 91 /* TODO handle tty-opening errors */ |
88 /* TODO make argument-finding into a method */ | 92 /* TODO make argument-finding into a method */ |
89 args = [games[game].path, "-n", user.toString()]; | 93 args = [this.game.path, "-n", user.toString()]; |
90 this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv}); | 94 this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv}); |
91 var ss = this; | 95 var ss = this; |
92 this.alive = true; | 96 this.alive = true; |
93 this.data = []; | 97 this.data = []; |
94 this.lock = files[0]; | 98 this.lock = files[0]; |
556 else { | 560 else { |
557 sendError(res, 1, null); | 561 sendError(res, 1, null); |
558 } | 562 } |
559 } | 563 } |
560 | 564 |
565 function statusmsg(req, res) { | |
566 var reply = {"s": allowlogin, "g": []}; | |
567 for (var sessid in sessions) { | |
568 if (sessions[sessid].alive) { | |
569 var gamedesc = {}; | |
570 gamedesc["p"] = sessions[sessid].player; | |
571 gamedesc["g"] = sessions[sessid].game.name; | |
572 reply["g"].push(gamedesc); | |
573 } | |
574 } | |
575 res.writeHead(200, { "Content-Type": "application/json" }); | |
576 res.write(JSON.stringify(reply)); | |
577 res.end(); | |
578 } | |
579 | |
561 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", | 580 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", |
562 "Login failed", "Already playing", "Game launch failed", | 581 "Login failed", "Already playing", "Game launch failed", |
563 "Server shutting down" ]; | 582 "Server shutting down" ]; |
564 | 583 |
565 function sendError(res, ecode, msg) { | 584 function sendError(res, ecode, msg) { |
635 sendError(res, 1, null); | 654 sendError(res, 1, null); |
636 return; | 655 return; |
637 } | 656 } |
638 readFeed(res, cterm); | 657 readFeed(res, cterm); |
639 } | 658 } |
640 /* Default page, create a new term */ | 659 else if (target == '/status') { |
641 /* FIXME New term not created anymore, is a special case still needed? */ | 660 statusmsg(req, res); |
642 else if (target == '/') { | |
643 serveStatic(req, res, "/"); | |
644 } | 661 } |
645 else /* Go look for it in the filesystem */ | 662 else /* Go look for it in the filesystem */ |
646 serveStatic(req, res, target); | 663 serveStatic(req, res, target); |
647 } | 664 } |
648 else { /* Some other method */ | 665 else { /* Some other method */ |