Created
March 23, 2011 06:37
-
-
Save cflewis/882706 to your computer and use it in GitHub Desktop.
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
package com.cflewis.lyss.kitty_server | |
import akka.actor.Actor | |
import akka.actor.Actor._ | |
import akka.config.Supervision._ | |
case class Cat(name: String, color: String, description: String) | |
case class OrderCat(name: String, color: String, description: String) | |
case class ReturnCat(cat: Cat) | |
case class CloseShop() | |
class KittyServer extends Actor { | |
self.lifeCycle = Permanent | |
var cats = List[Cat]() | |
def receive = { | |
case oc: OrderCat => { | |
val cat = new Cat(oc.name, oc.color, oc.description) | |
if (cats.isEmpty) { | |
cats = cat :: cats | |
self.reply(cat) | |
} else { | |
self.reply(cats.head) | |
cats = cat :: cats.tail | |
} | |
} | |
case rc: ReturnCat => cats = rc.cat :: cats | |
case cs: CloseShop => { | |
println(cats.head.name + " was set free") | |
self.stop | |
} | |
case s: String => throw new Exception("OH GOD CATS LOVE STRING") | |
case _ => println("Unexpected message to KittyServer") | |
} | |
} | |
class KittySupervisor extends Actor { | |
self.faultHandler = AllForOneStrategy(List(classOf[Throwable]), 3, 1000) | |
self.lifeCycle = Permanent | |
def receive = { | |
case "start" => start() | |
case _ => println("Unexpected message to KittySupervisor") | |
} | |
def start() = { | |
val kittyServer = actorOf[KittyServer] | |
self.link(kittyServer) | |
kittyServer.start | |
val cat = getCat(kittyServer !! OrderCat("Jeffery", "Orange", "Cutie")) | |
println(cat) | |
val cat1 = kittyServer !! OrderCat("Sara", "Pink", "Bestie") | |
kittyServer ! "Yarn!" | |
kittyServer ! ReturnCat(cat) | |
kittyServer ! CloseShop() | |
self.stop | |
} | |
def getCat(cat: Option[Any]): Cat = { | |
cat match { | |
case Some(c) => c.asInstanceOf[Cat] | |
case None => new Cat("Stray", "Grey", "Ugly") | |
} | |
} | |
} | |
object App extends Application { | |
val k = actorOf[KittySupervisor] | |
k.start | |
k ! "start" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs: