… | |
… | |
27 | use common::sense; |
27 | use common::sense; |
28 | |
28 | |
29 | use AnyEvent; |
29 | use AnyEvent; |
30 | |
30 | |
31 | BEGIN { |
31 | BEGIN { |
32 | our $VERSION = '2.0'; |
32 | our $VERSION = 2.1; |
33 | our @ISA = qw(Exporter); |
33 | our @ISA = qw(Exporter); |
34 | |
34 | |
35 | require Exporter; |
35 | require Exporter; |
36 | #Exporter::export_ok_tags (keys %EXPORT_TAGS); |
36 | #Exporter::export_ok_tags (keys %EXPORT_TAGS); |
37 | |
37 | |
… | |
… | |
39 | XSLoader::load (__PACKAGE__, $VERSION); |
39 | XSLoader::load (__PACKAGE__, $VERSION); |
40 | } |
40 | } |
41 | |
41 | |
42 | our ($THR_RES_FD, $ICMP4_FD, $ICMP6_FD); |
42 | our ($THR_RES_FD, $ICMP4_FD, $ICMP6_FD); |
43 | |
43 | |
44 | our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; |
44 | our $THR_RES_FH; |
45 | |
45 | |
46 | our $ICMP4_FH; our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; |
46 | our $ICMP4_FH; |
47 | our $ICMP6_FH; our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; |
47 | our $ICMP6_FH; |
|
|
48 | |
|
|
49 | our @IDLE_CB; |
48 | |
50 | |
49 | =item AnyEvent::FastPing::ipv4_supported |
51 | =item AnyEvent::FastPing::ipv4_supported |
50 | |
52 | |
51 | Returns true iff IPv4 is supported in this module and on this system. |
53 | Returns true iff IPv4 is supported in this module and on this system. |
52 | |
54 | |
… | |
… | |
151 | - you should be stingy with your pinger objects. |
153 | - you should be stingy with your pinger objects. |
152 | |
154 | |
153 | =cut |
155 | =cut |
154 | |
156 | |
155 | sub new { |
157 | sub new { |
156 | my ($klass) = @_; |
158 | _boot; |
157 | |
159 | |
|
|
160 | our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; |
|
|
161 | our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; |
|
|
162 | |
|
|
163 | open $THR_RES_FH, "<&=$THR_RES_FD" or die "AnyEvent::FastPing: FATAL: cannot fdopen thread result fd"; |
|
|
164 | |
|
|
165 | our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { |
|
|
166 | sysread $THR_RES_FH, my $buf, 8; |
|
|
167 | |
|
|
168 | for my $id (unpack "S*", $buf) { |
|
|
169 | _stop_id $id; |
|
|
170 | ($IDLE_CB[$id] || sub { })->(); |
|
|
171 | } |
|
|
172 | }; |
|
|
173 | |
|
|
174 | *new = sub { |
158 | _new $klass, (rand 65536), (rand 65536), (rand 65536) |
175 | _new shift, (rand 65536), (rand 65536), (rand 65536) |
|
|
176 | }; |
|
|
177 | |
|
|
178 | goto &new; |
159 | } |
179 | } |
160 | |
|
|
161 | our @IDLE_CB; |
|
|
162 | |
180 | |
163 | sub DESTROY { |
181 | sub DESTROY { |
164 | undef $IDLE_CB[ &id ]; |
182 | undef $IDLE_CB[ &id ]; |
165 | &_free; |
183 | &_free; |
166 | } |
184 | } |
… | |
… | |
226 | |
244 | |
227 | sub on_idle { |
245 | sub on_idle { |
228 | $IDLE_CB[ &id ] = $_[1]; |
246 | $IDLE_CB[ &id ] = $_[1]; |
229 | } |
247 | } |
230 | |
248 | |
231 | our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { |
|
|
232 | sysread $THR_RES_FH, my $buf, 8; |
|
|
233 | |
|
|
234 | for my $id (unpack "S*", $buf) { |
|
|
235 | _stop_id $id; |
|
|
236 | ($IDLE_CB[$id] || sub { })->(); |
|
|
237 | } |
|
|
238 | }; |
|
|
239 | |
|
|
240 | =item $pinger->interval ($seconds) |
249 | =item $pinger->interval ($seconds) |
241 | |
250 | |
242 | Configures the minimum interval between packet sends for this pinger - the |
251 | Configures the minimum interval between packet sends for this pinger - the |
243 | pinger will not send packets faster than this rate (or actually 1 / rate), |
252 | pinger will not send packets faster than this rate (or actually 1 / rate), |
244 | even if individual ranges have a lower interval. |
253 | even if individual ranges have a lower interval. |
… | |
… | |
284 | |
293 | |
285 | =item $pinger->add_hosts ([$host...], $interval, $interleave) |
294 | =item $pinger->add_hosts ([$host...], $interval, $interleave) |
286 | |
295 | |
287 | Similar to C<add_range>, but uses a list of single addresses instead. The |
296 | Similar to C<add_range>, but uses a list of single addresses instead. The |
288 | list is specified as an array reference as first argument. Each entry in |
297 | list is specified as an array reference as first argument. Each entry in |
289 | the array should be a binary host address, either IPv4 or IPv6. Currently, |
298 | the array should be a binary host address, either IPv4 or IPv6. If all |
290 | all entries in the list must be either IPv4 B<OR> IPv6, so you have to |
299 | addresses are IPv4 addresses, then a compact IPv4-only format will be used |
291 | create two host ranges if you have mixed addresses. |
300 | to store the list internally. |
292 | |
301 | |
293 | Minimum C<$interval> is the same as for C<add_range> and can be left out. |
302 | Minimum C<$interval> is the same as for C<add_range> and can be left out. |
294 | |
303 | |
295 | C<$interlave> specifies an increment between addresses: often address |
304 | C<$interlave> specifies an increment between addresses: often address |
296 | lists are generated in a way that results in clustering - first all |
305 | lists are generated in a way that results in clustering - first all |