Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package calculatePi
  2.  
  3. import akka.actor.{ Actor, ActorLogging, ActorRef, Props, ActorSystem }
  4. import akka.routing._
  5.  
  6. case object Start
  7.  
  8. case class CalculatePi(val precision: Int)
  9. case class ResultPi(val result: BigDecimal)
  10.  
  11. case class Calcul(startValue: Int, nbOfElts: Int)
  12. case class Result(value: BigDecimal)
  13.  
  14. class Calculator extends Actor with ActorLogging {
  15.  
  16. def calculatePiFor(start: Int, nbOfElts: Int) = {
  17. def iter(k: Int) = {
  18. val r1 = BigDecimal(4) / (8 * k + 1)
  19. val r2 = BigDecimal(2) / (8 * k + 4)
  20. val r3 = BigDecimal(1) / (8 * k + 5)
  21. val r4 = BigDecimal(1) / (8 * k + 6)
  22. val r5 = (BigDecimal(1) / 16).pow(k)
  23. r5 * (r1 - r2 - r3 - r4)
  24. }
  25.  
  26. (for {
  27. i <- start until (nbOfElts + start)
  28. n = iter(i)
  29. } yield n).sum
  30. }
  31.  
  32. def receive = {
  33. case Calcul(startValue, nbOfElts) =>
  34. log.info(s"calculate $nbOfElts elts of the series begining from $startValue")
  35. sender ! Result(calculatePiFor(startValue, nbOfElts))
  36. case _ => ()
  37. }
  38.  
  39. }
  40.  
  41. class TeamLeader extends Actor with ActorLogging {
  42.  
  43. val numberOfWorker = 10
  44. val calculator = context.actorOf(
  45. Props[Calculator].withRouter(RoundRobinPool(numberOfWorker)), name = "calculator")
  46.  
  47. val initReceive: PartialFunction[Any, Unit] = {
  48. case CalculatePi(precision) =>
  49. val p = precision / numberOfWorker
  50. log.info("starting for calculating pi")
  51. (0 to numberOfWorker).foreach { x =>
  52. calculator ! Calcul(x * p, p - 1)
  53. context.become(waitingReceive(numberOfWorker, 0, sender), true)
  54. }
  55. }
  56.  
  57. def waitingReceive(nbOfResponses: Int, sum: BigDecimal, initialDemandor: ActorRef): PartialFunction[Any, Unit] = {
  58. case Result(decimalPart) =>
  59. if (nbOfResponses > 0) {
  60. log.info(s"one partial result found. waiting for ${nbOfResponses} again")
  61. context.become(waitingReceive(nbOfResponses - 1, sum + decimalPart, initialDemandor), true)
  62. } else {
  63. log.info(s"All partial result received. sending computed result to asker")
  64. initialDemandor ! ResultPi(sum)
  65. }
  66. }
  67.  
  68. def receive = initReceive
  69.  
  70. }
  71.  
  72. class Master extends Actor with ActorLogging {
  73.  
  74. def receive = {
  75. case Start =>
  76. val leader = context.actorOf(Props[TeamLeader], "leader")
  77. leader ! CalculatePi(1000000)
  78.  
  79. case ResultPi(result) =>
  80. println(result)
  81. context.system.shutdown()
  82. }
  83. }
  84.  
  85. object Main extends App {
  86. val system = ActorSystem("SuperMario")
  87. val master = system.actorOf(Props[Master], "master")
  88. master ! Start
  89. println(s"waiting for something happens...")
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement