| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PerlWatcher::Watcher::Ping; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $App::PerlWatcher::Watcher::Ping::VERSION = '0.20'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Watches for host availablity via pingig it (ICMP) or knoking to it's port (TCP) | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 5427 | use 5.12.0; | 
|  | 4 |  |  |  |  | 16 |  | 
|  | 4 |  |  |  |  | 213 |  | 
| 8 | 4 |  |  | 4 |  | 24 | use strict; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 267 |  | 
| 9 | 4 |  |  | 4 |  | 22 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 152 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 4 |  |  | 4 |  | 4782 | use AnyEvent::Socket; | 
|  | 4 |  |  |  |  | 110196 |  | 
|  | 4 |  |  |  |  | 688 |  | 
| 12 | 4 |  |  | 4 |  | 53 | use AnyEvent::Util; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 280 |  | 
| 13 | 4 |  |  | 4 |  | 720 | use App::PerlWatcher::Watcher; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 117 |  | 
| 14 | 4 |  |  | 4 |  | 29 | use Carp; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 390 |  | 
| 15 | 4 |  |  | 4 |  | 21 | use Smart::Comments -ENV; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 80 |  | 
| 16 | 4 |  |  | 4 |  | 3052 | use Moo; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 59 |  | 
| 17 | 4 |  |  | 4 |  | 6132 | use Net::Ping::External qw(ping); | 
|  | 4 |  |  |  |  | 5913 |  | 
|  | 4 |  |  |  |  | 2629 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | has 'host'              => ( is => 'ro', required => 1 ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has 'port'              => ( is => 'ro', required => 0 ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has 'frequency'         => ( is => 'ro', default => sub{ 60; } ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | has 'timeout'           => ( is => 'lazy'); | 
| 31 |  |  |  |  |  |  | has 'watcher_callback'  => ( is => 'lazy'); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | with qw/App::PerlWatcher::Watcher/; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub _build_timeout { | 
| 36 | 0 |  | 0 | 0 |  | 0 | $_[0]->config->{timeout} // $_[0]->engine_config->{defaults}->{timeout} // 5; | 
|  |  |  | 0 |  |  |  |  | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub _build_watcher_callback { | 
| 40 | 4 |  |  | 4 |  | 1747 | my $self = shift; | 
| 41 | 4 | 100 |  |  |  | 40 | $self -> {_watcher} = $self->port | 
| 42 |  |  |  |  |  |  | ? $self->_tcp_watcher_callback | 
| 43 |  |  |  |  |  |  | : $self->_icmp_watcher_callback; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub _icmp_watcher_callback { | 
| 47 | 2 |  |  | 2 |  | 14 | my $self = shift; | 
| 48 | 2 |  |  |  |  | 30 | my $host = $self->host; | 
| 49 | 2 |  |  |  |  | 42 | my $timeout = $self->timeout; | 
| 50 |  |  |  |  |  |  | return sub { | 
| 51 | 2 |  |  | 2 |  | 21 | $self->poll_callback->($self); | 
| 52 |  |  |  |  |  |  | fork_call { | 
| 53 | 0 |  |  |  |  | 0 | my $alive = ping(host => $host, timeout => $timeout); | 
| 54 | 0 |  |  |  |  | 0 | $alive; | 
| 55 |  |  |  |  |  |  | } sub { | 
| 56 | 2 |  |  |  |  | 94159 | my $success = shift; | 
| 57 | 2 |  |  |  |  | 57 | $self->interpret_result( $success, $self->callback); | 
| 58 | 2 |  |  |  |  | 38 | }; | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 2 |  |  |  |  | 2269 | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub _tcp_watcher_callback { | 
| 63 | 2 |  |  | 2 |  | 4 | my $self = shift; | 
| 64 | 2 |  |  |  |  | 16 | my ($host, $port ) = ( $self->host, $self->port ); | 
| 65 |  |  |  |  |  |  | return sub { | 
| 66 | 6 |  |  | 6 |  | 97 | $self->poll_callback->($self); | 
| 67 |  |  |  |  |  |  | tcp_connect $host, $port, sub { | 
| 68 | 4 |  |  |  |  | 1757 | my $success = @_ != 0; | 
| 69 |  |  |  |  |  |  | # $! contains error | 
| 70 |  |  |  |  |  |  | # $host | 
| 71 |  |  |  |  |  |  | # $success | 
| 72 | 4 |  |  |  |  | 43 | $self->interpret_result( $success, $self->callback); | 
| 73 |  |  |  |  |  |  | }, sub { | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | #connect timeout | 
| 76 |  |  |  |  |  |  | #my ($fh) = @_; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 6 |  |  |  |  | 2847 | 1; | 
| 79 | 6 |  |  |  |  | 2062091 | }; | 
| 80 | 2 |  |  |  |  | 22 | }; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub build_watcher_guard { | 
| 84 | 5 |  |  | 5 | 0 | 9 | my $self = shift; | 
| 85 |  |  |  |  |  |  | return AnyEvent->timer( | 
| 86 |  |  |  |  |  |  | after    => 0, | 
| 87 |  |  |  |  |  |  | interval => $self->frequency, | 
| 88 |  |  |  |  |  |  | cb       => sub { | 
| 89 | 8 | 50 |  | 8 |  | 3904857 | $self->watcher_callback->() | 
| 90 |  |  |  |  |  |  | if $self->active; | 
| 91 |  |  |  |  |  |  | } | 
| 92 | 5 |  |  |  |  | 208 | ); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub description { | 
| 96 | 10 |  |  | 10 | 0 | 116 | my $self = shift; | 
| 97 | 10 | 100 |  |  |  | 176 | my $description = $self->port | 
| 98 |  |  |  |  |  |  | ? "Knocking at " . $self->host . ":" . $self->port | 
| 99 |  |  |  |  |  |  | : "Ping " . $self->host; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |