0

我对 akka typed 有点陌生,我试图发送一条需要在给定时间内回答的消息。
我发现了带有 ask 的请求-响应模式,这看起来很有趣,但是有没有办法在已经定义的 Behaviours.receive 中实现它?

这里的想法是每次玩家回答或超时后调用 nextPlayerTurn

override def refereeTurn(): Behavior[Msg] = Behaviors.receive {
   case (_, msg: GuessMsg) =>
      if(currentPlayer.isDefined && currentPlayer.get == msg.getSender) {
        controller ! msg
      } else {
        println("Player tried to guess after Timeout")
      }
      Behaviors.same
  case (context, msg: ReceivedResponseMsg) =>
      if(currentPlayer.isDefined && currentPlayer.get == msg.getSender) 
        nextPlayerTurn(context)     
      Behaviors.same
      ...
}

...

/**
   * Tells to a player to start his turn and sets a timer that defines time in which a player has to make a guess.
   * If such guess isn't made, sends that user an end turn message, fails the promise of his turn and allows next
   * player to play his turn
*/
  override def nextPlayerTurn(ctx: ActorContext[Msg]): Unit = {
    implicit val timeout: Timeout = Timeout.timeout
    currentPlayer = Option(turnManager.nextPlayer)
    ctx.ask[Msg,Msg](currentPlayer.get, ref => YourTurnMsg(ref)) {
      case Success(msg: GuessMsg) => println("\n SUCCESS"); msg
      case Failure(_) => println(currentPlayer.get +" didn't guess in time"); TurnEnd(currentPlayer.get)
      case _ => TurnEnd(currentPlayer.get)
    }
  }

在这种情况下,在发送 YourTurnMsg 之后,玩家应该使用停止计时器的 GuessMsg 进行响应,这永远不会发生,因为在执行裁判内部匹配的情况下,而不是成功(它总是在超时后给出失败) .

我是否对询问模式有错误的想法,应该只使用计时器做出新的行为?

4

1 回答 1

2

如果您想使用该ask模式,则处理结果的代码需要向主要参与者发送消息,而不是尝试直接进行任何处理。您可以根据结果发送不同的消息,或者只发送原始结果并在 actor 中处理它,但您不能在该代码中执行任何依赖于 actor 状态的事情,因为它可能在不同的线程上运行。

但这ask并不便宜,因此在这种情况下,最好设置一个计时器并查看哪个消息首先返回。

于 2021-03-26T18:06:12.493 回答