Created
September 19, 2014 06:47
-
-
Save helenwilliamson/021c85f912081fd6931f to your computer and use it in GitHub Desktop.
Actor gist from 18th September
This file contains hidden or 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
import java.util.Date | |
import javafx.scene.Parent | |
import akka.actor._ | |
import akka.pattern.ask | |
import akka.util.Timeout | |
import scala.annotation.tailrec | |
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
import scala.util.{Success, Failure} | |
case object PingMessage | |
case object PongMessage | |
case class StartMessage(other : ActorRef) | |
case object StopMessage | |
/** | |
* An Akka Actor example written by Alvin Alexander of | |
* <a href="http://devdaily.com" title="http://devdaily.com">http://devdaily.com</a> | |
* | |
* Shared here under the terms of the Creative Commons | |
* Attribution Share-Alike License: <a href="http://creativecommons.org/licenses/by-sa/2.5/" title="http://creativecommons.org/licenses/by-sa/2.5/">http://creativecommons.org/licenses/by-sa/2.5/</a> | |
* | |
* more akka info: <a href="http://doc.akka.io/docs/akka/snapshot/scala/actors.html" title="http://doc.akka.io/docs/akka/snapshot/scala/actors.html">http://doc.akka.io/docs/akka/snapshot/scala/actors.html</a> | |
*/ | |
class Ping extends Actor { | |
var count = 0 | |
def incrementAndPrint { count += 1; println("ping") } | |
def receive = { | |
case StartMessage(other) => | |
incrementAndPrint | |
other ! PingMessage | |
case PongMessage => | |
incrementAndPrint | |
if (count > 99) { | |
sender ! StopMessage | |
println("ping stopped") | |
context.stop(self) | |
} else { | |
sender ! PingMessage | |
} | |
} | |
} | |
class Pong extends Actor { | |
def receive = { | |
case PingMessage => | |
println(" pong") | |
sender ! PongMessage | |
case StopMessage => | |
println("pong stopped") | |
context.stop(self) | |
} | |
} | |
class BadActor extends Actor { | |
def receive = { | |
case Talk(t) => | |
println(s"Got: ${100/t}") | |
if ( t != 0) sender ! 100 | |
} | |
} | |
case class Talk(n: Int) | |
class ParentActor extends Actor { | |
val child = context.system.actorOf(Props[BadActor]) | |
def receive = { | |
case t: Talk => | |
println("Talking to child") | |
child ! t | |
} | |
} | |
case class FibRequest(n: Int) | |
class Fibber extends Actor { | |
def fib(n : Int) = { | |
@tailrec | |
def fib0(n: Int, curr: Int, next: Int): Int = | |
if (n==1) curr else fib0(n-1, next, curr+next) | |
fib0(n,1,1) | |
} | |
def receive = { | |
case FibRequest(n) => sender ! fib(n) | |
} | |
} | |
case object Tick | |
class DateTimeChecker extends Actor { | |
def receive = { | |
case Tick => println(s"It's now ${new Date()}") | |
} | |
} | |
object PingPongTest extends App { | |
implicit val system = ActorSystem("PingPongSystem") | |
val pong = system.actorOf(Props[Pong], name = "pong") | |
val ping = system.actorOf(Props[Ping], name = "ping") | |
// start them going | |
//ping ! StartMessage(pong) | |
import system.dispatcher | |
implicit val timeout = Timeout(5 seconds) | |
val f = system.actorOf(Props[Fibber]) | |
val requests = ( 1 to 20 ).map(FibRequest(_)) | |
val futs = requests.map( f ? _) | |
Future.sequence( futs ).onComplete { | |
case Success(s) => println(s"Woot $s") | |
case Failure(ex) => println(s"Fail $ex") | |
} | |
// (f ? FibRequest(10)).onSuccess { | |
// case x: Int => println(s"fib=$x") | |
// } | |
// val dateTimeChecker = system.actorOf(Props[DateTimeChecker]) | |
// system.scheduler.schedule(0 second, 2 seconds,dateTimeChecker, Tick) | |
// val badActor = system.actorOf(Props[BadActor]) | |
// val talks = ( -10 to 10 ).map(Talk) | |
// | |
// val futs2 = talks.map( badActor ? _) | |
// | |
// futs2 map { | |
// case Success(s) => println("s") | |
// case Failure(s) => println("Fail " + s) | |
// } | |
// val parent = system.actorOf(Props[ParentActor]) | |
// talks.map( parent ! _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment