Gnutella/Server.pm

filter: Statementstmt 30.3% 17 / 56 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 3 totaltotal 26.1% 22 / 84 CC 10 SCAR 22.5
CC
10
Cov
24%
AUTOLOAD
20.0
connections
12.0
_accept
12.0
CRAP 9.5
« Gnutella/Client.pm ↑ Summary Gnutella/Packet/Query.pm »
1 package Net::Gnutella::Server;
2 1 ▶use Net::Gnutella::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw/$VERSION $AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 $VERSION = $VERSION = "0.1";
9
10 # Use AUTOHANDLER to supply generic attribute methods
11 #
12 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
13 0 my $self = shift;
14 0 my $attr = $AUTOLOAD;
15 0 $attr =~ s/.*:://;
16 0 ▶ return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /[^A-Z]/ 0 0 0
17 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
18 0 ▶ $self->{_attr}->{lc $attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
19 0 return $self->{_attr}->{lc $attr};
20 }
21
22 0 ▶sub accept {
Subroutine accept: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my $self = shift;
24
25 0 my $server = IO::Socket::INET->new(
26 Listen => SOMAXCONN,
27 LocalAddr => $self->server,
28 LocalPort => $self->port,
29 Reuse => 1,
30 );
31
32 0 ▶ unless (defined $server) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $server) 0 0 0
33 0 $self->error("Couldn't bind to port: $!");
34 0 return;
35 }
36
37 0 $self->socket($server);
38 0 $self->server($server->sockhost);
39 0 $self->port($server->sockport);
40
41 0 $self->parent->_add_fh($self->socket, $self->can("_accept"), "r", $self);
42 }
43
44 0 ▶sub connections {
Subroutine connections: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my $self = shift;
46 0 my @ret;
47
48 0 foreach my $key (keys %{ $self->{_connhash} }) {
49 0 my $conn = $self->{_connhash}->{$key};
50
51 0 ▶ next unless $conn->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->connected 0 0 0
52
53 0 push @ret, $conn;
54 }
55
56 0 return @ret;
57 }
58
59 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
60 0 my $proto = shift;
61 0 my $parent = shift;
62 0 my %args = @_;
63
64 0 my $self = {
65 _attr => {
66 parent => $parent,
67 debug => $parent->debug,
68 timeout => $parent->timeout,
69 error => '',
70 socket => '',
71 server => undef,
72 port => 6346,
73 allow => 0,
74 },
75 };
76
77 0 bless $self, $proto;
78
79 0 foreach my $key (keys %args) {
80 0 my $lkey = lc $key;
81
82 0 $self->$lkey($args{$key});
83 }
84
85 0 $self->accept;
86
87 0 return $self;
88 }
89
90 0 ▶sub _accept {
Subroutine _accept: not called CC=3 SCAR=24.8
91 0 my $self = shift;
92 0 ▶ my $sock = $self->socket->accept || return;
Condition0%0/2
$self->socket->accept || (return)
l!l
00
MC/DC0%0/1
$self->socket->accept || (return)
$self->socket->accept
Truth table0%0/2
A
A= $self->socket->accept
Aresult
11
00
93
94 0 ▶ printf STDERR "+ Accepted connection from '%s'\n", $sock->peerhost if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
95
96 0 my $conn = Net::Gnutella::Connection->new($self->parent,
97 Debug => $self->debug,
98 Timeout => $self->timeout,
99 Allow => $self->allow,
100 Socket => $sock,
101 Ip => $sock->peerhost,
102 Connected => 2,
103 );
104
105 0 $self->{_connhash}->{$sock} = $conn;
106 }
107
108 1 1;
« Gnutella/Client.pm ↑ Summary Gnutella/Packet/Query.pm »