eengbrec / Scala Enhancements
In-progress enhancements to the Scala standard library, mostly centered around actors.
Clone this repository (size: 130.0 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/eengbrec/scala-enhancements/
| commit 175: | 9da337a0db7f |
| parent 174: | f10e617c8439 |
| branch: | break_oca_diamond |
| tags: | tip |
renamed ReallyAbstractActor to AbstractActor
8 months ago
| r175:9da337a0db7f | 258 loc | 7.5 KB | embed / history / annotate / raw / |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | /* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.actors
object ActorDSL {
def self(scheduler: IScheduler): ActorDSL = Actor.rawSelf(scheduler) match {
case a: ActorDSL => a
case _ => throw new UnsupportedOperationException
}
def self: ActorDSL = Actor.rawSelf match {
case a: ActorDSL => a
case _ => throw new UnsupportedOperationException
}
/**
* <p>This is a factory method for creating actors.</p>
*
* <p>The following example demonstrates its usage:</p>
*
* <pre>
* import scala.actors.Actor._
* ...
* val a = actor {
* ...
* }
* </pre>
*
* @param body the code block to be executed by the newly created actor
* @return the newly created actor. Note that it is automatically started.
*/
def actor(body: => Unit): Actor = {
val a = new Actor {
def act() = body
override final val scheduler: IScheduler = Actor.parentScheduler
}
a.start()
a
}
/**
* <p>
* This is a factory method for creating actors whose
* body is defined using a <code>Responder</code>.
* </p>
*
* <p>The following example demonstrates its usage:</p>
*
* <pre>
* import scala.actors.Actor._
* import Responder.exec
* ...
* val a = reactor {
* for {
* res <- b !! MyRequest;
* if exec(println("result: "+res))
* } yield {}
* }
* </pre>
*
* @param body the <code>Responder</code> to be executed by the newly created actor
* @return the newly created actor. Note that it is automatically started.
*/
def reactor(body: => Responder[Unit]): ActorDSL = {
val a = new Actor {
def act() {
Responder.run(body)
}
override final val scheduler: IScheduler = Actor.parentScheduler
}
a.start()
a
}
/**
* Receives the next message from the mailbox of the current actor
* <code>self</code>.
*/
def ? : Any = self.?
/**
* Receives a message from the mailbox of
* <code>self</code>. Blocks if no message matching any of the
* cases of <code>f</code> can be received.
*
* @param f a partial function specifying patterns and actions
* @return the result of processing the received message
*/
def receive[A](f: PartialFunction[Any, A]): A = self.receive(f)
/**
* Receives a message from the mailbox of
* <code>self</code>. Blocks at most <code>msec</code>
* milliseconds if no message matching any of the cases of
* <code>f</code> can be received. If no message could be
* received the <code>TIMEOUT</code> action is executed if
* specified.
*
* @param msec the time span before timeout
* @param f a partial function specifying patterns and actions
* @return the result of processing the received message
*/
def receiveWithin[R](msec: Long)(f: PartialFunction[Any, R]): R = self.receiveWithin(msec)(f)
/**
* Lightweight variant of <code>receive</code>.
*
* Actions in <code>f</code> have to contain the rest of the
* computation of <code>self</code>, as this method will never
* return.
*
* @param f a partial function specifying patterns and actions
* @return this function never returns
*/
def react(f: PartialFunction[Any, Unit]): Nothing = self.react(f)
/**
* Lightweight variant of <code>receiveWithin</code>.
*
* Actions in <code>f</code> have to contain the rest of the
* computation of <code>self</code>, as this method will never
* return.
*
* @param msec the time span before timeout
* @param f a partial function specifying patterns and actions
* @return this function never returns
*/
def reactWithin(msec: Long)(f: PartialFunction[Any, Unit]): Nothing =
self.reactWithin(msec)(f)
def eventloop(f: PartialFunction[Any, Unit]): Nothing =
self.react(new RecursiveProxyHandler(self, f))
private class RecursiveProxyHandler(a: ActorDSL, f: PartialFunction[Any, Unit])
extends PartialFunction[Any, Unit] {
def isDefinedAt(m: Any): Boolean =
true // events are immediately removed from the mailbox
def apply(m: Any) {
if (f.isDefinedAt(m)) f(m)
a.react(this)
}
}
def sender: Option[OutputChannel[Any]] = self.sender
/**
* Send <code>msg</code> to the actor waiting in a call to
* <code>!?</code>.
*/
def reply(msg: Any): Unit = self.reply(msg)
/**
* Send <code>()</code> to the actor waiting in a call to
* <code>!?</code>.
*/
def reply(): Unit = self.reply(())
/**
* Returns the number of messages in <code>self</code>'s mailbox
*
* @return the number of messages in <code>self</code>'s mailbox
*/
def mailboxSize: Int = self.mailboxSize
/**
* <p>
* Converts a synchronous event-based operation into
* an asynchronous <code>Responder</code>.
* </p>
*
* <p>The following example demonstrates its usage:</p>
*
* <pre>
* val adder = reactor {
* for {
* _ <- respondOn(react) { case Add(a, b) => reply(a+b) }
* } yield {}
* }
* </pre>
*/
def respondOn[A, B](fun: PartialFunction[A, Unit] => Nothing):
PartialFunction[A, B] => Responder[B] =
(caseBlock: PartialFunction[A, B]) => new Responder[B] {
def respond(k: B => Unit) = fun(caseBlock andThen k)
}
def loop(body: => Unit): Unit = self.loop(body)
def loopWhile(cond: => Boolean)(body: => Unit): Unit = self.loopWhile(cond)(body)
/**
* Links <code>self</code> to actor <code>to</code>.
*
* @param to the actor to link to
* @return the parameter actor
*/
def link(to: LinkableActor): LinkableActor = self.link(to)
/**
* Links <code>self</code> to the actor defined by <code>body</code>.
*
* @param body the body of the actor to link to
* @return the parameter actor
*/
def link(body: => Unit): ActorDSL = self.link(body)
/**
* Unlinks <code>self</code> from actor <code>from</code>.
*
* @param from the actor to unlink from
*/
def unlink(from: LinkableActor): Unit = self.unlink(from)
/**
* <p>
* Terminates execution of <code>self</code> with the following
* effect on linked actors:
* </p>
* <p>
* For each linked actor <code>a</code> with
* <code>trapExit</code> set to <code>true</code>, send message
* <code>Exit(self, reason)</code> to <code>a</code>.
* </p>
* <p>
* For each linked actor <code>a</code> with
* <code>trapExit</code> set to <code>false</code> (default),
* call <code>a.exit(reason)</code> if
* <code>reason != 'normal</code>.
* </p>
*/
def exit(reason: AnyRef): Nothing = self.exit(reason)
/**
* <p>
* Terminates execution of <code>self</code> with the following
* effect on linked actors:
* </p>
* <p>
* For each linked actor <code>a</code> with
* <code>trapExit</code> set to <code>true</code>, send message
* <code>Exit(self, 'normal)</code> to <code>a</code>.
* </p>
*/
def exit(): Nothing = self.exit()
def continue = self.continue
}
trait ActorDSL extends BaseActor with CommonActorMixin {
type Actor_T <: ActorDSL
def start(): ActorDSL
def link(body: => Unit): ActorDSL
}
|
