//Commentary:

// Expose a simple REPL as a server to emacspeak-maths.
// Usage Model:
// The Emacs  client sends requests of the form:
// command: args --- where args are comma-separated.
// The server responds with a Lisp S-expression.
// This S-expression is an annotated string.
// Annotations are ACSS property/value specifications.
// At any given time, the server loop is working with a given math expression,
// Emacs issues browse/render commands against that expression.

// Code:

// Load the TCP Library
var net = require('net');

// Initialize Math rendering system.
var mjx = require('mathjax-node');
var sre = require('speech-rule-engine');
sre.setupEngine({markup: 'acss'});


// Handle to connected Emacspeak-maths client:
var client;

// table of request handlers:

var handlers = {};

// Add the various handlers:

// Accept a LaTeX math expression:
//Warning: this errors out in sre.move(9)

handlers.enter = function (expr) {
  mjx.typeset({math: expr,
               format: 'TeX',
               mml:true},
              function(data) {sre.walk(data.mml)}
             );
  // Find a better alternative to passing raw kbd values.
  sre.move(9);
  return "done";
};

// Implement Handlers:
handlers.up = function () {};
handlers.down = function () {};
handlers.left = function () {};
handlers.right = function () {};
handlers.repeat = function () {};

// Start a TCP Server
net.createServer(function (socket) {
  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort 

  // Record this client:
  client = socket;

  // Announce yourself:
  socket.write("Welcome " + socket.name + "\n");
  // Handle incoming messages from Emacs:
  socket.on('data', function (data) {
    respond(data, socket);
  });

  // Shutdown server on disconnect:
  socket.on('end', function () {
    // server.shutdown();
  });
  
  // Send out response:
  function respond(message, sender ) {
    // handle requests that have single argument for now:
    var request = message.toString().split(':');
    var handler =handlers[request[0]];
    if (handler != undefined) {
    var result = handler.apply(request[1]);
    sender.write(result);
    // Debug: Log it to the server output too
    process.stdout.write(result);
      } else {
        process.stdout.write("Handler for " +request[0]  +" not defined.\n ");
        }
  }

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log("Math server running at port 5000\n");

@@ -685,17 +685,16 @@
 ss'%7D);%0A%0A
-%0A
 // Handl
@@ -939,16 +939,18 @@
 expr) %7B%0A
+  
   mjx.ty
@@ -968,16 +968,18 @@
 : expr,%0A
+  
         
@@ -1015,16 +1015,18 @@
         
+  
 mml:true
@@ -1028,16 +1028,18 @@
 :true%7D,%0A
+  
         
@@ -1093,19 +1093,23 @@
         
+  
 );%0A
+  
   // Fin
@@ -1158,16 +1158,18 @@
 values.%0A
+  
   sre.mo
@@ -1175,16 +1175,18 @@
 ove(9);%0A
+  
   return
@@ -1442,16 +1442,18 @@
 cket) %7B%0A
+  
   // Ide
@@ -1470,16 +1470,18 @@
  client%0A
+  
   socket
@@ -1538,11 +1538,12 @@
 Port
- 
 %0A%0A
+  
   //
@@ -1565,16 +1565,18 @@
 ient:%0A  
+ 
+ 
 client =
@@ -1585,16 +1585,18 @@
 ocket;%0A%0A
+  
   // Ann
@@ -1609,24 +1609,26 @@
 yourself:%0A  
+ 
+ 
 socket.write
@@ -1662,16 +1662,18 @@
  %22%5Cn%22);%0A
+  
   // Han
@@ -1704,24 +1704,26 @@
 om Emacs:%0A  
+ 
+ 
 socket.on('d
@@ -1742,24 +1742,28 @@
 on (data) %7B%0A
+    
     respond(
@@ -1777,16 +1777,18 @@
 ocket);%0A
+  
   %7D);%0A%0A 
@@ -1788,16 +1788,18 @@
  %7D);%0A%0A  
+ 
+ 
 // Shutd
@@ -1824,16 +1824,18 @@
 onnect:%0A
+  
   socket
@@ -1863,16 +1863,20 @@
 ) %7B%0A    
+ 
+   
 // serve
@@ -1895,15 +1895,17 @@
 ;%0A  
+ 
+ 
 %7D);%0A
+%0A
   
-%0A
   //
@@ -1924,16 +1924,18 @@
 sponse:%0A
+  
   functi
@@ -1969,16 +1969,20 @@
 ) %7B%0A    
+   
+ 
 // handl
@@ -2035,53 +2035,182 @@
     
-var request = message.toString().split(':');%0A
+    //FixMe: Only split on first ':'%0A        var request = message.toString();%0A        var cmd =request.split(':', 1)%5B0%5D;%0A        var args = request.slice(cmd.length+1);%0A    
     
@@ -2235,21 +2235,18 @@
 ers%5B
-request%5B0%5D%5D;%0A
+cmd%5D;%0A    
     
@@ -2269,24 +2269,32 @@
 ndefined) %7B%0A
+        
     var resu
@@ -2316,21 +2316,23 @@
 ply(
-request%5B1%5D);%0A
+args);%0A        
     
@@ -2349,24 +2349,32 @@
 te(result);%0A
+        
     // Debug
@@ -2407,16 +2407,24 @@
 put too%0A
+        
     proc
@@ -2455,16 +2455,18 @@
 ;%0A      
+  
 %7D else %7B
@@ -2466,16 +2466,20 @@
  else %7B%0A
+    
         
@@ -2559,16 +2559,18 @@
       %7D%0A
+  
   %7D%0A%0A%7D).

split correctly on first ':', leaving rest of string as args.