Component::Server::TCP: how to get access to the server's HEAP from a client handler?

Olivier Mengué <[email protected]>
Newsgroups gmane.comp.lang.perl.poe
Message-ID <[email protected]>
Hi,

I'm using POE for 3 months now with great pleasure. Thanks to all the
developers!

I'm now playing with TCP client and server.
I've created a POE::Component::Server::TCP which has global state stored in
its HEAP, initialized in the Started event.

How can I get access to that HEAP from the Client* events (ClientConnected
in particular) as those are called from a different session ?
Maybe there is a parent-child relationship between the sessions I could use
to retrieve the server session and call it's ->get_heap() ?.

Below is some sample code that I'm sure you'll help me to complete.

Olivier
dolmen on cpan.org


#!/usr/bin/perl

use strict;
use warnings;
use Test::More tests => 8;

use POE qw(Component::Server::TCP Component::Client::TCP);


my $host = 'localhost';
my $port = 12344;

POE::Component::Server::TCP->new(
  Alias         => "server",
  Address       => $host,
  Port          => $port,
  Started       => sub {
    pass "[Server] Started #".$_[SESSION]->ID;
    $_[HEAP]{test} = 42;
  },
  ClientConnected => sub {
    pass "[Server] ClientConnected #".$_[SESSION]->ID;
    # How to get access to {test} from the server session's HEAP ?
    #is(???, 42, "Server HEAP access");
  },
  ClientInput => sub {
    pass "[Server] ClientInput #".$_[SESSION]->ID;
    diag($_[ARG0]);
  },
  ClientDisconnected => sub {
    pass "[Server] ClientDisconnected #".$_[SESSION]->ID;
    $poe_kernel->post('server', 'shutdown');
  },
);

POE::Component::Client::TCP->new(
  Alias         => "client",
  RemoteAddress => $host,
  RemotePort    => $port,
  Started       => sub {
    pass "[Client] Started";
  },
  Connected => sub {
    pass "[Client] Connected";
    $_[HEAP]{server}->put("Hello");
    $poe_kernel->yield('shutdown');
  },
  ServerInput => sub { },
  Disconnected => sub {
    pass "[Client] Disconnected";
  },
);

$poe_kernel->run;

pass 'Stopped';
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.