Scala:接收管道模式实现

时间:2018-01-29 16:22:56

标签: scala akka

我正在研究scala程序的行为。 我不是scala程序员,但我需要了解它是如何工作的。 任何人都可以给我一个关于接收管道模式的好解释。 以下链接https://doc.akka.io/docs/akka/2.4-M1/contrib/receive-pipeline.html我找到了我想知道的一部分。

我也很感兴趣这种拦截器如何使用UI发送/ riceves消息。 在我的例子中,我有拦截器的实现,但我不明白它是如何设置“套接字”(如果是说明套接字)与UI通信。 我附上我的例子的代码(显然是有效的)

import actoverse._
import akka.actor._

class Output extends Actor with DebuggingSupporter {
  val receive: Receive = {
    case msg =>
      println(msg)
  }
}

@Comprehensive case class SetCount(num: Int)

class AsyncAnd(out: ActorRef) extends Actor with DebuggingSupporter {
  @State var count: Int = 0
  val receive: Receive = {
    case SetCount(n) => 
      count = n
    case (v: Boolean) =>
      if (v) {
        count -= 1
        if(count == 0) {
          out !+ true
        }
      } else {
        out !+ false
      }
  }
}

class MockResponder (response: Boolean, target: ActorRef)
 extends Actor with DebuggingSupporter {
  val receive: Receive = {
    case _ =>
      target !+ response
  }
}


class CoordinatorActor(asyncAnd: ActorRef, creditChecker: ActorRef, addressChecker: ActorRef) extends Actor with DebuggingSupporter {
  val receive: Receive = {
    case "start" => 
      asyncAnd !+ SetCount(2)
      creditChecker !+ "CARDNUMBER"
      addressChecker !+ "ADDRESS"
  }
}

object AsyncAndMain {
  def main(args: Array[String]){
    implicit val system = ActorSystem()
    val debuggingSystem = new DebuggingSystem
    debuggingSystem.introduce(system)

    val printer = system.actorOf(Props[Output], name="result")
    val asyncAnd = system.actorOf(Props(classOf[AsyncAnd], printer)
                                  , name="async_and")
    val creditChecker = system.actorOf(Props(classOf[MockResponder], true, asyncAnd)
                                  , name="credit_checker")
    val addressChecker = system.actorOf(Props(classOf[MockResponder], true, asyncAnd)
                                  , name="address_checker")

    val coordinator = system.actorOf(Props(classOf[CoordinatorActor], asyncAnd, creditChecker, addressChecker)
                                  , name="coordinator")

    coordinator ! "start"

    println("Ctrl-C to halt")
  }
}

0 个答案:

没有答案