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/ |