Created
April 4, 2015 02:44
-
-
Save hkdsun/0e2e52c3db18c1c4f1ce 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
object Calculator { | |
def props = Props(new Calculator) | |
} | |
class Calculator extends Actor{ | |
val log = Logging(context.system, this) | |
context.watch(sender) | |
def receive = { | |
case Matrix(rows) => { | |
sender ! rows.flatten.sum | |
} | |
case Terminated(sender) => println("they killed my mom") | |
case _ => log.error("Didn't receive a matrix") | |
} | |
} | |
class Dispatcher extends Actor{ | |
val calculator = context.actorOf(Calculator.props, name = "calculatorActor") | |
context.watch(calculator) | |
def receive = { | |
case mat: Matrix => { | |
val x = mat.pretty | |
println(x) | |
calculator ! mat | |
} | |
case x: Int => println(x) | |
case Terminated(calculator) => println("they killed him :(") | |
case _ => println("received garbage :(") | |
} | |
} | |
object Main extends App{ | |
def makeMatrix(): Matrix = { | |
Matrix.empty addCol (1 to 5) addRow Seq(10) addCol (20 to 25) addCol (31 to 36) addCol (15 to 20) addRow Seq(1, 1, 1, 1) | |
} | |
val system = ActorSystem("MatrixSystem") | |
val dispatcher = system.actorOf(Props[Dispatcher], name = "dispatcherActor") | |
val mat = makeMatrix | |
dispatcher ! mat | |
system.shutdown | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment