Re: 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]>
Le 26 mai 2009 16:46, Mark Morgan <[email protected]> a écrit :

> From the P:C:S:TCP docs, it looks like you should be able to do this
> by passing a reference to it via ClientArgs constructor argument.
> You may need to end up weakening it in client, to allow the server
> sessions and anything else the client heap is referring to to be
> reaped.
>
> Mark.
>

I have found a solution using ClientArgs.
However it is currently broken, at least it doesn't work as the
documentation says.

See my code below. I've shown with JSON what I get in ClientConnected args :
- socket is missing
- ARG0 is the given ClientArgs. It would me more useful to get Args
flattened in ARG0..$#_
The documentation for this code has still many TODOs and no one is
complaining on RT so probably not many use it.
http://search.cpan.org/~rcaputo/POE-1.005/lib/POE/Component/Server/TCP.pm#Default_Child_Connection_Sessions<http://search.cpan.org/%7Ercaputo/POE-1.005/lib/POE/Component/Server/TCP.pm#Default_Child_Connection_Sessions>

Olivier
http://search.cpan.org/~dolmen/ <http://search.cpan.org/%7Edolmen/>

#!/usr/bin/perl

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

use JSON 'to_json';

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,
  ClientArgs    => [ {} ],
  Started       => sub {
    pass "[Server] Started #".$_[SESSION]->ID;
    my $server_data = $_[HEAP]->{server_data} = $_[ARG0]; # The hash given
in Args
    $server_data->{test} = 42;
  },
  ClientConnected => sub {
    # According to the documentation ARG0 is the socket, ARG1 is ClientArgs
    # But the doc doesn't match the code :(
    # We get ClientArgs in ARG0, but it would be more consistent to get it
flattened
    diag(to_json([ @_[ARG0..$#_] ]));
    my $server_data = $_[HEAP]->{server_data} = $_[ARG0]->[0]; # The hash
given in Args
    pass "[Server] ClientConnected #".$_[SESSION]->ID;
    is($server_data->{test}, 42, "Server HEAP access");
  },
  ClientInput => sub {
    pass "[Server] ClientInput #".$_[SESSION]->ID;
    diag($_[ARG0]);
    my $server_data = $_[HEAP]->{server_data};
    is($server_data->{test}, 42, "Server HEAP access");
  },
  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.