Mercurial > hg > rlgwebd
annotate webtty.js @ 137:f14e92f6d955
webtty: don't hardcode paths.
author | John "Elwin" Edwards |
---|---|
date | Thu, 18 Jul 2013 10:36:58 -0700 |
parents | ad4229cf8321 |
children | 789c094675f4 |
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 |
137 | 2 |
3 var localModules = '/usr/lib/node_modules/'; | |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
4 var http = require('http'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
5 var url = require('url'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
6 var path = require('path'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 var fs = require('fs'); |
137 | 8 var pty = require(path.join(localModules, "pty.js")); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
9 |
137 | 10 var serveStaticRoot = fs.realpathSync("."); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 var sessions = {}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 |
2
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
13 var env_dontuse = {"TMUX": true, "TMUX_PANE": true}; |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
14 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
15 /* Constructor for TermSessions. Note that it opens the terminal and |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
16 * adds itself to the sessions dict. |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
17 */ |
9
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
18 function TermSession(sessid, h, w) { |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
19 /* Set up the sizes. */ |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
20 w = Math.floor(Number(w)); |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
21 if (!(w > 0 && w < 256)) |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
22 w = 80; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
23 this.w = w; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
24 h = Math.floor(Number(h)); |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
25 if (!(h > 0 && h < 256)) |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
26 h = 25; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
27 this.h = h; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
28 /* Customize the environment. */ |
2
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
29 var childenv = {}; |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
30 for (var key in process.env) { |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
31 if (!(key in env_dontuse)) |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
32 childenv[key] = process.env[key]; |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
33 } |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
34 var spawnopts = {"env": childenv, "cwd": process.env["HOME"], |
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
35 "rows": this.h, "cols": this.w}; |
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
36 this.term = pty.spawn("bash", [], spawnopts); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 var ss = this; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 /* Eventually we'll need to make sure the sessid isn't in use yet. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
39 this.sessid = sessid; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
40 this.alive = true; |
15
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
41 this.data = []; // Buffer for the process' output. |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
42 this.nsend = 0; // Number to use for the next message sent. |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
43 this.nrecv = 0; // Number expected on the next message received. |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
44 this.msgQ = []; // Queue for messages that arrived out of order. |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
45 this.term.on("data", function (buf) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 ss.data.push(buf); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 }); |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
48 this.term.on("exit", function () { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 ss.alive = false; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 /* Wait for all the data to get collected */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
51 setTimeout(ss.cleanup, 1000); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
52 }); |
15
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
53 this.write = function (data, n) { |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
54 if (!this.alive) { |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
55 /* Throw some kind of exception? */ |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
56 return; |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
57 } |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
58 if (n !== this.nrecv) { |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
59 console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n); |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
60 } |
7466927c17a5
webtty.js: check message order.
John "Elwin" Edwards <elwin@sdf.org>
parents:
13
diff
changeset
|
61 this.nrecv = n + 1; |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
62 this.term.write(data); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
63 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
64 this.read = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 if (this.data.length == 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
66 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 var pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 var i = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 for (i = 0; i < this.data.length; i++) |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
70 pos += Buffer.byteLength(this.data[i]); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
71 var nbuf = new Buffer(pos); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
72 var tptr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
73 pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
74 while (this.data.length > 0) { |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
75 tptr = new Buffer(this.data.shift()); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
76 tptr.copy(nbuf, pos); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 pos += tptr.length; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 return nbuf; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
80 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
81 this.close = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
82 if (this.alive) |
86
ad4229cf8321
WebTTY: use the pty.js module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
81
diff
changeset
|
83 this.term.kill('SIGHUP'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org> |