Guest User

Untitled

a guest
Jun 24th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package suereth.helper
  2.  
  3. import scala.actors._
  4.  
  5. /**
  6. * An Actor Scheduler which defers all processing to the Swing Event Queue.
  7. */
  8. object SwingEventQueueScheduler extends IScheduler {
  9. /** Submits a closure for execution.
  10. *
  11. * @param fun the closure to be executed
  12. */
  13. def execute(fun: => Unit): Unit = {
  14. execute(new Runnable {
  15. def run = fun
  16. })
  17. }
  18.  
  19. /** Submits a <code>Runnable</code> for execution.
  20. *
  21. * @param task the task to be executed
  22. */
  23. def execute(task: Runnable): Unit = {
  24. //TODO - If we are in the event queue thread, can we be greedy and just process now?
  25. import javax.swing.SwingUtilities
  26. SwingUtilities.invokeLater(task)
  27. }
  28.  
  29. /** Notifies the scheduler about activity of the
  30. * executing actor.
  31. *
  32. * @param a the active actor
  33. */
  34. def tick(a: Actor): Unit = {}
  35.  
  36. /** Shuts down the scheduler.
  37. */
  38. def shutdown(): Unit = {}
  39.  
  40. def onLockup(handler: () => Unit): Unit = {}
  41. def onLockup(millis: Int)(handler: () => Unit): Unit = {}
  42. def printActorDump: Unit = {}
  43. }
  44.  
  45. /**
  46. * An actor which processes all of its messages on the swing event queue.
  47. */
  48. trait SwingActor extends Actor {
  49. override protected def scheduler = SwingEventQueueScheduler
  50. //TODO - Do we need to override start and remove the ActorGC junk?
  51. //We automatically start the actor, because we rely on normal GC, and our scheduler doesn't do anything special besides processing on the AWT-Thread.
  52. start
  53. }
  54.  
  55.  
  56.  
  57. import scala.swing.Reactor
  58. import scala.swing.event.Event
  59. /** A marker class for messages passed into a component via the actors library */
  60. case class ExternalMessage[A](msg : A) extends Event
  61.  
  62. /** This trait can be used with scala.swing Reactor compoennts to allow the processing of external messages via reactions*/
  63. trait ActorReactorBridge extends SwingActor with Reactor {
  64. def act() : Unit = react {
  65. //TODO - handle internal messages directly
  66. case msg =>
  67. reactions(ExternalMessage(msg))
  68. act()
  69. }
  70. }
  71.  
  72. ---------------------------------------------------------------------------------------------
  73. package suereth.helper
  74.  
  75.  
  76. import scala.swing._
  77. import scala.actors.Actor._
  78. import scala.actors.Actor
  79.  
  80. object TestActors {
  81. def main(args : Array[String]) {
  82.  
  83. val text = new TextArea with ActorReactorBridge {
  84. text = "Initial Text"
  85. size = (300,300)
  86.  
  87. var globalCount = 1
  88.  
  89. reactions += {
  90. case ExternalMessage(x : String) =>
  91. text = (text + "\n" + "Global Msg #" + globalCount + ": " + x)
  92. globalCount += 1
  93. }
  94. }
  95.  
  96.  
  97. val frame = new MainFrame {
  98. frame =>
  99. size = (400, 400)
  100. title = "Test Actors"
  101. val panel = new Panel { panel =>
  102. _contents += text
  103. size = (400,400)
  104.  
  105. }
  106. contents = panel
  107. }
  108. frame.visible = true
  109.  
  110. def actorGenerator(id : Int) : Actor = actor {
  111. loop {
  112. react {
  113. case x : String =>
  114. Thread.sleep( (Math.random * 10).toLong)
  115. text ! ("Actor#" + id + ": " + x)
  116. }
  117. }
  118. }
  119. val actors = (1 to 9).map(actorGenerator).toList
  120. for(i <- 1 to 5; a <- actors) {
  121. a ! ("Msg#" + i)
  122. }
  123. ()
  124. //TODO - Wait for scala actors to wind down and check output
  125.  
  126. }
  127. }
Add Comment
Please, Sign In to add comment