blib/lib/POE/Component/Client/TCP.pm

filter: Statementstmt 85.9% 141 / 164 Branchbran 72.1% 75 / 104 Conditioncond 50.0% 9 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 74.0% 20 / 27 Podpod 100% 1 / 1 totaltotal 76.9% 250 / 325 CC 61 SCAR 26.5
CC
61
Cov
79%
new
32.3
_default_error
20.0
__ANON__
12.0
CRAP 14.2
« blib/lib/POE/Resource/Clock.pm ↑ Summary blib/lib/POE/Filter/Line.pm »
1 package POE::Component::Client::TCP;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
7  
8 11 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Errno qw(ETIMEDOUT ECONNRESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Explicit use to import the parameter constants;
12 11 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Create the client. This is just a handy way to encapsulate
19 # POE::Session->create(). Because the states are so small, it uses
20 # real inline coderefs.
21  
22 84 ▶sub new {
Subroutine new: called CC=28 SCAR=34.8
Pod documented
23 84 my $type = shift;
24  
25 # Helper so we don't have to type it all day. $mi is a name I call
26 # myself.
27 84 my $mi = $type . '->new()';
28  
29 # If they give us lemons, tell them to make their own damn
30 # lemonade.
31 84 ▶ croak "$mi requires an even number of parameters" if (@_ & 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 2 82 84
32 82 my %param = @_;
33  
34 # Validate what we're given.
35 croak "$mi needs a RemoteAddress parameter"
36 82 ▶ unless exists $param{RemoteAddress};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"RemoteAddress"} 2 80 82
37 croak "$mi needs a RemotePort parameter"
38 80 ▶ unless exists $param{RemotePort};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"RemotePort"} 2 78 80
39  
40 # Extract parameters.
41 78 my $alias = delete $param{Alias};
42 78 my $address = delete $param{RemoteAddress};
43 78 my $port = delete $param{RemotePort};
44 78 my $domain = delete $param{Domain};
45 78 my $bind_address = delete $param{BindAddress};
46 78 my $bind_port = delete $param{BindPort};
47 78 my $ctimeout = delete $param{ConnectTimeout};
48 78 my $args = delete $param{Args};
49 78 my $session_type = delete $param{SessionType};
50 78 my $session_params = delete $param{SessionParams};
51  
52 78 ▶ $args = [] unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 78 0 78
53 78 ▶ croak "Args must be an array reference" unless ref($args) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 78 78
54  
55 78 foreach (
56 qw(
57 PreConnect Connected ConnectError Disconnected ServerInput
58 ServerError ServerFlushed Started
59 ServerHigh ServerLow
60 )
61 ) {
62 croak "$_ must be a coderef" if(
63 780 ▶ defined($param{$_}) and ref($param{$_}) ne 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{$_} and ref $param{$_} ne "CODE" 0 780 780
Condition66%2/3
defined $param{$_} and ref $param{$_} ne "CODE"
!ll&&!rl&&r
3424380
MC/DC0%0/2
defined $param{$_} and ref $param{$_} ne "CODE"
defined $param{$_} ref $param{$_} ne "CODE"
Truth table66%2/3
A and B
A= defined $param{$_}B= ref $param{$_} ne "CODE"
ABresult
0X0
100
111
64 );
65 }
66  
67 78 my $high_mark_level = delete $param{HighMark};
68 78 my $low_mark_level = delete $param{LowMark};
69 78 my $high_event = delete $param{ServerHigh};
70 78 my $low_event = delete $param{ServerLow};
71  
72 # this is ugly, but now its elegant :) grep++
73 78 my $using_watermarks = grep { defined $_ }
74 ($high_mark_level, $low_mark_level, $high_event, $low_event);
75 78 ▶ if ($using_watermarks > 0 and $using_watermarks != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($using_watermarks > 0 and $using_watermarks != 4) 8 70 78
Condition66%2/3
$using_watermarks > 0 and $using_watermarks != 4
!ll&&!rl&&r
7008
MC/DC50%1/2
$using_watermarks > 0 and $using_watermarks != 4
$using_watermarks > 0 $using_watermarks != 4
Truth table66%2/3
A and B
A= $using_watermarks > 0B= $using_watermarks != 4
ABresult
0X0
100
111
76 8 croak "If you use the Mark settings, you must define all four";
77 }
78  
79 70 ▶ $high_event = sub { } unless defined $high_event;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high_event 70 0 70
80 70 ▶ $low_event = sub { } unless defined $low_event;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $low_event 70 0 70
81  
82 70 my $pre_conn_callback = delete $param{PreConnect};
83 70 my $conn_callback = delete $param{Connected};
84 70 my $conn_error_callback = delete $param{ConnectError};
85 70 my $disc_callback = delete $param{Disconnected};
86 70 my $input_callback = delete $param{ServerInput};
87 70 my $error_callback = delete $param{ServerError};
88 70 my $flush_callback = delete $param{ServerFlushed};
89 70 my $start_callback = delete $param{Started};
90 70 my $filter = delete $param{Filter};
91 # TODO should we have ServerInputFilter/ServerOutputFilter like Server-TCP does?
92  
93 # Extra states.
94  
95 70 my $inline_states = delete $param{InlineStates};
96 70 ▶ $inline_states = {} unless defined $inline_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inline_states 68 2 70
97  
98 70 my $package_states = delete $param{PackageStates};
99 70 ▶ $package_states = [] unless defined $package_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package_states 68 2 70
100  
101 70 my $object_states = delete $param{ObjectStates};
102 70 ▶ $object_states = [] unless defined $object_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object_states 68 2 70
103  
104 70 ▶ croak "InlineStates must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inline_states eq "HASH" 2 68 70
105 unless ref($inline_states) eq 'HASH';
106  
107 68 ▶ croak "PackageStates must be a list or array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $package_states eq "ARRAY" 2 66 68
108 unless ref($package_states) eq 'ARRAY';
109  
110 66 ▶ croak "ObjectStates must be a list or array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object_states eq "ARRAY" 2 64 66
111 unless ref($object_states) eq 'ARRAY';
112  
113 # Errors.
114  
115 64 ▶ croak "$mi requires a ServerInput parameter" unless defined $input_callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input_callback 2 62 64
116  
117 62 foreach (sort keys %param) {
118 0 carp "$mi doesn't recognize \"$_\" as a parameter";
119 }
120  
121 # Defaults.
122  
123 62 ▶ $session_type = 'POE::Session' unless defined $session_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session_type 62 0 62
124 62 ▶ if (defined($session_params) && ref($session_params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session_params and ref $session_params) { } 2 60 62
Condition66%2/3
defined $session_params and ref $session_params
!ll&&!rl&&r
6002
MC/DC50%1/2
defined $session_params and ref $session_params
defined $session_params ref $session_params
Truth table66%2/3
A and B
A= defined $session_paramsB= ref $session_params
ABresult
0X0
100
111
125 2 ▶ if (ref($session_params) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $session_params ne "ARRAY") 2 0 2
126 2 croak "SessionParams must be an array reference";
127 }
128 } else {
129 60 $session_params = [ ];
130 }
131  
132 60 ▶ $address = '127.0.0.1' unless defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 0 60 60
133  
134 60 ▶ $conn_error_callback = \&_default_error unless defined $conn_error_callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn_error_callback 18 42 60
135 60 ▶ $error_callback = \&_default_io_error unless defined $error_callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_callback 6 54 60
136  
137 # Spawn the session that makes the connection and then interacts
138 # with what was connected to.
139  
140 return $session_type->create
141 ( @$session_params,
142 inline_states =>
143 60 ▶ { _start => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
144 60 my ($kernel, $heap) = @_[KERNEL, HEAP];
145 60 $heap->{shutdown_on_error} = 1;
146 60 ▶ $kernel->alias_set( $alias ) if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 42 18 60
147 60 $kernel->yield( 'reconnect' );
148 60 ▶ $start_callback and $start_callback->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$start_callback and &$start_callback(@_) 4 56 60
149 },
150  
151 # To quiet ASSERT_STATES.
152 60 ▶ _stop => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 0 ▶ _child => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
154  
155 60 ▶ reconnect => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
156 60 my ($kernel, $heap) = @_[KERNEL, HEAP];
157  
158 60 $heap->{shutdown} = 0;
159 60 $heap->{connected} = 0;
160  
161 # Tentative patch to re-establish the alias upon reconnect.
162 # Necessary because otherwise the alias goes away for good.
163 # Unfortunately, there is a gap where the alias may not be
164 # set, and any events dispatched then will be dropped.
165 60 ▶ $kernel->alias_set( $alias ) if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 42 18 60
166  
167 60 $heap->{server} = POE::Wheel::SocketFactory->new
168 ( RemoteAddress => $address,
169 RemotePort => $port,
170 SocketDomain => $domain,
171 BindAddress => $bind_address,
172 BindPort => $bind_port,
173 SuccessEvent => 'got_connect_success',
174 FailureEvent => 'got_connect_error',
175 );
176 $_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
177 60 ▶ if exists $heap->{ctimeout_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"ctimeout_id"} 0 60 60
178 60 ▶ $heap->{ctimeout_id} = $_[KERNEL]->alarm_set
Branch: 1 branch
BranchTrueFalseTotal
if defined $ctimeout 10 50 60
179 ( got_connect_timeout => time + $ctimeout
180 ) if defined $ctimeout;
181 },
182  
183 0 ▶ connect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
184 0 my ($new_address, $new_port) = @_[ARG0, ARG1];
185 0 ▶ $address = $new_address if defined $new_address;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_address 0 0 0
186 0 ▶ $port = $new_port if defined $new_port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_port 0 0 0
187 0 $_[KERNEL]->yield("reconnect");
188 },
189  
190 58 ▶ got_connect_success => sub {
Subroutine __ANON__: called CC=6 SCAR=18.5
191 58 my ($kernel, $heap, $socket) = @_[KERNEL, HEAP, ARG0];
192  
193 $kernel->alarm_remove( delete $heap->{ctimeout_id} )
194 58 ▶ if exists $heap->{ctimeout_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"ctimeout_id"} 10 48 58
195  
196 # Pre-connected callback.
197 58 ▶ if ($pre_conn_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_conn_callback) 2 56 58
198 2 ▶ unless ($socket = $pre_conn_callback->(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket = &$pre_conn_callback(@_)) 0 2 2
199 0 $heap->{connected} = 0;
200 # TODO - Error callback? Disconnected callback?
201 0 return;
202 }
203 }
204  
205 # Ok to overwrite like this as of 0.13.
206 58 ▶ $_[HEAP]->{server} = POE::Wheel::ReadWrite->new
Branch: 1 branch
BranchTrueFalseTotal
$using_watermarks ? : 0 58 58
207 ( Handle => $socket,
208 Driver => POE::Driver::SysRW->new(),
209 Filter => _get_filter($filter),
210 InputEvent => 'got_server_input',
211 ErrorEvent => 'got_server_error',
212 FlushedEvent => 'got_server_flush',
213 (
214 $using_watermarks
215 ? (
216 HighMark => $high_mark_level,
217 HighEvent => 'got_high',
218 LowMark => $low_mark_level,
219 LowEvent => 'got_low',
220 )
221 : ()
222 )
223 );
224  
225 58 $heap->{connected} = 1;
226 58 ▶ $conn_callback and $conn_callback->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$conn_callback and &$conn_callback(@_) 57 1 58
227 },
228 got_high => $high_event,
229 got_low => $low_event,
230  
231 2 ▶ got_connect_error => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
232 2 my $heap = $_[HEAP];
233 $_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
234 2 ▶ if exists $heap->{ctimeout_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"ctimeout_id"} 0 2 2
235 2 $heap->{connected} = 0;
236 2 $conn_error_callback->(@_);
237 2 delete $heap->{server};
238 },
239  
240 0 ▶ got_connect_timeout => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
241 0 my $heap = $_[HEAP];
242 0 $heap->{connected} = 0;
243 $_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
244 0 ▶ if exists $heap->{ctimeout_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"ctimeout_id"} 0 0 0
245 0 $! = ETIMEDOUT;
246 0 @_[ARG0,ARG1,ARG2] = ('connect', $!+0, $!);
247 0 $conn_error_callback->(@_);
248 0 delete $heap->{server};
249 },
250  
251 18 ▶ got_server_error => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
252 18 $error_callback->(@_);
253 18 ▶ if ($_[HEAP]->{shutdown_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"shutdown_on_error"}) 18 0 18
254 18 $_[KERNEL]->yield("shutdown");
255 18 $_[HEAP]->{got_an_error} = 1;
256 }
257 },
258  
259 58 ▶ got_server_input => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
260 58 my $heap = $_[HEAP];
261 58 ▶ return if $heap->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"shutdown"} 0 58 58
262 58 $input_callback->(@_);
263 },
264  
265 60 ▶ got_server_flush => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
266 60 my $heap = $_[HEAP];
267 60 ▶ $flush_callback and $flush_callback->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$flush_callback and &$flush_callback(@_) 58 2 60
268 60 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 4 56 60
269 4 delete $heap->{server};
270 4 ▶ $disc_callback and $disc_callback->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$disc_callback and &$disc_callback(@_) 4 0 4
271 }
272 },
273  
274 69 ▶ shutdown => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
275 69 my ($kernel, $heap) = @_[KERNEL, HEAP];
276 69 $heap->{shutdown} = 1;
277  
278 $kernel->alarm_remove( delete $heap->{ctimeout_id} )
279 69 ▶ if exists $heap->{ctimeout_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"ctimeout_id"} 0 69 69
280  
281 69 ▶ if ($heap->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'connected'}) { } 58 11 69
282 58 $heap->{connected} = 0;
283 58 ▶ if (defined $heap->{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"server"}) 58 0 58
284 58 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"got_an_error"} or not $heap->{"server"}->get_driver_out_octets) 54 4 58
Condition100%3/3
$heap->{"got_an_error"} or not $heap->{"server"}->get_driver_out_octets
l!l&&r!l&&!r
18364
MC/DC100%2/2
$heap->{"got_an_error"} or not $heap->{"server"}->get_driver_out_octets
$heap->{"got_an_error"} not $heap->{"server"}->get_driver_out_octets
Truth table100%3/3
A or not(B)
A= $heap->{"got_an_error"}B= not $heap->{"server"}->get_driver_out_octets
ABresult
1X1
011
000
285 $heap->{got_an_error} or
286 not $heap->{server}->get_driver_out_octets()
287 ) {
288 54 delete $heap->{server};
289 54 ▶ $disc_callback and $disc_callback->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$disc_callback and &$disc_callback(@_) 49 5 54
290 }
291 }
292 }
293 else {
294 11 delete $heap->{server};
295 }
296  
297 69 ▶ $kernel->alias_remove($alias) if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 41 28 69
298 },
299  
300 # User supplied states.
301 60 %$inline_states,
302 },
303  
304 # User arguments.
305 args => $args,
306  
307 # User supplied states.
308 package_states => $package_states,
309 object_states => $object_states,
310 )->ID;
311 }
312  
313 58 ▶sub _get_filter {
Subroutine _get_filter: called CC=4 SCAR=14.8
314 58 my $filter = shift;
315 58 ▶ if (ref $filter eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $filter eq 'ARRAY') { } 2 56 58
elsif (ref $filter) { } 0 56 56
elsif (not defined $filter) { } 56 0 56
316 2 my @filter_args = @$filter;
317 2 $filter = shift @filter_args;
318 2 return $filter->new(@filter_args);
319 } elsif (ref $filter) {
320 0 return $filter->clone();
321 } elsif (!defined($filter)) {
322 56 return POE::Filter::Line->new();
323 } else {
324 0 return $filter->new();
325 }
326 }
327  
328 # The default error handler logs to STDERR and shuts down the socket.
329  
330 0 ▶sub _default_error {
Subroutine _default_error: not called CC=4 SCAR=30.0
331 0 ▶ unless ($_[ARG0] eq "read" and ($_[ARG1] == 0 or $_[ARG1] == ECONNRESET)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[10] eq "read" and $_[11] == 0 || $_[11] == 104) 0 0 0
Condition0%0/6
$_[10] eq "read" and $_[11] == 0 || $_[11] == 104
!ll&&!rl&&r
000
$_[11] == 0 or $_[11] == 104
l!l&&r!l&&!r
000
MC/DC0%0/3
$_[10] eq "read" and $_[11] == 0 || $_[11] == 104
$_[10] eq "read" $_[11] == 0 $_[11] == 104
Truth table0%0/4
A and B or C
A= $_[10] eq "read"B= $_[11] == 0C= $_[11] == 104
ABCresult
0XX0
1000
11X1
1011
332 0 warn(
333 'Client ', $_[SESSION]->ID, " got $_[ARG0] error $_[ARG1] ($_[ARG2])\n"
334 );
335 }
336 0 delete $_[HEAP]->{server};
337 }
338  
339 0 ▶sub _default_io_error {
Subroutine _default_io_error: not called CC=2 SCAR=17.9
340 0 my ($syscall, $errno, $error) = @_[ARG0..ARG2];
341 0 ▶ $error = "Normal disconnection" unless $errno;
Branch: 1 branch
BranchTrueFalseTotal
unless $errno 0 0 0
342 0 warn('Client ', $_[SESSION]->ID, " got $syscall error $errno ($error)\n");
343 0 $_[KERNEL]->yield("shutdown");
344 }
345  
346 11 1;
347  
348 __END__
« blib/lib/POE/Resource/Clock.pm ↑ Summary blib/lib/POE/Filter/Line.pm »