I’m working on a homework assignment, modifying code created by my professor. Unfortunately, he’s not available to me currently so I’m reaching out to the stackoverflow crowd.
This snippet is from a file “Peer.scala”, which communicates with an actor from another class “RemoteActorChat.scala”. I have only included the snippet that I believe is the cause of my issue, for the sake of brevity. Should the rest of the code be useful in finding a solution, I’ll gladly post it.
My intent is to have the Peer capture input from the console in a continuous loop, while simultaneously reacting to any messages received from the RemoteChatActor.
…
def act {
...
loop {
val textInput = Console.readLine(name + ">").toString()
textInput match {
case "Unsubscribe" =>
unsubscribe
case "Subscribe" =>
subscribe(name))
case "?" =>
println("Type any message to send it to the chatroom")
println("Type 'Unsubscribe' (without quotes) to leave the chatroom")
println("Type 'Subscribe' (without quotes) to re-join the chatroom")
case _ =>
post(textInput)
}
react {
case Post(msg) =>
println(name + " got a post = " + msg)
}
}
}
…
When the inputText is matched to _ the post function is called, does it’s thing and I get another prompt. I can post messages all day long like this if I want.
However, when inputText is matched to “Unsubscribe”, “Subscribe”, or “?”, the behavior is different. The statements in the case are executed IE) the unsubscribe or subscribe functions are called and do their thing as expected. However, I don’t get the prompt back to continue sending input from the console. The peer basically just hangs at this point. I expect another prompt but the console window is just empty and doesn’t take any additional input.
Clearly my understanding of how this should work is flawed.
What am I misunderstanding, and how can I make this do what I intend?
The actor is waiting for a
Postinsidereact. It won’t continue until it gets one. Since you only send one in one case, only that case gets through.If you want to make the other cases work, either have them also send a message that the
reactblock can receive (at least as acase _inside the react block), or move the react block so it will only be entered if there will in fact be aPostcoming back its way at some point.