annotate rlgwebd.js @ 104:7d444ba4739e

RLG-Web server: send status events over WebSockets. A WebSocket connection to /status will be sent periodic listings, along with notifications of the beginning and end of games.
author John "Elwin" Edwards <elwin@sdf.org>
date Fri, 13 Jul 2012 22:26:20 -0700
parents f30495f7ede8
children b64e31c5ec31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 // If you can't quite trust node to find it on its own
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var localModules = '/usr/local/lib/node_modules/';
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var http = require('http');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
6 var net = require('net');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 var fs = require('fs');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
10 var events = require('events');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11 var child_process = require('child_process');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 var daemon = require(path.join(localModules, "daemon"));
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
13 var pty = require(path.join(localModules, "pty.js"));
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
14 var WebSocketServer = require(path.join(localModules, "websocket")).server;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
16 /* Configuration variables */
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
17 // These first two files are NOT in the chroot.
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
18 var ctlsocket = "/var/local/rlgwebd/ctl";
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
19 var logfile = "/var/local/rlgwebd/log";
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
20 var httpPort = 8080;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 var chrootDir = "/var/dgl/";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 var dropToUID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
23 var dropToGID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 var serveStaticRoot = "/var/www/"; // inside the chroot
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
25 var playtimeout = 3600000; // Idle time before games are autosaved, in ms
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
26
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
27 /* Data on the games available. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
28 var games = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
29 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
30 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31 "uname": "rogue3",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
32 "suffix": ".r3sav",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
33 "path": "/bin/rogue3",
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
34 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
35 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
36 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 "uname": "rogue4",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
39 "suffix": ".r4sav",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
40 "path": "/bin/rogue4",
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
41 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 "uname": "rogue5",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
46 "suffix": ".r5sav",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
47 "path": "/bin/rogue5",
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
48 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 "uname": "srogue",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
53 "suffix": ".srsav",
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
54 "path": "/bin/srogue",
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
55 "clear": new Buffer([27, 91, 72, 27, 91, 74]) // CSI H CSI J
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
59 /* Global state */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
60 var logins = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
61 var sessions = {};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
62 var clients = {};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
63 var allowlogin = true;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
64 var nextsession = 0;
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
65 var gamemux = new events.EventEmitter();
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
66
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
67 /* Constructor. A TermSession handles a pty and the game running on it.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
68 * game: (String) Name of the game to launch.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
69 * lkey: (String, key) The user's id, a key into logins.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
70 * dims: (Array [Number, Number]) Height and width of the pty.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
71 * handlers: (Object) Key-value pairs, event names and functions to
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
72 * install to handle them.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
73 * Events:
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
74 * "open": Emitted on startup. Parameters: success (Boolean)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
75 * "data": Data generated by child. Parameters: buf (Buffer)
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
76 * "exit": Child terminated. Parameters: none
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
78 function TermSession(game, lkey, dims, handlers) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
79 var ss = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
80 /* Subclass EventEmitter to do the hard work. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
81 events.EventEmitter.call(this);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
82 for (var evname in handlers)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
83 this.on(evname, handlers[evname]);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
84 /* Don't launch anything that's not a real game. */
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
85 if (game in games) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
86 this.game = games[game];
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
87 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
88 else {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
89 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
90 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
91 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
92 if (lkey in logins) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
93 this.key = lkey;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
94 this.pname = logins[lkey].name;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
95 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
96 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
97 this.emit('open', false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
98 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 /* Grab a spot in the sessions table. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
101 this.sessid = nextsession++;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 sessions[this.sessid] = this;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
103 /* Set up the sizes. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
104 this.w = Math.floor(Number(dims[1]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
105 if (!(this.w > 0 && this.w < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
106 this.w = 80;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
107 this.h = Math.floor(Number(dims[0]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
108 if (!(this.h > 0 && this.h < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
109 this.h = 24;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
110 /* Environment. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
111 var childenv = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
112 for (var key in process.env) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
113 childenv[key] = process.env[key];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
114 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
115 var args = ["-n", this.pname];
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
116 var spawnopts = {"env": childenv, "cwd": "/", "rows": this.h, "cols": this.w,
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
117 "name": "xterm-256color"};
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
118 this.term = pty.spawn(this.game.path, args, spawnopts);
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
119 tslog("%s playing %s (index %d, pid %d)", this.pname, this.game.uname,
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
120 this.sessid, this.term.pid);
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
121 this.emit('open', true, this.sessid);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
122 gamemux.emit('begin', this.sessid, this.pname, this.game.uname);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
123 /* Set up the lockfile and ttyrec */
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
124 var ts = timestamp();
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
125 var progressdir = "/dgldir/inprogress-" + this.game.uname;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
126 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec");
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
127 var lmsg = this.term.pid.toString() + '\n' + this.w + '\n' + this.h + '\n';
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
128 fs.writeFile(this.lock, lmsg, "utf8");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
129 var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.game.uname,
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
130 ts + ".ttyrec");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
131 this.record = fs.createWriteStream(ttyrec, { mode: 0664 });
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
132 /* Holds the output since the last screen clear, so watchers can begin
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
133 * with a complete screen. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
134 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
135 this.frameoff = 0;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
136 logins[lkey].sessions.push(this.sessid);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 /* END setup */
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
138 function ttyrec_chunk(datastr) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 var ts = new Date();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
140 var buf = new Buffer(datastr);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 var chunk = new Buffer(buf.length + 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 /* TTYREC headers */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 chunk.writeUInt32LE(Math.floor(ts.getTime() / 1000), 0);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 chunk.writeUInt32LE(buf.length, 8);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 buf.copy(chunk, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 ss.record.write(chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
148 ss.framepush(buf);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
149 ss.emit('data', buf);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
151 this.term.on("data", ttyrec_chunk);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
152 this.framepush = function(chunk) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
153 /* If this chunk resets the screen, discard what preceded it. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
154 if (bufncmp(chunk, this.game.clear, this.game.clear.length)) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
155 this.framebuf = new Buffer(1024);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
156 this.frameoff = 0;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
157 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
158 /* Make sure there's space. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
159 while (this.framebuf.length < chunk.length + this.frameoff) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
160 var nbuf = new Buffer(this.framebuf.length * 2);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
161 this.framebuf.copy(nbuf, 0, 0, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
162 this.framebuf = nbuf;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
163 if (this.framebuf.length > 65536) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
164 tslog("Warning: Game %d frame buffer at %d bytes", this.sessid,
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
165 this.framebuf.length);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
166 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
167 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
168 chunk.copy(this.framebuf, this.frameoff);
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
169 this.frameoff += chunk.length;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
170 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
171 this.write = function(data) {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
172 this.term.write(data);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
173 };
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
174 // Teardown.
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
175 this.term.on("exit", function () {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
176 fs.unlink(ss.lock);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
177 ss.record.end();
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
178 ss.emit('exit');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
179 var id = ss.sessid;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
180 delete sessions[id];
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
181 tslog("Game %s ended.", id);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
182 gamemux.emit('end', id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 });
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
184 this.close = function () {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
185 this.term.kill('SIGHUP');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
186 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
187 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
188 TermSession.prototype = new events.EventEmitter();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
189
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
190 function Watcher(session) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
191 var ss = this; // that
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
192 this.session = session;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
193 this.alive = true;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
194 /* State for messaging. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
195 this.nsend = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
196 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
197 /* Get a place in the table. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
198 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
199 while (this.id in clients) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
200 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
201 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
202 clients[this.id] = this;
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
203 /* Recreate the current screen state from the session's buffer. */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
204 this.sendQ.push({"t": "d", "n": this.nsend++,
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
205 "d": session.framebuf.toString("hex", 0, session.frameoff)});
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
206 function dataH(buf) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
207 var reply = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
208 reply.t = "d";
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
209 reply.n = ss.nsend++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
210 reply.d = buf.toString("hex");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
211 ss.sendQ.push(reply);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
212 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
213 function exitH() {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
214 ss.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
215 ss.sendQ.push({"t": "q"});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
216 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
217 session.on('data', dataH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
218 session.on('exit', exitH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
219 this.read = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
220 /* Returns an array of all outstanding messages, empty if none. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
221 var temp = this.sendQ;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
222 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
223 /* Clean up if finished. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
224 if (!this.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
225 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
226 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
227 return temp;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
228 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
229 this.quit = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
230 this.session.removeListener('data', dataH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
231 this.session.removeListener('exit', exitH);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
232 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
233 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
234 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
235
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
236 function Player(gamename, lkey, dims, callback) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
237 var ss = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
238 this.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
239 /* State for messaging. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
240 this.nsend = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
241 this.nrecv = 0;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
242 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
243 this.recvQ = []
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
244 this.Qtimeout = null;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
245 /* Get a place in the table. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
246 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
247 while (this.id in clients) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
248 this.id = randkey(2);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
249 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
250 clients[this.id] = this;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
251
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
252 this.read = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
253 var temp = this.sendQ;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
254 this.sendQ = [];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
255 /* Clean up if finished. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
256 if (!this.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
257 clearTimeout(this.Qtimeout);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
258 delete clients[this.id];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
259 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
260 return temp;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
261 };
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
262 this.write = function (data, n) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
263 if (!this.alive || typeof (n) != "number") {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
264 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
265 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
266 var oindex = n - this.nrecv;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
267 if (oindex === 0) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
268 this.session.write(data);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
269 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
270 var next;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
271 while ((next = this.recvQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
272 this.session.write(next);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
273 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
274 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
275 if (this.recvQ.length == 0 && this.Qtimeout) {
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
276 clearTimeout(this.Qtimeout);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
277 this.Qtimeout = null;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
278 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
279 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
280 else if (oindex > 0 && oindex <= 1024) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
281 tslog("Client %s: Stashing message %d at %d", this.id, n, oindex - 1);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
282 this.recvQ[oindex - 1] = data;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
283 if (!this.Qtimeout) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
284 var nextn = this.nrecv + this.recvQ.length + 1;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
285 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
286 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
287 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
288 /* Otherwise, discard it */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
289 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
290 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
291 this.flushQ = function (client, n) {
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
292 /* Callback for when an unreceived message times out.
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
293 * n is the first empty space that will not be given up on. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
294 if (!client.alive || client.nrecv >= n)
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
295 return;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
296 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
297 var next;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
298 /* Clear the queue up to n */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
299 while (client.nrecv < n) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
300 next = client.recvQ.shift();
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
301 if (next !== undefined)
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
302 client.session.write(next);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
303 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
304 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
305 /* Clear out anything that's ready. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
306 while ((next = client.recvQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
307 client.session.write(next);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
308 client.nrecv++;
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
309 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
310 /* Now set another timeout if necessary. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
311 if (client.recvQ.length != 0) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
312 var nextn = client.nrecv + client.recvQ.length + 1;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
313 client.Qtimeout = setTimeout(client.flushQ, 30000, client, nextn);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
314 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
315 tslog("Flushing queue for player %s", player.id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
316 };
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
317 this.reset = function () {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
318 /* To be called when the game is taken over. */
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
319 if (this.Qtimeout) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
320 clearTimeout(this.Qtimeout);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
321 this.Qtimeout = null;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
322 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
323 for (var i = 0; i < this.recvQ.length; i++) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
324 if (this.recvQ[i] !== undefined) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
325 this.session.write(this.recvQ[i]);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
326 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
327 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
328 this.recvQ = [];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
329 this.nrecv = 0;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
330 this.nsend = 0;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
331 this.sendQ = [{"t": "d", "n": this.nsend++,
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
332 "d": this.session.framebuf.toString("hex", 0, this.session.frameoff)}];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
333 };
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
334 this.quit = function() {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
335 if (this.alive)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
336 this.session.close();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
337 };
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
338 function openH(success, id) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
339 if (success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
340 ss.alive = true;
84
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
341 ss.session = sessions[id];
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
342 ss.h = sessions[id].h;
d59dc5cef412 rlgwebd.js: set the width and height on Player objects.
John "Elwin" Edwards <elwin@sdf.org>
parents: 81
diff changeset
343 ss.w = sessions[id].w;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
344 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
345 callback(ss, success);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
346 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
347 function dataH(chunk) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
348 var reply = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
349 reply.t = "d";
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
350 reply.n = ss.nsend++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
351 reply.d = chunk.toString("hex");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
352 ss.sendQ.push(reply);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
353 }
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
354 function exitH() {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
355 ss.alive = false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
356 ss.sendQ.push({"t": "q"});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
357 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
358 var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
359 this.session = new TermSession(gamename, lkey, dims, handlers);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
362 // Also known as WebSocketAndTermSessionClosureGlueFactory
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
363 function wsWatcher(conn, session) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
364 var ss = this; // is this even needed?
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
365 var dataH = function(buf) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
366 conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
367 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
368 var exitH = function() {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
369 if (conn.connected)
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
370 conn.close();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
371 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
372 session.on('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
373 session.on('exit', exitH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
374 conn.on('close', function(code, desc) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
375 session.removeListener('data', dataH);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
376 session.removeListener('exit', exitH);
102
f34a286c51bd RLG-Web server: code cleanup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
377 if (session.sessid in sessions)
f34a286c51bd RLG-Web server: code cleanup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
378 tslog("A WebSocket watcher has left game %d", session.sessid);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
379 });
101
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
380 conn.sendUTF(JSON.stringify({
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
381 "t": "w", "w": session.w, "h": session.h,
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
382 "p": session.pname, "g": session.game.uname
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
383 }));
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
384 conn.sendUTF(JSON.stringify({"t": "d",
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
385 "d": session.framebuf.toString("hex", 0, session.frameoff)}));
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
386 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
387
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
388 /* Some functions which check whether a player is currently playing or
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
389 * has a saved game. Maybe someday they will provide information on
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
390 * the game. */
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
391 function checkprogress(user, game, callback, args) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
392 var progressdir = "/dgldir/inprogress-" + game.uname;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
393 fs.readdir(progressdir, function(err, files) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
394 if (err) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
395 args.unshift(err, null);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
396 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
397 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
398 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
399 var fre = RegExp("^" + user + ":");
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
400 for (var i = 0; i < files.length; i++) {
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
401 if (files[i].match(fre)) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
402 args.unshift(null, files[i]);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
403 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
404 return;
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
405 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
406 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
407 args.unshift(null, false);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
408 callback.apply(null, args);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
409 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
410 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
411
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
412 function checksaved(user, game, callback, args) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
413 var savedirc = game.uname + "save";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
414 var basename = String(dropToUID) + "-" + user + game.suffix;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
415 var savefile = path.join("/var/games/roguelike", savedirc, basename);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
416 fs.exists(savefile, function (exist) {
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
417 args.unshift(exist);
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
418 callback.apply(null, args);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
419 });
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
420 }
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
421
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
422 function playerstatus(user, callback) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
423 var sdata = {};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
424 function finishp() {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
425 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
426 if (!(gname in sdata))
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
427 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
428 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
429 callback(sdata);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
430 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
431 function regsaved(exists, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
432 if (exists)
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
433 sdata[game.uname] = "s";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
434 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
435 sdata[game.uname] = "0";
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
436 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
437 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
438 function regactive(err, filename, game) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
439 if (!err && filename) {
88
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
440 if (filename.match(/^[^:]*:node:/))
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
441 sdata[game.uname] = "p";
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
442 else
d644e7d46852 RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 87
diff changeset
443 sdata[game.uname] = "d";
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
444 finishp();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
445 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
446 else
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
447 checksaved(user, game, regsaved, [game]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
448 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
449 for (var gname in games) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
450 checkprogress(user, games[gname], regactive, [games[gname]]);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
451 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
452 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
453
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
454 /* A few utility functions */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
455 function timestamp() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
456 dd = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
457 sd = dd.toISOString();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
458 sd = sd.slice(0, sd.indexOf("."));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
459 return sd.replace("T", ".");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
460 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
461
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
462 function randkey(words) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
463 if (!words || !(words > 0))
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
464 words = 1;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
465 function rand32() {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
466 rnum = Math.floor(Math.random() * 65536 * 65536);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
467 hexstr = rnum.toString(16);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
468 while (hexstr.length < 8)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
469 hexstr = "0" + hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
470 return hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
471 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
472 var key = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
473 for (var i = 0; i < words; i++)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
474 key += rand32();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
475 return key;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
476 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
477
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
478 /* Compares two buffers, returns true for equality up to index n */
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
479 function bufncmp(buf1, buf2, n) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
480 if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2))
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
481 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
482 for (var i = 0; i < n; i++) {
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
483 if (i == buf1.length && i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
484 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
485 if (i == buf1.length || i == buf2.length)
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
486 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
487 if (buf1[i] != buf2[i])
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
488 return false;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
489 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
490 return true;
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
491 }
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
492
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
493 function tslog() {
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
494 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
495 console.log.apply(console, arguments);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
496 }
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
497
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
498 /* Returns a list of the cookies in the request, obviously. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
499 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
500 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
501 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
502 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
503 for (var i = 0; i < cookstrs.length; i++) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
504 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
505 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
506 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
507 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
508 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
509 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
510 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
511 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
512 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
513 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
514 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
515 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
516
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
517 function getMsg(posttext) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
518 var jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
519 if (!posttext)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
520 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
521 try {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
522 jsonobj = JSON.parse(posttext);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
523 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
524 catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
525 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
526 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
527 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
528 if (typeof(jsonobj) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
529 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
530 return jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
531 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
532
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
533 function reaper() {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
534 var now = new Date();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
535 function reapcheck(session) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
536 fs.fstat(session.record.fd, function (err, stats) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
537 if (!err && now - stats.mtime > playtimeout) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
538 tslog("Reaping session %s", session.sessid);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
539 /* Dissociate it with its login name. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
540 var sn = logins[session.key].sessions.indexOf(session.sessid);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
541 if (sn >= 0) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
542 logins[session.key].sessions.splice(sn, 1);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
543 if (now - logins[session.key].ts > playtimeout)
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
544 logins[session.key].ts = new Date(now - playtimeout);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
545 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
546 /* Shut it down. */
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
547 session.close();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
548 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
549 });
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
550 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
551 for (var sessid in sessions) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
552 reapcheck(sessions[sessid]);
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
553 }
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
554 /* HELPME this is about as clever as I can code, so I can't tell whether
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
555 * there are any bugs. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
556 for (var lkey in logins) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
557 if (logins[lkey].sessions.length == 0) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
558 /* A login with no current games can be killed for inactivity. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
559 if (now - logins[lkey].ts > playtimeout * 4) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
560 tslog("Login for %s (key %s) timed out", logins[lkey].name, lkey);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
561 delete logins[lkey];
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
562 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
563 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
564 else {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
565 /* Check for games that have terminated normally, and update
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
566 * the timestamp. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
567 var expired = [];
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
568 var targarray = logins[lkey].sessions;
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
569 /* Let's not find out what happens if you modify an array
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
570 * you're iterating through. */
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
571 for (var i = 0; i < targarray.length; i++) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
572 if (!(targarray[i] in sessions))
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
573 expired.push(targarray[i]);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
574 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
575 if (expired.length > 0) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
576 logins[lkey].ts = new Date(now);
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
577 for (var j = 0; j < expired.length; j++) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
578 targarray.splice(targarray.indexOf(expired[j]), 1);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
579 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
580 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
581 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
582 }
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
583 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
584
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
585 function login(req, res, formdata) {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
586 if (!allowlogin) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
587 sendError(res, 6, null, false);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
588 return;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
589 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
590 if (!("name" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
591 sendError(res, 2, "Username not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
592 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
593 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
594 else if (!("pw" in formdata)) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
595 sendError(res, 2, "Password not given.", false);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
596 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
597 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
598 var username = String(formdata["name"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
599 var password = String(formdata["pw"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
600 function checkit(code, signal) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
601 /* Checks the exit status, see sqlickrypt.c for details. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
602 if (code != 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
603 sendError(res, 3);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
604 if (code == 1)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
605 tslog("Password check failed for user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
606 else if (code == 2)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
607 tslog("Attempted login by nonexistent user %s", username);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
608 else
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
609 tslog("Login failed: sqlickrypt error %d", code);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
610 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
611 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
612 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
613 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
614 lkey = randkey(2);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
615 logins[lkey] = {"name": username, "ts": new Date(), "sessions": []};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
616 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
617 var reply = {"t": "l", "k": lkey, "u": username};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
618 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
619 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
620 tslog("%s has logged in (key %s)", username, lkey);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
621 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
622 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
623 /* Launch the sqlickrypt utility to check the password. */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
624 var pwchecker = child_process.spawn("/bin/sqlickrypt", ["check"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
625 pwchecker.on("exit", checkit);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
626 pwchecker.stdin.end(username + '\n' + password + '\n', "utf8");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
627 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
628 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
629
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
630 function startgame(req, res, formdata) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
631 if (!allowlogin) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
632 sendError(res, 6, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
633 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
634 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
635 if (!("key" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
636 sendError(res, 2, "No key given.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
637 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
638 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
639 else if (!("game" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
640 sendError(res, 2, "No game specified.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
641 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
642 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
643 var lkey = String(formdata["key"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
644 if (!(lkey in logins)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
645 sendError(res, 1, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
646 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
647 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
648 else {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
649 logins[lkey].ts = new Date();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
650 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
651 var username = logins[lkey].name;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
652 var gname = formdata["game"];
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
653 // If dims are not given or invalid, the constructor will handle it.
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
654 var dims = [formdata["h"], formdata["w"]];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
655 if (!(gname in games)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
656 sendError(res, 2, "No such game: " + gname);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
657 tslog("Request for nonexistant game \"%s\"", gname);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
658 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
659 }
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
660 // A callback to pass to the game-in-progress checker.
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
661 var launch = function(err, fname) {
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
662 var nodematch = new RegExp("^" + username + ":node:");
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
663 if (fname && (fname.match(nodematch) === null)) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
664 /* It's being played in dgamelaunch. */
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
665 sendError(res, 4, null);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
666 tslog("%s is already playing %s", username, gname);
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
667 return;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
668 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
669 // Game starting has been approved.
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
670 var respondlaunch = function(nclient, success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
671 if (success) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
672 res.writeHead(200, {'Content-Type': 'application/json'});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
673 var reply = {"t": "s", "id": nclient.id, "w": nclient.w, "h":
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
674 nclient.h, "p": username, "g": gname};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
675 res.write(JSON.stringify(reply));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
676 res.end();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
677 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
678 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
679 sendError(res, 5, "Failed to open TTY");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
680 tslog("Unable to allocate TTY for %s", gname);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
681 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
682 };
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
683 if (fname) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
684 for (var cid in clients) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
685 cli = clients[cid];
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
686 if ((cli instanceof Player) &&
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
687 cli.session.pname == username &&
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
688 cli.session.game.uname == gname) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
689 cli.reset();
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
690 respondlaunch(cli, true);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
691 tslog("Game %d has been taken over.", cli.session.sessid);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
692 return;
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
693 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
694 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
695 sendError(res, 7);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
696 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
697 else {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
698 new Player(gname, lkey, dims, respondlaunch);
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
699 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
700 };
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
701 checkprogress(username, games[gname], launch, []);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
702 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
703
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
704 function watch(req, res, formdata) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
705 if (!("n" in formdata)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
706 sendError(res, 2, "Game number not given");
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
707 return;
40
f7116eb3f791 rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents: 39
diff changeset
708 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
709 var gamenumber = Number(formdata["n"]);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
710 if (!(gamenumber in sessions)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
711 sendError(res, 7);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
712 return;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
713 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
714 var session = sessions[gamenumber];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
715 var watch = new Watcher(session);
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
716 var reply = {"t": "w", "id": watch.id, "w": session.w, "h": session.h,
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
717 "p": session.pname, "g": session.game.uname};
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
718 res.writeHead(200, {'Content-Type': 'application/json'});
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
719 res.write(JSON.stringify(reply));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
720 res.end();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
721 tslog("Game %d is being watched (key %s)", gamenumber, watch.id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
722 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
723
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
724 /* Sets things up for a new user, like dgamelaunch's commands[register] */
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
725 function regsetup(username) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
726 function regsetup_l2(err) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
727 for (var g in games) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
728 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
729 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
730 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
731 fs.mkdir(path.join("/dgldir/userdata", username), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
732 fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
733 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
734
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
735 function register(req, res, formdata) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
736 var uname, passwd, email;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
737 if (typeof (formdata.name) != "string" || formdata.name === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
738 sendError(res, 2, "No name given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
739 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
740 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
741 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
742 uname = formdata["name"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
743 if (typeof (formdata.pw) != "string" || formdata.pw === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
744 sendError(res, 2, "No password given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
745 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
746 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
747 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
748 passwd = formdata["pw"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
749 if (typeof (formdata.email) != "string" || formdata.email === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
750 /* E-mail is optional */
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
751 email = "nobody@nowhere.not";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
752 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
753 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
754 email = formdata["email"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
755 function checkreg(code, signal) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
756 if (code === 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
757 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
758 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
759 lkey = randkey(2);
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 43
diff changeset
760 logins[lkey] = {"name": uname, "ts": new Date(), "sessions": []};
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
761 var reply = {"t": "r", "k": lkey, "u": uname};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
762 res.writeHead(200, {'Content-Type': 'application/json'});
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
763 res.write(JSON.stringify(reply));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
764 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
765 tslog("Added new user: %s", uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
766 regsetup(uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
767 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
768 else if (code == 4) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
769 sendError(res, 2, "Invalid characters in name or email.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
770 tslog("Attempted registration: %s %s", uname, email);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
771 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
772 else if (code == 1) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
773 sendError(res, 2, "Username " + uname + " is already being used.");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
774 tslog("Attempted duplicate registration: %s", uname);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
775 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
776 else {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
777 sendError(res, 0, null);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
778 tslog("sqlickrypt register failed with code %d", code);
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
779 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
780 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
781 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
782 child_adder.on("exit", checkreg);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
783 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
784 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
785 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
786
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
787 /* Ends the game, obviously. Less obviously, stops watching the game if
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
788 * the client is a Watcher instead of a Player. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
789 function endgame(client, res) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
790 if (!client.alive) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
791 sendError(res, 7, null, true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
792 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
793 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
794 client.quit();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
795 // Give things some time to happen.
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
796 if (client instanceof Player)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
797 setTimeout(readFeed, 200, client, res);
62
7793ad53b90f rlgwebd.js: fix failure to respond.
John "Elwin" Edwards <elwin@sdf.org>
parents: 60
diff changeset
798 else
7793ad53b90f rlgwebd.js: fix failure to respond.
John "Elwin" Edwards <elwin@sdf.org>
parents: 60
diff changeset
799 readFeed(client, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
800 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
801 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
802
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
803 function findClient(formdata, playersOnly) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
804 if (typeof(formdata) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
805 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
806 if ("id" in formdata) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
807 var id = formdata["id"];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
808 if (id in clients && (!playersOnly || clients[id] instanceof Player)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
809 return clients[id];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
810 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
811 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
812 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
813 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
814
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
815 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
816 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
817 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
818 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
819 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
820 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
821 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
822 var extension = path.extname(realname);
81
e4773ac5d4d5 Switch to node v0.8.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
823 fs.exists(realname, function (exists) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
824 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
825 if (!exists || !extension || extension == ".html")
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
826 resheaders["Content-Type"] = "text/html; charset=utf-8";
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
827 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
828 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
829 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
830 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
831 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
832 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
833 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
834 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
835 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
836 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
837 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
838 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
839 if (error) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
840 res.writeHead(500, {});
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
841 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
842 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
843 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
844 res.writeHead(200, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
845 if (req.method != 'HEAD')
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
846 res.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
847 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
848 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
849 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
850 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
851 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
852 res.writeHead(404, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
853 if (req.method != 'HEAD') {
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
854 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
855 + nname + " Not Found</h1></body></html>\n");
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
856 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
857 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
858 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
859 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
860 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
861 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
862
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
863 function readFeed(client, res) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
864 if (!client) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
865 sendError(res, 7, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
866 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
867 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
868 var msgs = client.read();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
869 if (!allowlogin && !msgs.length) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
870 sendError(res, 6, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
871 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
872 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
873 res.writeHead(200, { "Content-Type": "application/json" });
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
874 res.write(JSON.stringify(msgs));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
875 res.end();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
876 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
877
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
878 function getStatus(callback) {
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
879 var now = new Date();
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
880 var statusinfo = {"s": allowlogin, "g": []};
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
881 function idleset(i, idletime) {
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
882 if (i >= 0 && i < statusinfo.g.length) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
883 statusinfo.g[i].i = idletime;
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
884 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
885 for (var j = 0; j < statusinfo.g.length; j++) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
886 if (!("i" in statusinfo.g[j]))
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
887 return;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
888 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
889 callback(statusinfo);
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
890 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
891 for (var sessid in sessions) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
892 var gamedesc = {};
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
893 gamedesc["n"] = sessid;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
894 gamedesc["p"] = sessions[sessid].pname;
64
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 62
diff changeset
895 gamedesc["g"] = sessions[sessid].game.uname;
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
896 statusinfo["g"].push(gamedesc);
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
897 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
898 if (statusinfo.g.length == 0) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
899 callback(statusinfo);
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
900 return;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
901 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
902 function makecallback(i) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
903 return function (err, stats) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
904 if (err)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
905 idleset(i, null);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
906 else
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
907 idleset(i, now - stats.mtime);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
908 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
909 }
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
910 for (var i = 0; i < statusinfo.g.length; i++) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
911 if (statusinfo.g[i].n in sessions) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
912 fs.fstat(sessions[statusinfo.g[i].n].record.fd, makecallback(i));
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
913 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
914 else {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
915 idleset(i, null);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
916 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
917 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
918 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
919
103
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
920 function statusmsg(req, res) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
921 function respond(info) {
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
922 res.writeHead(200, { "Content-Type": "application/json" });
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
923 if (req.method != 'HEAD')
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
924 res.write(JSON.stringify(info));
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
925 res.end();
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
926 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
927 getStatus(respond);
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
928 }
f30495f7ede8 RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 102
diff changeset
929
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
930 function pstatusmsg(req, res) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
931 if (req.method == 'HEAD') {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
932 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
933 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
934 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
935 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
936 var target = url.parse(req.url).pathname;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
937 var pmatch = target.match(/^\/pstatus\/(.*)/);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
938 if (pmatch && pmatch[1])
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
939 var pname = pmatch[1];
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
940 else {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
941 sendError(res, 2, "No name given.");
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
942 return;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
943 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
944 var reply = {"name": pname};
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
945 playerstatus(pname, function (pdata) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
946 reply["stat"] = pdata;
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
947 res.writeHead(200, { "Content-Type": "application/json" });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
948 res.write(JSON.stringify(reply));
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
949 res.end();
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
950 });
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
951 }
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
952
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
953 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
954 "Login failed", "Already playing", "Game launch failed",
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
955 "Server shutting down", "Game not in progress" ];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
956
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
957 function sendError(res, ecode, msg, box) {
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
958 res.writeHead(200, { "Content-Type": "application/json" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
959 var edict = {"t": "E"};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
960 if (!(ecode < errorcodes.length && ecode > 0))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
961 ecode = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
962 edict["c"] = ecode;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
963 edict["s"] = errorcodes[ecode];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
964 if (msg)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
965 edict["s"] += ": " + msg;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
966 if (box)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
967 res.write(JSON.stringify([edict]));
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
968 else
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
969 res.write(JSON.stringify(edict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
970 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
971 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
972
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
973 // TODO new-objects done to here
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
974 function webHandler(req, res) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
975 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
976 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
977 /* The request body will be added to this as it arrives. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
978 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
979 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
980
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
981 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
982 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
983 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
984 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
985 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
986
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
987 /* This will send the response once the whole request is here. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
988 function respond() {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
989 formdata = getMsg(reqbody);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
990 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
991 /* First figure out if the client is POSTing to a command interface. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
992 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
993 if (target == '/feed') {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
994 var client = findClient(formdata, false);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
995 if (!client) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
996 sendError(res, 7, null, true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
997 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
998 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
999 if (formdata.t == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1000 /* The client wants to terminate the process. */
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1001 endgame(client, res);
60
31bb3cf4f25f Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
1002 return; // endgame() calls readFeed() itself.
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1003 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1004 else if (formdata.t == "d" && typeof(formdata.d) == "string") {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1005 if (!(client instanceof Player)) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1006 sendError(res, 7, "Watching", true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1007 return;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1008 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1009 /* process the keys */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
1010 hexstr = formdata.d.replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1011 if (hexstr.length % 2 != 0) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1012 sendError(res, 2, "incomplete byte", true);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1013 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1014 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1015 keybuf = new Buffer(hexstr, "hex");
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1016 client.write(keybuf, formdata.n);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1017 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1018 readFeed(client, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1019 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1020 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1021 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1022 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1023 else if (target == "/addacct") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1024 register(req, res, formdata);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
1025 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1026 else if (target == "/play") {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1027 startgame(req, res, formdata);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
1028 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1029 else if (target == "/watch") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1030 watch(req, res, formdata);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1031 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1032 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1033 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1034 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1035 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1036 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1037 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1038 if (target == '/feed') {
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1039 if (req.method == 'HEAD') {
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1040 res.writeHead(200, {"Content-Type": "application/json"});
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1041 res.end();
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
1042 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1043 else
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1044 sendError(res, 7, null, true);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1045 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1046 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1047 else if (target == '/status') {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
1048 statusmsg(req, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1049 }
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1050 else if (target.match(/^\/pstatus\//)) {
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1051 pstatusmsg(req, res);
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
1052 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1053 else /* Go look for it in the filesystem */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1054 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1055 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1056 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1057 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1058 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1059 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1060 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1061 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1062 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1063 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1064
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1065 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1066
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1067 function wsHandler(wsRequest) {
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1068 var watchmatch = wsRequest.resource.match(/^\/watch\/([0-9]*)$/);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1069 if (watchmatch !== null) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1070 if (watchmatch[1] && Number(watchmatch[1]) in sessions) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1071 var tsession = sessions[Number(watchmatch[1])];
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1072 var conn = wsRequest.accept(null, wsRequest.origin);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1073 new wsWatcher(conn, tsession);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1074 tslog("Game %d is being watched via WebSockets", tsession.sessid);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1075 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1076 else
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1077 wsRequest.reject(404, errorcodes[7]);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1078 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1079 else if (wsRequest.resource == "/status") {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1080 var conn = wsRequest.accept(null, wsRequest.origin);
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1081 var tell = function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1082 getStatus(function (info) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1083 info["t"] = "t";
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1084 conn.sendUTF(JSON.stringify(info));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1085 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1086 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1087 var beginH = function (n, name, game) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1088 conn.sendUTF(JSON.stringify({"t": "b", "n": n, "p": name, "g": game}));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1089 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1090 var listH = function (list) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1091 conn.sendUTF(JSON.stringify(list));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1092 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1093 var endH = function (n) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1094 conn.sendUTF(JSON.stringify({"t": "e", "n": n}));
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1095 };
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1096 gamemux.on('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1097 gamemux.on('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1098 gamemux.on('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1099 conn.on('message', tell);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1100 conn.on('close', function () {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1101 gamemux.removeListener('begin', beginH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1102 gamemux.removeListener('list', listH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1103 gamemux.removeListener('end', endH);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1104 });
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1105 tell();
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1106 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1107 else
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1108 wsRequest.reject(404, "No such resource.");
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1109 }
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1110
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1111 function pushStatus() {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1112 getStatus(function(info) {
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1113 info["t"] = "t";
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1114 gamemux.emit('list', info);
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1115 });
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1116 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1117
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1118 function shutdown () {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1119 httpServer.close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1120 httpServer.removeAllListeners('request');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1121 ctlServer.close();
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1122 tslog("Shutting down...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1123 process.exit();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1124 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1125
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1126 function conHandler(chunk) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1127 var msg = chunk.toString().split('\n')[0];
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1128 if (msg == "quit") {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1129 allowlogin = false;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1130 tslog("Disconnecting...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1131 for (var sessid in sessions) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1132 sessions[sessid].close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1133 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
1134 setTimeout(shutdown, 2000);
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1135 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1136 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
1137
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1138 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1139 for (var sessid in sessions) {
87
bd2cf6dda28d RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents: 85
diff changeset
1140 sessions[sessid].term.kill('SIGHUP');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1141 }
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1142 tslog("Quitting...");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1143 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1144 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1145
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1146 /* Initialization STARTS HERE */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1147 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1148
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1149 if (process.getuid() != 0) {
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
1150 tslog("Not running as root, cannot chroot.");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1151 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1152 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1153
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1154 var httpServer; // declare here so shutdown() can find it
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1155 var wsServer;
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1156
85
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1157 /* This could be nonblocking, but nothing else can start yet anyway. */
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1158 if (fs.existsSync(ctlsocket)) {
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1159 fs.unlinkSync(ctlsocket);
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1160 }
4303d94d87a2 rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents: 84
diff changeset
1161
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1162 /* Open the control socket before chrooting where it can't be found */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1163 var ctlServer = net.createServer(function (sock) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1164 sock.on('data', conHandler);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1165 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1166 ctlServer.listen(ctlsocket, function () {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1167 /* fork off and die */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1168 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1169 daemon.start(logfile);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1170 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1171 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1172 tslog("Daemonization failed: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1173 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1174 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1175 /* chroot and drop permissions. daemon.chroot() does chdir() itself. */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1176 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1177 daemon.chroot(chrootDir);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1178 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1179 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1180 tslog("chroot to %s failed: %s", chrootDir, err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1181 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1182 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1183 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1184 // drop gid first, that requires UID=0
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1185 process.setgid(dropToGID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1186 process.setuid(dropToUID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1187 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1188 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1189 tslog("Could not drop permissions: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1190 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1191 }
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
1192 httpServer = http.createServer(webHandler);
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1193 httpServer.listen(httpPort);
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
1194 tslog('rlgwebd running on port %d', httpPort);
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
1195 setInterval(reaper, playtimeout / 4);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1196 wsServer = new WebSocketServer({"httpServer": httpServer});
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1197 wsServer.on("request", wsHandler);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1198 tslog('WebSockets are online');
104
7d444ba4739e RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 103
diff changeset
1199 setInterval(pushStatus, 4000);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1200 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
1201