lib/Net/BitTorrent/Tracker.pm

filter: Statementstmt 38.6% 41 / 106 Branchbran 7.5% 3 / 40 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 64.7% 11 / 17 Podpod 100% 5 / 5 totaltotal 28.5% 60 / 210 CC 47 SCAR 41.6
CC
47
Cov
25%
announce_all
155.0
__ANON__
90.0
_merge_scrape_stats
72.0
CRAP 64.3
« lib/Net/BitTorrent/Peer.pm ↑ Summary lib/Net/BitTorrent/Tracker/UDP.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class', 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #
5 26 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 class Net::BitTorrent::Tracker v2.1.1 : isa(Net::BitTorrent::Emitter) {
7 26 ▶ use Net::BitTorrent::Tracker::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶ use Net::BitTorrent::Tracker::UDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶ use List::Util qw[shuffle];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 field $tiers_raw : param; # [ [url1, url2], [url3] ]
11 26 field $debug : param = 0;
12 26 field @tiers; # [ [ { obj, last_announce, interval, ... }, ... ], ... ]
13 83 ▶ ADJUST {
Subroutine __ANON__: called CC=3 SCAR=11.0
14 83 for my $tier_list (@$tiers_raw) {
15 5 my @tier;
16 5 for my $url (@$tier_list) {
17 5 push @tier,
18 {
19 obj => $self->_create_tracker($url),
20 last_announce => 0,
21 interval => 0,
22 min_interval => 0,
23 tracker_id => undef,
24 consecutive_failures => 0
25 };
26 }
27  
28 # BEP 12: shuffle within tier
29 5 @tier = shuffle @tier;
30 5 push @tiers, \@tier;
31 }
32 }
33  
34 5 ▶ method _create_tracker ($url) {
Subroutine _create_tracker: called CC=3 SCAR=15.1
35 0 ▶ if ( $url =~ /^udp:/ ) { return Net::BitTorrent::Tracker::UDP->new( url => $url ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ /^udp:/u) { } 0 5 5
elsif ($url =~ /^https?:/u) { } 5 0 5
36 5 elsif ( $url =~ /^https?:/ ) { return Net::BitTorrent::Tracker::HTTP->new( url => $url ) }
37 0 $self->_emit_log( 'fatal', "Unsupported tracker protocol: $url" );
38 0 return undef;
39 }
40  
41 57 ▶ method announce_all ( $params, $cb = undef ) {
Subroutine announce_all: called CC=14 SCAR=50.4
Pod documented
42 57 my %unique_peers;
43 57 my $now = time();
44  
45 # If we have multiple infohashes (hybrid), we should ideally announce all.
46 57 ▶ my @ihs = ref( $params->{infohash} ) eq 'ARRAY' ? @{ $params->{infohash} } : ( $params->{infohash} );
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'infohash'} eq 'ARRAY' ? : 57 0 57
47 57 for my $tier (@tiers) {
48 0 my $tier_success = 0;
49 0 ▶ for ( my $i = 0; $i < scalar @$tier; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$tier) 0 0 0
50 0 my $entry = $tier->[$i];
51  
52 # Check interval (unless event is set like 'started', 'stopped')
53 0 ▶ if ( !$params->{event} && $entry->{last_announce} + ( $entry->{interval} || 60 ) > $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $params->{"event"} and $entry->{"last_announce"} + ($entry->{"interval"} || 60) > $now) 0 0 0
Condition0%0/5
not $params->{"event"} and $entry->{"last_announce"} + ($entry->{"interval"} || 60) > $now
!ll&&!rl&&r
000
$entry->{'interval'} || 60
l!l
00
MC/DC0%0/2
not $params->{"event"} and $entry->{"last_announce"} + ($entry->{"interval"} || 60) > $now
not $params->{"event"} $entry->{"last_announce"} + ($entry->{"interval"} || 60) > $now
MC/DC0%0/1
$entry->{'interval'} || 60
$entry->{'interval'}
Truth table0%0/3
not(A) and B
A= not $params->{"event"}B= $entry->{"last_announce"} + ($entry->{"interval"} || 60) > $now
ABresult
0X0
100
111
Truth table0%0/2
A
A= $entry->{'interval'}
Aresult
11
00
54 0 ▶ $tier_success = 1 if $i == 0 && $entry->{last_announce} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $entry->{"last_announce"} > 0 0 0 0
Condition0%0/3
$i == 0 and $entry->{"last_announce"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$i == 0 and $entry->{"last_announce"} > 0
$i == 0 $entry->{"last_announce"} > 0
Truth table0%0/3
A and B
A= $i == 0B= $entry->{"last_announce"} > 0
ABresult
0X0
100
111
55 0 next;
56 }
57 0 my $pending_ihs = scalar @ihs;
58 0 for my $ih (@ihs) {
59 0 my $ih_params = { %$params, infohash => $ih };
60 0 ▶ $ih_params->{trackerid} = $entry->{tracker_id} if $entry->{tracker_id};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"tracker_id"} 0 0 0
61 0 ▶ my $on_res = sub ($res) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
62 0 $entry->{last_announce} = time();
63 0 ▶ $entry->{interval} = $res->{interval} // 1800;
Condition0%0/2
$res->{"interval"} // 1800
l!l
00
MC/DC0%0/1
$res->{"interval"} // 1800
$res->{"interval"}
Truth table0%0/2
A
A= $res->{"interval"}
Aresult
11
00
64 0 ▶ $entry->{min_interval} = $res->{'min interval'} // 0;
Condition0%0/2
$res->{"min interval"} // 0
l!l
00
MC/DC0%0/1
$res->{"min interval"} // 0
$res->{"min interval"}
Truth table0%0/2
A
A= $res->{"min interval"}
Aresult
11
00
65 0 ▶ $entry->{tracker_id} = $res->{trackerid} if $res->{trackerid};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"trackerid"} 0 0 0
66 0 $entry->{consecutive_failures} = 0;
67 0 ▶ for my $peer ( @{ $res->{peers} // [] } ) {
Condition0%0/2
$res->{"peers"} // []
l!l
00
MC/DC0%0/1
$res->{"peers"} // []
$res->{"peers"}
Truth table0%0/2
A
A= $res->{"peers"}
Aresult
11
00
68 0 my $key = "$peer->{ip}:$peer->{port}";
69 0 $unique_peers{$key} = $peer;
70 }
71  
72 # Promote successful tracker to front of tier
73 0 ▶ if ( $i > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
74 0 splice( @$tier, $i, 1 );
75 0 unshift( @$tier, $entry );
76 }
77 0 $pending_ihs--;
78 0 ▶ if ( $pending_ihs <= 0 && $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending_ihs <= 0 and $cb) 0 0 0
Condition0%0/3
$pending_ihs <= 0 and $cb
!ll&&!rl&&r
000
MC/DC0%0/2
$pending_ihs <= 0 and $cb
$pending_ihs <= 0 $cb
Truth table0%0/3
A and B
A= $pending_ihs <= 0B= $cb
ABresult
0X0
100
111
79 0 $cb->( [ values %unique_peers ] );
80 }
81 0 };
82 0 try {
83 0 $entry->{obj}->perform_announce( $ih_params, $on_res );
84 0 $tier_success = 1;
85 }
86 catch ($e) {
87 0 ▶ $self->_emit_log( 'debug', 'Announce to ' . $entry->{obj}->url . " failed: $e" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
88 0 $entry->{consecutive_failures}++;
89 0 $pending_ihs--;
90 }
91 }
92 0 ▶ last if $tier_success;
Branch: 1 branch
BranchTrueFalseTotal
if $tier_success 0 0 0
93 }
94 }
95 57 return [ values %unique_peers ];
96 }
97  
98 0 ▶ method scrape_all ( $infohashes, $cb = undef ) {
Subroutine scrape_all: not called CC=4 SCAR=30.0
Pod documented
99 0 my %results;
100 0 for my $tier (@tiers) {
101 0 for my $entry (@$tier) {
102 0 ▶ my $on_res = sub ($res) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
103 0 ▶ if ( ref $res->{files} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res->{'files'} eq 'ARRAY') { } 0 0 0
104 0 ▶ for ( my $j = 0; $j < scalar @$infohashes; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$infohashes) 0 0 0
105 0 $self->_merge_scrape_stats( \%results, $infohashes->[$j], $res->{files}[$j] );
106 }
107 }
108 else {
109 0 ▶ for my $ih ( keys %{ $res->{files} // {} } ) {
Condition0%0/2
$res->{"files"} // {}
l!l
00
MC/DC0%0/1
$res->{"files"} // {}
$res->{"files"}
Truth table0%0/2
A
A= $res->{"files"}
Aresult
11
00
110 0 $self->_merge_scrape_stats( \%results, $ih, $res->{files}{$ih} );
111 }
112 }
113 0 ▶ $cb->( \%results ) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
114 0 };
115 0 try {
116 0 $entry->{obj}->perform_scrape( $infohashes, $on_res );
117 }
118 catch ($e) { }
119 }
120 }
121 0 return \%results;
122 }
123  
124 0 ▶ method _merge_scrape_stats ( $results, $ih, $stats ) {
Subroutine _merge_scrape_stats: not called CC=8 SCAR=42.8
125 0 ▶ $results->{$ih} //= { complete => 0, downloaded => 0, incomplete => 0 };
Condition0%0/2
$results->{$ih} //= {"complete", 0, "downloaded", 0, "incomplete", 0}
l!l
00
MC/DC0%0/1
$results->{$ih} //= {"complete", 0, "downloaded", 0, "incomplete", 0}
$results->{$ih}
Truth table0%0/2
A
A= $results->{$ih}
Aresult
11
00
126 0 ▶ $results->{$ih}{complete} = $stats->{complete} if ( $stats->{complete} // 0 ) > $results->{$ih}{complete};
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{"complete"} // 0) > $results->{$ih}{"complete"} 0 0 0
Condition0%0/2
$stats->{"complete"} // 0
l!l
00
MC/DC0%0/1
$stats->{"complete"} // 0
$stats->{"complete"}
Truth table0%0/2
A
A= $stats->{"complete"}
Aresult
11
00
127 0 ▶ $results->{$ih}{incomplete} = $stats->{incomplete} if ( $stats->{incomplete} // 0 ) > $results->{$ih}{incomplete};
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{"incomplete"} // 0) > $results->{$ih}{"incomplete"} 0 0 0
Condition0%0/2
$stats->{"incomplete"} // 0
l!l
00
MC/DC0%0/1
$stats->{"incomplete"} // 0
$stats->{"incomplete"}
Truth table0%0/2
A
A= $stats->{"incomplete"}
Aresult
11
00
128 0 ▶ $results->{$ih}{downloaded} = $stats->{downloaded} if ( $stats->{downloaded} // 0 ) > $results->{$ih}{downloaded};
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->{"downloaded"} // 0) > $results->{$ih}{"downloaded"} 0 0 0
Condition0%0/2
$stats->{"downloaded"} // 0
l!l
00
MC/DC0%0/1
$stats->{"downloaded"} // 0
$stats->{"downloaded"}
Truth table0%0/2
A
A= $stats->{"downloaded"}
Aresult
11
00
129 }
130  
131 0 ▶ method trackers () {
Subroutine trackers: not called CC=1 SCAR=6.9
Pod documented
132 return [
133 map {
134 0 map { $_->{obj}->url }
135 @$_
136 } @tiers
137 ];
138 }
139  
140 45 ▶ method tick ($delta) {
Subroutine tick: called CC=4 SCAR=25.8
Pod documented
141 45 for my $tier (@tiers) {
142 0 for my $entry (@$tier) {
143 0 ▶ if ( $entry->{obj}->can('tick') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"obj"}->can("tick")) 0 0 0
144 0 $entry->{obj}->tick($delta);
145 }
146 }
147 }
148 }
149  
150 0 ▶ method add_tracker ($url) {
Subroutine add_tracker: not called CC=4 SCAR=30.0
Pod documented
151  
152 # Check if already present
153 0 for my $tier (@tiers) {
154 0 for my $entry (@$tier) {
155 0 ▶ return if $entry->{obj}->url eq $url;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"obj"}->url eq $url 0 0 0
156 }
157 }
158 0 push @tiers,
159 [
160 { obj => $self->_create_tracker($url),
161 last_announce => 0,
162 interval => 0,
163 min_interval => 0,
164 tracker_id => undef,
165 consecutive_failures => 0
166 }
167 ];
168 }
169 };
170 #
171 26 1;
« lib/Net/BitTorrent/Peer.pm ↑ Summary lib/Net/BitTorrent/Tracker/UDP.pm »