WebTTY: use the pty.js module.

Convert TermSessions to spawn with the pty.js module instead of piping
everything through ptyhelper.
This commit is contained in:
John "Elwin" Edwards 2012-07-08 21:20:56 -07:00
parent 286626df57
commit b9a6a1e303

View file

@ -3,7 +3,7 @@ var http = require('http');
var url = require('url'); var url = require('url');
var path = require('path'); var path = require('path');
var fs = require('fs'); var fs = require('fs');
var child_process = require("child_process"); var pty = require("/usr/local/lib/node_modules/pty.js");
var serveStaticRoot = "/home/elwin/hk/nodejs/rlg/s/"; var serveStaticRoot = "/home/elwin/hk/nodejs/rlg/s/";
var ptyhelp = "/home/elwin/hk/nodejs/rlg/ptyhelper"; var ptyhelp = "/home/elwin/hk/nodejs/rlg/ptyhelper";
@ -30,10 +30,9 @@ function TermSession(sessid, h, w) {
if (!(key in env_dontuse)) if (!(key in env_dontuse))
childenv[key] = process.env[key]; childenv[key] = process.env[key];
} }
childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w); var spawnopts = {"env": childenv, "cwd": process.env["HOME"],
// Should setsid get set? "rows": this.h, "cols": this.w};
var spawnopts = {"env": childenv, "cwd": process.env["HOME"]}; this.term = pty.spawn("bash", [], spawnopts);
this.child = child_process.spawn(ptyhelp, ["bash"], spawnopts);
var ss = this; var ss = this;
/* Eventually we'll need to make sure the sessid isn't in use yet. */ /* Eventually we'll need to make sure the sessid isn't in use yet. */
this.sessid = sessid; this.sessid = sessid;
@ -42,14 +41,10 @@ function TermSession(sessid, h, w) {
this.nsend = 0; // Number to use for the next message sent. this.nsend = 0; // Number to use for the next message sent.
this.nrecv = 0; // Number expected on the next message received. this.nrecv = 0; // Number expected on the next message received.
this.msgQ = []; // Queue for messages that arrived out of order. this.msgQ = []; // Queue for messages that arrived out of order.
this.child.stdout.on("data", function (buf) { this.term.on("data", function (buf) {
ss.data.push(buf); ss.data.push(buf);
}); });
this.child.stderr.on("data", function (buf) { this.term.on("exit", function () {
ss.data.push(buf);
});
this.child.on("exit", function (code, signal) {
ss.exitcode = (code != null ? code : 255);
ss.alive = false; ss.alive = false;
/* Wait for all the data to get collected */ /* Wait for all the data to get collected */
setTimeout(ss.cleanup, 1000); setTimeout(ss.cleanup, 1000);
@ -63,7 +58,7 @@ function TermSession(sessid, h, w) {
console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n); console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n);
} }
this.nrecv = n + 1; this.nrecv = n + 1;
this.child.stdin.write(data); this.term.write(data);
}; };
this.read = function () { this.read = function () {
if (this.data.length == 0) if (this.data.length == 0)
@ -71,12 +66,12 @@ function TermSession(sessid, h, w) {
var pos = 0; var pos = 0;
var i = 0; var i = 0;
for (i = 0; i < this.data.length; i++) for (i = 0; i < this.data.length; i++)
pos += this.data[i].length; pos += Buffer.byteLength(this.data[i]);
var nbuf = new Buffer(pos); var nbuf = new Buffer(pos);
var tptr; var tptr;
pos = 0; pos = 0;
while (this.data.length > 0) { while (this.data.length > 0) {
tptr = this.data.shift(); tptr = new Buffer(this.data.shift());
tptr.copy(nbuf, pos); tptr.copy(nbuf, pos);
pos += tptr.length; pos += tptr.length;
} }
@ -84,7 +79,7 @@ function TermSession(sessid, h, w) {
}; };
this.close = function () { this.close = function () {
if (this.alive) if (this.alive)
this.child.kill('SIGHUP'); this.term.kill('SIGHUP');
}; };
this.cleanup = function () { this.cleanup = function () {
/* Call this when the child is dead. */ /* Call this when the child is dead. */
@ -186,7 +181,7 @@ function login(req, res, formdata) {
var logindict = {"login": true, "id": sessid, "w": w, "h": h}; var logindict = {"login": true, "id": sessid, "w": w, "h": h};
res.write(JSON.stringify(logindict)); res.write(JSON.stringify(logindict));
res.end(); res.end();
console.log("Started new session with key " + sessid + ", pid " + nsession.child.pid); console.log("Started new session with key " + sessid + ", pid " + nsession.term.pid);
return; return;
} }
@ -353,7 +348,7 @@ function handler(req, res) {
process.on("exit", function () { process.on("exit", function () {
for (var sessid in sessions) { for (var sessid in sessions) {
if (sessions[sessid].alive) if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP'); sessions[sessid].term.kill('SIGHUP');
} }
console.log("Quitting..."); console.log("Quitting...");
return; return;