POE::Component::Client::Ping(3pm) | User Contributed Perl Documentation | POE::Component::Client::Ping(3pm) |
POE::Component::Client::Ping - a non-blocking ICMP ping client
use POE qw(Component::Client::Ping); POE::Component::Client::Ping->spawn( Alias => "pingthing", # defaults to "pinger" Timeout => 10, # defaults to 1 second Retry => 3, # defaults to 1 attempt OneReply => 1, # defaults to disabled Parallelism => 64, # defaults to autodetect BufferSize => 65536, # defaults to undef AlwaysDecodeAddress => 1, # defaults to 0 ); sub some_event_handler { $kernel->post( "pingthing", # Post the request to the "pingthing" component. "ping", # Ask it to "ping" an address. "pong", # Have it post an answer as a "pong" event. $address, # This is the address we want to ping. $timeout, # Optional timeout. It overrides the default. $retry, # Optional retries. It overrides the default. ); } # This is the sub which is called when the session receives a "pong" # event. It handles responses from the Ping component. sub got_pong { my ($request, $response) = @_[ARG0, ARG1]; my ($req_address, $req_timeout, $req_time) = @$request; my ($resp_address, $roundtrip_time, $resp_time, $resp_ttl) = @$response; # The response address is defined if this is a response. if (defined $resp_address) { printf( "ping to %-15.15s at %10d. pong from %-15.15s in %6.3f s\n", $req_address, $req_time, $resp_address, $roundtrip_time, ); return; } # Otherwise the timeout period has ended. printf( "ping to %-15.15s is done.\n", $req_address, ); } or use POE::Component::Client::Ping ":const"; # Post an array ref as the callback to get data back to you $kernel->post("pinger", "ping", [ "pong", $user_data ]); # use the REQ_USER_ARGS constant to get to your data sub got_pong { my ($request, $response) = @_[ARG0, ARG1]; my $user_data = $request->[REQ_USER_ARGS]; ...; }
POE::Component::Client::Ping is non-blocking ICMP ping client. It lets several other sessions ping through it in parallel, and it lets them continue doing other things while they wait for responses.
Ping client components are not proper objects. Instead of being created, as most objects are, they are "spawned" as separate sessions. To avoid confusion (and hopefully not cause other confusion), they must be spawned with a "spawn" method, not created anew with a "new" one.
PoCo::Client::Ping's "spawn" method takes a few named parameters:
This is useful for people who would rather not perform a security audit on POE, since it allows them to create a raw socket in their own code and then run POE at reduced privileges.
This default timeout is only used for ping requests that don't include their own timeouts.
"OneReply" is disabled by default, and a single successful request will generate at least two responses. The first response is a successful ICMP ECHO REPLY event. The second is an undefined response event, signifying that the timeout period has ended.
A ping request will generate exactly one reply when "OneReply" is enabled. This reply will represent either the first ICMP ECHO REPLY to arrive or that the timeout period has ended.
The difference can be dramatic. A tuned Parallelism can enable responses down to 1ms, depending on the network, although it will take longer to get through the hosts list.
Pinging 762 hosts at Parallelism=64 Starting to ping hosts. Pinged 10.0.0.25 - Response from 10.0.0.25 in 0.002s Pinged 10.0.0.200 - Response from 10.0.0.200 in 0.003s Pinged 10.0.0.201 - Response from 10.0.0.201 in 0.001s real 1m1.923s user 0m2.584s sys 0m0.207s
Responses will take significantly longer with an untuned Parallelism, but the total run time will be quicker.
Pinging 762 hosts at Parallelism=500 Starting to ping hosts. Pinged 10.0.0.25 - Response from 10.0.0.25 in 3.375s Pinged 10.0.0.200 - Response from 10.0.0.200 in 1.258s Pinged 10.0.0.201 - Response from 10.0.0.201 in 2.040s real 0m13.410s user 0m6.390s sys 0m0.290s
Excessively high parallelism values may saturate the OS or network, resulting in few or no responses.
Pinging 762 hosts at Parallelism=1000 Starting to ping hosts. real 0m20.520s user 0m7.896s sys 0m0.297s
By default, POE::Component::Client::Ping will guess at an optimal Parallelism value based on the raw socket receive buffer size and the operating system's nominal ICMP packet size. The latter figure is 3000 octets for Linux and 100 octets for other systems. ICMP packets are generally under 90 bytes, but operating systems may use alternative numbers when calculating buffer capacities. The component tries to mimic calculations observed in the wild.
When in doubt, experiment with different Parallelism values and use the one that works best.
Increased BufferSize values can expand the practical limit for Parallelism.
Sessions communicate asynchronously with the Client::Ping component. They post ping requests to it, and they receive pong events back.
Requests are posted to the component's "ping" handler. They include the name of an event to post back, an address to ping, and an optional amount of time to wait for responses. The address may be a numeric dotted quad, a packed inet_aton address, or a host name. Host names are not recommended: they must be looked up for every ping request, and DNS lookups can be very slow. The optional timeout overrides the one set when "spawn" is called.
Ping responses come with two array references:
my ($request, $response) = @_[ARG0, ARG1];
$request contains information about the original request:
my ( $req_address, $req_timeout, $req_time, $req_user_args, ) = @$request;
It is useful along with $req_user_args for pairing requests with their corresponding responses.
To use it, replace the response event with an array reference in the original request. The array reference should contain two items: the actual response event and a scalar with the context data the program needs back. See the SYNOPSIS for an example.
$response contains information about the ICMP ping response. There may be multiple responses for a single request.
my ($response_address, $roundtrip_time, $reply_time, $reply_ttl) = @$response;
$response_address will be undefined if $request_timeout seconds have elapsed. This marks the end of responses for a given request. Programs can assume that no more responses will be sent for the request address. They may use this marker to initiate another ping request.
If the ":const" tagset is imported the following constants will be exported:
REQ_ADDRESS, REQ_TIMEOUT, REQ_TIME REQ_USER_ARGS, RES_ADDRESS, RES_ROUNDTRIP, RES_TIME, RES_TTL
This component's ICMP ping code was lifted from Net::Ping, which is an excellent module when you only need to ping one host at a time.
See POE, of course, which includes a lot of documentation about how POE works.
Also see the test program, t/01_ping.t, in the component's distribution.
https://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-Ping
http://github.com/rcaputo/poe-component-client-ping/
http://search.cpan.org/dist/POE-Component-Client-Ping/
POE::Component::Client::Ping is Copyright 1999-2020 by Rocco Caputo. All rights are reserved. POE::Component::Client::Ping is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
You can learn more about POE at http://poe.perl.org/
2021-02-15 | perl v5.32.1 |