SHARE
TWEET

Untitled

a guest Jan 15th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import akka.actor.{Actor, ActorRef, ActorSystem, PoisonPill, Props}
  2. class Player() extends Actor {
  3.   private var n = 3
  4.   override def receive: Receive = {
  5.     case Player.Ping(message) => {
  6.       println(message)
  7.       sender ! Player.Pong()
  8.     }
  9.     case Player.Pong(message) => {
  10.       println(message)
  11.       n -= 1
  12.       if (n <= 0) {
  13.         sender ! PoisonPill
  14.         self ! PoisonPill
  15.       }
  16.       else
  17.         sender() ! Player.Ping()
  18.     }
  19.   }
  20. }
  21. object Player {
  22.   def props = Props(classOf[Player]) //obiekt konf przy tworz aktora
  23.   case class Ping(msg: String = "ping")
  24.   case class Pong(msg: String = "pong")
  25. }
  26.  
  27. object Main extends App {
  28.   val ourSystem = ActorSystem("MySystem")
  29.   val pinger: ActorRef = ourSystem.actorOf(Player.props)
  30.   val ponger: ActorRef = ourSystem.actorOf(Player.props)
  31.   ponger.tell(Player.Ping(),pinger)
  32.   Thread.sleep(1000)
  33.   ourSystem.terminate()
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top