-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong.scala
53 lines (49 loc) · 1.31 KB
/
pingpong.scala
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
import akka.actor._
case object PingMessage
case object PongMessage
case object StartMessage
case object StopMessage
/**
* An Akka Actor example written by Alvin Alexander of
* http://alvinalexander.com
*
* Shared here under the terms of the Creative Commons
* Attribution Share-Alike License: http://creativecommons.org/licenses/by-sa/2.5/
*
* more akka info: http://doc.akka.io/docs/akka/snapshot/scala/actors.html
*/
class Ping(pong: ActorRef) extends Actor {
var count = 0
def incrementAndPrint { count += 1; println("ping") }
def receive = {
case StartMessage =>
incrementAndPrint
pong ! PingMessage
case PongMessage =>
incrementAndPrint
if (count > 99) {
sender ! StopMessage
println("ping stopped")
context.stop(self)
} else {
sender ! PingMessage
}
}
}
class Pong extends Actor {
def receive = {
case PingMessage =>
println(" pong")
sender ! PongMessage
case StopMessage =>
println("pong stopped")
context.stop(self)
}
}
object PingPongTest extends App {
val system = ActorSystem("PingPongSystem")
val pong = system.actorOf(Props[Pong], name = "pong")
val ping = system.actorOf(Props(new Ping(pong)), name = "ping")
// start them going
ping ! StartMessage
}