I try to implement different behaviours using Akka actors. In my approach,
traits define
message handling behaviours. The concrete actors mix in those traits and
then pick
which behaviours they want when building out their receive function by
using partial
function chaining.
Unfortunately, it seems there is some trouble considering the instanciation
of the sender
of messages. As shown in the following console message alice is unable to
indetify the
sender of the message "Good" which is bob.
alice send Fine? to bob
bob replies Good to Actor[akka://StrategiesSystem/user/alice]
alice receives Good from Actor[akka://StrategiesSystem/deadLetters]
As you will see in my code the expected result is that bob should be
stopped which is not the case.
Your help will be appreciated.
Thanks in advance,
Maxime.
--
import akka.actor._
import scala.util.Random
//The messages which can be exchanged
sealed trait Move
case object StartMessage extends Move
case object StopMessage extends Move
case object Fine extends Move
case object Good extends Move
case object Bad extends Move
//The general class representing my actors
abstract class MyActor(val others: List[String]) extends Actor{
//its name
val name=self.path.name
//it knows to choose on interlocutor
val interlocteur=Random.shuffle(others.filterNot(p=> p==name)).head
//All the actors are able to interpret the start/stop messages
def metaReceive : Receive= {
case StartMessage =>//start to ask question
println(name+" send Fine? to "+interlocteur)
context.actorSelection("../"+interlocteur) ! Fine
case StopMessage =>
println(name+" stops")
context.stop(self)
}
}
//An optimistic actor says it is fine
trait Optimistic{
self: MyActor =>
def handleFine:Receive = {
case Fine =>
println(self.name+" replies Good to "+sender)
sender ! Good
}
}
//A pessimistic actor says it is not fine
trait Pessimistic{
self: MyActor =>
def handleFine:Receive = {
case Fine =>
println(self.name+" replies Bad to "+sender)
sender ! Good
}
}
//An interpretor is an actor which is able to understand the reply
trait Interpretor{
self: MyActor =>
def handleAnswer:Receive = {
case Good =>
println(name+" receives Good from "+sender)
sender ! StopMessage
case Bad =>
println(name+" receives Bad from "+sender)
sender ! StopMessage
}
}
//My basic classes
class MyOptimisticActor(others: List[String]) extends MyActor(others) with
Optimistic{
override def receive = metaReceive orElse handleFine //orElse ...
}
class MyPessimisticActor(others: List[String]) extends MyActor(others)
with Pessimistic{
override def receive = metaReceive orElse handleFine //orElse ...
}
class MyInterpretorActor(others: List[String]) extends MyActor(others)
with Interpretor{
override def receive = metaReceive orElse handleAnswer
}
//My application
object TestStrategies extends Application {
val system = ActorSystem("StrategiesSystem")
val names= List("alice","bob","carla")
val alice = system.actorOf(Props(new MyInterpretorActor(names)), name =
"alice")// alice is able to ask question and interpret answer
val bob = system.actorOf(Props(new MyOptimisticActor(names)), name =
"bob") // bob is able to reply and it is fine
val carla = system.actorOf(Props(new MyPessimisticActor(names)), name =
"carla") //carla is able to reply and it is not fine
alice ! StartMessage //alice must ask a question
}
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.