| 1 | # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # vim: ts=4 sts=4 sw=4: | ||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | CPAN::Mirrors - Get CPAN mirror information and select a fast one | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | use CPAN::Mirrors; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | my $mirrors = CPAN::Mirrors->new( $mirrored_by_file ); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | my $seen = {}; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | my $best_continent = $mirrors->find_best_continents( { seen => $seen } ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | my @mirrors = $mirrors->get_mirrors_by_continents( $best_continent ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | my $callback = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | my( $m ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | printf "%s = %s\n", $m->hostname, $m->rtt | ||||||||||||||||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | $mirrors->get_mirrors_timings( \@mirrors, $seen, $callback, %args ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | @mirrors = sort { $a->rtt <=> $b->rtt } @mirrors; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | print "Best mirrors are ", map( { $_->rtt } @mirrors[0..3] ), "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | package CPAN::Mirrors; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | use vars qw($VERSION $urllist $silent); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | $VERSION = "2.27"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | use FileHandle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | use Fcntl ":flock"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | ▶ | use Net::Ping (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | ▶ | use CPAN::Version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =item new( LOCAL_FILE_NAME ) | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | Create a new CPAN::Mirrors object from LOCAL_FILE_NAME. This file | ||||||||||||||||||||||||||||||||||||||||||
| 48 | should look like that in http://www.cpan.org/MIRRORED.BY . | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my ($class, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | croak "CPAN::Mirrors->new requires a filename" unless defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | croak "The file [$file] was not found" unless -e $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 58 | mirrors => [], | ||||||||||||||||||||||||||||||||||||||||||
| 59 | geography => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | $self->parse_mirrored_by( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub parse_mirrored_by { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_mirrored_by: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | my $handle = FileHandle->new; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $handle->open($file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | or croak "Couldn't open $file: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | flock $handle, LOCK_SH; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | $self->_parse($file,$handle); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | flock $handle, LOCK_UN; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | $handle->close; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =item continents() | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Return a list of continents based on those defined in F<MIRRORED.BY>. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub continents { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
continents: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | return sort keys %{$self->{geography} || {}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'geography'} || {}
MC/DC0%0/1
$self->{'geography'} || {}
$self->{'geography'}
Truth table50%1/2
A
A= $self->{'geography'}
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =item countries( [CONTINENTS] ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | Return a list of countries based on those defined in F<MIRRORED.BY>. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | It only returns countries for the continents you specify (as defined | ||||||||||||||||||||||||||||||||||||||||||
| 93 | in C<continents>). If you don't specify any continents, it returns all | ||||||||||||||||||||||||||||||||||||||||||
| 94 | of the countries listed in F<MIRRORED.BY>. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub countries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
countries: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my ($self, @continents) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | @continents = $self->continents unless @continents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my @countries; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | for my $c (@continents) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | push @countries, sort keys %{ $self->{geography}{$c} || {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'geography'}{$c} || {}
MC/DC0%0/1
$self->{'geography'}{$c} || {}
$self->{'geography'}{$c}
Truth table50%1/2
A
A= $self->{'geography'}{$c}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | return @countries; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =item mirrors( [COUNTRIES] ) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Return a list of mirrors based on those defined in F<MIRRORED.BY>. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | It only returns mirrors for the countries you specify (as defined | ||||||||||||||||||||||||||||||||||||||||||
| 112 | in C<countries>). If you don't specify any countries, it returns all | ||||||||||||||||||||||||||||||||||||||||||
| 113 | of the mirrors listed in F<MIRRORED.BY>. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub mirrors { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mirrors: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my ($self, @countries) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return @{$self->{mirrors}} unless @countries; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my %wanted = map { $_ => 1 } @countries; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @found; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | for my $m (@{$self->{mirrors}}) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | push @found, $m if exists $wanted{$m->country}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return @found; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =item get_mirrors_by_countries( [COUNTRIES] ) | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | A more sensible synonym for mirrors. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub get_mirrors_by_countries { &mirrors } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_mirrors_by_countries: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =item get_mirrors_by_continents( [CONTINENTS] ) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | Return a list of mirrors for all of continents you specify. If you don't | ||||||||||||||||||||||||||||||||||||||||||
| 139 | specify any continents, it returns all of the mirrors. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | You can specify a single continent or an array reference of continents. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub get_mirrors_by_continents { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_mirrors_by_continents: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my ($self, $continents ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $continents = [ $continents ] unless ref $continents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $self->mirrors( $self->get_countries_by_continents( @$continents ) ); | |||||||||||||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =item get_countries_by_continents( [CONTINENTS] ) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | A more sensible synonym for countries. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub get_countries_by_continents { &countries } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_countries_by_continents: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =item default_mirror | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | Returns the default mirror, http://www.cpan.org/ . This mirror uses | ||||||||||||||||||||||||||||||||||||||||||
| 165 | dynamic DNS to give a close mirror. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub default_mirror { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_mirror: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | CPAN::Mirrored::By->new({ http => 'http://www.cpan.org/'}); | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =item best_mirrors | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | C<best_mirrors> checks for the best mirrors based on the list of | ||||||||||||||||||||||||||||||||||||||||||
| 176 | continents you pass, or, without that, all continents, as defined | ||||||||||||||||||||||||||||||||||||||||||
| 177 | by C<CPAN::Mirrored::By>. It pings each mirror, up to the value of | ||||||||||||||||||||||||||||||||||||||||||
| 178 | C<how_many>. In list context, it returns up to C<how_many> mirrors. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | In scalar context, it returns the single best mirror. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | how_many - the number of mirrors to return. Default: 1 | ||||||||||||||||||||||||||||||||||||||||||
| 184 | callback - a callback for find_best_continents | ||||||||||||||||||||||||||||||||||||||||||
| 185 | verbose - true or false on all the whining and moaning. Default: false | ||||||||||||||||||||||||||||||||||||||||||
| 186 | continents - an array ref of the continents to check | ||||||||||||||||||||||||||||||||||||||||||
| 187 | external_ping - if true, use external ping via Net::Ping::External. Default: false | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | If you don't specify the continents, C<best_mirrors> calls | ||||||||||||||||||||||||||||||||||||||||||
| 190 | C<find_best_continents> to get the list of continents to check. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | If you don't have L<Net::Ping> v2.13 or later, needed for timings, | ||||||||||||||||||||||||||||||||||||||||||
| 193 | this returns the default mirror. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | C<external_ping> should be set and then C<Net::Ping::External> needs | ||||||||||||||||||||||||||||||||||||||||||
| 196 | to be installed, if the local network has a transparent proxy. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub best_mirrors { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
best_mirrors: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | my $how_many = $args{how_many} || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'how_many'} || 1
MC/DC0%0/1
$args{'how_many'} || 1
$args{'how_many'}
Truth table0%0/2
A
A= $args{'how_many'}
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $callback = $args{callback}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | my $verbose = defined $args{verbose} ? $args{verbose} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | my $continents = $args{continents} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'continents'} || []
MC/DC0%0/1
$args{'continents'} || []
$args{'continents'}
Truth table0%0/2
A
A= $args{'continents'}
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $continents = [$continents] unless ref $continents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $args{external_ping} = 0 unless defined $args{external_ping}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $external_ping = $args{external_ping}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # Old Net::Ping did not do timings at all | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $min_version = '2.13'; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | unless( CPAN::Version->vgt(Net::Ping->VERSION, $min_version) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | carp sprintf "Net::Ping version is %s (< %s). Returning %s", | |||||||||||||||||||||||||||||||||||||||||
| 214 | Net::Ping->VERSION, $min_version, $self->default_mirror; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | return $self->default_mirror; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $seen = {}; | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if ( ! @$continents ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | print "Searching for the best continent ...\n" if $verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my @best_continents = $self->find_best_continents( | |||||||||||||||||||||||||||||||||||||||||
| 223 | seen => $seen, | ||||||||||||||||||||||||||||||||||||||||||
| 224 | verbose => $verbose, | ||||||||||||||||||||||||||||||||||||||||||
| 225 | callback => $callback, | ||||||||||||||||||||||||||||||||||||||||||
| 226 | external_ping => $external_ping, | ||||||||||||||||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | # Only add enough continents to find enough mirrors | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | for my $continent ( @best_continents ) { | |||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | push @$continents, $continent; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | $count += $self->mirrors( $self->countries($continent) ); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | last if $count >= $how_many; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return $self->default_mirror unless @$continents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | print "Scanning " . join(", ", @$continents) . " ...\n" if $verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my $trial_mirrors = $self->get_n_random_mirrors_by_continents( 3 * $how_many, $continents->[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $timings = $self->get_mirrors_timings( | |||||||||||||||||||||||||||||||||||||||||
| 244 | $trial_mirrors, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | $seen, | ||||||||||||||||||||||||||||||||||||||||||
| 246 | $callback, | ||||||||||||||||||||||||||||||||||||||||||
| 247 | %args, | ||||||||||||||||||||||||||||||||||||||||||
| 248 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | return $self->default_mirror unless @$timings; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | $how_many = @$timings if $how_many > @$timings; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | return wantarray ? @{$timings}[0 .. $how_many-1] : $timings->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | =item get_n_random_mirrors_by_continents( N, [CONTINENTS] ) | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | Returns up to N random mirrors for the specified continents. Specify the | ||||||||||||||||||||||||||||||||||||||||||
| 259 | continents as an array reference. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub get_n_random_mirrors_by_continents { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_n_random_mirrors_by_continents: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my( $self, $n, $continents ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $n ||= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$n ||= 3
MC/DC0%0/1
$n ||= 3
$n
Truth table0%0/2
A
A= $n
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $continents = [ $continents ] unless ref $continents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ( $n <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | return wantarray ? () : []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my @long_list = $self->get_mirrors_by_continents( $continents ); | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | if ( $n eq '*' or $n > @long_list ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$n eq "*" or $n > @long_list
MC/DC0%0/2
$n eq "*" or $n > @long_list
$n eq "*"
$n > @long_list
Truth table0%0/3
A or B
A= $n eq "*"B= $n > @long_list
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | return wantarray ? @long_list : \@long_list; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | @long_list = map {$_->[0]} | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | sort {$a->[1] <=> $b->[1]} | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | map {[$_, rand]} @long_list; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | splice @long_list, $n; # truncate | |||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | \@long_list; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | =item get_mirrors_timings( MIRROR_LIST, SEEN, CALLBACK, %ARGS ); | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | Pings the listed mirrors and returns a list of mirrors sorted in | ||||||||||||||||||||||||||||||||||||||||||
| 290 | ascending ping times. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | C<MIRROR_LIST> is an anonymous array of C<CPAN::Mirrored::By> objects to | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ping. | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | The optional argument C<SEEN> is a hash reference used to track the | ||||||||||||||||||||||||||||||||||||||||||
| 296 | mirrors you've already pinged. | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | The optional argument C<CALLBACK> is a subroutine reference to call | ||||||||||||||||||||||||||||||||||||||||||
| 299 | after each ping. It gets the C<CPAN::Mirrored::By> object after each | ||||||||||||||||||||||||||||||||||||||||||
| 300 | ping. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | sub get_mirrors_timings { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_mirrors_timings: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my( $self, $mirror_list, $seen, $callback, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $seen = {} unless defined $seen; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | croak "The mirror list argument must be an array reference" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | unless ref $mirror_list eq ref []; | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | croak "The seen argument must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | unless ref $seen eq ref {}; | ||||||||||||||||||||||||||||||||||||||||||
| 312 | croak "callback must be a subroutine" | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | if( defined $callback and ref $callback ne ref sub {} ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
defined $callback and ref $callback ne ref sub {
}
MC/DC0%0/2
defined $callback and ref $callback ne ref sub {
}
defined $callback
ref $callback ne ref sub {
}
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback ne ref sub {
}
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my $timings = []; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | for my $m ( @$mirror_list ) { | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $seen->{$m->hostname} = $m; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | next unless eval{ $m->http }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if( $self->_try_a_ping( $seen, $m, ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my $ping = $m->ping(%args); | |||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | next unless defined $ping; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | # printf "m %s ping %s\n", $m, $ping; | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | push @$timings, $m; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | $callback->( $m ) if $callback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 328 | push @$timings, $seen->{$m->hostname} | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if defined $seen->{$m->hostname}->rtt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | my @best = sort { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | if( defined $a->rtt and defined $b->rtt ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
defined $a->rtt and defined $b->rtt
MC/DC0%0/2
defined $a->rtt and defined $b->rtt
defined $a->rtt
defined $b->rtt
Truth table0%0/3
A and B
A= defined $a->rttB= defined $b->rtt
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $a->rtt <=> $b->rtt | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | ▶ | elsif( defined $a->rtt and ! defined $b->rtt ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $a->rtt and not defined $b->rtt
MC/DC0%0/2
defined $a->rtt and not defined $b->rtt
defined $a->rtt
not defined $b->rtt
Truth table0%0/3
A and not(B)
A= defined $a->rttB= not defined $b->rtt
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | return -1; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | ▶ | elsif( ! defined $a->rtt and defined $b->rtt ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
not defined $a->rtt and defined $b->rtt
MC/DC0%0/2
not defined $a->rtt and defined $b->rtt
not defined $a->rtt
defined $b->rtt
Truth table0%0/3
not(A) and B
A= not defined $a->rttB= defined $b->rtt
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | ▶ | elsif( ! defined $a->rtt and ! defined $b->rtt ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $a->rtt || defined $b->rtt
MC/DC0%0/2
defined $a->rtt || defined $b->rtt
defined $a->rtt
defined $b->rtt
Truth table0%0/3
A || B
A= defined $a->rttB= defined $b->rtt
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | } @$timings; | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | return wantarray ? @best : \@best; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | =item find_best_continents( HASH_REF ); | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | C<find_best_continents> goes through each continent and pings C<N> | ||||||||||||||||||||||||||||||||||||||||||
| 355 | random mirrors on that continent. It then orders the continents by | ||||||||||||||||||||||||||||||||||||||||||
| 356 | ascending median ping time. In list context, it returns the ordered list | ||||||||||||||||||||||||||||||||||||||||||
| 357 | of continent. In scalar context, it returns the same list as an | ||||||||||||||||||||||||||||||||||||||||||
| 358 | anonymous array. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | n - the number of hosts to ping for each continent. Default: 3 | ||||||||||||||||||||||||||||||||||||||||||
| 363 | seen - a hashref of cached hostname ping times | ||||||||||||||||||||||||||||||||||||||||||
| 364 | verbose - true or false for noisy or quiet. Default: false | ||||||||||||||||||||||||||||||||||||||||||
| 365 | callback - a subroutine to run after each ping. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | ping_cache_limit - how long, in seconds, to reuse previous ping times. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | Default: 1 day | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | The C<seen> hash has hostnames as keys and anonymous arrays as values. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | The anonymous array is a triplet of a C<CPAN::Mirrored::By> object, a | ||||||||||||||||||||||||||||||||||||||||||
| 371 | ping time, and the epoch time for the measurement. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | The callback subroutine gets the C<CPAN::Mirrored::By> object, the ping | ||||||||||||||||||||||||||||||||||||||||||
| 374 | time, and measurement time (the same things in the C<seen> hashref) as | ||||||||||||||||||||||||||||||||||||||||||
| 375 | arguments. C<find_best_continents> doesn't care what the callback does | ||||||||||||||||||||||||||||||||||||||||||
| 376 | and ignores the return value. | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | With a low value for C<N>, a single mirror might skew the results enough | ||||||||||||||||||||||||||||||||||||||||||
| 379 | to choose a worse continent. If you have that problem, try a larger | ||||||||||||||||||||||||||||||||||||||||||
| 380 | value. | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | ▶ | sub find_best_continents { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_best_continents: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | $args{n} ||= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"n"} ||= 3
MC/DC0%0/1
$args{"n"} ||= 3
$args{"n"}
Truth table0%0/2
A
A= $args{"n"}
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | $args{verbose} = 0 unless defined $args{verbose}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | $args{seen} = {} unless defined $args{seen}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | croak "The seen argument must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | unless ref $args{seen} eq ref {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | $args{ping_cache_limit} = 24 * 60 * 60 | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | unless defined $args{ping_cache_limit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | croak "callback must be a subroutine" | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | if( defined $args{callback} and ref $args{callback} ne ref sub {} ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
defined $args{"callback"} and ref $args{"callback"} ne ref sub {
}
MC/DC0%0/2
defined $args{"callback"} and ref $args{"callback"} ne ref sub {
}
defined $args{"callback"}
ref $args{"callback"} ne ref sub {
}
Truth table0%0/3
A and B
A= defined $args{"callback"}B= ref $args{"callback"} ne ref sub {
}
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | my %medians; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | CONT: for my $c ( $self->continents ) { | |||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my @mirrors = $self->mirrors( $self->countries($c) ); | |||||||||||||||||||||||||||||||||||||||||
| 400 | printf "Testing %s (%d mirrors)\n", $c, scalar @mirrors | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | if $args{verbose}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | next CONT unless @mirrors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | my $n = (@mirrors < $args{n}) ? @mirrors : $args{n}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my @tests; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my $tries = 0; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | RANDOM: while ( @mirrors && @tests < $n && $tries++ < 15 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@mirrors and @tests < $n and $tries++ < 15
@mirrors and @tests < $n
MC/DC0%0/3
@mirrors and @tests < $n and $tries++ < 15
@mirrors
@tests < $n
$tries++ < 15
Truth table0%0/4
A and B and C
A= @mirrorsB= @tests < $nC= $tries++ < 15
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my $m = splice( @mirrors, int(rand(@mirrors)), 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | if( $self->_try_a_ping( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | $args{seen}, $m, $args{ping_cache_limit} | ||||||||||||||||||||||||||||||||||||||||||
| 412 | )) { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | $self->get_mirrors_timings( | ||||||||||||||||||||||||||||||||||||||||||
| 414 | [ $m ], | ||||||||||||||||||||||||||||||||||||||||||
| 415 | $args{seen}, | ||||||||||||||||||||||||||||||||||||||||||
| 416 | $args{callback}, | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 418 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | next RANDOM unless defined $args{seen}{$m->hostname}->rtt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | printf "(%s -> %0.2f ms)", | ||||||||||||||||||||||||||||||||||||||||||
| 422 | $m->hostname, | ||||||||||||||||||||||||||||||||||||||||||
| 423 | join ' ', 1000 * $args{seen}{$m->hostname}->rtt | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | if $args{verbose}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | push @tests, $args{seen}{$m->hostname}->rtt; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | my $median = $self->_get_median_ping_time( \@tests, $args{verbose} ); | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | $medians{$c} = $median if defined $median; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | my @best_cont = sort { $medians{$a} <=> $medians{$b} } keys %medians; | |||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ( $args{verbose} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | print "Median result by continent:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if ( @best_cont ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | for my $c ( @best_cont ) { | |||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | printf( " %7.2f ms %s\n", $medians{$c}*1000, $c ); | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | print " **** No results found ****\n" | |||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | return wantarray ? @best_cont : $best_cont[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | # retry if | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | ▶ | sub _try_a_ping { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_try_a_ping: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | my ($self, $seen, $mirror, $ping_cache_limit ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | ( ! exists $seen->{$mirror->hostname} | ||||||||||||||||||||||||||||||||||||||||||
| 454 | or | ||||||||||||||||||||||||||||||||||||||||||
| 455 | ! defined $seen->{$mirror->hostname}->rtt | ||||||||||||||||||||||||||||||||||||||||||
| 456 | or | ||||||||||||||||||||||||||||||||||||||||||
| 457 | ! defined $ping_cache_limit | ||||||||||||||||||||||||||||||||||||||||||
| 458 | or | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | ▶ | time - $seen->{$mirror->hostname}->ping_time | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
exists $$seen{$mirror->hostname} && defined $$seen{$mirror->hostname}->rtt && defined $ping_cache_limit
exists $$seen{$mirror->hostname} and defined $$seen{$mirror->hostname}->rtt
MC/DC0%0/3
exists $$seen{$mirror->hostname} && defined $$seen{$mirror->hostname}->rtt && defined $ping_cache_limit
exists $$seen{$mirror->hostname}
defined $$seen{$mirror->hostname}->rtt
defined $ping_cache_limit
Truth table0%0/4
A and B && C
A= exists $$seen{$mirror->hostname}B= defined $$seen{$mirror->hostname}->rttC= defined $ping_cache_limit
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | > $ping_cache_limit | ||||||||||||||||||||||||||||||||||||||||||
| 461 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | sub _get_median_ping_time { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_median_ping_time: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my ($self, $tests, $verbose ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | my @sorted = sort { $a <=> $b } @$tests; | |||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | my $median = do { | |||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | ▶ | if ( @sorted == 0 ) { undef } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | elsif ( @sorted == 1 ) { $sorted[0] } | |||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | elsif ( @sorted % 2 ) { $sorted[ int(@sorted / 2) ] } | |||||||||||||||||||||||||||||||||||||||||
| 473 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | my $mid_high = int(@sorted/2); | |||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | ($sorted[$mid_high-1] + $sorted[$mid_high])/2; | |||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||
| 477 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | if ($verbose){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | ▶ | if ($median) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | printf " => median time: %.2f ms\n", $median * 1000 | |||||||||||||||||||||||||||||||||||||||||
| 482 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | printf " => **** no median time ****\n"; | |||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | return $median; | |||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | # Adapted from Parse::CPAN::MirroredBy by Adam Kennedy | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 1 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=19 SCAR=30.1
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 1 | my ($self, $file, $handle) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 1 | my $output = $self->{mirrors}; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 1 | my $geo = $self->{geography}; | |||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 1 | local $/ = "\012"; | |||||||||||||||||||||||||||||||||||||||||
| 497 | 1 | my $line = 0; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | my $mirror = undef; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 1 | while ( 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 500 | # Next line | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 4242 | my $string = <$handle>; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 4242 | ▶ | last if ! defined $string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 4241 | $line = $line + 1; | |||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | # Remove the useless lines | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 4241 | chomp( $string ); | |||||||||||||||||||||||||||||||||||||||||
| 507 | 4241 | ▶ | next if $string =~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 3673 | ▶ | next if $string =~ /^\s*#/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | # Hostname or property? | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 2783 | ▶ | if ( $string =~ /^\s/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | # Property | ||||||||||||||||||||||||||||||||||||||||||
| 513 | 2501 | ▶ | unless ( $string =~ /^\s+(\w+)\s+=\s+\"(.*)\"$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | croak("Invalid property on line $line"); | |||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||
| 516 | 2501 | my ($prop, $value) = ($1,$2); | |||||||||||||||||||||||||||||||||||||||||
| 517 | 2501 | ▶ | $mirror ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mirror ||= {}
MC/DC0%0/1
$mirror ||= {}
$mirror
Truth table50%1/2
A
A= $mirror
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 2501 | ▶ | if ( $prop eq 'dst_location' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 282 | my (@location,$continent,$country); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 282 | ▶ | @location = (split /\s*,\s*/, $value) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | and ($continent, $country) = @location[-1,-2]; | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 282 | $continent =~ s/\s\(.*//; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 282 | $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude | |||||||||||||||||||||||||||||||||||||||||
| 524 | 282 | ▶ | $geo->{$continent}{$country} = 1 if $continent && $country; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$continent and $country
MC/DC0%0/2
$continent and $country
$continent
$country
Truth table33%1/3
A and B
A= $continentB= $country
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | 282 | ▶ | $mirror->{continent} = $continent || "unknown"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$continent || 'unknown'
MC/DC0%0/1
$continent || 'unknown'
$continent
Truth table50%1/2
A
A= $continent
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | 282 | ▶ | $mirror->{country} = $country || "unknown"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$country || 'unknown'
MC/DC0%0/1
$country || 'unknown'
$country
Truth table50%1/2
A
A= $country
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | elsif ( $prop eq 'dst_http' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 201 | $mirror->{http} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | elsif ( $prop eq 'dst_ftp' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 282 | $mirror->{ftp} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||
| 534 | elsif ( $prop eq 'dst_rsync' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 57 | $mirror->{rsync} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 1679 | $prop =~ s/^dst_//; | |||||||||||||||||||||||||||||||||||||||||
| 539 | 1679 | $mirror->{$prop} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 542 | # Hostname | ||||||||||||||||||||||||||||||||||||||||||
| 543 | 282 | ▶ | unless ( $string =~ /^([\w\.-]+)\:\s*$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | croak("Invalid host name on line $line"); | |||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 282 | my $current = $mirror; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 282 | $mirror = { hostname => "$1" }; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 282 | ▶ | if ( $current ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 281 | push @$output, CPAN::Mirrored::By->new($current); | |||||||||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 1 | ▶ | if ( $mirror ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 1 | push @$output, CPAN::Mirrored::By->new($mirror); | |||||||||||||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | package CPAN::Mirrored::By; | ||||||||||||||||||||||||||||||||||||||||||
| 563 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | 7 | ▶ | use Net::Ping (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | 283 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | 283 | my($self,$arg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 568 | 283 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||||||
| 569 | 283 | bless $arg, $self; | |||||||||||||||||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | ▶ | sub hostname { shift->{hostname} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hostname: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | 1 | ▶ | sub continent { shift->{continent} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
continent: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | 1 | ▶ | sub country { shift->{country} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
country: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | ▶ | sub http { shift->{http} || '' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
http: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | ▶ | sub ftp { shift->{ftp} || '' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ftp: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | 0 | ▶ | sub rsync { shift->{rsync} || '' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rsync: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | ▶ | sub rtt { shift->{rtt} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rtt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | ▶ | sub ping_time { shift->{ping_time} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ping_time: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | 1 | ▶ | sub url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 1 | ▶ | return $self->{http} || $self->{ftp}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'http'} || $self->{'ftp'}
MC/DC0%0/2
$self->{'http'} || $self->{'ftp'}
$self->{'http'}
$self->{'ftp'}
Truth table33%1/3
A || B
A= $self->{'http'}B= $self->{'ftp'}
| |||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | ▶ | sub ping { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ping: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | my($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my $external_ping = $args{external_ping}; | |||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | if ($external_ping) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | ▶ | eval { require Net::Ping::External } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 591 | or die "Net::Ping::External required to use external ping command"; | ||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | ▶ | my $ping = Net::Ping->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | $external_ping ? 'external' : $^O eq 'VMS' ? 'icmp' : 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 595 | 1 | ||||||||||||||||||||||||||||||||||||||||||
| 596 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | my ($proto) = $self->url =~ m{^([^:]+)}; | |||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | ▶ | my $port = $proto eq 'http' ? 80 : 21; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | ▶ | return unless $port; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | ▶ | if ( $ping->can('port_number') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | $ping->port_number($port); | |||||||||||||||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||||||||||||||||
| 604 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | $ping->{'port_num'} = $port; | |||||||||||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | $ping->hires(1) if $ping->can('hires'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | my ($alive,$rtt) = eval { $ping->ping($self->hostname); }; | |||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | my $verbose = $args{verbose}; | |||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | ▶ | if ($verbose && !$alive) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$verbose and not $alive
MC/DC0%0/2
$verbose and not $alive
$verbose
not $alive
Truth table0%0/3
A and not(B)
A= $verboseB= not $alive
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | printf "(host %s not alive)", $self->hostname; | |||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | ▶ | $self->{rtt} = $alive ? $rtt : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | $self->{ping_time} = time; | |||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | $self->rtt; | |||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | Andreas Koenig C<< <andk@cpan.org> >>, David Golden C<< <dagolden@cpan.org> >>, | ||||||||||||||||||||||||||||||||||||||||||
| 629 | brian d foy C<< <bdfoy@cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||
| 634 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||
| 638 | =cut | ||||||||||||||||||||||||||||||||||||||||||