From 0edfb3537175136113b3287af070228acf1ff4e4 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Thu, 25 Mar 2010 13:18:23 +0000 Subject: Renamed Replyable* source files to the types th... Renamed Replyable* source files to the types they define. No review. --- src/actors/scala/actors/ActorCanReply.scala | 165 +++++++++++++++++++++++++ src/actors/scala/actors/CanReply.scala | 65 ++++++++++ src/actors/scala/actors/ReactorCanReply.scala | 115 +++++++++++++++++ src/actors/scala/actors/Replyable.scala | 65 ---------- src/actors/scala/actors/ReplyableActor.scala | 165 ------------------------- src/actors/scala/actors/ReplyableReactor.scala | 115 ----------------- 6 files changed, 345 insertions(+), 345 deletions(-) create mode 100644 src/actors/scala/actors/ActorCanReply.scala create mode 100644 src/actors/scala/actors/CanReply.scala create mode 100644 src/actors/scala/actors/ReactorCanReply.scala delete mode 100644 src/actors/scala/actors/Replyable.scala delete mode 100644 src/actors/scala/actors/ReplyableActor.scala delete mode 100644 src/actors/scala/actors/ReplyableReactor.scala (limited to 'src/actors') diff --git a/src/actors/scala/actors/ActorCanReply.scala b/src/actors/scala/actors/ActorCanReply.scala new file mode 100644 index 0000000000..8cba425b4c --- /dev/null +++ b/src/actors/scala/actors/ActorCanReply.scala @@ -0,0 +1,165 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + +package scala.actors + +import java.util.concurrent.ExecutionException + +/** + * The ActorCanReply trait provides message send operations that + * may result in a response from the receiver. + * + * @author Philipp Haller + */ +private[actors] trait ActorCanReply extends ReactorCanReply { + thiz: AbstractActor with ReplyReactor => + + /** + * Sends msg to this actor and awaits reply + * (synchronous). + * + * @param msg the message to be sent + * @return the reply + */ + override def !?(msg: Any): Any = { + val replyCh = new Channel[Any](Actor.self(thiz.scheduler)) + thiz.send(msg, replyCh) + replyCh.receive { + case x => x + } + } + + /** + * Sends msg to this actor and awaits reply + * (synchronous) within msec milliseconds. + * + * @param msec the time span before timeout + * @param msg the message to be sent + * @return None in case of timeout, otherwise + * Some(x) where x is the reply + */ + override def !?(msec: Long, msg: Any): Option[Any] = { + val replyCh = new Channel[Any](Actor.self(thiz.scheduler)) + thiz.send(msg, replyCh) + replyCh.receiveWithin(msec) { + case TIMEOUT => None + case x => Some(x) + } + } + + /** + * Sends msg to this actor and immediately + * returns a future representing the reply value. + * The reply is post-processed using the partial function + * handler. This also allows to recover a more + * precise type for the reply value. + */ + override def !![A](msg: Any, handler: PartialFunction[Any, A]): Future[A] = { + val ftch = new Channel[A](Actor.self(thiz.scheduler)) + thiz.send(msg, new OutputChannel[Any] { + def !(msg: Any) = + ftch ! handler(msg) + def send(msg: Any, replyTo: OutputChannel[Any]) = + ftch.send(handler(msg), replyTo) + def forward(msg: Any) = + ftch.forward(handler(msg)) + def receiver = + ftch.receiver + }) + Futures.fromInputChannel(ftch) + } + + /** + * Sends msg to this actor and immediately + * returns a future representing the reply value. + */ + override def !!(msg: Any): Future[Any] = { + val ftch = new Channel[Any](Actor.self(thiz.scheduler)) + val linkedChannel = new AbstractActor { + def !(msg: Any) = { + ftch ! msg + thiz unlinkFrom this + } + def send(msg: Any, replyTo: OutputChannel[Any]) = { + ftch.send(msg, replyTo) + thiz unlinkFrom this + } + def forward(msg: Any) = { + ftch.forward(msg) + thiz unlinkFrom this + } + def receiver = + ftch.receiver + def linkTo(to: AbstractActor) { /* do nothing */ } + def unlinkFrom(from: AbstractActor) { /* do nothing */ } + def exit(from: AbstractActor, reason: AnyRef) { + ftch.send(Exit(from, reason), thiz) + thiz unlinkFrom this + } + // should never be invoked; return dummy value + def !?(msg: Any) = msg + // should never be invoked; return dummy value + def !?(msec: Long, msg: Any): Option[Any] = Some(msg) + // should never be invoked; return dummy value + override def !!(msg: Any): Future[Any] = { + val someChan = new Channel[Any](Actor.self(thiz.scheduler)) + Futures.fromInputChannel(someChan) + } + // should never be invoked; return dummy value + override def !![A](msg: Any, f: PartialFunction[Any, A]): Future[A] = { + val someChan = new Channel[A](Actor.self(thiz.scheduler)) + Futures.fromInputChannel(someChan) + } + } + thiz linkTo linkedChannel + thiz.send(msg, linkedChannel) + new Future[Any](ftch) { + var exitReason: Option[Any] = None + val handleReply: PartialFunction[Any, Unit] = { + case Exit(from, reason) => + exitReason = Some(reason) + case any => + fvalue = Some(any) + } + + def apply(): Any = + if (isSet) { + if (!fvalue.isEmpty) + fvalue.get + else if (!exitReason.isEmpty) { + val reason = exitReason.get + if (reason.isInstanceOf[Throwable]) + throw new ExecutionException(reason.asInstanceOf[Throwable]) + else + throw new ExecutionException(new Exception(reason.toString())) + } + } else inputChannel.receive(handleReply andThen { _ => apply() }) + + def respond(k: Any => Unit): Unit = + if (isSet) + apply() + else + inputChannel.react(handleReply andThen { _ => k(apply()) }) + + def isSet = (fvalue match { + case None => + val handleTimeout: PartialFunction[Any, Boolean] = { + case TIMEOUT => + false + } + val whatToDo = + handleTimeout orElse (handleReply andThen { _ => true }) + inputChannel.receiveWithin(0)(whatToDo) + case Some(_) => true + }) || !exitReason.isEmpty + } + } + +} diff --git a/src/actors/scala/actors/CanReply.scala b/src/actors/scala/actors/CanReply.scala new file mode 100644 index 0000000000..23f0a5319b --- /dev/null +++ b/src/actors/scala/actors/CanReply.scala @@ -0,0 +1,65 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + +package scala.actors + +/** + * The CanReply trait defines result-bearing message send operations. + * + * @author Philipp Haller + */ +trait CanReply[-T, +R] { + + /** + * Sends msg to this CanReply and + * awaits reply (synchronous). + * + * @param msg the message to be sent + * @return the reply + */ + def !?(msg: T): R + + /** + * Sends msg to this CanReply and + * awaits reply (synchronous) within msec + * milliseconds. + * + * @param msec the time span before timeout + * @param msg the message to be sent + * @return None in case of timeout, otherwise + * Some(x) where x is the reply + */ + def !?(msec: Long, msg: T): Option[R] + + /** + * Sends msg to this CanReply and + * immediately returns a future representing the reply value. + * + * @param msg the message to be sent + * @return the future + */ + def !!(msg: T): () => R = + () => this !? msg + + /** + * Sends msg to this CanReply and + * immediately returns a future representing the reply value. + * The reply is post-processed using the partial function + * handler. This also allows to recover a more + * precise type for the reply value. + * + * @param msg the message to be sent + * @param handler the function to be applied to the response + * @return the future + */ + def !![P](msg: T, handler: PartialFunction[R, P]): () => P = + () => handler(this !? msg) + +} diff --git a/src/actors/scala/actors/ReactorCanReply.scala b/src/actors/scala/actors/ReactorCanReply.scala new file mode 100644 index 0000000000..c53e3a78e1 --- /dev/null +++ b/src/actors/scala/actors/ReactorCanReply.scala @@ -0,0 +1,115 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + +package scala.actors + +/** + * The ReactorCanReply trait provides message send operations that + * may result in a response from the receiver. + * + * @author Philipp Haller + */ +private[actors] trait ReactorCanReply extends CanReply[Any, Any] { + _: ReplyReactor => + + /** + * Sends msg to this actor and awaits reply + * (synchronous). + * + * @param msg the message to be sent + * @return the reply + */ + def !?(msg: Any): Any = + (this !! msg)() + + /** + * Sends msg to this actor and awaits reply + * (synchronous) within msec milliseconds. + * + * @param msec the time span before timeout + * @param msg the message to be sent + * @return None in case of timeout, otherwise + * Some(x) where x is the reply + */ + def !?(msec: Long, msg: Any): Option[Any] = { + val myself = Actor.rawSelf(this.scheduler) + val res = new scala.concurrent.SyncVar[Any] + val out = new OutputChannel[Any] { + def !(msg: Any) = + res set msg + def send(msg: Any, replyTo: OutputChannel[Any]) = + res set msg + def forward(msg: Any) = + res set msg + def receiver = + myself.asInstanceOf[Actor] + } + this.send(msg, out) + res.get(msec) + } + + /** + * Sends msg to this actor and immediately + * returns a future representing the reply value. + */ + override def !!(msg: Any): Future[Any] = + this !! (msg, { case x => x }) + + /** + * Sends msg to this actor and immediately + * returns a future representing the reply value. + * The reply is post-processed using the partial function + * handler. This also allows to recover a more + * precise type for the reply value. + */ + override def !![A](msg: Any, handler: PartialFunction[Any, A]): Future[A] = { + val myself = Actor.rawSelf(this.scheduler) + val ftch = new ReactChannel[A](myself) + val res = new scala.concurrent.SyncVar[A] + + val out = new OutputChannel[Any] { + def !(msg: Any) = { + val msg1 = handler(msg) + ftch ! msg1 + res set msg1 + } + def send(msg: Any, replyTo: OutputChannel[Any]) = { + val msg1 = handler(msg) + ftch.send(msg1, replyTo) + res set msg1 + } + def forward(msg: Any) = { + val msg1 = handler(msg) + ftch forward msg1 + res set msg1 + } + def receiver = + myself.asInstanceOf[Actor] + } + + this.send(msg, out) + + new Future[A](ftch) { + def apply() = { + if (!isSet) + fvalue = Some(res.get) + + fvalueTyped + } + def respond(k: A => Unit): Unit = + if (isSet) k(fvalueTyped) + else inputChannel.react { + case any => fvalue = Some(any); k(fvalueTyped) + } + def isSet = + !fvalue.isEmpty + } + } +} diff --git a/src/actors/scala/actors/Replyable.scala b/src/actors/scala/actors/Replyable.scala deleted file mode 100644 index 23f0a5319b..0000000000 --- a/src/actors/scala/actors/Replyable.scala +++ /dev/null @@ -1,65 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - -package scala.actors - -/** - * The CanReply trait defines result-bearing message send operations. - * - * @author Philipp Haller - */ -trait CanReply[-T, +R] { - - /** - * Sends msg to this CanReply and - * awaits reply (synchronous). - * - * @param msg the message to be sent - * @return the reply - */ - def !?(msg: T): R - - /** - * Sends msg to this CanReply and - * awaits reply (synchronous) within msec - * milliseconds. - * - * @param msec the time span before timeout - * @param msg the message to be sent - * @return None in case of timeout, otherwise - * Some(x) where x is the reply - */ - def !?(msec: Long, msg: T): Option[R] - - /** - * Sends msg to this CanReply and - * immediately returns a future representing the reply value. - * - * @param msg the message to be sent - * @return the future - */ - def !!(msg: T): () => R = - () => this !? msg - - /** - * Sends msg to this CanReply and - * immediately returns a future representing the reply value. - * The reply is post-processed using the partial function - * handler. This also allows to recover a more - * precise type for the reply value. - * - * @param msg the message to be sent - * @param handler the function to be applied to the response - * @return the future - */ - def !![P](msg: T, handler: PartialFunction[R, P]): () => P = - () => handler(this !? msg) - -} diff --git a/src/actors/scala/actors/ReplyableActor.scala b/src/actors/scala/actors/ReplyableActor.scala deleted file mode 100644 index 8cba425b4c..0000000000 --- a/src/actors/scala/actors/ReplyableActor.scala +++ /dev/null @@ -1,165 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - -package scala.actors - -import java.util.concurrent.ExecutionException - -/** - * The ActorCanReply trait provides message send operations that - * may result in a response from the receiver. - * - * @author Philipp Haller - */ -private[actors] trait ActorCanReply extends ReactorCanReply { - thiz: AbstractActor with ReplyReactor => - - /** - * Sends msg to this actor and awaits reply - * (synchronous). - * - * @param msg the message to be sent - * @return the reply - */ - override def !?(msg: Any): Any = { - val replyCh = new Channel[Any](Actor.self(thiz.scheduler)) - thiz.send(msg, replyCh) - replyCh.receive { - case x => x - } - } - - /** - * Sends msg to this actor and awaits reply - * (synchronous) within msec milliseconds. - * - * @param msec the time span before timeout - * @param msg the message to be sent - * @return None in case of timeout, otherwise - * Some(x) where x is the reply - */ - override def !?(msec: Long, msg: Any): Option[Any] = { - val replyCh = new Channel[Any](Actor.self(thiz.scheduler)) - thiz.send(msg, replyCh) - replyCh.receiveWithin(msec) { - case TIMEOUT => None - case x => Some(x) - } - } - - /** - * Sends msg to this actor and immediately - * returns a future representing the reply value. - * The reply is post-processed using the partial function - * handler. This also allows to recover a more - * precise type for the reply value. - */ - override def !![A](msg: Any, handler: PartialFunction[Any, A]): Future[A] = { - val ftch = new Channel[A](Actor.self(thiz.scheduler)) - thiz.send(msg, new OutputChannel[Any] { - def !(msg: Any) = - ftch ! handler(msg) - def send(msg: Any, replyTo: OutputChannel[Any]) = - ftch.send(handler(msg), replyTo) - def forward(msg: Any) = - ftch.forward(handler(msg)) - def receiver = - ftch.receiver - }) - Futures.fromInputChannel(ftch) - } - - /** - * Sends msg to this actor and immediately - * returns a future representing the reply value. - */ - override def !!(msg: Any): Future[Any] = { - val ftch = new Channel[Any](Actor.self(thiz.scheduler)) - val linkedChannel = new AbstractActor { - def !(msg: Any) = { - ftch ! msg - thiz unlinkFrom this - } - def send(msg: Any, replyTo: OutputChannel[Any]) = { - ftch.send(msg, replyTo) - thiz unlinkFrom this - } - def forward(msg: Any) = { - ftch.forward(msg) - thiz unlinkFrom this - } - def receiver = - ftch.receiver - def linkTo(to: AbstractActor) { /* do nothing */ } - def unlinkFrom(from: AbstractActor) { /* do nothing */ } - def exit(from: AbstractActor, reason: AnyRef) { - ftch.send(Exit(from, reason), thiz) - thiz unlinkFrom this - } - // should never be invoked; return dummy value - def !?(msg: Any) = msg - // should never be invoked; return dummy value - def !?(msec: Long, msg: Any): Option[Any] = Some(msg) - // should never be invoked; return dummy value - override def !!(msg: Any): Future[Any] = { - val someChan = new Channel[Any](Actor.self(thiz.scheduler)) - Futures.fromInputChannel(someChan) - } - // should never be invoked; return dummy value - override def !![A](msg: Any, f: PartialFunction[Any, A]): Future[A] = { - val someChan = new Channel[A](Actor.self(thiz.scheduler)) - Futures.fromInputChannel(someChan) - } - } - thiz linkTo linkedChannel - thiz.send(msg, linkedChannel) - new Future[Any](ftch) { - var exitReason: Option[Any] = None - val handleReply: PartialFunction[Any, Unit] = { - case Exit(from, reason) => - exitReason = Some(reason) - case any => - fvalue = Some(any) - } - - def apply(): Any = - if (isSet) { - if (!fvalue.isEmpty) - fvalue.get - else if (!exitReason.isEmpty) { - val reason = exitReason.get - if (reason.isInstanceOf[Throwable]) - throw new ExecutionException(reason.asInstanceOf[Throwable]) - else - throw new ExecutionException(new Exception(reason.toString())) - } - } else inputChannel.receive(handleReply andThen { _ => apply() }) - - def respond(k: Any => Unit): Unit = - if (isSet) - apply() - else - inputChannel.react(handleReply andThen { _ => k(apply()) }) - - def isSet = (fvalue match { - case None => - val handleTimeout: PartialFunction[Any, Boolean] = { - case TIMEOUT => - false - } - val whatToDo = - handleTimeout orElse (handleReply andThen { _ => true }) - inputChannel.receiveWithin(0)(whatToDo) - case Some(_) => true - }) || !exitReason.isEmpty - } - } - -} diff --git a/src/actors/scala/actors/ReplyableReactor.scala b/src/actors/scala/actors/ReplyableReactor.scala deleted file mode 100644 index c53e3a78e1..0000000000 --- a/src/actors/scala/actors/ReplyableReactor.scala +++ /dev/null @@ -1,115 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2005-2010, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - -package scala.actors - -/** - * The ReactorCanReply trait provides message send operations that - * may result in a response from the receiver. - * - * @author Philipp Haller - */ -private[actors] trait ReactorCanReply extends CanReply[Any, Any] { - _: ReplyReactor => - - /** - * Sends msg to this actor and awaits reply - * (synchronous). - * - * @param msg the message to be sent - * @return the reply - */ - def !?(msg: Any): Any = - (this !! msg)() - - /** - * Sends msg to this actor and awaits reply - * (synchronous) within msec milliseconds. - * - * @param msec the time span before timeout - * @param msg the message to be sent - * @return None in case of timeout, otherwise - * Some(x) where x is the reply - */ - def !?(msec: Long, msg: Any): Option[Any] = { - val myself = Actor.rawSelf(this.scheduler) - val res = new scala.concurrent.SyncVar[Any] - val out = new OutputChannel[Any] { - def !(msg: Any) = - res set msg - def send(msg: Any, replyTo: OutputChannel[Any]) = - res set msg - def forward(msg: Any) = - res set msg - def receiver = - myself.asInstanceOf[Actor] - } - this.send(msg, out) - res.get(msec) - } - - /** - * Sends msg to this actor and immediately - * returns a future representing the reply value. - */ - override def !!(msg: Any): Future[Any] = - this !! (msg, { case x => x }) - - /** - * Sends msg to this actor and immediately - * returns a future representing the reply value. - * The reply is post-processed using the partial function - * handler. This also allows to recover a more - * precise type for the reply value. - */ - override def !![A](msg: Any, handler: PartialFunction[Any, A]): Future[A] = { - val myself = Actor.rawSelf(this.scheduler) - val ftch = new ReactChannel[A](myself) - val res = new scala.concurrent.SyncVar[A] - - val out = new OutputChannel[Any] { - def !(msg: Any) = { - val msg1 = handler(msg) - ftch ! msg1 - res set msg1 - } - def send(msg: Any, replyTo: OutputChannel[Any]) = { - val msg1 = handler(msg) - ftch.send(msg1, replyTo) - res set msg1 - } - def forward(msg: Any) = { - val msg1 = handler(msg) - ftch forward msg1 - res set msg1 - } - def receiver = - myself.asInstanceOf[Actor] - } - - this.send(msg, out) - - new Future[A](ftch) { - def apply() = { - if (!isSet) - fvalue = Some(res.get) - - fvalueTyped - } - def respond(k: A => Unit): Unit = - if (isSet) k(fvalueTyped) - else inputChannel.react { - case any => fvalue = Some(any); k(fvalueTyped) - } - def isSet = - !fvalue.isEmpty - } - } -} -- cgit v1.2.3