Gnutella.pm

filter: Statementstmt 16.4% 35 / 213 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 37.0% 10 / 27 Podpod 0.0% 0 / 10 totaltotal 11.6% 45 / 385 CC 70 SCAR 47.5
CC
70
Cov
10%
_handler
380.0
do_one_loop
132.0
schedule
90.0
CRAP 115.4
« Gnutella/Connection.pm ↑ Summary Gnutella/Packet/Reply.pm »
1 package Net::Gnutella;
2 1 ▶use Net::Gnutella::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Net::Gnutella::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::Gnutella::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use vars qw/@ISA @EXPORT $VERSION $AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 $VERSION = $VERSION = "0.1";
12
13 1 ▶use constant GNUTELLA_CONNECT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant GNUTELLA_REQUEST => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 1 require Exporter;
17 1 @ISA = qw(Exporter);
18 1 @EXPORT = qw(GNUTELLA_CONNECT GNUTELLA_REQUEST);
19
20 # Use AUTOHANDLER to supply generic attribute methods
21 #
22 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
23 0 my $self = shift;
24 0 my $attr = $AUTOLOAD;
25 0 $attr =~ s/.*:://;
26 0 ▶ return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /[^A-Z]/ 0 0 0
27 0 ▶ croak sprintf "invalid attribute method: %s->%s()", ref($self), $attr unless exists $self->{_attr}->{lc $attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_attr"}{lc $attr} 0 0 0
28 0 ▶ $self->{_attr}->{lc $attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
29 0 return $self->{_attr}->{lc $attr};
30 }
31
32 0 ▶sub add_handler {
Subroutine add_handler: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my ($self, $event, $coderef, $replace, @args) = @_;
34
35 0 return $self->_add_handler($event, $coderef, $replace, $self->{_handler}, @args);
36 }
37
38 0 ▶sub dequeue {
Subroutine dequeue: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my ($self, $qid) = @_;
40
41 0 return delete $self->{_queue}->{$qid};
42 }
43
44 0 ▶sub do_one_loop {
Subroutine do_one_loop: not called CC=11 SCAR=48.8
Pod undocumented
45 0 my $self = shift;
46
47 0 my $timeout = $self->timeout;
48 0 my $time = time();
49
50 0 foreach my $key ($self->queue) {
51 0 my $event = $self->queue($key);
52
53 0 ▶ if ($event->[0] <= $time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->[0] <= $time) { } 0 0 0
54 0 $event->[1]->( @{$event}[2..$#{$event}] );
55
56 0 $self->dequeue($key);
57 } else {
58 0 my $nexttimeout = $event->[0] - $time;
59
60 0 ▶ $timeout = $nexttimeout if $nexttimeout < $timeout or not $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $nexttimeout < $timeout or not $timeout 0 0 0
Condition0%0/3
$nexttimeout < $timeout or not $timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
$nexttimeout < $timeout or not $timeout
$nexttimeout < $timeout not $timeout
Truth table0%0/3
A or not(B)
A= $nexttimeout < $timeoutB= not $timeout
ABresult
1X1
011
000
61 }
62 }
63
64 0 my ($rr, $wr, $er) = IO::Select->select(@{$self}{'_read', '_write', '_error'}, $timeout);
65
66 0 foreach my $sock (@$rr) {
67 0 ▶ my $conn = $self->{_connhash}->{read}->{$sock} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"_connhash"}{"read"}{$sock} or next 0 0 0
68
69 0 ▶ $conn->[0]->($conn->[1] ? ($conn->[1], $sock) : $sock, @{$conn}[2..$#{$conn}]);
Branch: 1 branch
BranchTrueFalseTotal
$conn->[1] ? : 0 0 0
70 }
71
72 0 foreach my $sock (@$wr) {
73 0 ▶ my $conn = $self->{_connhash}->{write}->{$sock} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"_connhash"}{"write"}{$sock} or next 0 0 0
74
75 0 ▶ $conn->[0]->($conn->[1] ? ($conn->[1], $sock) : $sock, @{$conn}[2..$#{$conn}]);
Branch: 1 branch
BranchTrueFalseTotal
$conn->[1] ? : 0 0 0
76 }
77 }
78
79 # Cache the latest 500 PONG hosts (host:port combinations)
80 #
81 0 ▶sub _host_cache {
Subroutine _host_cache: not called CC=4 SCAR=30.0
82 0 my $self = shift;
83
84 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
85 0 my $time = time();
86 0 my $count = 500;
87 0 my $cache = $self->{_host_cache};
88 0 my $new = {};
89 0 my $i = 0;
90
91 # Add the specified entries
92 #
93 0 foreach (@_) {
94 0 $cache->{$_} = $time;
95 }
96
97 # Build a new list containing the most recent n elements
98 #
99 0 foreach (grep { $i++ < $count } sort { $cache->{$b} <=> $cache->{$a} } keys %{$cache}) {
100 0 $new->{$_} = $cache->{$_};
101 }
102
103 0 $self->{_host_cache} = $new;
104 }
105
106 0 return keys %{ $self->{_host_cache} };
107 }
108
109 0 ▶sub connections {
Subroutine connections: not called CC=4 SCAR=30.0
Pod undocumented
110 0 my $self = shift;
111 0 my @ret;
112
113 0 foreach my $key (keys %{ $self->{_connhash}->{all} }) {
114 0 my $conn = $self->{_connhash}->{all}->{$key};
115
116 0 ▶ next unless ref $conn eq "Net::Gnutella::Connection";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn eq "Net::Gnutella::Connection" 0 0 0
117 0 ▶ next unless $conn->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->connected 0 0 0
118
119 0 push @ret, $conn;
120 }
121
122 0 return @ret;
123 }
124
125 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my $class = shift;
127 0 my %args = @_;
128
129 my $self = {
130 _connhash => {
131 read => {},
132 write => {},
133 all => {},
134 },
135 _read => new IO::Select,
136 _write => new IO::Select,
137 _attr => {
138 timeout => 10,
139 debug => 0,
140 0 id => [ map { rand(65535**2) } 0..4 ],
141 },
142 _handler => {},
143 _host_cache => {},
144 _msgid_source => {},
145 _qid => 'a',
146 _queue => {},
147 };
148
149 0 bless $self, $class;
150
151 0 foreach my $key (keys %args) {
152 0 my $lkey = lc $key;
153
154 0 $self->$lkey($args{$key});
155 }
156
157 0 return $self;
158 }
159
160 0 ▶sub new_client {
Subroutine new_client: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my $self = shift;
162 0 my $conn = Net::Gnutella::Client->new($self, @_);
163
164 0 ▶ return if $conn->error;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->error 0 0 0
165 0 return $conn;
166 }
167
168 0 ▶sub new_server {
Subroutine new_server: not called CC=2 SCAR=17.9
Pod undocumented
169 0 my $self = shift;
170 0 my $conn = Net::Gnutella::Server->new($self, @_);
171
172 0 ▶ return if $conn->error;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->error 0 0 0
173 0 return $conn;
174 }
175
176 0 ▶sub queue {
Subroutine queue: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my $self = shift;
178
179 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
180 0 return $self->{_queue}->{$_[0]};
181 } else {
182 0 return keys %{ $self->{_queue} };
183 }
184 }
185
186 0 ▶sub schedule {
Subroutine schedule: not called CC=9 SCAR=45.0
Pod undocumented
187 0 my ($self, $when, $coderef, @args) = @_;
188
189 0 ▶ unless ($when =~ /^\d+[dhmst]$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($when =~ /^\d+[dhmst]$/i) 0 0 0
190 0 croak "First argument must be a number";
191 }
192
193 0 ▶ unless (defined $coderef && ref $coderef eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $coderef and ref $coderef eq "CODE") 0 0 0
Condition0%0/3
defined $coderef and ref $coderef eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $coderef and ref $coderef eq "CODE"
defined $coderef ref $coderef eq "CODE"
Truth table0%0/3
A and B
A= defined $coderefB= ref $coderef eq "CODE"
ABresult
0X0
100
111
194 0 croak "Second argument must be a coderef!";
195 }
196
197 0 my $time = time();
198
199 0 ▶ $when *= 24*60*60 if $when =~ s/d$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $when =~ s/d$//i 0 0 0
200 0 ▶ $when *= 60*60 if $when =~ s/h$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $when =~ s/h$//i 0 0 0
201 0 ▶ $when *= 60 if $when =~ s/m$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $when =~ s/m$//i 0 0 0
202 0 $when =~ s/s$//i;
203
204 0 ▶ if ($when =~ s/t$//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($when =~ s/t$//i) { } 0 0 0
205 0 $time = $when;
206 } else {
207 0 $time += $when;
208 }
209
210 0 ▶ $self->{_qid} = 'a' if $self->{_qid} eq 'zzzzzzzz';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_qid"} eq "zzzzzzzz" 0 0 0
211
212 0 my $id = $self->{_qid}++;
213 0 $self->{_queue}->{$id} = [ $time, $coderef, @args ];
214 0 return $id;
215 }
216
217 # Returns the connection a msgid originated from if it
218 # has been seen previously.
219 #
220 0 ▶sub _msgid_source {
Subroutine _msgid_source: not called CC=6 SCAR=37.4
221 0 my ($self, $msgid, $conn) = @_;
222
223 0 ▶ unless ($msgid && ref($msgid) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msgid and ref $msgid eq "ARRAY") 0 0 0
Condition0%0/3
$msgid and ref $msgid eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$msgid and ref $msgid eq "ARRAY"
$msgid ref $msgid eq "ARRAY"
Truth table0%0/3
A and B
A= $msgidB= ref $msgid eq "ARRAY"
ABresult
0X0
100
111
224 0 carp "Invalid message ID: $msgid";
225 }
226
227 0 ▶ if ($conn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn) 0 0 0
228 0 my $i = 0;
229 0 my $count = 5000;
230 0 my $source = $self->{_msgid_source};
231
232 0 $source->{join(":", @$msgid)} = [ $conn, time() ];
233
234 0 foreach (grep { $i++ > $count } sort { $source->{$b}->[1] <=> $source->{$a}->[1] } keys %{$source}) {
235 0 delete $source->{$_};
236 }
237 }
238
239 0 ▶ return unless $self->{_msgid_source}->{join(":", @$msgid)};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_msgid_source"}{join ":", @$msgid} 0 0 0
240 0 return $self->{_msgid_source}->{join(":", @$msgid)}->[0];
241 }
242
243 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my $self = shift;
245
246 0 $self->do_one_loop while 1;
247 }
248
249 0 ▶sub _add_fh {
Subroutine _add_fh: not called CC=5 SCAR=34.0
250 0 my ($self, $fh, $coderef, $flags, $obj, @args) = @_;
251
252 0 ▶ unless (ref $coderef eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $coderef eq "CODE") 0 0 0
253 0 croak "Second argument to ->_add_fh not a coderef";
254 }
255
256 0 ▶ $flags ||= 'r';
Condition0%0/2
$flags ||= "r"
l!l
00
MC/DC0%0/1
$flags ||= "r"
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
257
258 0 ▶ if ($flags =~ /r/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /r/i) 0 0 0
259 0 $self->{_read}->add($fh);
260 0 $self->{_connhash}->{read}->{$fh} = [ $coderef, $obj, @args ];
261 }
262
263 0 ▶ if ($flags =~ /w/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /w/i) 0 0 0
264 0 $self->{_write}->add($fh);
265 0 $self->{_connhash}->{write}->{$fh} = [ $coderef, $obj, @args ];
266 }
267
268 0 $self->{_connhash}->{all}->{$fh} = $obj;
269 }
270
271 0 ▶sub _add_handler {
Subroutine _add_handler: not called CC=9 SCAR=45.0
272 0 my ($self, $event, $coderef, $replace, $hashref, @args) = @_;
273
274 0 ▶ unless (ref $coderef eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $coderef eq "CODE") 0 0 0
275 0 croak "Second argument to ->_add_handler not a coderef";
276 }
277
278 0 my %define = ( replace=>0, before=>1, after=>2 );
279
280 0 ▶ if (not defined $replace) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $replace) { } 0 0 0
elsif ($replace =~ /^\D/) { } 0 0 0
281 0 $replace = 2;
282 } elsif ($replace =~ /^\D/) {
283 0 ▶ $replace = $define{lc $replace} || 2;
Condition0%0/2
$define{lc $replace} || 2
l!l
00
MC/DC0%0/1
$define{lc $replace} || 2
$define{lc $replace}
Truth table0%0/2
A
A= $define{lc $replace}
Aresult
11
00
284 }
285
286 0 ▶ foreach my $ev (ref $event eq "ARRAY" ? @{$event} : $event) {
Branch: 1 branch
BranchTrueFalseTotal
ref $event eq 'ARRAY' ? : 0 0 0
287 0 ▶ if ($ev =~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev =~ /^\d/) 0 0 0
288 0 $ev = Net::Gnutella::Event->trans($ev);
289
290 0 ▶ unless ($ev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ev) 0 0 0
291 0 carp "Unknown event type in ->add_handler";
292 0 return;
293 }
294 }
295
296 0 $hashref->{lc $ev} = [ $coderef, $replace, @args ];
297 }
298 }
299
300 0 ▶sub _handler {
Subroutine _handler: not called CC=19 SCAR=59.4
301 0 my ($self, $event) = @_;
302 0 my $handler;
303
304 0 ▶ unless ($event) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($event) 0 0 0
305 0 confess "I messed up";
306 }
307
308 0 my $type = $event->type;
309 0 my $conn = $event->from;
310 0 ▶ my $default = $conn->can('_default') if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 0 0 0
311
312 0 ▶ if ($conn && exists $conn->{_handler}->{$type}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($conn and exists $conn->{'_handler'}{$type}) { } 0 0 0
elsif (exists $self->{'_handler'}{$type}) { } 0 0 0
elsif ($default) { } 0 0 0
Condition0%0/3
$conn and exists $conn->{"_handler"}{$type}
!ll&&!rl&&r
000
MC/DC0%0/2
$conn and exists $conn->{"_handler"}{$type}
$conn exists $conn->{"_handler"}{$type}
Truth table0%0/3
A and B
A= $connB= exists $conn->{"_handler"}{$type}
ABresult
0X0
100
111
313 0 ▶ printf STDERR " - Connection wide handler exists\n" if $self->debug >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 2 0 0 0
314 0 $handler = $conn->{_handler}->{$type};
315 } elsif (exists $self->{_handler}->{$type}) {
316 0 ▶ printf STDERR " - Global handler exists\n" if $self->debug >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 2 0 0 0
317 0 $handler = $self->{_handler}->{$type};
318 } elsif ($default) {
319 0 ▶ printf STDERR " - Calling default handler on connection\n" if $self->debug >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 2 0 0 0
320 0 return $conn->_default($event);
321 } else {
322 0 ▶ printf STDERR " - Calling default global handler\n" if $self->debug >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 2 0 0 0
323 0 return $self->_default($event);
324 }
325
326 0 my ($coderef, $replace, @args) = @$handler;
327
328 0 ▶ if ($replace == 0) { # REPLACE
Branch: 3 branches
BranchTrueFalseTotal
if ($replace == 0) { } 0 0 0
elsif ($replace == 1) { } 0 0 0
elsif ($replace == 2) { } 0 0 0
329 0 $coderef->($conn, $event, @args);
330 } elsif ($replace == 1) { # BEFORE
331 0 ▶ $coderef->($conn, $event, @args) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$coderef($conn, $event, @args) or return 0 0 0
332
333 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) { } 0 0 0
334 0 $conn->_default($event, @args);
335 } else {
336 0 $self->_default($event, @args);
337 }
338 } elsif ($replace == 2) { # AFTER
339 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) { } 0 0 0
340 0 ▶ $conn->_default($event, @args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$conn->_default($event, @args) or return 0 0 0
341 } else {
342 0 ▶ $self->_default($event, @args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_default($event, @args) or return 0 0 0
343 }
344
345 0 $coderef->($conn, $event, @args);
346 }
347 }
348
349 0 ▶sub _remove_fh {
Subroutine _remove_fh: not called CC=4 SCAR=30.0
350 0 my ($self, $fh, $flags) = @_;
351
352 0 ▶ $flags ||= 'r';
Condition0%0/2
$flags ||= "r"
l!l
00
MC/DC0%0/1
$flags ||= "r"
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
353
354 0 ▶ if ($flags =~ /r/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /r/i) 0 0 0
355 0 $self->{_read}->remove($fh);
356 0 delete $self->{_connhash}->{read}->{$fh};
357 }
358
359 0 ▶ if ($flags =~ /w/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /w/i) 0 0 0
360 0 $self->{_write}->remove($fh);
361 0 delete $self->{_connhash}->{write}->{$fh};
362 }
363 }
364
365 1 1;
« Gnutella/Connection.pm ↑ Summary Gnutella/Packet/Reply.pm »