From 60b3d90f81cf3f83440725a02afc7dc693fa9ea5 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Fri, 29 Sep 2006 16:50:44 +0000 Subject: Checked in examples for new actors lib. --- docs/examples/actors/BoundedBufferTest.scala | 33 +++++++++++++++ docs/examples/actors/Joins.scala | 60 +++++++++++++++++++++++++++ docs/examples/actors/Loop.scala | 20 +++++++++ docs/examples/actors/RemoteCounter.scala | 45 ++++++++++++++++++++ docs/examples/actors/Seq.scala | 24 +++++++++++ docs/examples/actors/counter.scala | 62 ---------------------------- docs/examples/actors/customer.scala | 56 ------------------------- 7 files changed, 182 insertions(+), 118 deletions(-) create mode 100644 docs/examples/actors/BoundedBufferTest.scala create mode 100644 docs/examples/actors/Joins.scala create mode 100644 docs/examples/actors/Loop.scala create mode 100644 docs/examples/actors/RemoteCounter.scala create mode 100644 docs/examples/actors/Seq.scala delete mode 100644 docs/examples/actors/counter.scala delete mode 100644 docs/examples/actors/customer.scala (limited to 'docs/examples/actors') diff --git a/docs/examples/actors/BoundedBufferTest.scala b/docs/examples/actors/BoundedBufferTest.scala new file mode 100644 index 0000000000..5a04f7aafd --- /dev/null +++ b/docs/examples/actors/BoundedBufferTest.scala @@ -0,0 +1,33 @@ +package examples.actors + +import scala.actors.Actor._ + +class BoundedBuffer[T](N: int) { + private case class Get + private case class Put(x: T) + + private val buffer = actor { + val buf = new Array[T](N) + var in = 0; var out = 0; var n = 0 + while(true) { + receive { + case Put(x) if n < N => + buf(in) = x; in = (in + 1) % N; n = n + 1; reply() + case Get() if n > 0 => + val r = buf(out); out = (out + 1) % N; n = n - 1; reply(r) + } + } + } + + def put(x: T): Unit = buffer !? Put(x) + + def get: T = (buffer !? Get()).asInstanceOf[T] +} + +object BoundedBufferTest { + def main(args: Array[String]) = { + val buf = new BoundedBuffer[Int](1) + buf.put(42) + scala.Console.println("" + buf.get) + } +} diff --git a/docs/examples/actors/Joins.scala b/docs/examples/actors/Joins.scala new file mode 100644 index 0000000000..7ea2a50e11 --- /dev/null +++ b/docs/examples/actors/Joins.scala @@ -0,0 +1,60 @@ +package examples.actors + +import scala.actors.Actor._ + +abstract class Producer[T] extends Iterator[T] { + + protected def produce(x: T): unit = coordinator !? HasValue(x) + protected def produceValues: unit + + def hasNext: boolean = { setCurrent(); !current.isEmpty } + def next: T = { + setCurrent() + val res = current.get + current = (coordinator !? Next()).asInstanceOf[Option[T]] + res + } + + private var current: Option[T] = null + private def setCurrent() = if (current == null) current = (coordinator !? Next()).asInstanceOf[Option[T]] + + private case class HasValue(value: T) + private case class Next + private case class Done + private case class Continue + + /** A thread-based coordinator */ + private val coordinator = actor { + while (true) { + receive { + case Next() => + reply { + receive { + case HasValue(v) => + reply() + Some(v) + case Done() => + None + } + } + } + } + } + + actor { + produceValues + coordinator !? Done() + } +} + +object Joins extends Application { + def from(m: int, n: int) = new Producer[int] { + def produceValues = for (val i <- m until n) produce(i) + } + + // note that it works from the main thread + val it = from(1, 10) + while (it.hasNext) { + Console.println(it.next) + } +} diff --git a/docs/examples/actors/Loop.scala b/docs/examples/actors/Loop.scala new file mode 100644 index 0000000000..0799996d81 --- /dev/null +++ b/docs/examples/actors/Loop.scala @@ -0,0 +1,20 @@ +package examples.actors + +import scala.actors.Actor._ + +object Loop extends Application { + + case object A + + val a = reactor { + loop { + react { + case A => scala.Console.println("got A") + } + } + } + + for (val i <- List.range(0, 10)) { + a ! A + } +} diff --git a/docs/examples/actors/RemoteCounter.scala b/docs/examples/actors/RemoteCounter.scala new file mode 100644 index 0000000000..b52259b6c2 --- /dev/null +++ b/docs/examples/actors/RemoteCounter.scala @@ -0,0 +1,45 @@ +package examples.actors + +import scala.actors.Actor._ +import scala.actors.RemoteActor._ +import scala.actors.{Actor,TcpNode} + +case object Incr +case object Value +case class Result(v: int) + +object RemoteCounter extends Application { + actor { + def loop(value: int): unit = { + Console.println("Value: " + value) + receive { + case Incr => loop(value + 1) + case Value => { sender ! Result(value); loop(value) } + case other => loop(value) + } + } + + alive(9010) + register('counter, self) + loop(0) + } + + actor { + val c = select(TcpNode("127.0.0.1", 9010), 'counter) + + c ! Incr + c ! Incr + + try { Thread.sleep(1000) } catch { + case ie: InterruptedException => + } + + c ! Value + receive { + case Result(v) => { + Console.println("Received result: " + v) + sender ! Incr + } + } + } +} diff --git a/docs/examples/actors/Seq.scala b/docs/examples/actors/Seq.scala new file mode 100644 index 0000000000..382c6c2d79 --- /dev/null +++ b/docs/examples/actors/Seq.scala @@ -0,0 +1,24 @@ +package examples.actors + +import scala.actors.Actor._ + +object Seq extends Application { + + case object A + + val a = reactor { + { + react { + case A => scala.Console.println("got A") + } + () + } andThen { + react { + case A => scala.Console.println("2nd reactor got A") + } + () + } + } + a ! A + a ! A +} diff --git a/docs/examples/actors/counter.scala b/docs/examples/actors/counter.scala deleted file mode 100644 index e768a8f9ef..0000000000 --- a/docs/examples/actors/counter.scala +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @author Philipp Haller - */ - -package examples.actors - -import scala.actors.Process -import scala.actors.distributed.{RemoteActor,TCP,TcpNode,TcpService} - -case class Incr() -case class Value(p: Process) -case class Result(v: int) - -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()) - - val host = java.net.InetAddress.getLocalHost().getHostAddress() - spawn(TcpNode(host, 9090), classOf[Counter].getName()) - - receive { - case p: Process => - // 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 deleted file mode 100644 index 32787092d1..0000000000 --- a/docs/examples/actors/customer.scala +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @author Philipp Haller - * - * This shows "customer passing" for implementing - * recursive algorithms using actors. - */ - -package examples.actors - -import scala.actors.single.Actor - -case class Factorial(n: int, resTo: Actor[int]) - -class FactorialProcess extends Actor[Factorial] { - override def run: unit = { - receive { - case Factorial(n, resTo) => - if (n == 0) { - resTo ! 1 - } - else { - val m = new MultiplyActor(n, resTo) - m.start() - this ! Factorial(n-1, m) - } - run - } - } -} - -class MultiplyActor(factor: int, resTo: Actor[int]) extends Actor[int] { - override def run: unit = - receive { - case value: int => - resTo ! factor * value - } -} - -object CustomerPassing { - def main(args: Array[String]): unit = { - val fac = new FactorialProcess - fac.start() - - val c = new Actor[int] { - override def run: unit = { - fac ! Factorial(3, this) - - receive { - case value: int => - System.out.println("Result: " + value) - } - } - } - c.start() - } -} -- cgit v1.2.3