changeset 86:ad4229cf8321

WebTTY: use the pty.js module. Convert TermSessions to spawn with the pty.js module instead of piping everything through ptyhelper.
author John "Elwin" Edwards <elwin@sdf.org>
date Sun, 08 Jul 2012 21:20:56 -0700
parents 4303d94d87a2
children bd2cf6dda28d
files webtty.js
diffstat 1 files changed, 12 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/webtty.js	Sun Jul 08 17:49:43 2012 -0700
+++ b/webtty.js	Sun Jul 08 21:20:56 2012 -0700
@@ -3,7 +3,7 @@
 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 @@
     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 @@
   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 @@
       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 @@
     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 @@
   };
   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 @@
   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 @@
 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;