summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2006-06-21 12:35:21 +0000
committerPhilipp Haller <hallerp@gmail.com>2006-06-21 12:35:21 +0000
commit0d8b14c6055e76c0bff3b65d0f428d711abe1f5a (patch)
tree70d30af5d460a943d17d2953041412b79814a2af /docs
parent3fe40a93ffda8571721ff574469171c633191fc4 (diff)
downloadscala-0d8b14c6055e76c0bff3b65d0f428d711abe1f5a.tar.gz
scala-0d8b14c6055e76c0bff3b65d0f428d711abe1f5a.tar.bz2
scala-0d8b14c6055e76c0bff3b65d0f428d711abe1f5a.zip
Added actors library.
Diffstat (limited to 'docs')
-rw-r--r--docs/examples/actors/counter.scala60
-rw-r--r--docs/examples/actors/customer.scala66
2 files changed, 126 insertions, 0 deletions
diff --git a/docs/examples/actors/counter.scala b/docs/examples/actors/counter.scala
new file mode 100644
index 0000000000..8578199bf5
--- /dev/null
+++ b/docs/examples/actors/counter.scala
@@ -0,0 +1,60 @@
+
+import scala.actors.multi.Pid
+import actors.distributed.RemoteActor
+import actors.distributed.TCP
+import actors.distributed.TcpNode
+import actors.distributed.TcpService
+
+abstract class CounterMessage
+case class Incr() extends CounterMessage
+case class Value(p: Pid) extends CounterMessage
+case class Result(v: int) extends CounterMessage
+
+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)
+ }
+ }
+}
+
+class CounterUser extends RemoteActor {
+ override def run(): unit = {
+ alive(TCP())
+
+ spawn(TcpNode("127.0.0.1", 9090), "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 CounterTest {
+ 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/customer.scala b/docs/examples/actors/customer.scala
new file mode 100644
index 0000000000..64453aee16
--- /dev/null
+++ b/docs/examples/actors/customer.scala
@@ -0,0 +1,66 @@
+/**
+ @author Philipp Haller <philipp.haller@epfl.ch>
+
+ This shows "customer passing" for implementing
+ recursive algorithms using actors.
+ */
+
+import scala.actors._;
+import scala.actors.single._;
+
+abstract class FactorialMessage;
+case class Factorial(n: int, resTo: Actor) extends FactorialMessage;
+case class Value(n: int) extends FactorialMessage;
+
+class FactorialProcess extends Actor {
+ override def run: unit = {
+ receive {
+ case Factorial(n, resTo) =>
+ if (n == 0) {
+ Debug.info("Sending Value(1) to " + resTo)
+ resTo send Value(1)
+ }
+ else {
+ // spawn process that multiplies
+ /*val m = spawnReceive({
+ case Value(value) => resTo send Value(n * value)
+ });*/
+
+ val m = new MultiplyActor(n, resTo)
+ m.start
+ Debug.info("Sending Factorial(" + (n-1) + ", " + m + ") to " + this)
+ this send Factorial(n-1, m)
+ }
+ run
+ }
+ }
+}
+
+class MultiplyActor(factor: int, resTo: Actor) extends Actor {
+ override def run: unit =
+ receive {
+ case Value(value) =>
+ Debug.info("Sending Value(" + factor * value + ") to " + resTo)
+ resTo send Value(factor * value)
+ Debug.info("Done sending.")
+ }
+}
+
+object CustomerPassing {
+ def main(args: Array[String]): unit = {
+ val fac = new FactorialProcess
+ fac.start
+
+ val c = new Actor {
+ override def run: unit = {
+ fac send Factorial(3, this)
+
+ receive {
+ case Value(value) =>
+ System.out.println("Result: " + value)
+ }
+ }
+ }
+ c.start
+ }
+}