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/


In reply to Re: Missing event from InlineStates in POE::Component::Server::TCP by rcaputo
in thread Missing event from InlineStates in POE::Component::Server::TCP by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.