Component::Server::TCP: how to get access to the server's HEAP from a client handler?
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';
Re: Component::Server::TCP: how to get access to the server's HEAP from a client handler?
Le 26 mai 2009 16:46, Mark Morgan makk...@gmail.com 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_Sessionshttp://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';
Re: Component::Server::TCP: how to get access to the server's HEAP from a client handler?
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. 2009/5/26 Olivier Mengué olivier.men...@gmail.com: 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';