Hi, my baby steps in the land of POE:
I have tried to setup a small application that would listen on a TCP port and get commands 'start' and 'stop', When receiving start it should create a new POE::Session to count. When receiving stop it should stop the counting and destroy that POE::Session. It seems I managed to do it but I'd be glad to receive comments about the code: #!/usr/bin/perl use warnings; use strict; use 5.010; use POE qw(Component::Server::TCP); my $port = 12345; print "telnet to $port\n"; my $session_id; POE::Component::Server::TCP->new( Port => $port, ClientInput => \&client_input, ClientConnected => \&client_connected, ); POE::Kernel->run(); exit; sub counter { #print "counter\n"; return if not $_[HEAP]{run}; $_[HEAP]{count}++; #$_[HEAP]{client}->put("Count: $_[HEAP]"); print "Count: $_[HEAP]{count}\n"; $_[KERNEL]->delay('counter' => 1); } sub session_start { print "session_start\n"; print "session kernel: $_[KERNEL]\n"; $_[HEAP]{count} = 0; $_[HEAP]{run} = 1; $_[KERNEL]->yield('counter'); print "after calling yield\n"; } sub session_stop { print "session_stop\n"; } sub stop_session { $_[HEAP]{run} = 0; } sub client_input { my ($heap, $input) = @_[HEAP, ARG0]; if ($input eq 'start') { $session_id = POE::Session->create( inline_states => { _start => \&session_start, _stop => \&session_stop, counter => \&counter, stop_session => \&stop_session, } )->ID; print "kernel: $_[KERNEL]\n"; print "SID: $session_id\n"; } elsif ($input eq 'stop') { print "stop received\n"; if ($session_id) { $_[KERNEL]->post( $_[KERNEL]->ID_id_to_session($session_id), 'stop_session' ); $session_id = undef; } print "stop done\n"; } else { $_[HEAP]{client}->put("Invalid command '$input'"); } return; } sub client_connected { print "connected\n"; $_[HEAP]{client}->put("type 'start' or 'stop':"); }; -- Gabor Szabo http://szabgab.com/