KleveR_

Untitled

Jan 24th, 2021
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package moje
  2.  
  3. import akka.actor.{ActorSystem, Actor, ActorRef, Props}
  4. import scala.io.StdIn
  5.  
  6. // case class Zlecenie(tekst: List[String])
  7. case class Wynik(nadzorca: ActorRef)
  8.  
  9. object Nadzorca {
  10.   case class Init(n: Int)
  11.   case class Zlecenie(tekst: String)
  12. }
  13.  
  14. object Pracownik {
  15.   case class Wykonaj(slowo: String)
  16. }
  17.  
  18. class Nadzorca extends Actor {
  19.   import Nadzorca._
  20.   def receive: Receive = {
  21.     case Init(n) => {
  22.       val initWorker = context.actorOf(Props[Pracownik], s"pracownik$n")
  23.       println(s"INIT worker - $n")
  24.       context.become(withWorker(Set(initWorker)))
  25.       self ! Init(n - 1)
  26.     }
  27.     case _ =>
  28.   }
  29.   def withWorker(pracownicy: Set[ActorRef]): Receive = {
  30.     case Init(n) => {
  31.       if(n > 0) {
  32.         val pracownik = context.actorOf(Props[Pracownik], s"pracownik$n")
  33.         println(s"INIT worker - $n")
  34.         context.become(withWorker(pracownicy + pracownik))
  35.         self ! Init(n - 1)
  36.       }
  37.     }
  38.     case Zlecenie(tekst) => {
  39.       for{ p <- pracownicy } {
  40.         println(s"-----------> ${p.path.name}")
  41.         p ! Pracownik.Wykonaj(tekst)
  42.       }
  43.     }
  44.   }
  45. }
  46.  
  47. class Pracownik extends Actor {
  48.   import Pracownik._
  49.   def receive: Receive = {
  50.     case Wykonaj(tekst: String) => {
  51.       println(s" ${self.path.name} - mowi > ${tekst}")
  52.     }
  53.     case _ => println("GOT RECEIVED WORKER")
  54.   }
  55. }
  56.  
  57. object MyZlicz extends App {
  58.   def dane(): List[String] = {
  59.     scala.io.Source.fromResource("ogniem_i_mieczem.txt").getLines.toList
  60.   }
  61.   val system = ActorSystem("LiczniSlow")
  62.   val nadzorca = system.actorOf(Props[Nadzorca], "Nadzorca")
  63.   val testList = List("zdanie jeden dwa trzy cztery", "siema eniu co tam u ciebie byczku", "ale jazda normalnie tak o nie")
  64.   nadzorca ! Nadzorca.Init(5)
  65.   nadzorca ! Nadzorca.Zlecenie("SCALA")
  66. //   nadzorca ! Zlecenie(testList)
  67. //   leonardo ! Zlecenie(dane())
  68.   StdIn.readLine()
  69.   system.terminate()
  70. }
  71.  
  72.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×