Facebook
From Toxic Pheasant, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 223
  1. import akka.actor._
  2.  
  3.  
  4.  
  5. case class Ping(target: ActorRef)
  6.  
  7. case class Pong(target: ActorRef)
  8.  
  9. case class Zong(target: ActorRef)
  10.  
  11.  
  12.  
  13. class Player(private val NUMBER_OF_RALLIES: Int) extends Actor{
  14.  
  15.   private var receivedBallCounter = 0
  16.  
  17.  
  18.  
  19.   def receive = {
  20.  
  21.       case Ping(targetPlayerRef) => {
  22.  
  23.         println("ping")
  24.  
  25.         receivedBallCounter += 1
  26.  
  27.  
  28.  
  29.         if ( receivedBallCounter < NUMBER_OF_RALLIES ) {
  30.  
  31.           targetPlayerRef ! Pong(self)
  32.  
  33.         }
  34.  
  35.         else {
  36.  
  37.           targetPlayerRef ! Pong(self)
  38.  
  39.           targetPlayerRef ! PoisonPill
  40.  
  41.         }
  42.  
  43.       }
  44.  
  45.  
  46.  
  47.       case Pong(targetPlayerRef) => {
  48.  
  49.         println("pong")
  50.  
  51.         receivedBallCounter += 1
  52.  
  53.  
  54.  
  55.         if ( receivedBallCounter < NUMBER_OF_RALLIES ) {
  56.  
  57.           targetPlayerRef ! Ping(self)
  58.  
  59.         }
  60.  
  61.         else {
  62.  
  63.           targetPlayerRef ! PoisonPill
  64.  
  65.         }
  66.  
  67.       }
  68.  
  69.  
  70.  
  71.       case _ => {
  72.  
  73.         println("Unhandled message")
  74.  
  75.       }
  76.  
  77.   }
  78.  
  79. }
  80.  
  81.  
  82.  
  83. val NUMBER_OF_RALLIES = 5
  84.  
  85. val actorSystem = ActorSystem("PingPongTraining")
  86.  
  87. val player1 = actorSystem.actorOf(Props(classOf[Player], NUMBER_OF_RALLIES))
  88.  
  89. val player2 = actorSystem.actorOf(Props(classOf[Player], NUMBER_OF_RALLIES))
  90.  
  91. player1 ! Zong(player2)
  92.  
  93. player1 ! Ping(player2)
  94.  
  95. player1 ! Zong(player2)
  96.  
  97. actorSystem.shutdown
  98.  
  99.  
  100.  
  101. /********************* POISON_PILL *********************
  102.  
  103.  * A PoisonPill message will stop an actor when the message is processed.
  104.  
  105.  * A PoisonPill message is queued just like an ordinary message
  106.  
  107.  * and will be handled after other messages queued ahead of it in its mailbox.
  108.  
  109.  */
  110.  
  111.  
  112.  
  113. /********************* context.stop(self) *********************
  114.  
  115.  * The difference is in which messages get processed before this sequence starts.
  116.  
  117.  * In the case of the stop call, the message currently being processed is completed first,
  118.  
  119.  * with all others discarded.
  120.  
  121.  */
  122.  
  123.  
  124.  
  125. /********************* Console output *********************
  126.  
  127.  * scala> :load 12_1.scala
  128.  
  129.  * Loading 12_1.scala...
  130.  
  131.  * import akka.actor._
  132.  
  133.  * defined class Ping
  134.  
  135.  * defined class Pong
  136.  
  137.  * defined class Zong
  138.  
  139.  * defined class Player
  140.  
  141.  * NUMBER_OF_RALLIES: Int = 5
  142.  
  143.  * actorSystem: akka.actor.ActorSystem = akka://PingPongTraining
  144.  
  145.  * player1: akka.actor.ActorRef = Actor[akka://PingPongTraining/user/$a#-1157019602]
  146.  
  147.  * player2: akka.actor.ActorRef = Actor[akka://PingPongTraining/user/$b#-546795948]
  148.  
  149.  * Unhandled message
  150.  
  151.  * ping
  152.  
  153.  * pong
  154.  
  155.  * ping
  156.  
  157.  * pong
  158.  
  159.  * ping
  160.  
  161.  * pong
  162.  
  163.  * ping
  164.  
  165.  * pong
  166.  
  167.  * ping
  168.  
  169.  * pong
  170.  
  171.  * [INFO] [01/15/2018 22:07:07.545] [PingPongTraining-akka.actor.default-dispatcher-5] [akka://PingPongTraining/user/$a] Message [$line233.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$Zong] from Actor[akka://PingPongTraining/deadLetters] to Actor[akka://PingPongTraining/user/$a#-1157019602] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
  172.  
  173.  *  */