summaryrefslogtreecommitdiff
path: root/test/files/jvm/actmig-PinS.scala
diff options
context:
space:
mode:
authorVojin Jovanovic <vojin.jovanovic@epfl.ch>2012-05-18 17:53:05 +0200
committerVojin Jovanovic <vojin.jovanovic@epfl.ch>2012-05-18 17:53:05 +0200
commite99fb0c93842d517b8a185458f405bace2bbb46b (patch)
tree070e9117c43fc09e3c850594b76f1957fb68c045 /test/files/jvm/actmig-PinS.scala
parentef7708812fac32ca0c2a05330222a6b0806c9054 (diff)
downloadscala-e99fb0c93842d517b8a185458f405bace2bbb46b.tar.gz
scala-e99fb0c93842d517b8a185458f405bace2bbb46b.tar.bz2
scala-e99fb0c93842d517b8a185458f405bace2bbb46b.zip
Adding the Actor Migration Kit.
Kit consists of: 1) The StashingActor which adopts an interface similar to Akka. 2) Props mockup for creating Akka like code 3) Pattern mockup 4) Test cases for every step in the migration. 5) MigrationSystem which will paired on the Akka side. Review of the code : @phaller Review of the build: @jsuereth
Diffstat (limited to 'test/files/jvm/actmig-PinS.scala')
-rw-r--r--test/files/jvm/actmig-PinS.scala118
1 files changed, 118 insertions, 0 deletions
diff --git a/test/files/jvm/actmig-PinS.scala b/test/files/jvm/actmig-PinS.scala
new file mode 100644
index 0000000000..db5713dde4
--- /dev/null
+++ b/test/files/jvm/actmig-PinS.scala
@@ -0,0 +1,118 @@
+import scala.actors._
+
+import scala.actors.Actor._
+
+/* PinS, Listing 32.1: A simple actor
+ */
+object SillyActor extends Actor {
+ def act() {
+ for (i <- 1 to 5) {
+ println("I'm acting!")
+ //Thread.sleep(1000)
+ Thread.sleep(10)
+ }
+ }
+}
+
+object SeriousActor extends Actor {
+ def act() {
+ for (i <- 1 to 5) {
+ println("To be or not to be.")
+ //Thread.sleep(1000)
+ Thread.sleep(10)
+ }
+ }
+}
+
+/* PinS, Listing 32.3: An actor that calls react
+ */
+object NameResolver extends Actor {
+ import java.net.{InetAddress, UnknownHostException}
+
+ def act() {
+ react {
+ case (name: String, actor: Actor) =>
+ actor ! getIp(name)
+ act()
+ case "EXIT" =>
+ println("Name resolver exiting.")
+ // quit
+ case msg =>
+ println("Unhandled message: " + msg)
+ act()
+ }
+ }
+
+ 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(): Actor = actor {
+ while (true) {
+ receive {
+ case 'stop =>
+ exit()
+ case msg =>
+ println("received message: " + msg)
+ }
+ }
+ }
+
+ /* PinS, page 696
+ */
+ def makeIntActor(): Actor = actor {
+ receive {
+ case x: Int => // I only want Ints
+ println("Got an Int: " + x)
+ }
+ }
+
+
+ actor {
+ self.trapExit = true
+ self.link(SillyActor)
+ SillyActor.start()
+
+ react {
+ case Exit(SillyActor, _) =>
+ self.link(SeriousActor)
+ SeriousActor.start()
+ react {
+ case Exit(SeriousActor, _) =>
+ // PinS, page 694
+ val seriousActor2 = actor {
+ for (i <- 1 to 5)
+ println("That is the question.")
+ //Thread.sleep(1000)
+ Thread.sleep(10)
+ }
+
+ Thread.sleep(200)
+ val echoActor = makeEchoActor()
+ self.link(echoActor)
+ echoActor ! "hi there"
+ echoActor ! 15
+ echoActor ! 'stop
+
+ react {
+ case Exit(_, _) =>
+ val intActor = makeIntActor()
+ intActor ! "hello"
+ intActor ! math.Pi
+ // only the following send leads to output
+ intActor ! 12
+ }
+ }
+ }
+ }
+}