Ion/Conn.pm

filter: Statementstmt 94.7% 72 / 76 Branchbran 83.3% 10 / 12 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 80.9% 17 / 21 Podpod 80.0% 8 / 10 totaltotal 85.1% 115 / 135 CC 11 SCAR 8.7
CC
11
Cov
90%
connect
5.2
new
4.0
close
3.0
CRAP 2.4
« Ion/Server.pm ↑ Summary Ion/Util.pm »
1 package Ion::Conn;
2 # ABSTRACT: An Ion TCP socket connection
3 3 $Ion::Conn::VERSION = '0.07_01'; # TRIAL
4  
5 3 ▶$Ion::Conn::VERSION = '0.0701';use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use AnyEvent::Socket qw(tcp_connect);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Coro::Handle qw(unblock);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use overload (
14 3 '<>' => 'readline',
15 '&{}' => 'writer',
16 '>>' => 'encodes',
17 '>>=' => 'encodes',
18 '<<' => 'decodes',
19 '<<=' => 'decodes',
20 fallback => 1,
21 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub new {
24 4 ▶ my ($class, %param) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
25  
26 return bless {
27 port => $param{port},
28 host => $param{host},
29 guard => $param{guard},
30 handle => $param{handle},
31 encoders => $param{encoders} || [],
32 decoders => $param{decoders} || [],
33 4 ▶ connected => $param{handle} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$param{'handle'} ? : 1 3 4
Condition100%4/4
$param{'encoders'} || []
l!l
13
$param{'decoders'} || []
l!l
13
MC/DC100%1/1
$param{'encoders'} || []
$param{'encoders'}
MC/DC100%1/1
$param{'decoders'} || []
$param{'decoders'}
Truth table100%2/2
A
A= $param{'encoders'}
Aresult
11
00
Truth table100%2/2
A
A= $param{'decoders'}
Aresult
11
00
34 }, $class;
35 }
36  
37 sub DESTROY {
38 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
39 1 $self->close;
40 }
41  
42 sub connect {
43 11 ▶ my $self = shift;
Subroutine connect: called CC=5 SCAR=16.5
Pod documented
44  
45 11 ▶ unless ($self->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"connected"}) 3 8 11
46 3 ▶ my $host = $self->{host} || croak 'host is required when handle is not specified';
Condition33%1/3
$self->{'host'} || croak('host is required when handle is not specified')
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{'host'} || croak('host is required when handle is not specified')
$self->{'host'} croak('host is required when handle is not specified')
Truth table33%1/3
A || B
A= $self->{'host'}B= croak('host is required when handle is not specified')
ABresult
1X1
011
000
47 3 ▶ my $port = $self->{port} || croak 'port is required when handle is not specified';
Condition33%1/3
$self->{'port'} || croak('port is required when handle is not specified')
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{'port'} || croak('port is required when handle is not specified')
$self->{'port'} croak('port is required when handle is not specified')
Truth table33%1/3
A || B
A= $self->{'port'}B= croak('port is required when handle is not specified')
ABresult
1X1
011
000
48 3 my $fh;
49  
50 3 my $guard = tcp_connect($host, $port, rouse_cb);
51 3 ($fh, $host, $port) = rouse_wait;
52  
53 1 ▶ croak "connection failed: $!" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 1 1
54 1 $self->{handle} = unblock $fh;
55 1 $self->{guard} = $guard;
56 1 $self->{host} = $host;
57 1 $self->{port} = $port;
58 1 $self->{connected} = 1;
59  
60 1 return 1;
61 }
62  
63 8 return;
64 }
65  
66 0 ▶sub host { $_[0]->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
67 0 ▶sub port { $_[0]->{port} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
68  
69 sub print {
70 4 ▶ my ($self, $msg) = @_;
Subroutine print: called CC=1 SCAR=0.1
Pod documented
71 4 ▶ $msg = reduce{ $b->($a) } $msg, @{$self->{encoders}};
Subroutine __ANON__: not called CC=1 SCAR=0.4
72 4 $self->{handle}->print($msg, $/);
73 }
74  
75 sub readline {
76 5 ▶ my $self = shift;
Subroutine readline: called CC=2 SCAR=7.1
Pod documented
77 5 $self->connect;
78 5 ▶ my $line = $self->{handle}->readline($/) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $self->{"handle"}->readline($/) or return 0 4 4
79 4 chomp $line;
80 4 ▶ reduce{ $b->($a) } $line, @{$self->{decoders}};
Subroutine __ANON__: not called CC=1 SCAR=0.4
81 }
82  
83 sub close {
84 2 ▶ my $self = shift;
Subroutine close: called CC=3 SCAR=11.0
Pod documented
85 2 ▶ $self->{handle}->shutdown if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 1 1 2
86 2 ▶ $self->{handle}->close if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 1 1 2
87 2 undef $self->{handle};
88 2 undef $self->{guard};
89 2 $self->{connected} = 0;
90 2 return 1;
91 }
92  
93 sub writer {
94 5 ▶ my $self = shift;
Subroutine writer: called CC=1 SCAR=0.0
Pod undocumented
95 sub {
96 5 ▶ $self->connect;
Subroutine __ANON__: called CC=1 SCAR=0.0
97 4 $self->print(shift);
98 5 };
99 }
100  
101 sub encodes {
102 2 ▶ my ($self, $encoder) = @_;
Subroutine encodes: called CC=1 SCAR=0.0
Pod documented
103 2 push @{$self->{encoders}}, $encoder;
104 2 return $self;
105 }
106  
107 sub decodes {
108 2 ▶ my ($self, $decoder) = @_;
Subroutine decodes: called CC=1 SCAR=0.0
Pod documented
109 2 push @{$self->{decoders}}, $decoder;
110 2 return $self;
111 }
112  
113 3 1;
114  
115 __END__
« Ion/Server.pm ↑ Summary Ion/Util.pm »