KleveR_

Untitled

Jan 24th, 2021
73
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.  
  8.  
  9. object Nadzorca {
  10.   case class Init(n: Int)
  11.   case class Zlecenie(text: List[String])
  12.   case class Wynik(suma: Int)
  13.   case object Result
  14. }
  15.  
  16. object Pracownik {
  17.   case class Wykonaj(slowo: Option[String], boss: ActorRef)
  18. }
  19.  
  20. class Nadzorca extends Actor {
  21.   import Nadzorca._
  22.   def receive: Receive = {
  23.     case Init(liczbaPracownikow) =>
  24.       for {
  25.         n <- 1 to liczbaPracownikow
  26.       } context.actorOf(Props[Pracownik](), s"worker$n")
  27.       context.become(withWorker(liczbaPracownikow, 0))
  28.     case _ =>
  29.   }
  30.   def withWorker(liczprac: Int, suma: Int): Receive = {
  31.     case Zlecenie(text) => {
  32.       for (line  <- text.zipWithIndex ) {
  33.         context.actorSelection(s"/user/Nadzorca/worker${line._2 % liczprac + 1}") ! Pracownik.Wykonaj(Some(line._1), self)
  34.       }
  35.       context.actorSelection(s"/user/Nadzorca/worker1") ! Pracownik.Wykonaj(None, self)
  36.     }
  37.     case Wynik(wynik) => {
  38.       context.become(withWorker(liczprac, suma + wynik))
  39.     }
  40.     case Result => {
  41.       println(s"WYNIK => ${suma}")
  42.     }
  43.  
  44.   }
  45. }
  46.  
  47. class Pracownik extends Actor {
  48.   import Pracownik._
  49.   def receive: Receive = {
  50.     case Wykonaj(Some(tekst), boss) => {
  51.       boss ! Nadzorca.Wynik(tekst.split(" ").length)
  52.     }
  53.     case Wykonaj(None, boss) => {
  54.       boss ! Nadzorca.Result
  55.     }
  56.     case _ => println("GOT RECEIVED WORKER")
  57.   }
  58. }
  59.  
  60. object MyZlicz extends App {
  61.   def dane(): List[String] = {
  62.     scala.io.Source.fromResource("ogniem_i_mieczem.txt").getLines.toList
  63.   }
  64.   val system = ActorSystem("LiczniSlow")
  65.   val nadzorca = system.actorOf(Props[Nadzorca], "Nadzorca")
  66.   val testList = List("zdanie jeden dwa trzy cztery", "siema eniu co tam u ciebie byczku", "ale jazda normalnie tak o nie", "testuje", "scale na jutro")
  67.   nadzorca ! Nadzorca.Init(2)
  68.   nadzorca ! Nadzorca.Zlecenie(testList)
  69.   // nadzorca ! Nadzorca.Zlecenie(dane())
  70. //   nadzorca ! Zlecenie(testList)
  71.   // leonardo ! Zlecenie()
  72.   StdIn.readLine()
  73.   system.terminate()
  74. }
  75.  
  76.  
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.

×