no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | blog:scala_distributed_ping_pong [2009/11/27 17:54] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Scala distributed ping pong ====== | ||
+ | |||
+ | This small scala example demonstrates the simplicity in which distributed programming is possible using the actor model. | ||
+ | |||
+ | <code scala> | ||
+ | /* | ||
+ | * EchoServer.scala | ||
+ | */ | ||
+ | |||
+ | package echoapp | ||
+ | |||
+ | import scala.actors.Actor._ | ||
+ | import scala.actors.remote.{Locator, | ||
+ | import scala.actors.remote.RemoteActor.{alive, | ||
+ | |||
+ | class Server(me : Locator) { | ||
+ | RemoteActor.classLoader = getClass().getClassLoader() | ||
+ | actor { | ||
+ | alive(me.node.port) | ||
+ | register(me.name, | ||
+ | loop { | ||
+ | react { | ||
+ | case 'PING => reply(" | ||
+ | case msg => println(msg) | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | object EchoServer | ||
+ | def main(args: Array[String]) { | ||
+ | val me = new Locator(Node(" | ||
+ | new Server(me) | ||
+ | println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | For the sake of brevity the IP address of where the ECHOSERVER is running has been hardcoded. | ||
+ | |||
+ | <code scala> | ||
+ | /* | ||
+ | * EchoClient.scala | ||
+ | */ | ||
+ | |||
+ | package echoapp | ||
+ | |||
+ | import scala.actors.Actor._ | ||
+ | import scala.actors.remote.{Locator, | ||
+ | import scala.actors.remote.RemoteActor._ | ||
+ | |||
+ | class Client(servloc : Locator) { | ||
+ | RemoteActor.classLoader = getClass().getClassLoader() | ||
+ | val server = select(servloc.node, | ||
+ | actor { | ||
+ | // The response is sent back to the thread that did the send. | ||
+ | // If we move to this to main() then this is where the reponse will go | ||
+ | // as we have no react/ | ||
+ | server ! 'PING | ||
+ | loop { | ||
+ | react { | ||
+ | case msg => | ||
+ | println(msg) | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | object EchoClient { | ||
+ | def main(args: Array[String]) { | ||
+ | val echoServer = Locator(Node(" | ||
+ | new Client(echoServer) | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | {{tag> | ||