blib/lib/XAS/Lib/Net/POE/Client.pm

filter: Statementstmt 18.6% 28 / 150 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 32.0% 8 / 25 Podpod 100% 11 / 11 totaltotal 23.3% 47 / 201 CC 10 SCAR 18.1
CC
10
Cov
17%
_server_reconnect
20.0
_server_connection_failed
12.0
_server_error
12.0
CRAP 6.1
« blib/lib/XAS/Lib/Stomp/POE/Filter.pm ↑ Summary blib/lib/XAS/Base.pm »
1 package XAS::Lib::Net::POE::Client;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Socket ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Errno ':POSIX';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use XAS::Class
14 1 debug => 0,
15 version => $VERSION,
16 base => 'XAS::Lib::POE::Service',
17 mixin => 'XAS::Lib::Mixins::Keepalive',
18 accessors => 'wheel host port listener socket',
19 utils => 'dotid',
20 vars => {
21 PARAMS => {
22 -port => 1,
23 -retry_reconnect => { optional => 1, default => 1 },
24 -tcp_keepalive => { optional => 1, default => 0 },
25 -filter => { optional => 1, default => undef },
26 -alias => { optional => 1, default => 'client' },
27 -eol => { optional => 1, default => "\012\015" },
28 -host => { optional => 1, default => 'localhost'},
29 }
30 }
31 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our @ERRORS = (0, EPIPE, ETIMEDOUT, ECONNRESET, ECONNREFUSED, ENETUNREACH, ENETDOWN, ENETRESET);
34 1 our @RECONNECTIONS = (60, 120, 240, 480, 960, 1920, 3840);
35  
36 #use Data::Dumper;
37  
38 # ----------------------------------------------------------------------
39 # Public Events
40 # ----------------------------------------------------------------------
41  
42 # ---------------------------------------------------------------------
43 # Public Methods
44 # ---------------------------------------------------------------------
45  
46 sub session_initialize {
47 0 ▶ my $self = shift;
Subroutine session_initialize: not called CC=1 SCAR=6.9
Pod documented
48  
49 0 my $alias = $self->alias;
50  
51 0 $self->log->debug("$alias: entering session_initialize()");
52  
53 # private events
54  
55 0 $self->log->debug("$alias: doing private events");
56  
57 # private events
58  
59 0 $poe_kernel->state('server_error', $self, '_server_error');
60 0 $poe_kernel->state('server_message', $self, '_server_message');
61 0 $poe_kernel->state('server_connect', $self, '_server_connect');
62 0 $poe_kernel->state('server_connected', $self, '_server_connected');
63 0 $poe_kernel->state('server_reconnect', $self, '_server_reconnect');
64 0 $poe_kernel->state('server_connection_failed', $self, '_server_connection_failed');
65  
66 # public events
67  
68 0 $self->log->debug("$alias: doing public events");
69  
70 0 $poe_kernel->state('read_data', $self);
71 0 $poe_kernel->state('write_data', $self);
72 0 $poe_kernel->state('connection_up', $self);
73 0 $poe_kernel->state('connection_down', $self);
74 0 $poe_kernel->state('handle_connection', $self);
75  
76 # walk the chain
77  
78 0 $self->SUPER::session_initialize();
79  
80 0 $self->log->debug("$alias: leaving session_initialize()");
81  
82 }
83  
84 sub session_startup {
85 0 ▶ my $self = shift;
Subroutine session_startup: not called CC=1 SCAR=6.9
Pod documented
86  
87 0 my $alias = $self->alias;
88  
89 0 $self->log->debug("$alias: entering session_startup");
90  
91 0 $poe_kernel->post($alias, 'server_connect');
92  
93 # walk the chain
94  
95 0 $self->SUPER::session_startup();
96  
97 0 $self->log->debug("$alias: leaving session_startup");
98  
99 }
100  
101 sub session_shutdown {
102 0 ▶ my $self = shift;
Subroutine session_shutdown: not called CC=1 SCAR=6.9
Pod documented
103
104 0 my $alias = $self->alias;
105  
106 0 $self->log->debug("$alias: entering session_shutdown");
107  
108 0 $self->{socket} = undef;
109 0 $self->{wheel} = undef;
110 0 $self->{listener} = undef;
111  
112 # walk the chain
113  
114 0 $self->SUPER::session_shutdown();
115  
116 0 $self->log->debug("$alias: leaving session_shutdown");
117
118 }
119  
120 sub session_pause {
121 0 ▶ my ($self) = $_[OBJECT];
Subroutine session_pause: not called CC=1 SCAR=6.9
Pod documented
122  
123 0 my $alias = $self->alias;
124  
125 0 $self->log->debug("$alias: entering session_pause");
126  
127 0 $poe_kernel->call($alias, 'connection_down');
128  
129 # walk the chain
130  
131 0 $self->SUPER::session_pause();
132  
133 0 $self->log->debug("$alias: leaving session_pause");
134  
135 }
136  
137 sub session_resume {
138 0 ▶ my ($self) = $_[OBJECT];
Subroutine session_resume: not called CC=1 SCAR=6.9
Pod documented
139  
140 0 my $alias = $self->alias;
141  
142 0 $self->log->debug("$alias: entering session_resume");
143  
144 0 $poe_kernel->call($alias, 'connection_up');
145  
146 # walk the chain
147  
148 0 $self->SUPER::session_resume();
149  
150 0 $self->log->debug("$alias: leaving session_resume");
151  
152 }
153  
154 # ---------------------------------------------------------------------
155 # Public Events
156 # ---------------------------------------------------------------------
157  
158 sub handle_connection {
159 0 ▶ my ($self) = $_[OBJECT];
Subroutine handle_connection: not called CC=1 SCAR=6.9
Pod documented
160  
161 }
162  
163 sub connection_down {
164 0 ▶ my ($self) = $_[OBJECT];
Subroutine connection_down: not called CC=1 SCAR=6.9
Pod documented
165  
166 }
167  
168 sub connection_up {
169 0 ▶ my ($self) = $_[OBJECT];
Subroutine connection_up: not called CC=1 SCAR=6.9
Pod documented
170  
171 }
172  
173 sub read_data {
174 0 ▶ my ($self, $data) = @_[OBJECT, ARG0];
Subroutine read_data: not called CC=1 SCAR=6.9
Pod documented
175  
176 0 my $alias = $self->alias;
177  
178 0 $poe_kernel->post($alias, 'write_data', $data);
179  
180 }
181  
182 sub write_data {
183 0 ▶ my ($self, $data) = @_[OBJECT, ARG0];
Subroutine write_data: not called CC=2 SCAR=17.9
Pod documented
184  
185 0 my @packet;
186 0 my $alias = $self->alias;
187  
188 0 ▶ if (my $wheel = $self->wheel) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wheel = $self->wheel) { } 0 0 0
189  
190 0 push(@packet, $data);
191 0 $wheel->put(@packet);
192  
193 } else {
194  
195 0 $self->throw_msg(
196 dotid($self->class) . '.write_data.nowheel',
197 'net_server_nowheel',
198 $alias
199 );
200  
201 }
202  
203 }
204  
205 # ---------------------------------------------------------------------
206 # Private Events
207 # ---------------------------------------------------------------------
208  
209 sub _server_message {
210 0 ▶ my ($self, $data, $wheel_id) = @_[OBJECT, ARG0, ARG1];
Subroutine _server_message: not called CC=1 SCAR=6.9
211  
212 0 my $alias = $self->alias;
213  
214 0 $self->log->debug("$alias: _server_message()");
215  
216 0 $poe_kernel->post($alias, 'read_data', $data);
217  
218 }
219  
220 sub _server_connected {
221 0 ▶ my ($self, $socket, $peeraddr, $peerport, $wheel_id) = @_[OBJECT,ARG0..ARG3];
Subroutine _server_connected: not called CC=1 SCAR=6.9
222  
223 0 my $alias = $self->alias;
224  
225 0 $self->log->debug("$alias: _server_connected()");
226  
227 0 my $wheel = POE::Wheel::ReadWrite->new(
228 Handle => $socket,
229 Filter => $self->filter,
230 InputEvent => 'server_message',
231 ErrorEvent => 'server_error',
232 );
233  
234 0 my $host = gethostbyaddr($peeraddr, AF_INET);
235  
236 0 $self->{host} = $host;
237 0 $self->{port} = $peerport;
238 0 $self->{wheel} = $wheel;
239 0 $self->{socket} = $socket;
240 0 $self->{attempts} = 0;
241  
242 0 $poe_kernel->post($alias, 'handle_connection');
243  
244 }
245  
246 sub _server_connect {
247 0 ▶ my ($self) = $_[OBJECT];
Subroutine _server_connect: not called CC=1 SCAR=6.9
248  
249 0 my $alias = $self->alias;
250  
251 0 $self->log->debug("$alias: _server_connect()");
252  
253 0 $self->{listner} = POE::Wheel::SocketFactory->new(
254 RemoteAddress => $self->host,
255 RemotePort => $self->port,
256 SocketType => SOCK_STREAM,
257 SocketDomain => AF_INET,
258 Reuse => 'no',
259 SocketProtocol => 'tcp',
260 SuccessEvent => 'server_connected',
261 FailureEvent => 'server_connection_failed',
262 );
263  
264 }
265  
266 sub _server_connection_failed {
267 0 ▶ my ($self, $operation, $errnum, $errstr, $wheel_id) = @_[OBJECT,ARG0..ARG3];
Subroutine _server_connection_failed: not called CC=3 SCAR=24.8
268  
269 0 my $alias = $self->alias;
270  
271 0 $self->log->debug("$alias: _server_connection_failed()");
272 0 $self->log->error_msg('net_server_connection_failed', $alias, $operation, $errnum, $errstr);
273  
274 0 delete $self->{socket};
275 0 delete $self->{listner};
276 0 delete $self->{wheel};
277  
278 0 foreach my $error (@ERRORS) {
279  
280 0 ▶ if ($errnum == $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == $error) 0 0 0
281  
282 0 $poe_kernel->post($alias, 'server_reconnect');
283 0 last;
284  
285 }
286  
287 }
288  
289 }
290  
291 sub _server_error {
292 0 ▶ my ($self, $operation, $errnum, $errstr, $wheel_id) = @_[OBJECT,ARG0..ARG3];
Subroutine _server_error: not called CC=3 SCAR=24.8
293  
294 0 my $alias = $self->alias;
295  
296 0 $self->log->debug("$alias: _server_error()");
297 0 $self->log->error_msg('net_server_error', $alias, $operation, $errnum, $errstr);
298  
299 0 delete $self->{socket};
300 0 delete $self->{listner};
301 0 delete $self->{wheel};
302  
303 0 $poe_kernel->post($alias, 'connection_down');
304  
305 0 foreach my $error (@ERRORS) {
306  
307 0 ▶ if ($errnum == $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == $error) 0 0 0
308  
309 0 $poe_kernel->post($alias, 'server_reconnect');
310 0 last;
311  
312 }
313  
314 }
315  
316 }
317  
318 sub _server_reconnect {
319 0 ▶ my ($self) = $_[OBJECT];
Subroutine _server_reconnect: not called CC=4 SCAR=30.0
320  
321 0 my $retry;
322 0 my $alias = $self->alias;
323  
324 0 $self->log->warn_msg('net_server_reconnect', $alias, $self->{attempts}, $self->{count});
325  
326 0 ▶ if ($self->{attempts} < $self->{count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attempts'} < $self->{'count'}) { } 0 0 0
327  
328 0 my $delay = $RECONNECTIONS[$self->{attempts}];
329 0 $self->log->warn_msg('net_server_attempts', $alias, $self->{attempts}, $delay);
330 0 $self->{attempts} += 1;
331 0 $poe_kernel->delay('server_connect', $delay);
332  
333 } else {
334  
335 0 ▶ $retry = $self->retry_reconnect || 0;
Condition0%0/2
$self->retry_reconnect || 0
l!l
00
MC/DC0%0/1
$self->retry_reconnect || 0
$self->retry_reconnect
Truth table0%0/2
A
A= $self->retry_reconnect
Aresult
11
00
336  
337 0 ▶ if ($retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry) { } 0 0 0
338  
339 0 $self->log->warn_msg('net_server_recycle', $alias);
340 0 $self->{attempts} = 0;
341 0 $poe_kernel->post($alias, 'server_connect');
342  
343 } else {
344  
345 0 $self->log->warn_msg('net_server_shutdown', $alias);
346 0 $poe_kernel->post($alias, 'session_shutdown');
347  
348 }
349  
350 }
351  
352 }
353  
354 # ---------------------------------------------------------------------
355 # Private Methods
356 # ---------------------------------------------------------------------
357  
358 sub init {
359 0 ▶ my $class = shift;
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
360  
361 0 my $self = $class->SUPER::init(@_);
362  
363 0 $self->{attempts} = 0;
364 0 $self->{count} = scalar(@RECONNECTIONS);
365  
366 0 ▶ unless (defined($self->{filter})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"filter"}) 0 0 0
367  
368 0 $self->{filter} = POE::Filter::Line->new(
369 InputLiteral => $self->eol,
370 OutputLiteral => $self->eol,
371 );
372  
373 }
374  
375 0 return $self;
376  
377 }
378  
379 1 1;
380  
381 __END__
382  
383 =head1 NAME
384  
385 XAS::Lib::Net::POE::Client - An asynchronous network client based on POE
386  
387 =head1 SYNOPSIS
388  
389 This module is a class used to create network clients.
390  
391 package Client;
392  
393 use POE;
394 use XAS::Class
395 version => '1.0',
396 base => 'XAS::Lib::Net::POE::Client'
397 ;
398  
399 sub handle_connection {
400 my ($self) = $_[OBJECT];
401  
402 my $packet = "hello!";
403  
404 $poe_kernel->yield('write_data', $packet);
405  
406 }
407  
408 =head1 DESCRIPTION
409  
410 This module handles the nitty-gritty details of setting up the communications
411 channel to a server. You will need to sub-class this module with your own for
412 it to be useful.
413  
414 An attempt to maintain that channel will be made when/if that server should
415 happen to disappear off the network. There is nothing more unpleasant then
416 having to go around to dozens of servers and restarting processes.
417  
418 The following methods are responding to POE events and use the POE argument
419 passing conventions.
420  
421 =head1 METHODS
422  
423 =head2 new
424  
425 This method initializes the class and starts a session to handle the
426 communications channel. It takes the following parameters:
427  
428 =over 4
429  
430 =item B<-alias>
431  
432 The session alias, defaults to 'client'.
433  
434 =item B<-host>
435  
436 The servers host name, defaults to 'localhost'.
437  
438 =item B<-port>
439  
440 The servers port number.
441  
442 =item B<-retry_count>
443  
444 Wither to attempt reconnections after they run out. Defaults to true.
445  
446 =item B<-tcp_keepalive>
447  
448 For those pesky firewalls, defaults to false.
449  
450 =back
451  
452 =head2 read_data(OBJECT, ARG0)
453  
454 This event is triggered when data is received for the server. It accepts
455 these parameters:
456  
457 =over 4
458  
459 =item B<OBJECT>
460  
461 The current class object.
462  
463 =item B<ARG0>
464  
465 The data that has been read.
466  
467 =back
468  
469 =head2 write_data(OBJECT, ARG0)
470  
471 You use this event to send data to the server. It accepts
472 these parameters:
473  
474 =over 4
475  
476 =item B<OBJECT>
477  
478 The current class object.
479  
480 =item B<ARGO>
481  
482 The data to write out.
483  
484 =back
485  
486 =head2 handle_connection(OBJECT)
487  
488 This event is triggered upon initial connection to the server. It accepts
489 these parameters:
490  
491 =over 4
492  
493 =item B<OBJECT>
494  
495 The current class object.
496  
497 =back
498  
499 =head2 connection_down(OBJECT)
500  
501 This event is triggered to allow you to be notified if the connection to
502 the server is currently down. It accepts
503 these parameters:
504  
505 =over 4
506  
507 =item B<OBJECT>
508  
509 The current class object.
510  
511 =back
512  
513 =head2 connection_up(OBJECT)
514  
515 This event is triggered to allow you to be notified when the connection
516 to the server is restored. It accepts
517 these parameters:
518  
519 =over 4
520  
521 =item B<OBJECT>
522  
523 The current class object.
524  
525 =back
526  
527 =head1 VARIABLES
528  
529 The following class variables are available if you want to adjust them.
530  
531 =over 4
532  
533 =item B<ERRORS>
534  
535 An array of POSIX error codes.
536  
537 =item B<RECONNECTIONS>
538  
539 An array of seconds to wait for the next reconnect attempt.
540  
541 =back
542  
543 =head1 SEE ALSO
544  
545 =over 4
546  
547 =item L<XAS|XAS>
548  
549 =back
550  
551 =head1 AUTHOR
552  
553 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
554  
555 =head1 COPYRIGHT AND LICENSE
556  
557 Copyright (C) 2014 Kevin L. Esteb
558  
559 This is free software; you can redistribute it and/or modify it under
560 the terms of the Artistic License 2.0. For details, see the full text
561 of the license at http://www.perlfoundation.org/artistic_license_2_0.
562  
563 =cut
« blib/lib/XAS/Lib/Stomp/POE/Filter.pm ↑ Summary blib/lib/XAS/Base.pm »