| 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.1.1 : 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 | 68 | ▶ | ADJUST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 68 | ▶ | if ($piece_priorities) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 61 | @piece_priorities = @$piece_priorities; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | @piece_priorities = (1) x $bitfield->size; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 34 | ▶ | 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 | 0 | ▶ | 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
Condition0%0/3
$index < 0 or $index >= $bitfield->size
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$piece_priorities[$index] // 1
$piece_priorities[$index]
Truth table0%0/2
A
A= $piece_priorities[$index]
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | method get_availability ($index) { $rarest_first->get_availability($index) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_availability: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 16 | ▶ | method is_interesting ($peer) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_interesting: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 16 | my $p_bf = $peer->torrent->peer_bitfields->{$peer}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 16 | ▶ | unless ($p_bf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | for ( my $i = 0; $i < $bitfield->size; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 8 | ▶ | if ( $p_bf->get($i) && !$bitfield->get($i) && $piece_priorities[$i] > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$p_bf->get($i) and not $bitfield->get($i) and $piece_priorities[$i] > 0
$p_bf->get($i) and not $bitfield->get($i)
MC/DC0%0/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 table50%2/4
A and not(B) and C
A= $p_bf->get($i)B= not $bitfield->get($i)C= $piece_priorities[$i] > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 8 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | method pick_piece ( $peer_bitfield, $blocks_pending ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_piece: called CC=10 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | return undef unless $peer_bitfield; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # Get all candidates | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | my @candidates; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | for ( my $i = 0; $i < $bitfield->size; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | next if $bitfield->get($i); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | next if !$peer_bitfield->get($i); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | next if $piece_priorities[$i] <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | push @candidates, $i; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | return undef unless @candidates; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Apply Strategy to candidates | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | if ( $strategy == PICK_SEQUENTIAL ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
MC/DC0%0/2
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
$piece_priorities[$b] <=> $piece_priorities[$a]
$a <=> $b
Truth table0%0/3
A || B
A= $piece_priorities[$b] <=> $piece_priorities[$a]B= $a <=> $b
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 34 | ▶ | method pick_block ( $peer, $blocks_pending ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_block: called CC=18 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 34 | my $peer_bitfield = $peer->torrent->peer_bitfields->{$peer}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 34 | ▶ | return undef unless $peer_bitfield; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # Get all candidates | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 34 | my @candidates; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 34 | ▶ | for ( my $i = 0; $i < $bitfield->size; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 42 | ▶ | next if $bitfield->get($i); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 37 | ▶ | next if !$peer_bitfield->get($i); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 37 | ▶ | next if $piece_priorities[$i] <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 37 | push @candidates, $i; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 34 | ▶ | return undef unless @candidates; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # Apply Strategy to candidates | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 32 | ▶ | if ( $strategy == PICK_SEQUENTIAL ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
MC/DC0%0/2
$piece_priorities[$b] <=> $piece_priorities[$a] || $a <=> $b
$piece_priorities[$b] <=> $piece_priorities[$a]
$a <=> $b
Truth table0%0/3
A || B
A= $piece_priorities[$b] <=> $piece_priorities[$a]B= $a <=> $b
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # RAREST_FIRST | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 32 | @candidates = sort { $rarest_first->get_availability($a) <=> $rarest_first->get_availability($b) } @candidates; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 32 | for my $piece_idx (@candidates) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 32 | my $piece_len = $peer->torrent->piece_length($piece_idx); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 32 | my $offset = 0; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 32 | my $blocks_received = $peer->torrent->blocks_received; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 32 | ▶ | while ( $offset < $piece_len ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 32 | ▶ | my $is_pending = $blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$blocks_pending->{$piece_idx} && $blocks_pending->{$piece_idx}{$offset}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 32 | ▶ | my $is_received = $blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$blocks_received->{$piece_idx} && $blocks_received->{$piece_idx}{$offset}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 32 | ▶ | if ( $end_game || ( !$is_pending && !$is_received ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$end_game or not $is_pending || $is_received
$is_pending || $is_received
MC/DC0%0/3
$end_game or not $is_pending || $is_received
$end_game
$is_pending
$is_received
Truth table50%2/4
A or not(B || C)
A= $end_gameB= $is_pendingC= $is_received
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 32 | ▶ | if ( !$is_received ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 17 | return ( $piece_idx, $offset, 16384 ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 15 | $offset += 16384; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | 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; | |||||||||||||||||||||||||||||||||||||||||