changeset 15:7466927c17a5

webtty.js: check message order. Implement checking the numbers of the client's messages on the server. Fixing out-of-ordering isn't implemented because the problem hasn't been observed yet, though it likely will once actual network transit is involved.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 15 May 2012 16:26:28 -0700
parents 155f3c104759
children ef6127ed6da3
files webtty.js
diffstat 1 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/webtty.js	Tue May 15 09:30:12 2012 -0700
+++ b/webtty.js	Tue May 15 16:26:28 2012 -0700
@@ -38,8 +38,10 @@
   /* Eventually we'll need to make sure the sessid isn't in use yet. */
   this.sessid = sessid;
   this.alive = true;
-  this.data = [];
-  this.nsend = 0;
+  this.data = []; // Buffer for the process' output.
+  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) {
     ss.data.push(buf);
   });
@@ -52,10 +54,16 @@
     /* Wait for all the data to get collected */
     setTimeout(ss.cleanup, 1000);
   });
-  this.write = function (data) {
-    if (this.alive)
-      this.child.stdin.write(data);
-    /* Otherwise, throw some kind of exception? */
+  this.write = function (data, n) {
+    if (!this.alive) {
+      /* Throw some kind of exception? */
+      return;
+    }
+    if (n !== this.nrecv) {
+      console.log("Session " + this.sessid + ": Expected message " + this.nrecv + ", got " + n);
+    }
+    this.nrecv = n + 1;
+    this.child.stdin.write(data);
   };
   this.read = function () {
     if (this.data.length == 0)
@@ -303,7 +311,7 @@
             return;
           }
           keybuf = new Buffer(hexstr, "hex");
-          cterm.write(keybuf);
+          cterm.write(keybuf, formdata["n"]);
         }
         readFeed(res, cterm);
       }