Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import akka.event.Logging
- import akka.actor.{Actor, ActorSystem, Props}
- import org.scalatest.time.Milliseconds
- import org.scalatest.time.Milliseconds
- import scala.concurrent.duration._
- import akka.actor.OneForOneStrategy
- import akka.actor.SupervisorStrategy._
- import scala.util.Random
- import scala.collection.immutable.Range
- import scala.collection.mutable.MutableList
- import scala.collection.mutable.ListBuffer
- import Messages._
- import akka.routing.Router
- import akka.routing.RoundRobinRouter
- import akka.routing.RoundRobinRoutingLogic
- import akka.routing.ActorRefRoutee
- import akka.io._
- import akka.routing._
- import akka.actor.Terminated
- import akka.actor.ActorLogging
- object Messages{
- /** from ParticleSystemManager to Worker update its particles */
- val Process = "process"
- /** Request from ParticleSystemManager to Worker to grab its stats. */
- val DoStats = "stats"
- /** An answer from Worker to ParticleSystemManager about its stats */
- case class ReadyStats( slowest: Particle, fastest: Particle )
- }
- // Configuration parameters for Point class
- object Particle{
- val MAX_LIFE = 1024
- val START_X = 0
- val START_Y = 0
- val START_VX = 0
- val START_VY = 0
- val START_AX = 0
- val START_AY = 0
- val MAX_DIFRACTION = 5
- }
- // Configuration for Workers
- object Worker {
- val PARTICLES_PER_WORKER = 64
- }
- // Just another configuration object
- object ParticleSystemManager {
- val ACTORS_COUNT = 64
- }
- /** Representation of Particle class -
- * position, velocity, acceleration, "life"-time and toString-"rendering"
- */
- class Particle{
- protected var x: Int = 0
- protected var y: Int = Particle.START_Y
- protected var vX: Int = Particle.START_VX
- protected var vY: Int = Particle.START_VY
- protected var aX: Int = Particle.START_AX
- protected var aY: Int = Particle.START_AY
- protected var life: Int = 0 // times of processing (countdown to 0)
- reset
- override def toString = s"($x, $y, $vX, $vY, $aX, $aY, $life)"
- def process(){
- if (life >= Particle.MAX_LIFE){
- reset()
- }
- vX += aX
- vY += aY
- x += vX
- y += vY
- life += 1
- }
- def reset(){
- life = 0;
- x = Particle.START_X + distortion
- y = Particle.START_Y + distortion
- vX = Particle.START_VX + distortion
- vY = Particle.START_VY + distortion
- aX = Particle.START_AX + distortion
- aY = Particle.START_AY + distortion
- }
- def v = Math.abs(Math.sqrt(vX * vX + vY * vY)).toInt
- protected def distortion = Random.nextInt(Particle.MAX_DIFRACTION) - Particle.MAX_DIFRACTION / 2
- }
- /**
- * Worker - is a particles buffer (@see Particle) and processor.
- * Receives messages from ParticleSystemManager and updates data.
- * Also can grab some stats and send them to manager
- */
- class Worker extends Actor {
- val points = Array.fill(Worker.PARTICLES_PER_WORKER){ new Particle() }
- def receive = {
- case Process => {
- points.foreach { _.process };
- }
- case DoStats => {
- sender ! ReadyStats(points.minBy( _.v ), points.maxBy { _.v } )
- }
- }
- implicit val timeout = 1 second
- }
- /** Manager of all workers. Send update messages to workers (@see Messages#Process).
- * Also sends "grab stat"-messages (@see Messages#DoStat) and print stats
- */
- class ParticleSystemManager extends Actor with ActorLogging{
- val router = context.actorOf( Props[ Worker ].withRouter(BroadcastRouter(ParticleSystemManager.ACTORS_COUNT)), "router" )
- val slowestAndFastests = ListBuffer[Particle]()
- var actorsAnsweredStatistics = 0
- def receive = {
- case Process => {
- router ! Process
- }
- case DoStats => {
- router ! DoStats
- actorsAnsweredStatistics = ParticleSystemManager.ACTORS_COUNT
- }
- case ReadyStats(slowest, fastest) => stats(slowest, fastest)
- }
- def stats(slowest: Particle, fastest: Particle){
- slowestAndFastests += slowest += fastest
- actorsAnsweredStatistics -= 1
- if ( actorsAnsweredStatistics <= 0){
- printAndClearStats();
- }
- }
- def printAndClearStats(){
- val slowest = slowestAndFastests.minBy(_.v)
- val fastest = slowestAndFastests.maxBy(_.v)
- log.info(s"Stats. Total particles count: ${ParticleSystemManager.ACTORS_COUNT * Worker.PARTICLES_PER_WORKER}. Particle with min speed: ${slowest}, particle with max speed: ${fastest}");
- slowestAndFastests.clear
- }
- override val supervisorStrategy =
- OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 millisecond) {
- case _: Exception => Restart
- }
- }
- object Main extends App {
- Random.setSeed(System.currentTimeMillis() )
- val system = ActorSystem("particlesystem");
- val actor = system.actorOf( Props[ParticleSystemManager], name = "manager" );
- val frameTime = (1000 / 1) milliseconds
- import system.dispatcher
- system.scheduler.schedule ( 0 milliseconds, frameTime, actor, Process)
- system.scheduler.schedule ( 0 milliseconds, 3 seconds, actor, DoStats)
- }
Advertisement
Add Comment
Please, Sign In to add comment