| 1 | package Net::Prober::Probe::Base; | ||||||||||||||||||||||||||||
| 2 | 8 | $Net::Prober::Probe::Base::VERSION = '0.16'; | |||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Sys::Syslog (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | our $USE_SYSLOG = 0; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||
| 13 | 8 | ▶ | my ($class, $opt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | $opt ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt ||= {}
MC/DC0%0/1
$opt ||= {}
$opt
Truth table50%1/2
A
A= $opt
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $self = { | ||||||||||||||||||||||||||||
| 19 | 8 | %{ $opt }, | |||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 8 | bless $self, $class; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub name { | ||||||||||||||||||||||||||||
| 26 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | if ($class =~ m{^ .* :: (?<probename>.*?) $}x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 6 | return $+{probename}; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | Carp::croak("Couldn't determine the probe name from class $class"); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub probe_failed { | ||||||||||||||||||||||||||||
| 36 | 3 | ▶ | my ($self, %info) = @_; | ||||||||||||||||||||||||||
|
Subroutine
probe_failed: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | my $name = $self->name(); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | if (! exists $info{time}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | my $elapsed = exists $self->{_time} | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | ? $self->time_elapsed() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | : 0.0; | ||||||||||||||||||||||||||||
| 44 | 2 | $info{time} = $elapsed; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | if ($USE_SYSLOG) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | my $msg = sprintf "Probe %s failed, reason %s, elapsed: %3.2f s", | ||||||||||||||||||||||||||||
| 49 | $name, | ||||||||||||||||||||||||||||
| 50 | $info{reason} || 'unknown', | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $info{time}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$info{'reason'} || 'unknown'
MC/DC0%0/1
$info{'reason'} || 'unknown'
$info{'reason'}
Truth table0%0/2
A
A= $info{'reason'}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | Sys::Syslog::syslog('warning', $msg); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | return { ok => 0, %info }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub probe_ok { | ||||||||||||||||||||||||||||
| 60 | 3 | ▶ | my ($self, %info) = @_; | ||||||||||||||||||||||||||
|
Subroutine
probe_ok: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | my $name = $self->name(); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | if (! exists $info{time}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | my $elapsed = exists $self->{_time} | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | ? $self->time_elapsed() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | : 0.0; | ||||||||||||||||||||||||||||
| 68 | 3 | $info{time} = $elapsed; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($USE_SYSLOG) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | my $msg = sprintf "Probe %s ok, elapsed: %3.2f s", | ||||||||||||||||||||||||||||
| 73 | 0 | $name, $info{time}; | |||||||||||||||||||||||||||
| 74 | 0 | Sys::Syslog::syslog('info', $msg); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | return { ok => 1, %info }; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub time_now { | ||||||||||||||||||||||||||||
| 81 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
time_now: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | return $self->{_time} = [ Time::HiRes::gettimeofday() ]; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub time_elapsed { | ||||||||||||||||||||||||||||
| 87 | 9 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
time_elapsed: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 9 | ▶ | if (! exists $self->{_time} || ! defined $self->{_time}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"_time"} and defined $self->{"_time"}
MC/DC0%0/2
exists $self->{"_time"} and defined $self->{"_time"}
exists $self->{"_time"}
defined $self->{"_time"}
Truth table33%1/3
A and B
A= exists $self->{"_time"}B= defined $self->{"_time"}
| |||||||||||||||||||||||||||||
| 90 | 0 | Carp::croak('time_elapsed() called without a starting time_now()'); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 9 | my $last_mark = $self->{_time}; | |||||||||||||||||||||||||||
| 94 | 9 | my $elapsed = Time::HiRes::tv_interval($last_mark); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 9 | return $elapsed; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub process_defaults { | ||||||||||||||||||||||||||||
| 100 | 14 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_defaults: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 14 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args ||= {}
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 103 | 14 | my %args_with_defaults = %{ $args }; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Process and inject defaults if an arg is not supplied | ||||||||||||||||||||||||||||
| 106 | 14 | my $defaults = $self->defaults; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 14 | ▶ | if ($defaults && ref $defaults eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$defaults and ref $defaults eq "HASH"
MC/DC0%0/2
$defaults and ref $defaults eq "HASH"
$defaults
ref $defaults eq "HASH"
Truth table33%1/3
A and B
A= $defaultsB= ref $defaults eq "HASH"
| |||||||||||||||||||||||||||||
| 109 | 14 | for (keys %{ $defaults }) { | |||||||||||||||||||||||||||
| 110 | $args_with_defaults{$_} = $defaults->{$_} | ||||||||||||||||||||||||||||
| 111 | 98 | ▶ | if ! exists $args_with_defaults{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 14 | return \%args_with_defaults; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub parse_args { | ||||||||||||||||||||||||||||
| 119 | 14 | ▶ | my ($self, $args, @wanted) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_args: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 14 | my $args_with_def = $self->process_defaults($args); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 14 | ▶ | if (exists $args_with_def->{port} && defined $args_with_def->{port}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args_with_def->{"port"} and defined $args_with_def->{"port"}
MC/DC50%1/2
exists $args_with_def->{"port"} and defined $args_with_def->{"port"}
exists $args_with_def->{"port"}
defined $args_with_def->{"port"}
Truth table66%2/3
A and B
A= exists $args_with_def->{"port"}B= defined $args_with_def->{"port"}
| |||||||||||||||||||||||||||||
| 124 | $args_with_def->{port} = Net::Prober::port_name_to_num( | ||||||||||||||||||||||||||||
| 125 | $args_with_def->{port} | ||||||||||||||||||||||||||||
| 126 | 12 | ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 14 | ▶ | if (! @wanted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | return $args_with_def; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 14 | my @arg_values; | |||||||||||||||||||||||||||
| 134 | 14 | push @arg_values, $args_with_def->{$_} for @wanted; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 14 | return @arg_values; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub defaults { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | Carp::croak("defaults() is an abstract method. You must implement it in your class."); | ||||||||||||||||||||||||||
|
Subroutine
defaults: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub probe { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | Carp::croak("probe() is an abstract method. You must implement it in your class."); | ||||||||||||||||||||||||||
|
Subroutine
probe: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 8 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||