On Sat, Nov 15, 2003 at 02:29:47AM +0100, Waldemar Rachwal wrote:
> Hi all,
> I want to get a tree structure of sessions created at one place.
> For example, at some place in code to create at once the session A, its
> children B1, B2 that have in turn their own children: C11, C12 and C21, C22,
> C23 correspondingly:
>     A => (B1 => (C11, C12), B2  => (C21, C22, C23))
> Is it possible in POE? Documentation says instantiating a new session
> POE::Session->create() causes it to be a child of a "current" session. This
> means all above sessions (A, B1, ..., C23) created "at one place" would be
> siblings, and childeren of the session in which they were instantiated.
> Thanks in advance for any help,
> Waldemar.

Within POE::Kernel, the parent/child distinction exists to assist with
job control and signal dispatching.  It's quite possible to create other
relationships between sessions by rolling your own registries.

For example:

  my %parent;
  my %children;

  sub handle_start {
    my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
    my ($my_alias, $parent_alias) = @_[ARG0..$#_];

    # Register my alias so there's a symbolic name for this session.
    $kernel->alias_set($my_alias);

    # Register the new parent/child relationships.
    $parent{$my_alias} = $parent_alias;
    push @{$children{$parent_alias}}, $my_alias;

    # Other initialization here.
  }

You would create these sessions with:

  POE::Session->create(
    inline_states => {
      _start => \&handle_start,
      ... etc. ...,
    },
    args => [ "A", "" ],
  );

  POE::Session->create(
    inline_states => { ... },
    args => [ "B1", "A" ],
  );

  POE::Session->create(
    inline_states => { ... },
    args => [ "C11", "B" ],
  );

... and so on.

Now you can walk %children from "" (a virtual root session) down to
leaves on the tree, or you can pick a leaf and work your way back up.


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

Reply via email to