Ident/Client.pm

filter: Statementstmt 69.5% 57 / 82 Branchbran 55.0% 11 / 20 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 75.0% 12 / 16 Podpod 66.6% 2 / 3 totaltotal 65.9% 87 / 132 CC 18 SCAR 15.9
CC
18
Cov
66%
_cleanup
12.0
ident
7.4
new
7.3
CRAP 4.9
« Ident/Request.pm ↑ Summary Ident/Response.pm »
1 package AnyEvent::Ident::Client;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use AnyEvent::Socket qw( tcp_connect );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Simple asynchronous ident client
10 5 our $VERSION = '0.08'; # VERSION
11  
12  
13 sub new
14 7 ▶{
Subroutine new: called CC=6 SCAR=19.9
Pod undocumented
15 7 my $class = shift;
16 0 ▶ my $args = ref $_[0] eq 'HASH' ? (\%{$_[0]}) : ({@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 7 7
17 7 my $port = $args->{port};
18 7 ▶ $port = 113 unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 1 6 7
19 bless {
20 hostname => $args->{hostname} || '127.0.0.1',
21 port => $port,
22 0 ▶ on_error => $args->{on_error} || sub { carp $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 7 ▶ response_class => $args->{response_class} || 'AnyEvent::Ident::Response',
Condition57%4/7
$args->{'hostname'} || '127.0.0.1'
l!l
70
$args->{'on_error'} || sub { carp($_[0]); }
l!l&&r!l&&!r
070
$args->{'response_class'} || 'AnyEvent::Ident::Response'
l!l
16
MC/DC0%0/1
$args->{'hostname'} || '127.0.0.1'
$args->{'hostname'}
MC/DC0%0/2
$args->{'on_error'} || sub { carp($_[0]); }
$args->{'on_error'} sub { carp($_[0]); }
MC/DC100%1/1
$args->{'response_class'} || 'AnyEvent::Ident::Response'
$args->{'response_class'}
Truth table50%1/2
A
A= $args->{'hostname'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args->{'on_error'}B= sub { carp($_[0]); }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args->{'response_class'}
Aresult
11
00
24 }, $class;
25 }
26  
27  
28 sub ident
29 11 ▶{
Subroutine ident: called CC=6 SCAR=20.0
Pod documented
30 11 my($self, $server_port, $client_port, $cb) = @_;
31
32 11 ▶ unless(eval { $self->{response_class}->can('new') })
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->{"response_class"}->can("new") } }) 0 11 11
33 {
34 0 eval 'use ' . $self->{response_class};
35 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
36 }
37
38 11 my $key = join ':', $server_port, $client_port;
39 11 push @{ $self->{$key} }, $cb;
40 11 ▶ return if @{ $self->{$key} } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{$key};} > 1 0 11 11
41
42 # if handle is defined then the connection is open and we can push
43 # the request right away.
44 11 ▶ if(defined $self->{handle})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"handle"}) 5 6 11
45 {
46 5 $self->{handle}->push_write("$server_port,$client_port\015\012");
47 5 return;
48 }
49
50 # if handle is not defined, but wait is, then we are waiting for
51 # the connection, and we queue up the request
52 6 ▶ if(defined $self->{wait})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"wait"}) 0 6 6
53 {
54 0 push @{ $self->{wait} }, "$server_port,$client_port\015\012";
55 0 return;
56 }
57
58 6 $self->{wait} = [];
59
60 6 ▶ tcp_connect $self->{hostname}, $self->{port}, sub {
Subroutine __ANON__: called CC=3 SCAR=12.1
61 6 my($fh) = @_;
62 6 ▶ return $self->_cleanup->{on_error}->("unable to connect: $!") unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 6 6
63
64 $self->{handle} = AnyEvent::Handle->new(
65 fh => $fh,
66 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my ($hdl, $fatal, $msg) = @_;
68 0 $self->{on_error}->($msg);
69 0 $self->_cleanup;
70 0 $_[0]->destroy;
71 0 delete $self->{handle};
72 },
73 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 $self->_cleanup;
75 0 $self->{handle}->destroy;
76 0 delete $self->{handle};
77 },
78 6 );
79
80 6 $self->{handle}->push_write("$server_port,$client_port\015\012");
81 6 $self->{handle}->push_write($_) for @{ $self->{wait} };
82 6 delete $self->{wait};
83
84 11 ▶ $self->{handle}->on_read(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 11 ▶ $self->{handle}->push_read( line => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
86 11 my($handle, $line) = @_;
87 11 $line =~ s/\015?\012//g;
88 11 my $res = $self->{response_class}->new($line);
89 11 my $key = $res->_key;
90 11 ▶ if(defined $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) 11 0 11
91 {
92 11 $_->($res) for @{ $self->{$key} };
93 11 delete $self->{$key};
94 }
95 11 });
96 6 });
97 6 };
98
99 6 return $self;
100 }
101  
102  
103 sub _cleanup
104 0 ▶{
Subroutine _cleanup: not called CC=3 SCAR=24.8
105 0 my $self = shift;
106 0 foreach my $key (grep /^(\d+):(\d+)$/, keys %$self)
107 {
108 $_->($self->{response_class}->new("$1,$2:ERROR:UNKNOWN-ERROR"))
109 0 for @{ $self->{$key} };
110 0 delete $self->{$key};
111 }
112 0 $self;
113 }
114  
115 sub close
116 1 ▶{
Subroutine close: called CC=2 SCAR=10.9
Pod documented
117 1 my $self = shift;
118 1 ▶ if(defined $self->{handle})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"handle"}) 0 1 1
119 {
120 0 $self->_cleanup;
121 0 $self->{handle}->destroy;
122 0 delete $self->{handle};
123 0 delete $self->{wait};
124 }
125 }
126  
127 sub DESTROY
128 1 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
129 1 shift->close;
130 }
131  
132 5 1;
133  
134 __END__
« Ident/Request.pm ↑ Summary Ident/Response.pm »