Permalink
Browse files

Added actors library.

  • Loading branch information...
1 parent 3fe40a9 commit 0d8b14c6055e76c0bff3b65d0f428d711abe1f5a @phaller phaller committed Jun 21, 2006
Showing with 4,916 additions and 1 deletion.
  1. +39 −1 build.xml
  2. +60 −0 docs/examples/actors/counter.scala
  3. +66 −0 docs/examples/actors/customer.scala
  4. +38 −0 src/actors/scala/actors/Debug.scala
  5. +19 −0 src/actors/scala/actors/Done.scala
  6. +74 −0 src/actors/scala/actors/SPanel.scala
  7. +236 −0 src/actors/scala/actors/Scheduler.scala
  8. +156 −0 src/actors/scala/actors/TimerThread.scala
  9. +34 −0 src/actors/scala/actors/WorkerThread.scala
  10. +9 −0 src/actors/scala/actors/distributed/JXTAServiceBase.scala
  11. +34 −0 src/actors/scala/actors/distributed/JavaSerializer.scala
  12. +40 −0 src/actors/scala/actors/distributed/Messages.scala
  13. +36 −0 src/actors/scala/actors/distributed/MessagesComb.scala
  14. +7 −0 src/actors/scala/actors/distributed/Name.scala
  15. +591 −0 src/actors/scala/actors/distributed/NetKernel.scala
  16. +6 −0 src/actors/scala/actors/distributed/Node.scala
  17. +12 −0 src/actors/scala/actors/distributed/NodeComb.scala
  18. +168 −0 src/actors/scala/actors/distributed/RemoteActor.scala
  19. +184 −0 src/actors/scala/actors/distributed/RemotePid.scala
  20. +53 −0 src/actors/scala/actors/distributed/Serializer.scala
  21. +72 −0 src/actors/scala/actors/distributed/Service.scala
  22. +126 −0 src/actors/scala/actors/distributed/TcpSerializerComb.scala
  23. +215 −0 src/actors/scala/actors/distributed/TcpService.scala
  24. +86 −0 src/actors/scala/actors/distributed/TcpServiceWorker.scala
  25. +58 −0 src/actors/scala/actors/distributed/Util.scala
  26. +436 −0 src/actors/scala/actors/distributed/picklers/BytePickle.scala
  27. +519 −0 src/actors/scala/actors/distributed/picklers/SStreamPickle.scala
  28. +87 −0 src/actors/scala/actors/distributed/picklers/Streams.scala
  29. +77 −0 src/actors/scala/actors/distributed/picklers/UTF8Codec.scala
  30. +20 −0 src/actors/scala/actors/gui/Button.scala
  31. +8 −0 src/actors/scala/actors/gui/Caret.scala
  32. +9 −0 src/actors/scala/actors/gui/Component.scala
  33. +18 −0 src/actors/scala/actors/gui/Container.scala
  34. +8 −0 src/actors/scala/actors/gui/EmptyBorder.scala
  35. +9 −0 src/actors/scala/actors/gui/FormattedTextField.scala
  36. +33 −0 src/actors/scala/actors/gui/Frame.scala
  37. +20 −0 src/actors/scala/actors/gui/GUIApplication.scala
  38. +14 −0 src/actors/scala/actors/gui/Label.scala
  39. +14 −0 src/actors/scala/actors/gui/MainFrame.scala
  40. +11 −0 src/actors/scala/actors/gui/Orientation.scala
  41. +15 −0 src/actors/scala/actors/gui/Panel.scala
  42. +126 −0 src/actors/scala/actors/gui/Publisher.scala
  43. +14 −0 src/actors/scala/actors/gui/SimpleGUIApplication.scala
  44. +11 −0 src/actors/scala/actors/gui/SwingComponent.scala
  45. +22 −0 src/actors/scala/actors/gui/TextComponent.scala
  46. +22 −0 src/actors/scala/actors/gui/TextField.scala
  47. +3 −0 src/actors/scala/actors/gui/event/ButtonPressed.scala
  48. +3 −0 src/actors/scala/actors/gui/event/CaretUpdate.scala
  49. +3 −0 src/actors/scala/actors/gui/event/Event.scala
  50. +3 −0 src/actors/scala/actors/gui/event/MouseDragged.scala
  51. +3 −0 src/actors/scala/actors/gui/event/MouseEvent.scala
  52. +3 −0 src/actors/scala/actors/gui/event/MouseMoved.scala
  53. +3 −0 src/actors/scala/actors/gui/event/TextModified.scala
  54. +3 −0 src/actors/scala/actors/gui/event/WindowActivated.scala
  55. +3 −0 src/actors/scala/actors/gui/event/WindowClosed.scala
  56. +3 −0 src/actors/scala/actors/gui/event/WindowClosing.scala
  57. +3 −0 src/actors/scala/actors/gui/event/WindowDeactivated.scala
  58. +3 −0 src/actors/scala/actors/gui/event/WindowDeiconified.scala
  59. +5 −0 src/actors/scala/actors/gui/event/WindowEvent.scala
  60. +3 −0 src/actors/scala/actors/gui/event/WindowIconified.scala
  61. +3 −0 src/actors/scala/actors/gui/event/WindowOpened.scala
  62. +12 −0 src/actors/scala/actors/gui/layout.scala
  63. +10 −0 src/actors/scala/actors/multi/AbstractPid.scala
  64. +300 −0 src/actors/scala/actors/multi/Actor.scala
  65. +94 −0 src/actors/scala/actors/multi/LocalPid.scala
  66. +175 −0 src/actors/scala/actors/multi/MailBox.scala
  67. +20 −0 src/actors/scala/actors/multi/Pid.scala
  68. +16 −0 src/actors/scala/actors/multi/ReceiverTask.scala
  69. +10 −0 src/actors/scala/actors/single/AbstractPid.scala
  70. +72 −0 src/actors/scala/actors/single/Actor.scala
  71. +83 −0 src/actors/scala/actors/single/LocalPid.scala
  72. +155 −0 src/actors/scala/actors/single/MailBox.scala
  73. +10 −0 src/actors/scala/actors/single/Pid.scala
View
@@ -53,6 +53,7 @@ PROPERTIES
<property name="dist.dir" value="${basedir}/dists"/>
<property name="lib.jar.name" value="scala-library.jar"/>
<property name="dbc.jar.name" value="scala-dbc.jar"/>
+ <property name="actors.jar.name" value="scala-actors.jar"/>
<property name="comp.jar.name" value="scala-compiler.jar"/>
<property name="scala.exec.name" value="scala"/>
<property name="scalac.exec.name" value="scalac"/>
@@ -289,6 +290,7 @@ BUILD LOCAL REFERENCE (LOCKER) LAYER
<include name="**/*.scala"/>
<exclude name="scala/Predef.scala"/>
<exclude name="scala/dbc/**"/>
+ <exclude name="scala/actors/**"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</starr>
<!-- Build compiler -->
@@ -405,6 +407,7 @@ BUILD QUICK-TEST LAYER
<include name="**/*.scala"/>
<exclude name="scala/Predef.scala"/>
<exclude name="scala/dbc/**"/>
+ <exclude name="scala/actors/**"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</locker>
<!-- Build DBC -->
@@ -421,6 +424,20 @@ BUILD QUICK-TEST LAYER
<include name="scala/dbc/**/*.scala"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</locker>
+ <!-- Build actors library -->
+ <mkdir dir="${quick.dir}/lib/actors"/>
+ <locker
+ srcdir="${src.dir}/actors"
+ destdir="${quick.dir}/lib/actors"
+ addparams="${nsc.params}"
+ scalacdebugging="${nsc.log-files}">
+ <classpath>
+ <pathelement location="${quick.dir}/lib/library"/>
+ <pathelement location="${quick.dir}/lib/actors"/>
+ </classpath>
+ <include name="scala/actors/**/*.scala"/>
+ <excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
+ </locker>
<!-- Build compiler -->
<mkdir dir="${quick.dir}/lib/compiler"/>
<locker
@@ -540,6 +557,7 @@ TEST
<include name="**/*.scala"/>
<exclude name="scala/Predef.scala"/>
<exclude name="scala/dbc/**"/>
+ <exclude name="scala/actors/**"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</quick>
<!-- Build DBC -->
@@ -555,6 +573,19 @@ TEST
<include name="scala/dbc/**/*.scala"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</quick>
+ <!-- Build actors library -->
+ <mkdir dir="${strap.dir}/lib/actors"/>
+ <quick
+ srcdir="${src.dir}/actors"
+ destdir="${strap.dir}/lib/actors"
+ addparams="${nsc.params}">
+ <classpath>
+ <pathelement location="${strap.dir}/lib/library"/>
+ <pathelement location="${strap.dir}/lib/actors"/>
+ </classpath>
+ <include name="scala/actors/**/*.scala"/>
+ <excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
+ </quick>
<!-- Build compiler -->
<mkdir dir="${strap.dir}/lib/compiler"/>
<quick
@@ -683,6 +714,7 @@ DOCUMENTATION
documenttitle="&lt;div&gt;Scala ${version.number}&lt;/div&gt;"
classpath="${quick.dir}/lib/library">
<include name="dbc/**/*.scala"/>
+ <include name="actors/**/*.scala"/>
<include name="library/**/*.scala"/>
<excludesfile name="${nsc.excludes.file}" if="excludes.avail"/>
</quickdoc>
@@ -824,6 +856,12 @@ GENERATES A DISTRIBUTION
<attribute name="Signature-Version" value="${version.number}"/>
</manifest>
</jar>
+ <jar destfile="${dist.current.dir}/lib/${actors.jar.name}">
+ <fileset dir="${strap.dir}/lib/actors"/>
+ <manifest>
+ <attribute name="Signature-Version" value="${version.number}"/>
+ </manifest>
+ </jar>
<!-- Copy executable files -->
<mkdir dir="${dist.current.dir}/bin"/>
<copy todir="${dist.current.dir}/bin">
@@ -886,7 +924,7 @@ GENERATES A DISTRIBUTION
version="${version.number}"
desc="The Scala library. This is the minimal requirement to run any Scala program."
link="${sbaz.universe}/scala-library-${version.number}.sbp">
- <libset dir="${dist.current.dir}/lib" includes="${lib.jar.name},${dbc.jar.name}"/>
+ <libset dir="${dist.current.dir}/lib" includes="${lib.jar.name},${dbc.jar.name},${actors.jar.name}"/>
</quicksbaz>
<!-- Create the Scala developper package -->
<quicksbaz
@@ -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()
+ }
+}
@@ -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
+ }
+}
@@ -0,0 +1,38 @@
+package scala.actors;
+
+/**
+ * @author Philipp Haller
+ */
+object Debug {
+ var lev = 2
+
+ def level = lev
+ def level_= (lev: int) = {
+ //Console.println("Setting debug level to " + lev)
+ this.lev = lev
+ }
+
+ def info(s: String) =
+ if (lev > 2) System.out.println("Info: " + s)
+ def warning(s: String) =
+ if (lev > 1) System.err.println("Warning: " + s)
+ def error(s: String) =
+ if (lev > 0) System.err.println("Error: " + s)
+}
+
+class Debug(tag: String) {
+ var lev = 2
+
+ def level = lev
+ def level_= (lev: int) = {
+ //Console.println("Setting debug level (" + tag + ") to " + lev)
+ this.lev = lev
+ }
+
+ def info(s: String) =
+ if (lev > 2) System.out.println(tag + " (info): " + s)
+ def warning(s: String) =
+ if (lev > 1) System.err.println(tag + " (warn): " + s)
+ def error(s: String) =
+ if (lev > 0) System.err.println(tag + " (erro): " + s)
+}
@@ -0,0 +1,19 @@
+package scala.actors;
+
+/**
+ * @author Philipp Haller
+ */
+class Done extends Throwable {
+ override def fillInStackTrace(): Throwable =
+ this;
+}
+
+class ContinueException extends Throwable {
+ override def fillInStackTrace(): Throwable =
+ this;
+}
+
+class AbortException extends Throwable {
+ override def fillInStackTrace(): Throwable =
+ this;
+}
@@ -0,0 +1,74 @@
+package scala.actors;
+
+
+/*
+ * SPanel.scala
+ * GUI for simple texts
+ *
+ */
+import java.awt._;
+import java.awt.event._;
+import javax.swing.event._;
+import javax.swing._;
+import java.io._;
+
+
+
+class SPanel (WIDTH:int, HEIGHT:int, title:String) extends JFrame{
+
+ private var textArea:JTextArea = null;
+ private var resetButton:JButton = null;
+
+ private var scrollPane:JScrollPane = null;
+ private var panel:JPanel = null;
+ private var contentPane:Container = null;
+ private var levelChoice:JComboBox = null;
+ private var formatChoice:JComboBox = null;
+
+ //init
+ setTitle(title);
+ setSize(WIDTH, HEIGHT);
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ contentPane = getContentPane();
+ panel = new JPanel();
+ contentPane.add(panel, BorderLayout.SOUTH);
+
+ // Add a text area with scroll bars
+ textArea = new JTextArea(8, 40);
+ scrollPane = new JScrollPane(textArea);
+ contentPane.add(scrollPane, BorderLayout.CENTER);
+
+ // Add a "reset textarea" button
+ resetButton = new JButton("Clear");
+ panel.add(resetButton);
+ resetButton.addActionListener(
+ new ActionListener() {
+ def actionPerformed(evt:ActionEvent):Unit= {
+ textArea.setText("");
+ }
+ }
+ );
+
+ this.repaint();
+ this.show();
+
+
+ def addText(text:String):Unit = {
+ textArea.append(text+'\n');
+
+ if ( textArea.getHeight() > scrollPane.getHeight() ) {
+ scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
+ }
+ repaint();
+ }
+/*
+ def actionPerformed(ActionEvent e):Unit {
+ }
+*/
+
+ override def paint(g:Graphics ): unit = {
+ super.paint( g );
+ }
+
+}
Oops, something went wrong.

0 comments on commit 0d8b14c

Please sign in to comment.