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 path = require('path');
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 ptyhelp = "/home/elwin/hk/nodejs/rlg/ptyhelper";
@ -30,10 +30,9 @@ function TermSession(sessid, h, w) {
if (!(key in env_dontuse))
childenv[key] = process.env[key];
}
childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
// Should setsid get set?
var spawnopts = {"env": childenv, "cwd": process.env["HOME"]};
this.child = child_process.spawn(ptyhelp, ["bash"], spawnopts);
var spawnopts = {"env": childenv, "cwd": process.env["HOME"],
"rows": this.h, "cols": this.w};
this.term = pty.spawn("bash", [], spawnopts);
var ss = this;
/* Eventually we'll need to make sure the sessid isn't in use yet. */
this.sessid = sessid;
@ -42,14 +41,10 @@ function TermSession(sessid, h, w) {
this.nsend = 0; // Number to use for the next message sent.
this.nrecv = 0; // Number expected on the next message received.
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);
});
this.child.stderr.on("data", function (buf) {
ss.data.push(buf);
});
this.child.on("exit", function (code, signal) {
ss.exitcode = (code != null ? code : 255);
this.term.on("exit", function () {
ss.alive = false;
/* Wait for all the data to get collected */
setTimeout(ss.cleanup, 1000);
@ -63,7 +58,7 @@ function TermSession(sessid, h, w) {
console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n);
}
this.nrecv = n + 1;
this.child.stdin.write(data);
this.term.write(data);
};
this.read = function () {
if (this.data.length == 0)
@ -71,12 +66,12 @@ function TermSession(sessid, h, w) {
var pos = 0;
var i = 0;
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 tptr;
pos = 0;
while (this.data.length > 0) {
tptr = this.data.shift();
tptr = new Buffer(this.data.shift());
tptr.copy(nbuf, pos);
pos += tptr.length;
}
@ -84,7 +79,7 @@ function TermSession(sessid, h, w) {
};
this.close = function () {
if (this.alive)
this.child.kill('SIGHUP');
this.term.kill('SIGHUP');
};
this.cleanup = function () {
/* 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};
res.write(JSON.stringify(logindict));
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;
}
@ -353,7 +348,7 @@ function handler(req, res) {
process.on("exit", function () {
for (var sessid in sessions) {
if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP');
sessions[sessid].term.kill('SIGHUP');
}
console.log("Quitting...");
return;