diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-05-23 03:22:01 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-05-23 03:22:01 -0700 |
commit | 0f198a63d2f1a141b43f40f639d5fff8697510d0 (patch) | |
tree | a00c180516a2714ea5107547eae766c82a799f05 /test/files/jvm/actmig-PinS_3.scala | |
parent | cebf241ad87358b0e8d2804750a4ac36e76f7091 (diff) | |
parent | 033463f87fe71e8f6d5b17f7cb82b8c2ea5f5271 (diff) | |
download | scala-0f198a63d2f1a141b43f40f639d5fff8697510d0.tar.gz scala-0f198a63d2f1a141b43f40f639d5fff8697510d0.tar.bz2 scala-0f198a63d2f1a141b43f40f639d5fff8697510d0.zip |
Merge pull request #575 from vjovanov/actors-migration-kit
Adding the Actor Migration Kit.
Diffstat (limited to 'test/files/jvm/actmig-PinS_3.scala')
-rw-r--r-- | test/files/jvm/actmig-PinS_3.scala | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/test/files/jvm/actmig-PinS_3.scala b/test/files/jvm/actmig-PinS_3.scala new file mode 100644 index 0000000000..047bf53c32 --- /dev/null +++ b/test/files/jvm/actmig-PinS_3.scala @@ -0,0 +1,159 @@ +import scala.actors.{ MigrationSystem, StashingActor, ActorRef, Terminated, Props } + +object SillyActor { + val ref = MigrationSystem.actorOf(Props(() => new SillyActor, "default-stash-dispatcher")) +} + +/* PinS, Listing 32.1: A simple actor + */ +class SillyActor extends StashingActor { + def receive = { case _ => println("Why are you not dead"); context.stop(self) } + + override def preStart() { + for (i <- 1 to 5) { + println("I'm acting!") + Thread.sleep(10) + } + context.stop(self) + } + + override def postStop() { + println("Post stop") + } +} + +object SeriousActor { + val ref = MigrationSystem.actorOf(Props(() => new SeriousActor, "default-stash-dispatcher")) +} + +class SeriousActor extends StashingActor { + def receive = { case _ => println("Nop") } + override def preStart() { + for (i <- 1 to 5) { + println("To be or not to be.") + //Thread.sleep(1000) + Thread.sleep(10) + } + context.stop(self) + } +} + +/* PinS, Listing 32.3: An actor that calls react + */ +object NameResolver { + val ref = MigrationSystem.actorOf(Props(() => new NameResolver, "default-stash-dispatcher")) +} + +class NameResolver extends StashingActor { + import java.net.{ InetAddress, UnknownHostException } + + def receive = { + case (name: String, actor: ActorRef) => + actor ! getIp(name) + case "EXIT" => + println("Name resolver exiting.") + context.stop(self) // quit + case msg => + println("Unhandled message: " + msg) + } + + def getIp(name: String): Option[InetAddress] = { + try { + Some(InetAddress.getByName(name)) + } catch { + case _: UnknownHostException => None + } + } + +} + +object Test extends App { + + /* PinS, Listing 32.2: An actor that calls receive + */ + def makeEchoActor(): ActorRef = MigrationSystem.actorOf(Props(() => new StashingActor { + + def receive = { // how to handle receive + case 'stop => + context.stop(self) + case msg => + println("received message: " + msg) + } + }, "default-stash-dispatcher")) + + /* PinS, page 696 + */ + def makeIntActor(): ActorRef = MigrationSystem.actorOf(Props(() => new StashingActor { + + def receive = { + case x: Int => // I only want Ints + unstashAll() + println("Got an Int: " + x) + context.stop(self) + case _ => stash() + } + }, "default-stash-dispatcher")) + + MigrationSystem.actorOf(Props(() => new StashingActor { + val silly = SillyActor.ref + + override def preStart() { + context.watch(SillyActor.ref) + } + + def receive = { + case Terminated(`silly`) => + unstashAll() + val serious = SeriousActor.ref + context.watch(SeriousActor.ref) + context.become { + case Terminated(`serious`) => + // PinS, page 694 + val seriousActor2 = MigrationSystem.actorOf(Props(() => { + new StashingActor { + + def receive = { case _ => context.stop(self) } + + override def preStart() = { + for (i <- 1 to 5) { + println("That is the question.") + //Thread.sleep(1000) + Thread.sleep(10) + } + context.stop(self) + } + } + }, "default-stash-dispatcher")) + + Thread.sleep(200) + val echoActor = makeEchoActor() + context.watch(echoActor) + echoActor ! "hi there" + echoActor ! 15 + echoActor ! 'stop + + context.become { + case Terminated(_) => + unstashAll() + val intActor = makeIntActor() + intActor ! "hello" + intActor ! math.Pi + // only the following send leads to output + intActor ! 12 + context.unbecome() + context.unbecome() + context.stop(self) + case m => + println("Stash 1 " + m) + stash(m) + } + case m => + println("Stash 2 " + m) + stash(m) + } + case m => + println("Stash 3 " + m) + stash(m) + } + }, "default-stash-dispatcher")) +} |