Hi Josh,

If Ping.pm is a separate file, you need to "use POE" to import the
KERNEL, HEAP, etc. constants.  Try that and see if things improve.

Dan

Josh wrote:
> 
> I am having a strange problem w/package methods not seeming to work
> properly, at least as far as I understand them.  This is my first POE
> program, it is a simple program that pings some hosts using
> POE::Component::Client::Ping.  I am trying to put the event handlers related
> to pinging in their own package.  If anyone has any ideas or suggestions I
> would really appreciate it, bc I have read all the docs and online resource
> and this is erally starting to bake my noodle.  Example output and the code
> is below.
> 
> -josh
> 
> I first wrote the program using inline handlers w/all the coderefs in the
> same file.  running this works fine:
> 
> # perl daemon.pl
> START KERNEL
> 216.33.69.110 0.0165859460830688
> 216.33.68.131 0.0180959701538086
> 216.33.69.33 0.0192290544509888
> 216.33.68.132 0.0205649137496948
> 216.33.66.190 0.0219370126724243
> 216.33.66.150 0.0233149528503418
> 64.37.201.22 0.0246679782867432
> 216.33.69.110 : NO RESPONSE
> 216.33.68.131 : NO RESPONSE
> 216.33.69.33 : NO RESPONSE
> 216.33.68.132 : NO RESPONSE
> 216.33.66.190 : NO RESPONSE
> 216.33.66.150 : NO RESPONSE
> 64.37.201.22 : NO RESPONSE
> STOP sess 3
> STOP KERNEL
> 
> However, if I move the handlers out of this file to a package and change the
> Session's event mapping to use package methods, none of my handlers get
> invoked (w/assertions and debuggin this time):
> 
> START KERNEL
> <sg> POE::Kernel is polling for signals at 1056131081.00576 at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2711.
> <sg> POE::Kernel has no child processes at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2770.
> <sg> POE::Kernel will poll again after a delay at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2790.
> STOP sess 3
> <sg> dispatching ET_SIGNAL (IDLE) to session raef-3ef348070000a03d
> (POE::Kernel=ARRAY(0x8304598)) at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2407.
> <sg> propagating compatible signal (IDLE) to session 2 (pinger) at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2451.
> <sg> propagated to session 2 (pinger) at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 2463.
> <sg> stopping signaled session session 2 (pinger) at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 703.
> <sg> stopping signaled session session raef-3ef348070000a03d
> (POE::Kernel=ARRAY(0x8304598)) at
> /usr/local/lib/perl5/site_perl/5.8.0/POE/Kernel.pm line 703.
> STOP KERNEL
> 
> Here is the program and the package:
> 
> use warnings;
> use strict;
> 
> sub POE::Kernel::ASSERT_DEFAULT () { 1 }
> sub POE::Kernel::TRACE_SIGNALS ()  { 1 }
> 
> use POE qw( Component::RRDTool Component::Client::Ping);
> 
> use cwexodus::Device;
> use Ping;
> 
> sub refresh_index {
>                 my ($kern, $heap) = @_[KERNEL, HEAP];
>         my $d = new cwexodus::Device;
>         my $dev = $d->getDevices();
>         my $i = 0;
>         foreach my $devid (keys %$dev) {
>                 $d->setFocus($devid);
>                 my $a = $d->getAttrs();
>                 if (($a->{ipaddresses} =~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/))
> {
>                         $a->{id} = $devid;
>                         $heap->{deviceByIp}->{qq($a->{'ipaddresses'})} = $a;
>                         $heap->{deviceById}->{$devid} = $a;
>                         $heap->{deviceByName}->{qq($a->{'name'})} = $a;
>                         $i++;
>                 }
>         }
>         $heap->{deviceNum} = $i;
> }
> 
> sub schedule_start {
>                 my ($kern, $heap, $sess) = @_[KERNEL, HEAP, SESSION];
>         print "START sess " . $sess->ID . "\n";
>         $heap->{deviceById} = {};
>         $heap->{deviceByIp} = {};
>         $heap->{deviceByName} = {};
>         $heap->{deviceNum} = 0;
>         $kern->yield('refresh_index');
>         $kern->yield('ping_start');
> }
> 
> sub schedule_stop {
>                 my ($sess) = $_[SESSION];
>         print "STOP sess " . $sess->ID . "\n";
> }
> 
> sub _default {
>                 print "Default caught an unhandled $_[ARG0] event.\n";
>                 print "The $_[ARG0] event was given these parameters:
> @{$_[ARG1]}\n";
>                 return 0 ;
> }
> 
> # COMPONENTS
> 
> POE::Component::Client::Ping->spawn
>   ( Alias => 'pinger',    # The component's name will be "pinger".
>     Timeout => 5,    # The default ping timeout.
>   );
> 
> # SESSIONS
> 
> POE::Session->create(
>         inline_states => {
>                 _start => \&schedule_start,
>                 _stop => \&schedule_stop,
>                 refresh_index => \&refresh_index,
>                                 _default => \&_default,
>         },
>         package_states => [
>                 Ping => [qw(ping_start pong)],
>         ],
> );
> 
> print "START KERNEL\n";
> POE::Kernel->run();
> print "STOP KERNEL\n";
> 
> # HERE IS THE Ping.pm PACKAGE file
> 
> package Ping;
> 
> sub ping_start {
>                 my ($kernel, $heap) = @_[KERNEL, HEAP];
>         foreach my $ip (keys %{$heap->{deviceByIp}}) {
>                 $kernel->post(qw(pinger ping pong),$ip);
>         }
> }
> 
> sub pong {
>         my ($req, $res) = @_[ARG0, ARG1];
>         my ( $request_address, $request_timeout, $request_time ) = @{$req};
>         my ( $response_address, $roundtrip_time, $reply_time ) = @{$res};
>         if (defined $response_address) {
>                 print "$response_address $roundtrip_time\n";
>         } else {
>                 print "$request_address : NO RESPONSE\n";
>         }
> }
> 
> 1;
> 
> This message is for the designated recipient only and may contain
> privileged, proprietary, or otherwise private information.  If you are not
> the intended recipient or otherwise believe that you have received this
> message in error, please notify the sender immediately and delete the
> original.  Any other use of this message by you is prohibited.

Reply via email to