summaryrefslogtreecommitdiff
path: root/test/files/jvm/actmig-PinS_1.scala
blob: d203526513c749dd6942a5981f322ba7e6812898 (plain) (blame)
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
import scala.actors._

object SillyActor {
  val ref = MigrationSystem.actorOf(Props(() => new SillyActor, "akka.actor.default-stash-dispatcher"))
}

/* PinS, Listing 32.1: A simple actor
 */
class SillyActor extends Actor {
  def act() {
    for (i <- 1 to 5) {
      println("I'm acting!")
      Thread.sleep(10)
    }
  }
}

object SeriousActor {
  val ref = MigrationSystem.actorOf(Props(() => new SeriousActor, "akka.actor.default-stash-dispatcher"))
}

class 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(): ActorRef = MigrationSystem.actorOf(Props(() => new Actor {
    def act() {
      while (true) {
        receive {
          case 'stop =>
            exit()
          case msg =>
            println("received message: " + msg)
        }
      }
    }
  }, "akka.actor.default-stash-dispatcher"))

  /* PinS, page 696
   */
  def makeIntActor(): ActorRef = MigrationSystem.actorOf(Props(() => new Actor {
    def act() {
      receive {
        case x: Int => // I only want Ints
          println("Got an Int: " + x)
      }
    }
  }, "akka.actor.default-stash-dispatcher"))

  MigrationSystem.actorOf(Props(() => new Actor {
    def act() {
      trapExit = true
      link(SillyActor.ref)
      react {
        case Exit(_: SillyActor, _) =>
          link(SeriousActor.ref)
          react {
            case Exit(_: SeriousActor, _) =>
              // PinS, page 694
              val seriousActor2 = MigrationSystem.actorOf(Props(() =>
                new Actor {
                  def act() {
                    for (i <- 1 to 5) {
                      println("That is the question.")
                      //Thread.sleep(1000)
                      Thread.sleep(10)
                    }
                  }
                }
              , "akka.actor.default-stash-dispatcher"))

              Thread.sleep(200)
              val echoActor = makeEchoActor()
              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
              }
          }
      }
    }
  }, "akka.actor.default-stash-dispatcher"))
}