IProto/Server/Connection.pm

filter: Statementstmt 26.6% 25 / 94 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 23.5% 4 / 17 Podpod 66.6% 2 / 3 totaltotal 21.5% 31 / 144 CC 16 SCAR 23.6
CC
16
Cov
20%
__ANON__
20.0
__ANON__
20.0
__ANON__
20.0
CRAP 10.6
« IProto/Response.pm ↑ Summary IProto/Cluster/Server.pm »
1 package MR::IProto::Server::Connection;
2  
3 =head1 NAME
4  
5 =head1 DESCRIPTION
6  
7 =cut
8  
9 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use AnyEvent::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 with 'MR::IProto::Role::Debuggable';
14  
15 1 has handler => (
16 is => 'ro',
17 isa => 'CodeRef',
18 required => 1,
19 );
20  
21 1 has on_accept => (
22 is => 'ro',
23 isa => 'CodeRef',
24 );
25  
26 1 has on_close => (
27 is => 'ro',
28 isa => 'CodeRef',
29 );
30  
31 1 has on_error => (
32 is => 'ro',
33 isa => 'CodeRef',
34 );
35  
36 1 has fh => (
37 is => 'ro',
38 isa => 'FileHandle',
39 required => 1,
40 );
41  
42 1 has host => (
43 is => 'ro',
44 isa => 'Str',
45 required => 1,
46 );
47  
48 1 has port => (
49 is => 'ro',
50 isa => 'Int',
51 required => 1,
52 );
53  
54 1 has hostname => (
55 is => 'ro',
56 isa => 'Str',
57 writer => '_hostname',
58 lazy_build => 1,
59 );
60  
61 1 has _handle => (
62 is => 'ro',
63 isa => 'AnyEvent::Handle',
64 lazy_build => 1,
65 );
66  
67 1 has _recv_header => (
68 is => 'ro',
69 isa => 'CodeRef',
70 lazy_build => 1,
71 );
72  
73 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod documented
74 0 my ($self) = @_;
75 0 ▶ $self->_debug(sprintf "Connection accepted") if $self->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 1 0 0 0
76 0 weaken($self);
77 0 ▶ AnyEvent::DNS::reverse_verify $self->host, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
78 0 my ($hostname) = @_;
79 0 ▶ if ($hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname) { } 0 0 0
80 0 $self->_hostname($hostname);
81 0 ▶ $self->_debug(sprintf "%s resolved as %s", $self->host, $hostname) if $self->debug >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 4 0 0 0
82 } else {
83 0 $self->_hostname($self->host);
84 0 $self->_debug(sprintf "Can't resolve %s", $self->host);
85 }
86 0 $self->_handle;
87 0 ▶ $self->on_accept->($self) if $self->on_accept;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_accept 0 0 0
88 0 return;
89 0 };
90 0 return;
91 }
92  
93 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod documented
94 0 my ($self) = @_;
95 0 ▶ $self->_debug(sprintf "Object for connection was destroyed\n") if $self->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 1 0 0 0
96 0 return;
97 }
98  
99 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my ($self) = @_;
101 0 ▶ $self->on_close->($self) if $self->on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_close 0 0 0
102 0 return;
103 }
104  
105 0 ▶sub _build_hostname {
Subroutine _build_hostname: not called CC=1 SCAR=6.9
106 0 my ($self) = @_;
107 0 return $self->host;
108 }
109  
110 0 ▶sub _build__handle {
Subroutine _build__handle: not called CC=1 SCAR=6.9
111 0 my ($self) = @_;
112 0 weaken($self);
113 0 my $peername = join ':', $self->host, $self->port;
114 return AnyEvent::Handle->new(
115 fh => $self->fh,
116 peername => $peername,
117 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 my ($handle) = @_;
119 0 $handle->unshift_read( chunk => 12, $self->_recv_header );
120 0 return;
121 },
122 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
123 0 my ($handle) = @_;
124 0 ▶ $self->_debug("Connection closed by foreign host\n") if $self->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 1 0 0 0
125 0 $handle->destroy();
126 0 ▶ $self->on_close->($self) if $self->on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_close 0 0 0
127 0 return;
128 },
129 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
130 0 my ($handle, $fatal, $message) = @_;
131 0 $handle->destroy();
132 0 ▶ if ($self->on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_error) { } 0 0 0
133 0 ▶ $self->_debug("error: $message\n") if $self->debug >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 1 0 0 0
134 0 $self->on_error->($self, $message);
135 } else {
136 0 $self->_debug("error: $message\n");
137 }
138 0 ▶ $self->on_close->($self) if $self->on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_close 0 0 0
139 0 return;
140 }
141 0 );
142 0 return;
143 }
144  
145 0 ▶sub _build__recv_header {
Subroutine _build__recv_header: not called CC=1 SCAR=6.9
146 0 my ($self) = @_;
147 0 weaken($self);
148 0 my $handler = $self->handler;
149 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
150 0 my ($handle, $data) = @_;
151 0 ▶ $self->_debug_dump('recv header: ', $data) if $self->debug >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 5 0 0 0
152 0 my ($cmd, $length, $sync) = unpack 'L3', $data;
153 $handle->unshift_read(
154 chunk => $length,
155 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
156 0 my ($handle, $data) = @_;
157 0 ▶ $self->_debug_dump('recv payload: ', $data) if $self->debug >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 5 0 0 0
158 0 my $result;
159 0 ▶ if (eval { $result = $handler->($self, $cmd, $data); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $result = &$handler($self, $cmd, $data); 1 } }) { } 0 0 0
160 0 my $header = pack 'L3', $cmd, length $result, $sync;
161 0 ▶ if ($self->debug >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug >= 6) 0 0 0
162 0 $self->_debug_dump('send header: ', $header);
163 0 $self->_debug_dump('send payload: ', $result);
164 }
165 0 $handle->push_write($header . $result);
166 } else {
167 0 warn $@;
168 0 $self->_debug("Failed to handle cmd=$cmd\n");
169 }
170 0 return;
171 }
172 0 );
173 0 return;
174 0 };
175 }
176  
177 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
178 0 my ($self, $msg) = @_;
179 0 $self->debug_cb->( sprintf "%s(%s:%d): %s", $self->hostname, $self->host, $self->port, $msg );
180 0 return;
181 }
182  
183 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 1 __PACKAGE__->meta->make_immutable();
185  
186 1 1;
« IProto/Response.pm ↑ Summary IProto/Cluster/Server.pm »