tags:

views:

198

answers:

1

Within my actor I have to create a class which sends a message to another actor. The other actor should reply back to actor A

class A extends Actor {

val b = new B
b.start

val i = new DefaultHandler() {
      override def fun(a: String) = {  
       b ! payload
      }
     }
someotherclass.registerHandler(i)

def act = {
      loop {
     react {
       case reply => //do something

}

class B extends Actor {

def act = {
      loop {
     react {
       case msg => sender ! reply

          }
}

The problem now is that while sending from the inner class I'm not within the actor itself anymore and as a result actor B does not get a correct reference to actor B. One way to fix this would be to pass a reference to A via the message but this seems quite ugly to me.

val ref = self
val i = new DefaultClass() {
      override def fun(a: String) = {  
       b ! message(payload, ref)
      }
     }

Is there a more elegant way to solve this?

+1  A: 

Actor B receives the correct reference, which is not A. However you already answered your own question.

A different approach would include:

  1. Extending from Actor
  2. Optional: Have an implicit conversion to your kind of Actor
  3. Have an implicit actor reference in your message operator

In that case your DefaultHandler would have to keep a reference to your original Actor as well.

Joa Ebert