Mercurial > hg > rlgwebd
annotate webtty.js @ 11:481dcee353c9
webtty: switch all server responses to JSON.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Fri, 11 May 2012 13:33:48 -0700 |
parents | d051aad3e95f |
children | bf7c26d0b66d |
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 var http = require('http'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
3 var url = require('url'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
4 var path = require('path'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
5 var fs = require('fs'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
6 var child_process = require("child_process"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
8 var serveStaticRoot = "/home/elwin/hk/nodejs/rlg/s/"; |
7
d1b3c3af34d6
WebTTY: switch terminal size to 80x25.
John "Elwin" Edwards <elwin@sdf.org>
parents:
3
diff
changeset
|
9 var ptyhelp = "/home/elwin/hk/nodejs/rlg/ptyhelper"; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
10 var sessions = {}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 |
2
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
12 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
|
13 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
14 /* 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
|
15 * adds itself to the sessions dict. |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
16 */ |
9
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
17 function TermSession(sessid, h, w) { |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
18 /* Set up the sizes. */ |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
19 w = Math.floor(Number(w)); |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
20 if (!(w > 0 && w < 256)) |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
21 w = 80; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
22 this.w = w; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
23 h = Math.floor(Number(h)); |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
24 if (!(h > 0 && h < 256)) |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
25 h = 25; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
26 this.h = h; |
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
27 /* Customize the environment. */ |
2
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
28 var childenv = {}; |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 } |
9
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
7
diff
changeset
|
33 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); |
2
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
34 // Should setsid get set? |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
35 var spawnopts = {"env": childenv, "cwd": process.env["HOME"]}; |
98bf7c94c954
webtty.js: set up the environment and working directory.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
36 this.child = child_process.spawn(ptyhelp, ["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; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 this.data = []; |
11
481dcee353c9
webtty: switch all server responses to JSON.
John "Elwin" Edwards <elwin@sdf.org>
parents:
10
diff
changeset
|
42 this.nsend = 0; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 this.child.stdout.on("data", function (buf) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 ss.data.push(buf); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 this.child.stderr.on("data", function (buf) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 ss.data.push(buf); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 this.child.on("exit", function (code, signal) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 ss.exitcode = (code != null ? code : 255); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
51 ss.alive = false; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
52 /* 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
|
53 setTimeout(ss.cleanup, 1000); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
54 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
55 this.write = function (data) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 if (this.alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
57 this.child.stdin.write(data); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
58 /* Otherwise, throw some kind of exception? */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
59 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
60 this.read = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 if (this.data.length == 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 return null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
63 var pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
64 var i = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 for (i = 0; i < this.data.length; i++) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
66 pos += this.data[i].length; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 var nbuf = new Buffer(pos); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 var tptr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 pos = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
70 while (this.data.length > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
71 tptr = this.data.shift(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
72 tptr.copy(nbuf, pos); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
73 pos += tptr.length; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
74 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
75 return nbuf; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
76 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 this.close = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 if (this.alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 this.child.kill('SIGHUP'); |
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.cleanup = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
82 /* Call this when the child is dead. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
83 if (this.alive) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
84 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
85 /* Give the client a chance to read any leftover data. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org> |