Sprocket/Client.pm

filter: Statementstmt 65.9% 93 / 141 Branchbran 47.6% 20 / 42 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 82.6% 19 / 23 Podpod 28.5% 4 / 14 totaltotal 59.3% 146 / 246 CC 30 SCAR 16.6
CC
30
Cov
61%
connect
13.4
accept
7.2
remote_connect_error
6.0
CRAP 5.3
« Sprocket/Util/Observable.pm ↑ Summary Sprocket/Common.pm »
1 package Sprocket::Client;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use POE qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 Filter::Stackable
8 Filter::Stream
9 Driver::SysRW
10 Component::Client::DNS
11 4 );
12 4 ▶use Sprocket qw( Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use base qw( Sprocket::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util qw( dualvar );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 $sprocket->register_hook( [qw(
18 sprocket.remote.connection.accept
19 sprocket.remote.connection.reject
20 sprocket.remote.connection.receive
21 sprocket.remote.address.resolved
22 sprocket.remote.wheel.error
23 )] );
24 }
25  
26 4 ▶sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
27 4 my $class = shift;
28
29 4 my $self = $class->SUPER::spawn(
30 $class->SUPER::new(
31 @_,
32 _type => 'remote'
33 ),
34 qw(
35 _startup
36 _stop
37  
38 connect
39 reconnect
40 remote_connect_success
41 remote_connect_timeout
42 remote_connect_error
43 remote_error
44 remote_receive
45 remote_flushed
46  
47 resolved_address
48  
49 accept
50 reject
51 )
52 );
53  
54 4 return $self;
55 }
56  
57 4 ▶sub check_params {
Subroutine check_params: called CC=2 SCAR=7.1
Pod undocumented
58 4 my $self = shift;
59  
60 4 ▶ $self->{name} ||= "Client";
Condition50%1/2
$self->{"name"} ||= "Client"
l!l
40
MC/DC0%0/1
$self->{"name"} ||= "Client"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
61  
62 4 return;
63 }
64  
65 4 ▶sub _startup {
Subroutine _startup: called CC=5 SCAR=17.2
66 4 my ( $self, $kernel, $session ) = @_[ OBJECT, KERNEL, SESSION ];
67
68 # XXX ok to doc
69 0 $session->option( @{$self->{opts}->{client_session_options}} )
70 4 ▶ if ( $self->{opts}->{client_session_options} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"client_session_options"} 0 4 4
71
72 # XXX don't doc yet
73 $kernel->alias_set( $self->{opts}->{client_alias} )
74 4 ▶ if ( $self->{opts}->{client_alias} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"client_alias"} 0 4 4
75
76 # connect to our client list
77 4 foreach ( @{$self->{opts}->{client_list}} ) {
78 4 ▶ $self->connect( ref( $_ ) eq 'ARRAY' ? @$_ : $_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 4 4
79 }
80  
81 4 return;
82 }
83  
84 4 ▶sub _stop {
Subroutine _stop: called CC=1 SCAR=0.0
85 4 my $self = $_[ OBJECT ];
86 4 $self->_log( v => 2, msg => $self->{name}." stopped.");
87 }
88  
89 4 ▶sub remote_connect_success {
Subroutine remote_connect_success: called CC=2 SCAR=7.3
Pod undocumented
90 4 my ( $kernel, $self, $con, $socket ) = @_[ KERNEL, OBJECT, HEAP, ARG0 ];
91
92 4 $con->peer_addr( $con->peer_ip.':'.$con->peer_port );
93
94 4 $self->_log( v => 3, msg => $self->{name}." connected");
95  
96 4 ▶ if ( my $tid = $con->time_out_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tid = $con->time_out_id) 0 4 4
97 0 $kernel->alarm_remove( $tid );
98 0 $con->time_out_id( undef );
99 }
100  
101 4 $con->socket( $socket );
102 4 $self->process_plugins( [ 'remote_accept', $self, $con, $socket ] );
103  
104 4 return;
105 }
106  
107 4 ▶sub accept {
Subroutine accept: called CC=6 SCAR=19.7
Pod undocumented
108 4 my ( $self, $con, $opts ) = @_[ OBJECT, HEAP, ARG0 ];
109
110 4 ▶ $opts = {} unless ( $opts );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts 4 0 4
111  
112 4 ▶ $opts->{block_size} ||= 2048;
Condition50%1/2
$opts->{"block_size"} ||= 2048
l!l
04
MC/DC0%0/1
$opts->{"block_size"} ||= 2048
$opts->{"block_size"}
Truth table50%1/2
A
A= $opts->{"block_size"}
Aresult
11
00
113 # XXX don't document this yet, we need to be able to set
114 # the input and output filters seperately
115 4 ▶ $opts->{filter} ||= POE::Filter::Stackable->new(
Condition33%1/3
$opts->{"filter"} ||= "POE::Filter::Stackable"->new("Filters", ["POE::Filter::Stream"->new])
l!l&&r!l&&!r
040
MC/DC0%0/2
$opts->{"filter"} ||= "POE::Filter::Stackable"->new("Filters", ["POE::Filter::Stream"->new])
$opts->{"filter"} "POE::Filter::Stackable"->new("Filters", ["POE::Filter::Stream"->new])
Truth table33%1/3
A ||= B
A= $opts->{"filter"}B= "POE::Filter::Stackable"->new("Filters", ["POE::Filter::Stream"->new])
ABresult
1X1
011
000
116 Filters => [
117 POE::Filter::Stream->new(),
118 ]
119 );
120 $opts->{time_out} = $self->{opts}->{time_out}
121 4 ▶ unless( defined( $opts->{time_out} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"time_out"} 4 0 4
122  
123 4 my $socket = $con->socket;
124
125 $con->wheel_readwrite(
126 Handle => $socket,
127 Driver => POE::Driver::SysRW->new( BlockSize => $opts->{block_size} ),
128 Filter => $opts->{filter},
129 4 InputEvent => $con->event( 'remote_receive' ),
130 ErrorEvent => $con->event( 'remote_error' ),
131 FlushedEvent => $con->event( 'remote_flushed' ),
132 );
133
134 4 $sprocket->broadcast( 'sprocket.remote.connection.accept', {
135 source => $self,
136 target => $con,
137 } );
138
139 4 $con->socket( undef );
140
141 4 $self->process_plugins( [ 'remote_connected', $self, $con, $socket ] );
142  
143 # nothing took the connection
144 4 ▶ unless ( $con->plugin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($con->plugin) 0 4 4
145 0 $self->_log( v => 2, msg => "No plugin took this connection, Dropping.");
146 0 $con->close();
147 }
148
149 4 return;
150 }
151  
152 0 ▶sub reject {
Subroutine reject: not called CC=1 SCAR=6.9
Pod undocumented
153 0 my ( $self, $con ) = @_[ OBJECT, HEAP ];
154
155 0 $sprocket->broadcast( 'sprocket.remote.connection.reject', {
156 source => $self,
157 target => $con,
158 } );
159
160 # XXX other?
161 0 $con->socket( undef );
162 0 $con->close( 1 );
163
164 0 return;
165 }
166  
167 0 ▶sub remote_connect_error {
Subroutine remote_connect_error: not called CC=2 SCAR=17.9
Pod documented
168 0 my ( $kernel, $self, $con, $operation, $errnum, $errstr ) =
169 @_[ KERNEL, OBJECT, HEAP, ARG0, ARG1, ARG2 ];
170  
171 0 $con->error( dualvar( $errnum, "$operation - $errstr" ) );
172  
173 0 $self->_log( v => 2, msg => $self->{name}." : Error connecting to ".$con->peer_addr
174 ." : $operation error $errnum ($errstr)");
175  
176 0 ▶ if ( my $tid = $con->time_out_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tid = $con->time_out_id) 0 0 0
177 0 $kernel->alarm_remove( $tid );
178 0 $con->time_out_id( undef );
179 }
180  
181 # if ( $con->connected ) {
182 0 $self->process_plugins( [ 'remote_disconnected', $self, $con, @_[ ARG0 .. ARG2 ] ] );
183 # } else {
184 # $self->process_plugins( [ 'remote_connect_error', $self, $con ] );
185 # }
186
187 0 return;
188 }
189  
190 0 ▶sub remote_connect_timeout {
Subroutine remote_connect_timeout: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = $_[ OBJECT ];
192
193 0 $self->_log( v => 2, msg => $self->{name}." : timeout while connecting");
194  
195 0 $self->process_plugins( [ 'remote_connect_error', $self, $_[ HEAP ] ] );
196  
197 0 return;
198 }
199  
200 6 ▶sub remote_receive {
Subroutine remote_receive: called CC=1 SCAR=0.0
Pod documented
201 6 my $self = $_[ OBJECT ];
202  
203 6 $sprocket->broadcast( 'sprocket.remote.connection.receive', {
204 source => $self,
205 target => $_[ HEAP ],
206 data => $_[ ARG0 ],
207 } );
208
209 6 $self->process_plugins( [ 'remote_receive', $self, @_[ HEAP, ARG0 ] ] );
210
211 6 return;
212 }
213  
214 2 ▶sub remote_error {
Subroutine remote_error: called CC=2 SCAR=7.1
Pod undocumented
215 2 my ( $self, $con, $operation, $errnum, $errstr ) =
216 @_[ OBJECT, HEAP, ARG0, ARG1, ARG2 ];
217
218 2 $con->error( dualvar( $errnum, "$operation - $errstr" ) );
219
220 2 ▶ if ( $errnum != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum != 0) 0 2 2
221 0 $self->_log( v => 3, msg => $self->{name}." encountered $operation error $errnum: $errstr");
222 }
223
224 2 $sprocket->broadcast( 'sprocket.remote.wheel.error', {
225 source => $self,
226 operation => $operation,
227 errnum => $errnum,
228 errstr => $errstr,
229 } );
230
231 2 $self->process_plugins( [ 'remote_disconnected', $self, $con, 1, $operation, $errnum, $errstr ] );
232
233 2 return;
234 }
235  
236 5 ▶sub remote_flushed {
Subroutine remote_flushed: called CC=3 SCAR=12.5
Pod undocumented
237 5 my ( $self, $con ) = @_[ OBJECT, HEAP ];
238  
239 # we'll get called again if there are octets out
240 5 ▶ $con->close()
Branch: 1 branch
BranchTrueFalseTotal
if $con->close_on_flush and not $con->get_driver_out_octets 0 5 5
Condition33%1/3
$con->close_on_flush and not $con->get_driver_out_octets
!ll&&!rl&&r
500
MC/DC0%0/2
$con->close_on_flush and not $con->get_driver_out_octets
$con->close_on_flush not $con->get_driver_out_octets
Truth table33%1/3
A and not(B)
A= $con->close_on_flushB= not $con->get_driver_out_octets
ABresult
0X0
100
111
241 if ( $con->close_on_flush && not $con->get_driver_out_octets() );
242
243 5 return;
244 }
245  
246 8 ▶sub connect {
Subroutine connect: called CC=8 SCAR=25.9
Pod documented
247 # must call in this in our session's context
248 8 ▶ unless ( $_[KERNEL] && ref $_[KERNEL] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2] and ref $_[2]) 4 4 8
Condition66%2/3
$_[2] and ref $_[2]
!ll&&!rl&&r
404
MC/DC50%1/2
$_[2] and ref $_[2]
$_[2] ref $_[2]
Truth table66%2/3
A and B
A= $_[2]B= ref $_[2]
ABresult
0X0
100
111
249 4 return $poe_kernel->call( shift->{session_id} => connect => @_ );
250 }
251
252 4 my ( $self, $kernel, $address, $port ) = @_[ OBJECT, KERNEL, ARG0, ARG1 ];
253
254 # support an array ref
255 4 ▶ ( $address, $port ) = @$address if ( ref( $address ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $address eq "ARRAY" 0 4 4
256
257 4 ▶ ( $address, $port ) = ( $address =~ /^([^:]+):(\d+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 4 0 4
258 unless( defined $port );
259  
260 4 ▶ return $self->_log( v => 1, msg => 'Port not defined in call to connect, IGNORED. address: '.$address )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 4 4
261 unless ( defined $port );
262
263 4 my $con;
264  
265 # PoCo DNS
266 # XXX ipv6?!
267 4 ▶ if ( $address !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 4 4
268 0 my $named_ses = $kernel->alias_resolve( 'named' );
269  
270 # no DNS resolver found, load one instead
271 0 ▶ unless ( $named_ses ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($named_ses) 0 0 0
272 # could use the object here, but I don't want
273 # duplicated code, so just use the session reference
274 0 POE::Component::Client::DNS->spawn( Alias => 'named' );
275 0 $named_ses = $kernel->alias_resolve( 'named' );
276 # release ownership of this session
277 #$kernel->detach_child( $named_ses );
278 }
279  
280 # a new unconnected connection
281 0 $con = $self->new_connection(
282 peer_port => $port,
283 peer_hostname => $address,
284 peer_addr => "$address:$port", # temp until resolved
285 );
286  
287 0 $kernel->call( $named_ses => 'resolve' => {
288 host => $address,
289 context => 1,
290 event => $con->event( 'resolved_address' ),
291 });
292  
293 # we will connect after resolving the address
294 0 return $con;
295 } else {
296 4 $con = $self->new_connection(
297 peer_ip => $address,
298 peer_port => $port,
299 peer_addr => "$address:$port",
300 );
301 }
302  
303 4 return $self->reconnect( $con );
304 }
305  
306 0 ▶sub resolved_address {
Subroutine resolved_address: not called CC=2 SCAR=17.9
Pod undocumented
307 0 my ( $self, $con, $response ) = @_[ OBJECT, HEAP, ARG0 ];
308
309 0 my ( $response_obj, $response_err ) = @{$response}{qw( response error )};
310  
311 0 ▶ unless ( defined $response_obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response_obj) 0 0 0
312 0 $self->_log( v => 4, msg => 'resolution of '.$con->peer_hostname.' failed: '.$response_err );
313 0 $self->process_plugins( [ 'remote_connect_error', $self, $con, $response_err, $response_obj ] );
314 0 return;
315 }
316  
317 0 my @addr = map { $_->rdatastr } ( $response_obj->answer );
318 0 my $peer_ip = $addr[ int rand( @addr ) ];
319
320 0 $con->peer_ips( \@addr );
321
322 # pick a random ip
323 0 $self->_log( v => 4, msg => 'resolved '.$con->peer_hostname.' to '.join(',',@addr).' using: '.$peer_ip );
324
325 0 $con->peer_ip( $peer_ip );
326 0 $con->peer_addr( $peer_ip.':'.$con->peer_port );
327  
328 0 $sprocket->broadcast( 'sprocket.remote.address.resolved', {
329 source => $self,
330 addresses => \@addr,
331 response => $response_obj,
332 peer_ip => $peer_ip,
333 } );
334  
335 0 $self->reconnect( $con, 1 );
336  
337 0 return;
338 }
339  
340 8 ▶sub reconnect {
Subroutine reconnect: called CC=5 SCAR=16.5
Pod undocumented
341 8 my ( $self, $con, $noclose );
342 8 ▶ unless ( $_[KERNEL] && ref $_[KERNEL] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2] and ref $_[2]) 4 4 8
Condition66%2/3
$_[2] and ref $_[2]
!ll&&!rl&&r
404
MC/DC50%1/2
$_[2] and ref $_[2]
$_[2] ref $_[2]
Truth table66%2/3
A and B
A= $_[2]B= ref $_[2]
ABresult
0X0
100
111
343 4 ( $self, $con ) = ( shift, shift );
344 4 return $poe_kernel->call( $self->{session_id} => $con->event( 'reconnect' ) => @_ );
345 }
346
347 4 ( $self, $con, $noclose ) = @_[ OBJECT, HEAP, ARG0 ];
348  
349 # XXX include backoff?
350  
351 4 $con->connected( 0 );
352
353 # this would force fused connections to shut each other down
354 # so $noclose is passed during a reconnect call, post address resolve
355 4 ▶ $con->close( 1 ) unless ( $noclose );
Branch: 1 branch
BranchTrueFalseTotal
unless $noclose 4 0 4
356
357 # $con->sf( undef );
358 # $con->wheel( undef );
359  
360 4 ▶ if ( $self->{opts}->{connect_time_out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"connect_time_out"}) 0 4 4
361 $con->time_out_id(
362 $poe_kernel->alarm_set(
363 $con->event( 'remote_connect_timeout' ),
364 time() + $self->{opts}->{connect_time_out}
365 )
366 0 );
367 }
368  
369 $con->socket_factory(
370 4 RemoteAddress => $con->peer_ip,
371 RemotePort => $con->peer_port,
372 SuccessEvent => $con->event( 'remote_connect_success' ),
373 FailureEvent => $con->event( 'remote_connect_error' ),
374 );
375  
376 4 return $con;
377 }
378  
379 1 ▶sub begin_soft_shutdown {
Subroutine begin_soft_shutdown: called CC=3 SCAR=11.0
Pod undocumented
380 1 my $self = $_[ OBJECT ];
381
382 1 $self->_log( v => 2, msg => $self->{name}." is shuting down (soft)");
383  
384 1 foreach ( values %{$self->{heaps}} ) {
385 1 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 1 1
386 1 $self->process_plugins( [ 'remote_shutdown', $self, $_ ] );
387 }
388 }
389  
390 4 1;
391  
392 __END__
« Sprocket/Util/Observable.pm ↑ Summary Sprocket/Common.pm »