lib/Net/BitTorrent/Torrent/PiecePicker.pm

filter: Statementstmt 91.3% 74 / 81 Branchbran 67.3% 35 / 52 Conditioncond 65.2% 15 / 23 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 86.6% 13 / 15 Podpod 100% 8 / 8 totaltotal 79.1% 152 / 192 SCAR 38.5
CC
36
Cov
79%
pick_block
19.8
pick_piece
13.1
set_priority
12.0
CRAP 47.2
« lib/Net/BitTorrent/SSRF.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP10.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class';
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::Torrent::PiecePicker v2.0.0 : isa(Net::BitTorrent::Emitter) {
7 26 ▶ use Acme::Selection::RarestFirst;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶ use Net::BitTorrent::Types qw[:pick];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 field $bitfield : param;
10 26 field $rarest_first = Acme::Selection::RarestFirst->new( size => $bitfield->size );
11 26 field $piece_priorities : param = undef;
12 26 field @piece_priorities;
13 26 field $strategy : param : reader : writer = PICK_RAREST_FIRST; # SEQUENTIAL, RAREST_FIRST, STREAMING
14 26 field $end_game : reader = 0;
15 #
16 ADJUST {
17 68 ▶ if ($piece_priorities) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($piece_priorities) { } 61 7 68
18 61 @piece_priorities = @$piece_priorities;
19 }
20 else {
21 7 @piece_priorities = (1) x $bitfield->size;
22 }
23 }
24 36 ▶ method update_availability ( $peer_bitfield, $delta ) { $rarest_first->update( $peer_bitfield, $delta ) }
Subroutine update_availability: called CC=1 SCAR=0.0
Pod documented
25  
26 ▶ method set_priority ( $index, $priority ) {
Subroutine set_priority: not called CC=3 SCAR=24.8
Pod documented
27 0 ▶ return if $index < 0 || $index >= $bitfield->size;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $bitfield->size 0 0 0
Condition0%0/3
$index < 0 or $index >= $bitfield->size
l!l&&r!l&&!r
000
MC/DC0%0/2
$index < 0 or $index >= $bitfield->size
$index < 0 $index >= $bitfield->size
Truth table0%0/3
A or B
A= $index < 0B= $index >= $bitfield->size
ABresult
1X1
011
000
28 0 $piece_priorities[$index] = $priority;
29 }
30 0 ▶ method get_priority ($index) { $piece_priorities[$index] // 1 }
Subroutine get_priority: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$piece_priorities[$index] // 1
l!l
00
MC/DC0%0/1
$piece_priorities[$index] // 1
$piece_priorities[$index]
Truth table0%0/2
A
A= $piece_priorities[$index]
Aresult
11
00
31 3 ▶ method get_availability ($index) { $rarest_first->get_availability($index) }
Subroutine get_availability: called CC=1 SCAR=0.0
Pod documented
32  
33 ▶ method is_interesting ($peer) {
Subroutine is_interesting: called CC=6 SCAR=17.9
Pod documented
34 44 my $p_bf = $peer->torrent->peer_bitfields->{$peer};
35 44 ▶ unless ($p_bf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p_bf) 12 32 44
36 12 return 0;
37 }
38 32 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 32 30 62
39 32 ▶ if ( $p_bf->get($i) && !$bitfield->get($i) && $piece_priorities[$i] > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0) 2 30 32
Condition83%5/6
$p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0
!ll&&!rl&&r
3002
$p_bf->get($i) and not $bitfield->get($i)
!ll&&!rl&&r
20102
MC/DC66%2/3
$p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0
$p_bf->get($i) not $bitfield->get($i) $piece_priorities[$i] > 0
Truth table75%3/4
A and not(B) and C
A= $p_bf->get($i)B= not $bitfield->get($i)C= $piece_priorities[$i] > 0
ABCresult
0XX0
10X0
1100
1111
40 2 return 1;
41 }
42 }
43 30 return 0;
44 }
45  
46 ▶ method pick_piece ( $peer_bitfield, $blocks_pending ) {
Subroutine pick_piece: called CC=10 SCAR=25.7
Pod documented
47 2 ▶ return undef unless $peer_bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield 0 2 2
48  
49 # Get all candidates
50 2 my @candidates;
51 2 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 6 2 8
52 6 ▶ next if $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($i) 0 6 6
53 6 ▶ next if !$peer_bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield->get($i) 2 4 6
54 4 ▶ next if $piece_priorities[$i] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $piece_priorities[$i] <= 0 0 4 4
55 4 push @candidates, $i;
56 }
57 2 ▶ return undef unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 2 2
58  
59 # Apply Strategy to candidates
60 2 ▶ if ( $strategy == PICK_SEQUENTIAL ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($strategy == 0) { } 0 2 2
elsif ($strategy == 2) { } 0 2 2
61  
62 # Already sorted by index
63 }
64 elsif ( $strategy == PICK_STREAMING ) {
65 0 ▶ @candidates = sort { ( $piece_priorities[$b] <=> $piece_priorities[$a] ) || ( $a <=> $b ) } @candidates;
Branch: 1 branch
BranchTrueFalseTotal
$piece_priorities[$b] <=> $piece_priorities[$a] or $a <=> $b 0 0 0
66 }
67 else {
68 # RAREST_FIRST
69 2 @candidates = sort { $rarest_first->get_availability($a) <=> $rarest_first->get_availability($b) } @candidates;
70 }
71 2 return $candidates[0];
72 }
73  
74 ▶ method pick_block ( $peer, $blocks_pending ) {
Subroutine pick_block: called CC=18 SCAR=29.9
Pod documented
75 36 my $peer_bitfield = $peer->torrent->peer_bitfields->{$peer};
76 36 ▶ return undef unless $peer_bitfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield 0 36 36
77  
78 # Get all candidates
79 36 my @candidates;
80 36 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 44 36 80
81 44 ▶ next if $bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
if $bitfield->get($i) 5 39 44
82 39 ▶ next if !$peer_bitfield->get($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $peer_bitfield->get($i) 0 39 39
83 39 ▶ next if $piece_priorities[$i] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $piece_priorities[$i] <= 0 0 39 39
84 39 push @candidates, $i;
85 }
86 36 ▶ return undef unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 2 34 36
87  
88 # Apply Strategy to candidates
89 34 ▶ if ( $strategy == PICK_SEQUENTIAL ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($strategy == 0) { } 0 34 34
elsif ($strategy == 2) { } 0 34 34
90  
91 # Already sorted by index
92 }
93 elsif ( $strategy == PICK_STREAMING ) {
94 0 ▶ @candidates = sort { ( $piece_priorities[$b] <=> $piece_priorities[$a] ) || ( $a <=> $b ) } @candidates;
Branch: 1 branch
BranchTrueFalseTotal
$piece_priorities[$b] <=> $piece_priorities[$a] or $a <=> $b 0 0 0
95 }
96 else {
97 # RAREST_FIRST
98 34 @candidates = sort { $rarest_first->get_availability($a) <=> $rarest_first->get_availability($b) } @candidates;
99 }
100 34 for my $piece_idx (@candidates) {
101 34 my $piece_len = $peer->torrent->piece_length($piece_idx);
102 34 my $offset = 0;
103 34 my $blocks_received = $peer->torrent->blocks_received;
104 34 ▶ while ( $offset < $piece_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $piece_len) 34 17 51
105 34 ▶ my $is_pending = $blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset};
Condition100%3/3
$blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}
!ll&&!rl&&r
51514
MC/DC100%2/2
$blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}
$blocks_pending->{$piece_idx} $blocks_pending->{$piece_idx}{$offset}
Truth table100%3/3
A && B
A= $blocks_pending->{$piece_idx}B= $blocks_pending->{$piece_idx}{$offset}
ABresult
0X0
100
111
106 34 ▶ my $is_received = $blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset};
Condition66%2/3
$blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}
!ll&&!rl&&r
19015
MC/DC50%1/2
$blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}
$blocks_received->{$piece_idx} $blocks_received->{$piece_idx}{$offset}
Truth table66%2/3
A && B
A= $blocks_received->{$piece_idx}B= $blocks_received->{$piece_idx}{$offset}
ABresult
0X0
100
111
107 34 ▶ if ( $end_game || ( !$is_pending && !$is_received ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_game or not $is_pending || $is_received) 32 2 34
Condition83%5/6
$end_game or not $is_pending || $is_received
l!l&&r!l&&!r
3022
$is_pending || $is_received
l!l&&r!l&&!r
202
MC/DC66%2/3
$end_game or not $is_pending || $is_received
$end_game $is_pending $is_received
Truth table75%3/4
A or not(B || C)
A= $end_gameB= $is_pendingC= $is_received
ABCresult
1XX1
0001
01X0
0010
108 32 ▶ if ( !$is_received ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_received) 17 15 32
109 17 return ( $piece_idx, $offset, 16384 );
110 }
111 }
112 17 $offset += 16384;
113 }
114 }
115 17 return undef;
116 }
117 6 ▶ method enter_end_game () { $end_game = 1 }
Subroutine enter_end_game: called CC=1 SCAR=0.0
Pod documented
118 }
119 #
120 26 1;
« lib/Net/BitTorrent/SSRF.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP10.pm »