More progress. More work.
[tripe-android] / util.scala
index 79ac861..8ede691 100644 (file)
@@ -28,12 +28,16 @@ package uk.org.distorted; package object tripe {
 /*----- Imports -----------------------------------------------------------*/
 
 import scala.concurrent.duration.{Deadline, Duration};
-import scala.util.control.Breaks;
+import scala.util.control.{Breaks, ControlThrowable};
 
-import java.io.{BufferedReader, Closeable, File, Reader};
-import java.net.{URL, URLConnection};
+import java.io.{BufferedReader, Closeable, File, InputStream, Reader};
+import java.net.{HttpURLConnection, URL, URLConnection};
 import java.nio.{ByteBuffer, CharBuffer};
+import java.nio.channels.{SelectionKey, Selector};
+import java.nio.channels.spi.{AbstractSelector, AbstractSelectableChannel};
 import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.{Set => JSet};
 import java.util.concurrent.locks.{Lock, ReentrantLock};
 
 /*----- Miscellaneous useful things ---------------------------------------*/
@@ -41,6 +45,9 @@ import java.util.concurrent.locks.{Lock, ReentrantLock};
 val rng = new java.security.SecureRandom;
 
 def unreachable(msg: String): Nothing = throw new AssertionError(msg);
+def unreachable(): Nothing = unreachable("unreachable");
+final val ok = ();
+final class Brand;
 
 /*----- Various pieces of implicit magic ----------------------------------*/
 
@@ -134,13 +141,125 @@ def closing[T, U <: Closeable](thing: U)(body: U => T): T =
   try { body(thing) }
   finally { thing.close(); }
 
+/*----- Control structures ------------------------------------------------*/
+
+private case class ExitBlock[T](brand: Brand, result: T)
+       extends ControlThrowable;
+
+def block[T](body: (T => Nothing) => T): T = {
+  /* block { exit[T] => ...; exit(x); ... }
+   *
+   * Execute the body until it calls the `exit' function or finishes.
+   * Annoyingly, Scala isn't clever enough to infer the return type, so
+   * you'll have to write it explicitly.
+   */
+
+  val mybrand = new Brand;
+  try { body { result => throw new ExitBlock(mybrand, result) } }
+  catch {
+    case ExitBlock(brand, result) if brand eq mybrand =>
+      result.asInstanceOf[T]
+  }
+}
+
+def blockUnit(body: (=> Nothing) => Unit) {
+  /* blockUnit { exit => ...; exit; ... }
+   *
+   * Like `block'; it just saves you having to write `exit[Unit] => ...;
+   * exit(ok); ...'.
+   */
+
+  val mybrand = new Brand;
+  try { body { throw new ExitBlock(mybrand, null) }; }
+  catch { case ExitBlock(brand, result) if brand eq mybrand => ok; }
+}
+
+def loop[T](body: (T => Nothing) => Unit): T = {
+  /* loop { exit[T] => ...; exit(x); ... }
+   *
+   * Repeatedly execute the body until it calls the `exit' function.
+   * Annoyingly, Scala isn't clever enough to infer the return type, so
+   * you'll have to write it explicitly.
+   */
+
+  block { exit => while (true) body(exit); unreachable }
+}
+
+def loopUnit(body: (=> Nothing) => Unit): Unit = {
+  /* loopUnit { exit => ...; exit; ... }
+   *
+   * Like `loop'; it just saves you having to write `exit[Unit] => ...;
+   * exit(()); ...'.
+   */
+
+  blockUnit { exit => while (true) body(exit); }
+}
+
+val BREAKS = new Breaks;
+import BREAKS.{breakable, break};
+
+/*----- Interruptably doing things ----------------------------------------*/
+
+private class InterruptCatcher[T](body: => T, onWakeup: => Unit)
+       extends AbstractSelector(null) {
+  /* Hook onto the VM's thread interruption machinery.
+   *
+   * The `run' method is the only really interesting one.  It will run the
+   * BODY, returning its result; if the thread is interrupted during this
+   * time, ONWAKEUP is invoked for effect.  The expectation is that ONWAKEUP
+   * will somehow cause BODY to stop early.
+   *
+   * Credit for this hack goes to Nicholas Wilson: see
+   * <https://github.com/NWilson/javaInterruptHook>.
+   */
+
+  private def nope: Nothing =
+    { throw new UnsupportedOperationException("can't do that"); }
+  protected def implCloseSelector() { }
+  protected def register(chan: AbstractSelectableChannel,
+                                 ops: Int, att: Any): SelectionKey = nope;
+  def keys(): JSet[SelectionKey] = nope;
+  def selectedKeys(): JSet[SelectionKey] = nope;
+  def select(): Int = nope;
+  def select(millis: Long): Int = nope;
+  def selectNow(): Int = nope;
+
+  def run(): T = try {
+    begin();
+    val ret = body;
+    if (Thread.interrupted()) throw new InterruptedException;
+    ret
+  } finally {
+    end();
+  }
+  def wakeup(): Selector = { onWakeup; this }
+}
+
+class PendingInterruptable[T] private[tripe](body: => T) {
+  /* This class exists to provide the `onInterrupt THUNK' syntax. */
+
+  def onInterrupt(thunk: => Unit): T =
+    new InterruptCatcher(body, thunk).run();
+}
+def interruptably[T](body: => T) = {
+  /* interruptably { BODY } onInterrupt { THUNK }
+   *
+   * Execute BODY and return its result.  If the thread receives an
+   * interrupt -- or is already in an interrupted state -- execute THUNK for
+   * effect; it is expected to cause BODY to return expeditiously, and when
+   * the BODY completes, an `InterruptedException' is thrown.
+   */
+
+  new PendingInterruptable(body);
+}
+
 /*----- A gadget for fetching URLs ----------------------------------------*/
 
 class URLFetchException(msg: String) extends Exception(msg);
 
 trait URLFetchCallbacks {
   def preflight(conn: URLConnection) { }
-  def write(buf: Array[Byte], n: Int, len: Int): Unit;
+  def write(buf: Array[Byte], n: Int, len: Long): Unit;
   def done(win: Boolean) { }
 }
 
@@ -148,50 +267,74 @@ def fetchURL(url: URL, cb: URLFetchCallbacks) {
   /* Fetch the URL, feeding the data through the callbacks CB. */
 
   withCleaner { clean =>
-    var win: Boolean = false;
-    clean { cb.done(win); }
+    var win: Boolean = false; clean { cb.done(win); }
 
-    /* Set up the connection, and run a preflight check. */
+    /* Set up the connection.  This isn't going to block, I think, and we
+     * need to use it in the interrupt handler.
+     */
     val c = url.openConnection();
-    cb.preflight(c);
-
-    /* Start fetching data. */
-    val in = c.getInputStream; clean { in.close(); }
-    val explen = c.getContentLength();
 
-    /* Read a buffer at a time, and give it to the callback.  Maintain a
-     * running total.
+    /* Java's default URL handlers don't respond to interrupts, so we have to
+     * take over this duty.
      */
-    val buf = new Array[Byte](4096);
-    var n = 0;
-    var len = 0;
-    while ({n = in.read(buf); n >= 0 && (explen == -1 || len <= explen)}) {
-      cb.write(buf, n, len);
-      len += n;
-    }
+    interruptably {
+      /* Run the caller's preflight check.  This must be done here, since it
+       * might well block while it discovers things like the content length.
+       */
+      cb.preflight(c);
 
-    /* I can't find it documented anywhere that the existing machinery
-     * checks the received stream against the advertised content length.
-     * It doesn't hurt to check again, anyway.
-     */
-    if (explen != -1 && explen != len) {
-      throw new URLFetchException(
-       s"received $len /= $explen bytes from `$url'");
-    }
+      /* Start fetching data. */
+      val in = c.getInputStream; clean { in.close(); }
+      val explen = c.getContentLength;
 
-    /* Glorious success is ours. */
-    win = true;
-  }
-}
+      /* Read a buffer at a time, and give it to the callback.  Maintain a
+       * running total.
+       */
+      var len: Long = 0;
+      blockUnit { exit =>
+       for ((buf, n) <- blocks(in)) {
+         cb.write(buf, n, len);
+         len += n;
+         if (explen != -1 && len > explen) exit;
+       }
+      }
 
-/*----- Running processes -------------------------------------------------*/
+      /* I can't find it documented anywhere that the existing machinery
+       * checks the received stream against the advertised content length.
+       * It doesn't hurt to check again, anyway.
+       */
+      if (explen != -1 && explen != len) {
+       throw new URLFetchException(
+         s"received $len /= $explen bytes from `$url'");
+      }
 
-//def runProgram(
+      /* Glorious success is ours. */
+      win = true;
+    } onInterrupt {
+      /* Oh.  How do we do this? */
+
+      c match {
+       case c: HttpURLConnection =>
+         /* It's an HTTP connection (what happened to the case here?).
+          * HTTPS connections match too because they're a subclass.  Getting
+          * the input stream will block, but there's an easier way.
+          */
+         c.disconnect();
+
+       case _ =>
+         /* It's something else.  Let's hope that getting the input stream
+          * doesn't block.
+          */
+       c.getInputStream.close();
+      }
+    }
+  }
+}
 
 /*----- Threading things --------------------------------------------------*/
 
-def thread[T](name: String, run: Boolean = true, daemon: Boolean = true)
-            (f: => T): Thread = {
+def thread(name: String, run: Boolean = true, daemon: Boolean = true)
+         (f: => Unit): Thread = {
   /* Make a thread with a given name, and maybe start running it. */
 
   val t = new Thread(new Runnable { def run() { f; } }, name);
@@ -200,6 +343,28 @@ def thread[T](name: String, run: Boolean = true, daemon: Boolean = true)
   t
 }
 
+class ValueThread[T](name: String, group: ThreadGroup = null,
+                    stacksz: Long = 0)(body: => T)
+       extends Thread(group, null, name, stacksz) {
+  private[this] var exc: Throwable = _;
+  private[this] var ret: T = _;
+
+  override def run() {
+    try { ret = body; }
+    catch { case e: Throwable => exc = e; }
+  }
+  def get: T =
+    if (isAlive) throw new IllegalArgumentException("still running");
+    else if (exc != null) throw exc;
+    else ret;
+}
+def valueThread[T](name: String, run: Boolean = true)
+                 (body: => T): ValueThread[T] = {
+  val t = new ValueThread(name)(body);
+  if (run) t.start();
+  t
+}
+
 /*----- Quoting and parsing tokens ----------------------------------------*/
 
 def quoteTokens(v: Seq[String]): String = {
@@ -294,38 +459,120 @@ def splitTokens(s: String, pos: Int = 0): Seq[String] = {
   val b = List.newBuilder[String];
   var i = pos;
 
-  while (nextToken(s, i) match {
-    case Some((w, j)) => b += w; i = j; true
-    case None => false
-  }) ();
+  loopUnit { exit => nextToken(s, i) match {
+    case Some((w, j)) => b += w; i = j;
+    case None => exit;
+  } }
   b.result
 }
 
+/*----- Other random things -----------------------------------------------*/
+
 trait LookaheadIterator[T] extends BufferedIterator[T] {
-  private[this] var st: Option[T] = None;
+  /* An iterator in terms of a single `maybe there's another item' function.
+   *
+   * It seems like every time I write an iterator in Scala, the only way to
+   * find out whether there's a next item, for `hasNext', is to actually try
+   * to fetch it.  So here's an iterator in terms of a function which goes
+   * off and maybe returns a next thing.  It turns out to be easy to satisfy
+   * the additional requirements for `BufferedIterator', so why not?
+   */
+
+  /* Subclass responsibility. */
   protected def fetch(): Option[T];
+
+  /* The machinery.  `st' is `None' if there's no current item, null if we've
+   * actually hit the end, or `Some(x)' if the current item is x.
+   */
+  private[this] var st: Option[T] = None;
   private[this] def peek() {
+    /* Arrange to have a current item. */
     if (st == None) fetch() match {
       case None => st = null;
       case x@Some(_) => st = x;
     }
   }
+
+  /* The `BufferedIterator' protocol. */
   override def hasNext: Boolean = { peek(); st != null }
-  override def head(): T =
+  override def head: T =
     { peek(); if (st == null) throw new NoSuchElementException; st.get }
-  override def next(): T = { val it = head(); st = None; it }
+  override def next(): T = { val it = head; st = None; it }
 }
 
-def lines(r: Reader) = new LookaheadIterator[String] {
-  /* Iterates over the lines of text in a `Reader' object. */
+def bufferedReader(r: Reader): BufferedReader = r match {
+  case br: BufferedReader => br
+  case _ => new BufferedReader(r)
+}
 
-  private[this] val in = r match {
-    case br: BufferedReader => br;
-    case _ => new BufferedReader(r);
+def lines(r: BufferedReader): BufferedIterator[String] =
+  new LookaheadIterator[String] {
+    /* Iterates over the lines of text in a `Reader' object. */
+    override protected def fetch() = Option(r.readLine());
+  }
+def lines(r: Reader): BufferedIterator[String] = lines(bufferedReader(r));
+
+def blocks(in: InputStream, blksz: Int):
+       BufferedIterator[(Array[Byte], Int)] =
+  /* Iterates over (possibly irregularly sized) blocks in a stream. */
+  new LookaheadIterator[(Array[Byte], Int)] {
+    val buf = new Array[Byte](blksz)
+    override protected def fetch() = {
+      val n = in.read(buf);
+      if (n < 0) None
+      else Some((buf, n))
+    }
+  }
+def blocks(in: InputStream):
+       BufferedIterator[(Array[Byte], Int)] = blocks(in, 65536);
+
+def blocks(in: BufferedReader, blksz: Int):
+       BufferedIterator[(Array[Char], Int)] =
+  /* Iterates over (possibly irregularly sized) blocks in a reader. */
+  new LookaheadIterator[(Array[Char], Int)] {
+    val buf = new Array[Char](blksz)
+    override protected def fetch() = {
+      val n = in.read(buf);
+      if (n < 0) None
+      else Some((buf, n))
+    }
   }
-  protected override def fetch(): Option[String] = Option(in.readLine);
+def blocks(in: BufferedReader):
+       BufferedIterator[(Array[Char], Int)] = blocks(in, 65536);
+def blocks(r: Reader, blksz: Int): BufferedIterator[(Array[Char], Int)] =
+  blocks(bufferedReader(r), blksz);
+def blocks(r: Reader): BufferedIterator[(Array[Char], Int)] =
+  blocks(bufferedReader(r));
+
+def oxford(conj: String, things: Seq[String]): String = things match {
+  case Seq() => "<nothing>"
+  case Seq(a) => a
+  case Seq(a, b) => s"$a $conj $b"
+  case Seq(a, tail@_*) =>
+    val sb = new StringBuilder;
+    sb ++= a; sb ++= ", ";
+    def iter(rest: Seq[String]) {
+      rest match {
+       case Seq() => unreachable;
+       case Seq(a) => sb ++= conj; sb += ' '; sb ++= a;
+       case Seq(a, tail@_*) => sb ++= a; sb ++= ", "; iter(tail);
+      }
+    }
+    iter(tail);
+    sb.result
 }
 
+val datefmt = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
+
+def formatDuration(t: Int): String =
+  if (t < -1) "???"
+  else {
+    val (s, t1) = (t%60, t/60);
+    val (m, h) = (t1%60, t1/60);
+    if (h > 0) f"$h%d:$m%02d:$s%02d"
+    else f"$m%02d:$s%02d"
+  }
+
 /*----- That's all, folks -------------------------------------------------*/
 
 }