diff options
author | Philipp Haller <hallerp@gmail.com> | 2006-06-21 11:35:26 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2006-06-21 11:35:26 +0000 |
commit | fe052478812dc753292c5efc3ac5e32df09a1808 (patch) | |
tree | b1883c87f222fbf93a7e0fd334df67d4d82640c4 /docs/examples | |
parent | 2933e3f3cce9221a1fbe76b434957f8affb54548 (diff) | |
download | scala-fe052478812dc753292c5efc3ac5e32df09a1808.tar.gz scala-fe052478812dc753292c5efc3ac5e32df09a1808.tar.bz2 scala-fe052478812dc753292c5efc3ac5e32df09a1808.zip |
Diffstat (limited to 'docs/examples')
-rw-r--r-- | docs/examples/actors/counter/CounterMessages.scala | 8 | ||||
-rw-r--r-- | docs/examples/actors/counter/CounterUserTcp.scala | 38 | ||||
-rw-r--r-- | docs/examples/actors/counter/RemoteCounter.scala | 21 |
3 files changed, 67 insertions, 0 deletions
diff --git a/docs/examples/actors/counter/CounterMessages.scala b/docs/examples/actors/counter/CounterMessages.scala new file mode 100644 index 0000000000..6bacac0ad9 --- /dev/null +++ b/docs/examples/actors/counter/CounterMessages.scala @@ -0,0 +1,8 @@ +package actors.examples.counter + +import actors.multi.Pid + +abstract class CounterMessage +case class Incr() extends CounterMessage +case class Value(p: Pid) extends CounterMessage +case class Result(v: int) extends CounterMessage diff --git a/docs/examples/actors/counter/CounterUserTcp.scala b/docs/examples/actors/counter/CounterUserTcp.scala new file mode 100644 index 0000000000..b6c304bb1c --- /dev/null +++ b/docs/examples/actors/counter/CounterUserTcp.scala @@ -0,0 +1,38 @@ +package actors.examples.counter + +import actors.multi.Pid +import actors.distributed.RemoteActor +import actors.distributed.TCP +import actors.distributed.TcpNode +import actors.distributed.TcpService + +class CounterUser extends RemoteActor { + override def run(): unit = { + alive(TCP()) + + spawn(TcpNode("127.0.0.1", 9090), "actors.examples.counter.Counter") + + receive { + case p: Pid => + // communicate with counter + Console.println("" + node + ": Sending Incr() to remote Counter (" + p + ")...") + p ! Incr() + p ! Incr() + p ! Value(self) + receive { + case Result(v) => + Console.println("Received result: " + v) + } + } + } +} + +object Main { + def main(args: Array[String]): unit = { + val serv = new TcpService(9090) + serv.start() + + val cu = new CounterUser + cu.start() + } +} diff --git a/docs/examples/actors/counter/RemoteCounter.scala b/docs/examples/actors/counter/RemoteCounter.scala new file mode 100644 index 0000000000..1f5c730a1a --- /dev/null +++ b/docs/examples/actors/counter/RemoteCounter.scala @@ -0,0 +1,21 @@ +package actors.examples.counter + +import actors.distributed.RemoteActor + +class Counter extends RemoteActor { + override def run(): unit = + loop(0) + + def loop(value: int): unit = { + Console.println("Value: " + value) + receive { + case Incr() => + loop(value + 1) + case Value(p) => + p ! Result(value) + loop(value) + case other => + loop(value) + } + } +} |