On Wed, Feb 18, 2004 at 08:13:15PM +0000, James Brown wrote:
> Hi All,
> 
> A quick question here.
> 
> I have read the POE Cookbook example on broadcasting events (more 
> correctly, multicasting events) to various sessions and I'd like to 
> implement it (for keypress events) in my program.
> 
> I intend to have a hash in my 'master' session and pass this hash (by 
> reference) to all the various child sessions which would like to 
> 'subscribe' to receive keypress events. They can then add their 
> $_[SESSION]->ID()'s to the hash. Another child session, somewhere else 
> in the program, will then post keypress events to all the key's in the hash.
> 
> My question is this: Passing the hash around is quite a lot of work - 
> I'm sending it to a spawn() method and then from there to the child 
> session's heap. Is there an alternative method?
> 
> Is it possible for my 'master' session to find the SESSION ID's of it's 
> children? Is there any way for the children to gain access and modify 
> the master's heap?

The chat server recipe accomplishes this by keeping the hash in an
outer scope, where it's available to all the sessions that want to
exchange information.

A better implementation would be to create a simple object with just a
few methods.  Here's an off-the-cuff (untested!) prototype:

  package Channel;

  sub new {
    my $class = shift;
    return bless { }, $class;
  }

  sub enter {
    my ($self, $session_id) = @_;
    $self->{$session_id} = 1;
  }

  sub leave {
    my ($self, $session_id) = @_;
    delete $self->{$session_id};
  }

  sub send {
    my ($self, $sender_id, $event, @params) = @_;
    foreach (keys %$self) {
      next if $_ == $sender_id;  # not back to me
      $poe_kernel->post($_, $event, @params);
    }
  }

  1;

-- 
Rocco Caputo - [EMAIL PROTECTED] - http://poe.perl.org/

Reply via email to