2009-03-12 21:19:21 +01:00
|
|
|
/**
|
2009-12-27 16:01:53 +01:00
|
|
|
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
|
2009-03-12 21:19:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2010-06-19 17:41:35 +12:00
|
|
|
package se.scalablesolutions.akka.dataflow
|
2009-03-12 21:19:21 +01:00
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
import java.util.concurrent.{ConcurrentLinkedQueue, LinkedBlockingQueue}
|
|
|
|
|
|
2010-05-06 08:13:12 +02:00
|
|
|
import se.scalablesolutions.akka.actor.{Actor, ActorRef}
|
2010-05-02 10:46:49 +02:00
|
|
|
import se.scalablesolutions.akka.actor.Actor._
|
2010-03-01 22:03:17 +01:00
|
|
|
import se.scalablesolutions.akka.dispatch.CompletableFuture
|
2010-08-19 07:01:09 +02:00
|
|
|
import se.scalablesolutions.akka.AkkaException
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-04-27 20:06:48 +02:00
|
|
|
/**
|
|
|
|
|
* Implements Oz-style dataflow (single assignment) variables.
|
2009-12-08 16:17:22 +01:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-12-08 16:17:22 +01:00
|
|
|
object DataFlow {
|
2010-08-23 15:51:02 +02:00
|
|
|
object Start
|
|
|
|
|
object Exit
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2010-08-20 12:41:37 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
import java.util.concurrent.{ConcurrentLinkedQueue, LinkedBlockingQueue}
|
|
|
|
|
import scala.collection.JavaConversions._
|
|
|
|
|
import se.scalablesolutions.akka.actor.Actor
|
|
|
|
|
import se.scalablesolutions.akka.dispatch.CompletableFuture
|
2010-03-18 08:37:44 +01:00
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def thread(body: => Unit): Unit = spawn(body)
|
2009-03-12 21:19:21 +01:00
|
|
|
|
2009-12-08 16:17:22 +01:00
|
|
|
def thread[A <: AnyRef, R <: AnyRef](body: A => R) =
|
2010-05-16 20:15:08 +02:00
|
|
|
actorOf(new ReactiveEventBasedThread(body)).start
|
2009-12-08 16:17:22 +01:00
|
|
|
|
|
|
|
|
private class ReactiveEventBasedThread[A <: AnyRef, T <: AnyRef](body: A => T)
|
|
|
|
|
extends Actor {
|
|
|
|
|
def receive = {
|
2010-08-20 12:41:37 +02:00
|
|
|
case Exit => self.stop
|
2010-05-16 10:59:06 +02:00
|
|
|
case message => self.reply(body(message.asInstanceOf[A]))
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-27 20:06:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-12-08 16:17:22 +01:00
|
|
|
sealed class DataFlowVariable[T <: Any] {
|
2010-08-20 12:41:37 +02:00
|
|
|
val TIME_OUT = 1000 * 60 // 60 seconds default timeout
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-03-12 21:19:21 +01:00
|
|
|
private sealed abstract class DataFlowVariableMessage
|
2009-12-08 16:17:22 +01:00
|
|
|
private case class Set[T <: Any](value: T) extends DataFlowVariableMessage
|
2010-08-23 15:51:02 +02:00
|
|
|
private object Get extends DataFlowVariableMessage
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-03-12 21:19:21 +01:00
|
|
|
private val value = new AtomicReference[Option[T]](None)
|
2010-05-06 08:13:12 +02:00
|
|
|
private val blockedReaders = new ConcurrentLinkedQueue[ActorRef]
|
2009-03-12 21:19:21 +01:00
|
|
|
|
2009-12-08 16:17:22 +01:00
|
|
|
private class In[T <: Any](dataFlow: DataFlowVariable[T]) extends Actor {
|
2010-05-16 10:59:06 +02:00
|
|
|
self.timeout = TIME_OUT
|
2009-12-08 16:17:22 +01:00
|
|
|
def receive = {
|
2010-08-20 12:41:37 +02:00
|
|
|
case s@Set(v) =>
|
2009-07-07 22:11:27 +02:00
|
|
|
if (dataFlow.value.compareAndSet(None, Some(v.asInstanceOf[T]))) {
|
2010-08-20 12:41:37 +02:00
|
|
|
while(dataFlow.blockedReaders.peek ne null)
|
|
|
|
|
dataFlow.blockedReaders.poll ! s
|
2009-03-12 21:19:21 +01:00
|
|
|
} else throw new DataFlowVariableException(
|
|
|
|
|
"Attempt to change data flow variable (from [" + dataFlow.value.get + "] to [" + v + "])")
|
2010-08-23 15:51:02 +02:00
|
|
|
case Exit => self.stop
|
2009-12-08 16:17:22 +01:00
|
|
|
}
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
2009-12-08 16:17:22 +01:00
|
|
|
|
|
|
|
|
private class Out[T <: Any](dataFlow: DataFlowVariable[T]) extends Actor {
|
2010-05-16 10:59:06 +02:00
|
|
|
self.timeout = TIME_OUT
|
2010-08-23 15:51:02 +02:00
|
|
|
private var readerFuture: Option[CompletableFuture[Any]] = None
|
2009-12-08 16:17:22 +01:00
|
|
|
def receive = {
|
2009-11-21 19:34:42 +01:00
|
|
|
case Get =>
|
2010-08-23 15:51:02 +02:00
|
|
|
dataFlow.value.get match {
|
|
|
|
|
case Some(value) => self reply value
|
|
|
|
|
case None => readerFuture = self.senderFuture
|
|
|
|
|
}
|
|
|
|
|
case Set(v:T) => readerFuture.map(_ completeWithResult v)
|
|
|
|
|
case Exit => self.stop
|
2009-12-08 16:17:22 +01:00
|
|
|
}
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2010-05-16 20:15:08 +02:00
|
|
|
private[this] val in = actorOf(new In(this)).start
|
2009-03-12 21:19:21 +01:00
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def <<(ref: DataFlowVariable[T]) = if(this.value.get.isEmpty) in ! Set(ref())
|
2009-03-12 21:19:21 +01:00
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def <<(value: T): Unit = if(this.value.get.isEmpty) in ! Set(value)
|
2009-12-08 16:17:22 +01:00
|
|
|
|
|
|
|
|
def apply(): T = {
|
2010-08-20 12:41:37 +02:00
|
|
|
value.get getOrElse {
|
2010-05-16 20:15:08 +02:00
|
|
|
val out = actorOf(new Out(this)).start
|
2010-08-23 15:51:02 +02:00
|
|
|
blockedReaders offer out
|
2010-06-21 12:25:24 +02:00
|
|
|
val result = (out !! Get).as[T]
|
2010-03-30 23:58:50 +02:00
|
|
|
out ! Exit
|
2010-08-20 12:41:37 +02:00
|
|
|
result.getOrElse(throw new DataFlowVariableException("Timed out (after " + TIME_OUT + " milliseconds) while waiting for result"))
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2010-03-30 23:58:50 +02:00
|
|
|
def shutdown = in ! Exit
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
|
|
|
|
|
2009-04-27 20:06:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2010-08-23 15:51:02 +02:00
|
|
|
/*FIXME I do not work
|
|
|
|
|
class DataFlowStream[T <: Any] extends Seq[T] {
|
2009-03-12 21:19:21 +01:00
|
|
|
private[this] val queue = new LinkedBlockingQueue[DataFlowVariable[T]]
|
|
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def <<<(ref: DataFlowVariable[T]): Boolean = queue offer ref
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def <<<(value: T): Boolean = {
|
2009-03-12 21:19:21 +01:00
|
|
|
val ref = new DataFlowVariable[T]
|
|
|
|
|
ref << value
|
2010-08-23 15:51:02 +02:00
|
|
|
queue offer ref
|
2009-12-08 16:17:22 +01:00
|
|
|
}
|
|
|
|
|
|
2010-08-23 15:51:02 +02:00
|
|
|
def apply(): T = queue.take.apply
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-03-12 21:19:21 +01:00
|
|
|
def take: DataFlowVariable[T] = queue.take
|
|
|
|
|
|
|
|
|
|
//==== For Seq ====
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-03-12 21:19:21 +01:00
|
|
|
def length: Int = queue.size
|
|
|
|
|
|
|
|
|
|
def apply(i: Int): T = {
|
|
|
|
|
if (i == 0) apply()
|
2009-12-08 16:17:22 +01:00
|
|
|
else throw new UnsupportedOperationException(
|
|
|
|
|
"Access by index other than '0' is not supported by DataFlowStream")
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-21 16:39:32 +01:00
|
|
|
def iterator: Iterator[T] = new Iterator[T] {
|
2010-08-23 15:51:02 +02:00
|
|
|
private val i = queue.iterator
|
|
|
|
|
def hasNext: Boolean = i.hasNext
|
|
|
|
|
def next: T = { val ref = i.next; ref() }
|
2009-03-12 21:19:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def toList: List[T] = queue.toArray.toList.asInstanceOf[List[T]]
|
2010-08-23 15:51:02 +02:00
|
|
|
}*/
|
2009-12-08 16:17:22 +01:00
|
|
|
|
2009-04-27 20:06:48 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2010-08-19 07:01:09 +02:00
|
|
|
class DataFlowVariableException(msg: String) extends AkkaException(msg)
|
2010-08-19 17:34:32 +02:00
|
|
|
}
|