-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PiApproximation example using TypedActors.
- Loading branch information
Showing
13 changed files
with
158 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.gvolpe.typed.app | ||
|
||
import akka.actor.ActorSystem | ||
import com.gvolpe.typed.app.actor.{Listener, Master} | ||
import com.gvolpe.typed.app.messages.Calculate | ||
import de.knutwalker.akka.typed._ | ||
|
||
object Pi extends App { | ||
|
||
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000) | ||
|
||
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int): Unit = { | ||
|
||
implicit val system = ActorSystem("PiSystem") | ||
|
||
val listener = ActorOf(Listener.props, name = "listener") | ||
|
||
val master = ActorOf(Master.props(nrOfWorkers, nrOfElements, nrOfMessages, listener), name = "master") | ||
|
||
master ! Calculate | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.gvolpe.typed.app.actor | ||
|
||
import com.gvolpe.typed.app.messages.PiApproximation | ||
import de.knutwalker.akka.typed._ | ||
|
||
object Listener { | ||
def props = PropsFor[Listener] | ||
} | ||
|
||
class Listener extends TypedActor.Of[PiApproximation] { | ||
|
||
override def typedReceive: TypedReceive = { | ||
case PiApproximation(pi, duration) => | ||
println("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s ms" | ||
.format(pi, duration)) | ||
context.system.terminate() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.gvolpe.typed.app.actor | ||
|
||
import akka.routing.{ActorRefRoutee, RoundRobinRoutingLogic, Router} | ||
import com.gvolpe.typed.app.messages._ | ||
import de.knutwalker.akka.typed._ | ||
|
||
object Master { | ||
def props(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int, listener: ActorRef[PiApproximation]) = | ||
PropsFor(new Master(nrOfWorkers, nrOfElements, nrOfMessages, listener)) | ||
} | ||
|
||
class Master(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int, listener: ActorRef[PiApproximation]) extends TypedActor.Of[PiMessage] { | ||
|
||
var pi: Double = _ | ||
var nrOfResults: Int = _ | ||
val start: Long = System.currentTimeMillis | ||
|
||
val workerRouter: Router = { | ||
val routees = Vector.fill(5) { | ||
val r = ActorOf(Worker.props).untyped | ||
context watch r | ||
ActorRefRoutee(r) | ||
} | ||
Router(RoundRobinRoutingLogic(), routees) | ||
} | ||
|
||
override def typedReceive: TypedReceive = { | ||
case Calculate => | ||
for (i ← 0 until nrOfMessages) workerRouter.route(Work(i * nrOfElements, nrOfElements), self) | ||
case Result(value) => | ||
pi += value | ||
nrOfResults += 1 | ||
if (nrOfResults == nrOfMessages) { | ||
listener ! PiApproximation(pi, duration = System.currentTimeMillis - start) | ||
// Stops this actor and all its supervised children | ||
context.stop(self) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.gvolpe.typed.app.actor | ||
|
||
import com.gvolpe.typed.app.messages.{Result, Work} | ||
import de.knutwalker.akka.typed._ | ||
|
||
object Worker { | ||
def props = PropsFor[Worker] | ||
} | ||
|
||
class Worker extends TypedActor.Of[Work] { | ||
|
||
private def calculatePiFor(start: Int, nrOfElements: Int): Double = { | ||
var acc = 0.0 | ||
for (i ← start until (start + nrOfElements)) | ||
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1) | ||
acc | ||
} | ||
|
||
override def typedReceive: TypedReceive = { | ||
case Work(start, nrOfElements) => | ||
sender ! Result(calculatePiFor(start, nrOfElements)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.gvolpe.typed.app | ||
|
||
object messages { | ||
|
||
sealed trait PiMessage extends Product with Serializable | ||
case object Calculate extends PiMessage | ||
final case class Result(value: Double) extends PiMessage | ||
|
||
final case class Work(start: Int, nrOfElements: Int) | ||
|
||
final case class PiApproximation(pi: Double, duration: Long) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...com/gvolpe/typed/ReplyToPatternDemo.scala → ...e/typed/examples/ReplyToPatternDemo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...lpe/typed/actor/ReplyToPatternActor.scala → .../examples/actor/ReplyToPatternActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
.../gvolpe/typed/actor/SimpleAkkaActor.scala → ...yped/examples/actor/SimpleAkkaActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...gvolpe/typed/actor/SimpleTypedActor.scala → ...ped/examples/actor/SimpleTypedActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
.../gvolpe/typed/actor/UnionTypedActor.scala → ...yped/examples/actor/UnionTypedActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters