#!/usr/bin/perl use POE qw( Wheel::SocketFactory Wheel::ReadWrite Filter::Line Driver::SysRW ); new POE::Session ( _start => \&collector, agent_handler=>\&agent_handler, accept_failed=>\&accept_failed, agent_input=>\&agent_input, agent_error=>\&agent_error ); $poe_kernel->run(); exit; # Session Sub Defs sub collector { $_[HEAP]->{listener} = new POE::Wheel::SocketFactory ( BindPort => PORT, Reuse => 'yes', SuccessEvent => 'agent_handler', FailureEvent => 'accept_failed' ); print "SERVER: Started Collector on port ",PORT,"\n"; } # Non-Session Sub Defs sub accept_failed { my ($function, $error) = @_[ARG0, ARG2]; delete $_[HEAP]->{listener}; print "SERVER: call to $function() failed: $error.\n"; } sub agent_handler { print "Agent handler called\n"; my ($heap, $socket) = @_[HEAP, ARG0]; $heap->{readwrite} = new POE::Wheel::ReadWrite ( Handle => $socket, Driver => new POE::Driver::SysRW (), Filter => new POE::Filter::Line (), InputEvent => agent_input, ErrorEvent => agent_error, ); $heap->{peerhost} = inet_ntoa($_[ARG1]); print "CHILD: Connected to $heap->{peerhost}.\n"; } sub agent_input { $heap=$_[HEAP]; print "agent_input called\n"; $input_sysid=$_[ARG0]; print "input system id is $input_sysid from $heap->{peerhost}\n"; print "Sending ack back\n"; $heap->{readwrite}->put( "Hello, client!" ); $agent_alarm = $poe_kernel->delay_set( agent_error, 5 ); } sub agent_error { print "Got an agent error, exiting agent session!\n"; my ($function, $error) = @_[ARG0, ARG2]; print "SERVER: call to $function() failed: $error.\n"; # I sure would like to close the socket here... }