This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Actors: send message to a class instead of an instance

13 replies
Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.

Hi,

Is it possible to pass a message to a class: to whole set of instances
of a specific class. Here I have the code:

import scala.actors._

class Fill extends Actor {
def act = {
react {
case i:String =>
println(i + ": rebut ")
}
}
}

class Mare extends Actor {
def act = {
react {
Fill ! "hola"
}
}
}

object Main {
def main(arg:Array[String]) = {
var m = new Mare
var fill = new Fill
var fill2 = new Fill

m.start
fill.start
fill2.start
}
}

m could pass a message to **all** instances of class Fill? How?

Thanks in advance,
Xan.

Paulo Siqueira
Joined: 2011-04-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance
Can't you just have a List of the actors, and 'foreach' to send the message to all of then? Or perhaps write an object 'FrontActor' that could do this for you. I don't know if any of those are good ideas, but anyway... =)

[]s,

2011/5/14 Xavier <xancorreu@gmail.com>
Hi,

Is it possible to pass a message to a class: to whole set of instances
of a specific class. Here I have the code:

import scala.actors._


class Fill extends Actor {
 def act = {
     react {
         case i:String =>
             println(i + ": rebut ")
     }
 }
}

class Mare extends Actor {
 def act = {
     react {
         Fill ! "hola"
     }
 }
}


object Main {
 def main(arg:Array[String]) = {
         var m = new Mare
         var fill = new Fill
         var fill2 = new Fill

         m.start
         fill.start
         fill2.start
     }
 }


m could pass a message to **all** instances of class Fill? How?

Thanks in advance,
Xan.



--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/
Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

On May 14, 9:12 pm, Paulo Siqueira wrote:
> Can't you just have a List of the actors, and 'foreach' to send the message
> to all of then?

Do you mean have a list of Fill instances in Mare class as a var?

Mare means Mother in catalan
Fill means Son in catalan.

The problem is to send a message from the Mother to all instances of
Sons.

> Or perhaps write an object 'FrontActor' that could do this
> for you.

Can you specify it?

> I don't know if any of those are good ideas, but anyway... =)
>
> []s,
>
> 2011/5/14 Xavier
>
>
>
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
> --
> Paulo "JCranky" Siqueira
> Visit my blog:http://www.jcranky.com/

Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

Mmmm,
thanks.
Press return after complete message.

Xan.

On May 14, 9:12 pm, Paulo Siqueira wrote:
> Can't you just have a List of the actors, and 'foreach' to send the message
> to all of then? Or perhaps write an object 'FrontActor' that could do this
> for you. I don't know if any of those are good ideas, but anyway... =)
>
> []s,
>
> 2011/5/14 Xavier
>
>
>
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
> --
> Paulo "JCranky" Siqueira
> Visit my blog:http://www.jcranky.com/

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

You could auto-register all your instances with a global object using
a weak map. Then send your message to the object that will forward it
to all the instances:

trait Registered extends Actor {
Forwarder.register(this)
}

object Forwarder {
private val registered = new
scala.collection.mutable.WeakHashMap[Registered, Any]()
def register(actor: Registered) {
registered.put(actor, Nil)
}

def forward(message: Any) {
for (actor <- registered.keys) {
actor ! message
}
}
}

class Fill extends Actor with Registered {
def act = {
react {
case i:String =>
println(i + ": rebut ")
}
}
}

On Sat, May 14, 2011 at 1:43 PM, Xavier wrote:
> Hi,
>
> Is it possible to pass a message to a class: to whole set of instances
> of a specific class. Here I have the code:
>
> import scala.actors._
>
>
> class Fill extends Actor {
>  def act = {
>      react {
>          case i:String =>
>              println(i + ": rebut ")
>      }
>  }
> }
>
> class Mare extends Actor {
>  def act = {
>      react {
>          Fill ! "hola"
>      }
>  }
> }
>
>
> object Main {
>  def main(arg:Array[String]) = {
>          var m = new Mare
>          var fill = new Fill
>          var fill2 = new Fill
>
>          m.start
>          fill.start
>          fill2.start
>      }
>  }
>
>
> m could pass a message to **all** instances of class Fill? How?
>
> Thanks in advance,
> Xan.

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Actors: send message to a class instead of an instance
Make the child actors register themself with the root actor, and let the root actor keep a list of all actors. When you want to update all, send a message to the root actor to perform the update. In the root actor, do a foreach on the list of child actors matching the type like Paulo Siqueira suggest, and send a message to each child actors to perform something.

case class Register(id: String, actor: Actor)
case object TriggerSomething(id: String)

rootActor = ...
childActor1 = ...
childActor2 = ...
childActor3 = ...
rootActor ! Register("id1", childActor1)
rootActor ! Register("id1", childActor2)
rootActor ! Register("id2", childActor3)
rootActor ! TriggerSomething("test1")

On 14 May 2011 20:43, Xavier <xancorreu@gmail.com> wrote:
Hi,

Is it possible to pass a message to a class: to whole set of instances
of a specific class. Here I have the code:

import scala.actors._


class Fill extends Actor {
 def act = {
     react {
         case i:String =>
             println(i + ": rebut ")
     }
 }
}

class Mare extends Actor {
 def act = {
     react {
         Fill ! "hola"
     }
 }
}


object Main {
 def main(arg:Array[String]) = {
         var m = new Mare
         var fill = new Fill
         var fill2 = new Fill

         m.start
         fill.start
         fill2.start
     }
 }


m could pass a message to **all** instances of class Fill? How?

Thanks in advance,
Xan.

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

Oh, and you should probably synchronized around the weak map... Or
make the Forwarder object itself an actor and only allow forwarding as
a response to a message.

On Sat, May 14, 2011 at 2:53 PM, Lex wrote:
> You could auto-register all your instances with a global object using
> a weak map. Then send your message to the object that will forward it
> to all the instances:
>
> trait Registered extends Actor {
>  Forwarder.register(this)
> }
>
> object Forwarder {
>  private val registered = new
> scala.collection.mutable.WeakHashMap[Registered, Any]()
>  def register(actor: Registered) {
>    registered.put(actor, Nil)
>  }
>
>  def forward(message: Any) {
>    for (actor <- registered.keys) {
>      actor ! message
>    }
>  }
> }
>
> class Fill extends Actor with Registered {
>  def act = {
>     react {
>         case i:String =>
>             println(i + ": rebut ")
>     }
>  }
> }
>
>

Paulo Siqueira
Joined: 2011-04-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance
I like the idea of making the Forwarder an Actor itself better.

[]s,

2011/5/14 Lex <lexn82@gmail.com>
Oh, and you should probably synchronized around the weak map... Or
make the Forwarder object itself an actor and only allow forwarding as
a response to a message.


On Sat, May 14, 2011 at 2:53 PM, Lex <lexn82@gmail.com> wrote:
> You could auto-register all your instances with a global object using
> a weak map. Then send your message to the object that will forward it
> to all the instances:
>
> trait Registered extends Actor {
>  Forwarder.register(this)
> }
>
> object Forwarder {
>  private val registered = new
> scala.collection.mutable.WeakHashMap[Registered, Any]()
>  def register(actor: Registered) {
>    registered.put(actor, Nil)
>  }
>
>  def forward(message: Any) {
>    for (actor <- registered.keys) {
>      actor ! message
>    }
>  }
> }
>
> class Fill extends Actor with Registered {
>  def act = {
>     react {
>         case i:String =>
>             println(i + ": rebut ")
>     }
>  }
> }
>
>



--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/
Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

The idea is good. But what is "case class"?

Thanks,
Xan.

On May 14, 9:48 pm, Trond Olsen wrote:
> Make the child actors register themself with the root actor, and let the
> root actor keep a list of all actors. When you want to update all, send a
> message to the root actor to perform the update. In the root actor, do a
> foreach on the list of child actors matching the type like Paulo Siqueira
> suggest, and send a message to each child actors to perform something.
>
> case class Register(id: String, actor: Actor)
> case object TriggerSomething(id: String)
>
> rootActor = ...
> childActor1 = ...
> childActor2 = ...
> childActor3 = ...
> rootActor ! Register("id1", childActor1)
> rootActor ! Register("id1", childActor2)
> rootActor ! Register("id2", childActor3)
> rootActor ! TriggerSomething("test1")
>
> On 14 May 2011 20:43, Xavier wrote:
>
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>

Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors: send message to a class instead of an instance

I like it very much, Lex.

On May 14, 9:53 pm, Lex wrote:
> You could auto-register all your instances with a global object using
> a weak map. Then send your message to the object that will forward it
> to all the instances:
>
> trait Registered extends Actor {
>   Forwarder.register(this)
>
> }
>
> object Forwarder {
>   private val registered = new
> scala.collection.mutable.WeakHashMap[Registered, Any]()
>   def register(actor: Registered) {
>     registered.put(actor, Nil)
>   }
>
>   def forward(message: Any) {
>     for (actor <- registered.keys) {
>       actor ! message
>     }
>   }
>
> }
>
> class Fill extends Actor with Registered {
>   def act = {
>      react {
>          case i:String =>
>              println(i + ": rebut ")
>      }
>   }
>
> }
> On Sat, May 14, 2011 at 1:43 PM, Xavier wrote:
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>

Paulo Siqueira
Joined: 2011-04-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Actors: send message to a class instead of an instance
About case classes: http://www.scala-lang.org/node/107

[]s,

2011/5/16 Xavier <xancorreu@gmail.com>
I like it very much, Lex.

On May 14, 9:53 pm, Lex <lex...@gmail.com> wrote:
> You could auto-register all your instances with a global object using
> a weak map. Then send your message to the object that will forward it
> to all the instances:
>
> trait Registered extends Actor {
>   Forwarder.register(this)
>
> }
>
> object Forwarder {
>   private val registered = new
> scala.collection.mutable.WeakHashMap[Registered, Any]()
>   def register(actor: Registered) {
>     registered.put(actor, Nil)
>   }
>
>   def forward(message: Any) {
>     for (actor <- registered.keys) {
>       actor ! message
>     }
>   }
>
> }
>
> class Fill extends Actor with Registered {
>   def act = {
>      react {
>          case i:String =>
>              println(i + ": rebut ")
>      }
>   }
>
> }
> On Sat, May 14, 2011 at 1:43 PM, Xavier <xancor...@gmail.com> wrote:
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>



--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/
tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Re: Actors: send message to a class instead of an instance
There was an error in the example. The last one should be a case class, not a case object since it has variables. You can read more about case classes on http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html also.

case class Add(group: String, actor: Actor)
case class TriggerActionOn(group: String)

rootActor = ...
childActor1 = ...
childActor2 = ...
childActor3 = ...
rootActor ! Add(group="a", actor=childActor1)
rootActor ! Add(group="a", actor=childActor2)
rootActor ! Add(group="b", actor=childActor3)
rootActor ! TriggerActionOn(group="a")


On 16 May 2011 21:56, Xavier <xancorreu@gmail.com> wrote:
The idea is good. But what is "case class"?

Thanks,
Xan.

On May 14, 9:48 pm, Trond Olsen <tr...@steinbit.org> wrote:
> Make the child actors register themself with the root actor, and let the
> root actor keep a list of all actors. When you want to update all, send a
> message to the root actor to perform the update. In the root actor, do a
> foreach on the list of child actors matching the type like Paulo Siqueira
> suggest, and send a message to each child actors to perform something.
>
> case class Register(id: String, actor: Actor)
> case object TriggerSomething(id: String)
>
> rootActor = ...
> childActor1 = ...
> childActor2 = ...
> childActor3 = ...
> rootActor ! Register("id1", childActor1)
> rootActor ! Register("id1", childActor2)
> rootActor ! Register("id2", childActor3)
> rootActor ! TriggerSomething("test1")
>
> On 14 May 2011 20:43, Xavier <xancor...@gmail.com> wrote:
>
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>

Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Actors: send message to a class instead of an instance
Thanks for references,
Xan.

Al 17/05/11 01:19, En/na Trond Olsen ha escrit:
BANLkTinavUUjsQsYegU8ZZW74yXjU4yPug [at] mail [dot] gmail [dot] com" type="cite">There was an error in the example. The last one should be a case class, not a case object since it has variables. You can read more about case classes on http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html also.

case class Add(group: String, actor: Actor)
case class TriggerActionOn(group: String)

rootActor = ...
childActor1 = ...
childActor2 = ...
childActor3 = ...
rootActor ! Add(group="a", actor=childActor1)
rootActor ! Add(group="a", actor=childActor2)
rootActor ! Add(group="b", actor=childActor3)
rootActor ! TriggerActionOn(group="a")


On 16 May 2011 21:56, Xavier <xancorreu [at] gmail [dot] com" rel="nofollow">xancorreu@gmail.com> wrote:
The idea is good. But what is "case class"?

Thanks,
Xan.

On May 14, 9:48 pm, Trond Olsen <tr [dot] [dot] [dot] [at] steinbit [dot] org" rel="nofollow">tr...@steinbit.org> wrote:
> Make the child actors register themself with the root actor, and let the
> root actor keep a list of all actors. When you want to update all, send a
> message to the root actor to perform the update. In the root actor, do a
> foreach on the list of child actors matching the type like Paulo Siqueira
> suggest, and send a message to each child actors to perform something.
>
> case class Register(id: String, actor: Actor)
> case object TriggerSomething(id: String)
>
> rootActor = ...
> childActor1 = ...
> childActor2 = ...
> childActor3 = ...
> rootActor ! Register("id1", childActor1)
> rootActor ! Register("id1", childActor2)
> rootActor ! Register("id2", childActor3)
> rootActor ! TriggerSomething("test1")
>
> On 14 May 2011 20:43, Xavier <xancor [dot] [dot] [dot] [at] gmail [dot] com" rel="nofollow">xancor...@gmail.com> wrote:
>
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>


Xavier
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Actors: send message to a class instead of an instance
Thanks for references,
Xan.

Al 17/05/11 01:07, En/na Paulo Siqueira ha escrit:
BANLkTinracbi9F4HGMQT3Lezqsmz-M1v_A [at] mail [dot] gmail [dot] com" type="cite">About case classes: http://www.scala-lang.org/node/107

[]s,

2011/5/16 Xavier <xancorreu [at] gmail [dot] com" rel="nofollow">xancorreu@gmail.com>
I like it very much, Lex.

On May 14, 9:53 pm, Lex <lex [dot] [dot] [dot] [at] gmail [dot] com" rel="nofollow">lex...@gmail.com> wrote:
> You could auto-register all your instances with a global object using
> a weak map. Then send your message to the object that will forward it
> to all the instances:
>
> trait Registered extends Actor {
>   Forwarder.register(this)
>
> }
>
> object Forwarder {
>   private val registered = new
> scala.collection.mutable.WeakHashMap[Registered, Any]()
>   def register(actor: Registered) {
>     registered.put(actor, Nil)
>   }
>
>   def forward(message: Any) {
>     for (actor <- registered.keys) {
>       actor ! message
>     }
>   }
>
> }
>
> class Fill extends Actor with Registered {
>   def act = {
>      react {
>          case i:String =>
>              println(i + ": rebut ")
>      }
>   }
>
> }
> On Sat, May 14, 2011 at 1:43 PM, Xavier <xancor [dot] [dot] [dot] [at] gmail [dot] com" rel="nofollow">xancor...@gmail.com> wrote:
> > Hi,
>
> > Is it possible to pass a message to a class: to whole set of instances
> > of a specific class. Here I have the code:
>
> > import scala.actors._
>
> > class Fill extends Actor {
> >  def act = {
> >      react {
> >          case i:String =>
> >              println(i + ": rebut ")
> >      }
> >  }
> > }
>
> > class Mare extends Actor {
> >  def act = {
> >      react {
> >          Fill ! "hola"
> >      }
> >  }
> > }
>
> > object Main {
> >  def main(arg:Array[String]) = {
> >          var m = new Mare
> >          var fill = new Fill
> >          var fill2 = new Fill
>
> >          m.start
> >          fill.start
> >          fill2.start
> >      }
> >  }
>
> > m could pass a message to **all** instances of class Fill? How?
>
> > Thanks in advance,
> > Xan.
>
>



--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland