Advertisement
Guest User

Untitled

a guest
Jan 15th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement