-
Notifications
You must be signed in to change notification settings - Fork 5
/
PingPong.scala
48 lines (32 loc) · 909 Bytes
/
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
import akka.actor._
object PingPong {
val random = new scala.util.Random
case class SetPartner(partner: ActorRef)
case class Ball(volley: Int)
class Player extends Actor {
var partner = context.system.deadLetters
def receive = {
case SetPartner(p: ActorRef) =>
partner = p
case Ball(volley) =>
Thread.sleep(500)
if (random.nextInt(100) < volley) {
println(self.path.name + ": I dropped the ball... Sorry!")
} else {
println (self.path.name + ": Volley number " + volley)
partner ! Ball(volley+1)
}
}
}
def main (args: Array[String]) {
val system = ActorSystem("PingPongDemo")
val ping = system.actorOf(Props[Player], "Ping")
val pong = system.actorOf(Props[Player], "Pong")
ping ! SetPartner(pong)
pong ! SetPartner(ping)
pong ! Ball(1)
Thread.sleep(5000)
println("Times up!")
system.shutdown
}
}