changeset 2:2a754f9be68f

set incremental
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 29 Jan 2018 16:46:26 +0900
parents 8c3460ff323a
children 4ceb0fc4c564
files src/main/scala/IncrementSample.scala
diffstat 1 files changed, 48 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/IncrementSample.scala	Mon Jan 29 15:16:14 2018 +0900
+++ b/src/main/scala/IncrementSample.scala	Mon Jan 29 16:46:26 2018 +0900
@@ -1,29 +1,55 @@
 package IncrementSample
 
 
-import akka.actor.{Actor, ActorRef, ActorSystem, Props}
+import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props, TypedActor}
+import akka.japi.Util
+
+import scala.concurrent.Await
 
 object Incrementer {
-  def props: Props = Props[Incrementer]
+  //def props: Props = Props[Incrementer]
+  def props(printerActor:ActorRef): Props = Props(new Incrementer(printerActor))
 
-//  final case class IncrementAndSend(data:Int, otherActor: ActorRef)
-  case object Print
+  final case class SendData(data:Int)
+  final case class SendActor(actor:ActorRef)
 }
 
 class Incrementer(printerActor: ActorRef) extends Actor {
   import  Incrementer._
+  import  Printer._
 
-  /*
-  def IncrementAndSend = {
-    case  IncrementAndSend(data,otherActor) =>
-            otherActor ! IncrementAndSend(data,self)
 
-  }
-  */
+  var otherActor = ActorRef.noSender
+
 
   def receive = {
-    case IntData(data) =>
-          incrementData(data)
+    case SendData(data) =>
+        if (data < 10 ) {
+          //val send_data = incrementData(data)
+          val send_data = data + 1
+          otherActor ! SendData(send_data)
+          printerActor ! Println(send_data.toString)
+        } else {
+          // インクリメント終了を伝達
+          printerActor ! Println("fin")
+        }
+    case SendActor(actor :ActorRef) =>
+      otherActor = actor
+  }
+}
+
+object Printer {
+  def props: Props = Props[Printer]
+
+  final case class Println(message: String)
+}
+
+class Printer extends  Actor with ActorLogging {
+  import Printer._
+
+  def receive = {
+    case Println(message) =>
+      log.info(s"Printer received (from ${sender()} ): $message")
   }
 }
 
@@ -34,5 +60,14 @@
 
   val system: ActorSystem = ActorSystem("incrementSample")
 
-  val object1 :ActorRef = system.actorOf(Incrementer.props,"incrementActor")
+  val printer: ActorRef = system.actorOf(Printer.props,"printerActor")
+
+  val hoge: ActorRef = system.actorOf(Incrementer.props(printer),"HogeActor")
+
+  val foo: ActorRef = system.actorOf(Incrementer.props(printer),"FooActor")
+
+  foo ! SendActor(hoge)
+  hoge ! SendActor(foo)
+
+  foo ! SendData(1:Int)
 }