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, count: Int )
- }
- object Config{
- val FRAME_TIME = (1000 / 1) milliseconds
- val NEXT_GENERATION_TIME = FRAME_TIME
- val PARTICLE_SYSTEM = "particlesystem"
- }
- import Config._
- // Configuration parameters for Point class
- object Particle{
- val MAX_LIFE = 64
- 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
- val MAX_NEW_PARTICLES_PER_UPDATE = 5
- }
- // Just another configuration object
- object ParticleSystemManager {
- val ACTORS_COUNT = 1
- }
- /** 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(){
- 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 isDying = life > Particle.MAX_LIFE
- 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 runningParticles = ListBuffer[Particle](new Particle() )
- val deadParticles = ListBuffer.fill(Worker.PARTICLES_PER_WORKER - 1){ new Particle() }
- val system = ActorSystem( PARTICLE_SYSTEM )
- import system.dispatcher
- system.scheduler.schedule(0 seconds, NEXT_GENERATION_TIME) { aliveParticles(numParticlesCanBeCreated) }
- def receive = {
- case Process => {
- processRunnings
- val dyings = getDyings
- deadParticles ++= dyings
- runningParticles --= dyings
- }
- case DoStats => {
- sender ! ReadyStats(runningParticles.minBy( _.v ), runningParticles.maxBy { _.v }, runningParticles.size )
- }
- }
- def aliveParticles(num: Int) {
- for (i <- 0 to num - 1) {
- deadParticles(i)
- runningParticles += deadParticles(i)
- }
- deadParticles.remove(0, num)
- }
- def numParticlesCanBeCreated = {
- List(Random.nextInt(Worker.MAX_NEW_PARTICLES_PER_UPDATE - 1) + 1,
- deadParticles.size - 1,
- Worker.PARTICLES_PER_WORKER - runningParticles.size).min
- }
- def processRunnings = runningParticles.foreach { _.process }
- def getDyings = runningParticles.filter( _.isDying )
- 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 count = 0;
- var actorsAnsweredStatistics = 0
- def receive = {
- case Process => {
- router ! Process
- }
- case DoStats => {
- router ! DoStats
- actorsAnsweredStatistics = ParticleSystemManager.ACTORS_COUNT
- }
- case ReadyStats(slowest, fastest, count) => stats(slowest, fastest, count)
- }
- def stats(slowest: Particle, fastest: Particle, count: Int){
- slowestAndFastests += slowest += fastest
- this.count += count
- 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: ${count}. Particle with min speed: ${slowest}, particle with max speed: ${fastest}");
- slowestAndFastests.clear
- count = 0
- }
- override val supervisorStrategy =
- OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 millisecond) {
- case _: Exception => Resume
- }
- }
- object Main extends App {
- Random.setSeed(System.currentTimeMillis() )
- val system = ActorSystem( PARTICLE_SYSTEM );
- val actor = system.actorOf( Props[ParticleSystemManager], name = "manager" );
- import system.dispatcher
- system.scheduler.schedule ( 0 milliseconds, FRAME_TIME, actor, Process)
- system.scheduler.schedule ( 0 milliseconds, 3 seconds, actor, DoStats)
- }
Advertisement
Add Comment
Please, Sign In to add comment