Integrate the TrIPE server into the Java edifice.
[tripe-android] / admin.scala
index fab8305..52a2912 100644 (file)
@@ -27,7 +27,7 @@ package uk.org.distorted.tripe; package object admin {
 
 /*----- Imports -----------------------------------------------------------*/
 
-import java.io.{BufferedReader, Reader, Writer};
+import java.io.{BufferedReader, InputStreamReader, OutputStreamWriter};
 import java.util.concurrent.locks.{Condition, ReentrantLock => Lock};
 
 import scala.collection.mutable.{HashMap, Publisher};
@@ -35,6 +35,7 @@ import scala.concurrent.Channel;
 import scala.util.control.Breaks;
 
 import Implicits._;
+import sys.{serverInput, serverOutput};
 
 /*----- Classification of server messages ---------------------------------*/
 
@@ -77,8 +78,7 @@ class CommandFailed(val msg: Seq[String]) extends Exception {
 
 class ConnectionLostException extends Exception;
 
-class Connection(val in: Reader, val out: Writer)
-       extends Publisher[AsyncMessage]
+object Connection extends Publisher[AsyncMessage]
 {
   /* Synchronization.
    *
@@ -87,12 +87,15 @@ class Connection(val in: Reader, val out: Writer)
    * hold the `Connection' lock before locking any individual `Job' objects.
    */
 
-  var livep: Boolean = true;           // Is this connection still alive?
-  var fgjob: Option[this.Job] = None;  // Foreground job, if there is one.
-  val jobmap = new HashMap[String, this.Job]; // Maps tags to extant jobs.
-  var bgseq = 0;                       // Next background job tag.
+  private var livep: Boolean = true;   // Is this connection still alive?
+  private var fgjob: Option[this.Job] = None; // Foreground job, if there is one.
+  private val jobmap = new HashMap[String, this.Job]; // Maps tags to extant jobs.
+  private var bgseq = 0;               // Next background job tag.
 
-  type Pub = Connection;
+  private val in = new BufferedReader(new InputStreamReader(serverInput));
+  private val out = new OutputStreamWriter(serverOutput);
+
+  type Pub = Connection.type;
 
   class Job extends Iterator[Seq[String]] {
     private[Connection] val ch = new Channel[JobMessage];
@@ -183,8 +186,6 @@ println(";; write command");
 
   def submit(toks: String*): this.Job = submit(false, toks: _*);
 
-  def close() { synchronized { out.close(); } }
-
   /* These two expect the connection lock to be held. */
   def foregroundJob: Job =
     fgjob.getOrElse { throw new ServerFailed("no foreground job"); }
@@ -267,7 +268,6 @@ println(s";; line: $line");
        }
       }
       publish(ConnectionLost);
-      in.close(); out.close();
     }
   }
 }