I am building an implementation and I would like to switch around behaviours. How do I switch between three behaviours and back? My issues is that I do not know the right way to change the behaviour and then change it back to the main behaviour after processing that message. I have tried this
def answerMachine: Behavior[Message] = Behaviors.setup{ context: ActorContext[Message] =>
Behaviors.receiveMessage{
case s@Behave =>
println("Am told to behave: changing like chameleon")
EchoMachine(s, Behaviors.same)
case Back() =>
println("Now am back")
Behaviors.same
}
}
def EchoMachine(firstMsg: Message, behav: Behavior[Message]): Behavior[Message] = Behaviors.setup{ context: ActorContext[Message] =>
Behaviors.receiveMessage {
case Back() =>
println("I have changed to EchoMachine..")
behav
case Behave =>
println("I am behaving oo")
behav
}
}
val system = ActorSystem(TypedConvo.answerMachine, "Machine")
system.ref ! Behave
system.ref ! Back()
By the time I sent Back() I expected answerMachine to be the one that get the message but it was EchoMachine even after changing behavior.
Thanks.