Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: POE: post an event on tcp server from a session

by rcaputo (Chaplain)
on Jul 26, 2005 at 16:07 UTC ( #478266=note: print w/replies, xml ) Need Help??

in reply to POE: post an event on tcp server from a session

A brand new POE::Component::Server::TCP has a single session that listens for new connections. It spawns off a new session for each connection that arrives.

The server session uses the given Alias. In your code, that means "TOF" addresses the server itself. The individual connections do not have aliases. Even if they did, it would be difficult to address a particular connection without some arbitration: Which connection does "Moe" refer to?

The best pattern devised so far is to have each connection register itself with a data structure to keep track of them all. The chat server recipe in the POE Cookbook shows a relatively simple way to do this.

On a stylistic note, it is a bad idea to mix event handlers from multiple sessions in one file. It becomes difficult to tell which session a particular function belongs to.

What follows is an untested version of your original program using concepts lifted from the Chat Server recipe.

#!/usr/bin/perl -w use strict; use YAML; sub POE::Kernel::ASSERT_DEFAULT () { 1 } use POE; use POE::Component::Server::TCP; use POE::Filter::Reference; # This hash will keep track of every connected client. It is used by # the updater session and each connection, so it's scoped where # everything can see it. my %connected_clients; { POE::Component::Server::TCP->new( Alias => 'TOF', Port => 2222, ClientFilter => ['POE::Filter::Reference'], ClientInput => \&client_input, # Register handlers for client connection and disconnection. They # will manage the contents of %connected_clients. ClientConnected => \&add_new_client, ClientDisconnected => \&remove_old_client, # InlineStates are for each client, not for the server as a whole. InlineStates => { update_client => \&update_client, }, ); sub client_input { my $input = $_[ARG0]; print 'my input = ' . YAML::Dump($input); } # Each client session has its own "update" method, scoped to the # single client. sub update_client { my $update = $_[ARG0]; my $client = $_[HEAP]->{client}; if ($client) { print 'my update is ' . YAML::Dump($update); $client->put($update); } } sub add_new_client { $connected_clients{$_[SESSION]->ID} = { # Store information about the client here. # Remote user ID? Address? Port? Whatever. # The code to send updates to clients can examine this data to # decide whether a client needs an update. For now, it's empty, # and all clients receive all updates. }; } sub remove_old_client { delete $connected_clients{$_[SESSION]->ID}; } } { POE::Session->create( inline_states => { _start => \&_start, Send_updates => \&send_update, _stop => \&_stop, }, ); sub _start { my $kernel = $_[KERNEL]; print "session started\n"; $kernel->yield('Send_update'); } sub send_update { my $kernel = $_[KERNEL]; my $update = { Header => 'update', Msg => 'Hello Server', }; # At update time, send an update to every currently connected # client. As previously mentioned, this code can examine the # contents of $connected_clients{$client_id} and skip clients that # don't need or deserve updates. foreach my $client_id (keys %connected_clients) { $kernel->post($client_id, 'update_client', $update); } $kernel->delay('Send_update', 10); } sub _stop { print "session end\n"; } } POE::Kernel->run(); exit;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://478266]
[jdporter]: "When all your nails look like trolls, all your hammers look like trolls."
erix hammers a jdporter nail randomly
[erix]: average between 1 and 20

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-06-21 16:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.