in reply to Missing event from InlineStates in POE::Component::Server::TCP

Your TCPServer alias is for the main server session. This is the session that listens on the server socket, accepts connections, and spawns off new sessions for each client/server connection.

The InlineStates parameter defines new events and handlers for each client/server session. These sessions are different than the one with the TCPServer alias. When you later post() to TCPServer, you're sending the event to a session that doesn't handle it.

If you want the AddPublisher event to go to every client/server connection, you'll need to register the connections in a hash. This untested code might help:

my %clients; POE::Component::Server::TCP->new( Alias => 'TCPServer', Port => 53511, ClientConnected => sub { $clients{$_[SESSION]->ID} = 1 }, ClientDisconnected => sub { delete $clients{$_[SESSION]->ID} }, ClientInput => sub { }, InlineStates => { AddPublisher => sub { }, }, ); POE::Session->create( inline_states => { _start => sub { foreach my $client (keys %clients) { $_[KERNEL]->post( $client, AddPublisher => $_[SESSION], 1 ); } }, _stop => sub { }, } ); POE::Kernel->run(); exit;

On the other hand, you may really want to enter each publisher into a central registry, and have the client/server connections interact with them that way. Not only is this code untested, but it also borrows a lot of its bulk from the previous untested code. Consider it doubly potentially buggy.

my %plugins; POE::Component::Server::TCP->new( Alias => 'TCPServer', Port => 53511, ClientInput => sub { my $input = $_[ARG0]; foreach my $plugin (keys %plugins) { send_input_to_plugin($input, $plugin); } }, ); POE::Session->create( inline_states => { _start => sub { $plugins{$_[SESSION]->ID} = 1 }, _stop => sub { delete $plugins{$_[SESSION]->ID} }, } ); POE::Kernel->run(); exit;

I like the second way better than the first. Clients can come and go, but the plugins remain the same. Still, you may have a really good reason for doing it the first way. I don't know. It's hard to say which (if either) is best without knowing more about the code's purpose.

-- Rocco Caputo - http://poe.perl.org/

Replies are listed 'Best First'.
Re^2: Missing event from InlineStates in POE::Component::Server::TCP
by diotalevi (Canon) on Jul 06, 2004 at 16:53 UTC

    I did not understand that POE::Component::Server::TCP->create( Alias => ... ) refers to to the listening server session and that InlineStates is declaring events for its children. I get it now. So instead of one TCPServer session I really have one parent TCPServer session and lots of children.

    How then do I register events for the hub server? I thought I should store stuff for it in its heap instead of just some global.

      Heaps are just session-scoped hash references. Consider them like $self, but they're kept aside for your event handlers to use.

      You certainly can use them for inter-session storage, but it won't be as practical as setting aside a plain hash. Consider that your accessors would need to be event-based, which makes them insanely slow compared to $hash{foo} = 1 or just incredibly slow compared to $object->set_foo(1).

      -- Rocco Caputo - http://poe.perl.org/