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

filter: Statementstmt 23.5% 43 / 183 Branchbran 4.7% 2 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 28.2% 11 / 39 Podpod 82.3% 14 / 17 totaltotal 23.8% 70 / 294 CC 25 SCAR 28.1
CC
25
Cov
19%
spawn
72.0
_server_message
30.0
_server_connected
20.0
CRAP 16.6
↑ Summary
1 package POE::Component::Client::Stomp;
2  
3 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Socket ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POE::Filter::Stomp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POE::Component::Client::Stomp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use 5.8.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $TCP_KEEPCNT = 0;
16 1 my $TCP_KEEPIDLE = 0;
17 1 my $TCP_KEEPINTVL = 0;
18  
19 1 ▶if ($^O eq "aix") { # from /usr/include/netinet/tcp.h
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'aix') { } 0 1 1
elsif ($^O eq 'linux') { } 1 0 1
20  
21 0 $TCP_KEEPIDLE = 0x11;
22 0 $TCP_KEEPINTVL = 0x12;
23 0 $TCP_KEEPCNT = 0x13;
24  
25 } elsif ($^O eq "linux"){ # from /usr/include/netinet/tcp.h
26  
27 1 $TCP_KEEPIDLE = 4;
28 1 $TCP_KEEPINTVL = 5;
29 1 $TCP_KEEPCNT = 6;
30  
31 }
32  
33 1 my @errors = qw(0 32 68 73 78 79 110 104 111);
34 1 my @reconnections = qw(60 120 240 480 960 1920 3840);
35  
36 1 our $VERSION = '0.12';
37  
38 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # ---------------------------------------------------------------------
41 # Public Methods
42 # ---------------------------------------------------------------------
43  
44 0 ▶sub spawn {
Subroutine spawn: not called CC=8 SCAR=42.8
Pod documented
45 0 my $package = shift;
46  
47 0 ▶ croak "$package requires an even number of parameters" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
48  
49 0 my %args = @_;
50 0 my $self = bless ({}, $package);
51  
52 0 ▶ $args{Alias} = 'stomp-client' unless defined $args{Alias} and $args{Alias};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"Alias"} and $args{"Alias"} 0 0 0
Condition0%0/3
defined $args{"Alias"} and $args{"Alias"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"Alias"} and $args{"Alias"}
defined $args{"Alias"} $args{"Alias"}
Truth table0%0/3
A and B
A= defined $args{"Alias"}B= $args{"Alias"}
ABresult
0X0
100
111
53 0 ▶ $args{RetryReconnect} = 1 unless defined $args{RetryReconnect};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"RetryReconnect"} 0 0 0
54 0 ▶ $args{EnableKeepAlive} = defined($args{EnableKeepAlive}) ? $args{EnableKeepAlive} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'EnableKeepAlive'} ? : 0 0 0
55 0 ▶ $args{RemoteAddress} = 'localhost' unless defined $args{RemoteAddress};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"RemoteAddress"} 0 0 0
56 0 ▶ $args{RemotePort} = 61613 unless defined $args{RemotePort};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"RemotePort"} 0 0 0
57  
58 0 $self->{CONFIG} = \%args;
59 0 $self->{count} = scalar(@reconnections);
60 0 $self->{stomp} = POE::Component::Client::Stomp::Utils->new();
61 0 $self->{attempts} = 0;
62  
63 0 POE::Session->create(
64 object_states => [
65 $self => {
66 _start => '_session_start',
67 _stop => '_session_stop',
68 server_connect => '_server_connect',
69 server_connected => '_server_connected',
70 server_reconnect => '_server_connect',
71 server_error => '_server_error',
72 server_message => '_server_message',
73 server_connection_failed => '_server_connection_failed',
74 session_interrupt => '_session_interrupt',
75 session_reload => '_session_reload',
76 shutdown => '_session_shutdown',
77 },
78 $self => [ qw(
79 handle_message
80 handle_receipt
81 handle_error
82 handle_connected
83 handle_connection
84 send_data
85 gather_data
86 connection_down
87 connection_up
88 ) ],
89 ],
90 );
91  
92 0 return $self;
93  
94 }
95  
96 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($self, $kernel, $level, @args) = @_;
98  
99 0 warn sprintf("%-5s - %s\n", uc($level), join(' ', @args));
100  
101 }
102  
103 0 ▶sub handle_reload {
Subroutine handle_reload: not called CC=1 SCAR=6.9
Pod documented
104 0 my ($self, $kernel, $session) = @_;
105  
106 0 $kernel->sig_handled();
107  
108 }
109  
110 0 ▶sub handle_shutdown {
Subroutine handle_shutdown: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $kernel, $session) = @_;
112  
113 0 my $params = {};
114 0 my $frame = $self->stomp->disconnect($params);
115  
116 0 $kernel->call($session, 'send_data', $frame);
117  
118 }
119  
120 # ---------------------------------------------------------------------
121 # Public Accessors
122 # ---------------------------------------------------------------------
123  
124 0 ▶sub stomp {
Subroutine stomp: not called CC=1 SCAR=6.9
Pod documented
125 0 my $self = shift;
126  
127 0 return $self->{stomp};
128  
129 }
130  
131 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
132 0 my ($self, $arg) = @_;
133  
134 0 return $self->{CONFIG}->{$arg};
135  
136 }
137  
138 0 ▶sub host {
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my $self = shift;
140  
141 0 return $self->{Host};
142  
143 }
144  
145 0 ▶sub port {
Subroutine port: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my $self = shift;
147  
148 0 return $self->{Port};
149  
150 }
151  
152 # ---------------------------------------------------------------------
153 # Public Events
154 # ---------------------------------------------------------------------
155  
156 0 ▶sub handle_connection {
Subroutine handle_connection: not called CC=1 SCAR=6.9
Pod documented
157 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
158  
159 }
160  
161 0 ▶sub handle_connected {
Subroutine handle_connected: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
163  
164 }
165  
166 0 ▶sub handle_message {
Subroutine handle_message: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
168  
169 }
170  
171 0 ▶sub handle_receipt {
Subroutine handle_receipt: not called CC=1 SCAR=6.9
Pod documented
172 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
173  
174 }
175  
176 0 ▶sub handle_error {
Subroutine handle_error: not called CC=1 SCAR=6.9
Pod documented
177 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
178  
179 }
180  
181 0 ▶sub connection_down {
Subroutine connection_down: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
183  
184 }
185  
186 0 ▶sub connection_up {
Subroutine connection_up: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
188  
189 }
190  
191 0 ▶sub gather_data {
Subroutine gather_data: not called CC=1 SCAR=6.9
Pod documented
192 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
193  
194 }
195  
196 0 ▶sub send_data {
Subroutine send_data: not called CC=2 SCAR=17.9
Pod documented
197 0 my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0];
198  
199 0 ▶ if (defined($self->{Wheel})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Wheel"}) 0 0 0
200  
201 0 $self->{Wheel}->put($frame);
202  
203 }
204  
205 }
206  
207 # ---------------------------------------------------------------------
208 # Private Events
209 # ---------------------------------------------------------------------
210  
211 0 ▶sub _session_start {
Subroutine _session_start: not called CC=2 SCAR=17.9
212 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
213  
214 0 my $alias = $self->config('Alias');
215  
216 0 $self->log($kernel, 'debug', "$alias: _session_start()");
217  
218 0 ▶ if ((my $rc = $kernel->alias_set($alias)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $rc = $kernel->alias_set($alias)) > 0) 0 0 0
219  
220 0 croak 'unable to assign an alias to this session';
221  
222 }
223  
224 # set up signal handling.
225  
226 0 $kernel->sig(HUP => 'session_interrupt');
227 0 $kernel->sig(INT => 'session_interrupt');
228 0 $kernel->sig(TERM => 'session_interrupt');
229 0 $kernel->sig(QUIT => 'session_interrupt');
230  
231 0 $kernel->yield('server_connect');
232  
233 }
234  
235 0 ▶sub _session_stop {
Subroutine _session_stop: not called CC=1 SCAR=6.9
236 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
237  
238 0 delete $self->{Listner};
239 0 delete $self->{Wheel};
240  
241 0 $kernel->alias_remove($self->config('Alias'));
242  
243 }
244  
245 0 ▶sub _session_reload {
Subroutine _session_reload: not called CC=1 SCAR=6.9
246 0 my ($kernel, $self, $session) = @_[KERNEL,OBJECT,SESSION];
247  
248 0 $self->handle_reload($kernel, $session);
249  
250 }
251  
252 0 ▶sub _session_interrupt {
Subroutine _session_interrupt: not called CC=2 SCAR=17.9
253 0 my ($kernel, $self, $session, $signal) = @_[KERNEL,OBJECT,SESSION,ARG0];
254  
255 0 my $alias = $self->config('Alias');
256  
257 0 $self->log($kernel, 'debug', "$alias: _session_interrupt()");
258  
259 0 ▶ if ($signal eq 'HUP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal eq 'HUP') { } 0 0 0
260  
261 0 $self->handle_reload($kernel, $session);
262  
263 } else {
264  
265 0 $self->handle_shutdown($kernel, $session);
266  
267 }
268  
269 }
270  
271 0 ▶sub _session_shutdown {
Subroutine _session_shutdown: not called CC=1 SCAR=6.9
272 0 my ($kernel, $self, $session) = @_[KERNEL, OBJECT, SESSION];
273  
274 0 my $alias = $self->config('Alias');
275  
276 0 $self->log($kernel, 'debug', "$alias: _session_shutdown()");
277  
278 0 $self->handle_shutdown($kernel, $session);
279  
280 }
281  
282 0 ▶sub _server_connect {
Subroutine _server_connect: not called CC=1 SCAR=6.9
283 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
284  
285 0 my $alias = $self->config('Alias');
286  
287 0 $self->log($kernel, 'debug', "$alias: _server_connect()");
288  
289 0 $self->{Listner} = POE::Wheel::SocketFactory->new(
290 RemoteAddress => $self->config('RemoteAddress'),
291 RemotePort => $self->config('RemotePort'),
292 SocketType => SOCK_STREAM,
293 SocketDomain => AF_INET,
294 Reuse => 'no',
295 SocketProtocol => 'tcp',
296 SuccessEvent => 'server_connected',
297 FailureEvent => 'server_connection_failed',
298 );
299  
300 }
301  
302 0 ▶sub _server_connected {
Subroutine _server_connected: not called CC=4 SCAR=30.0
303 0 my ($kernel, $self, $socket, $peeraddr, $peerport, $wheel_id) =
304 @_[KERNEL, OBJECT, ARG0 .. ARG3];
305  
306 0 my $os = $^O;
307 0 my $alias = $self->config('Alias');
308
309 0 $self->log($kernel, 'debug', "$alias: _server_connected()");
310  
311 0 my $wheel = POE::Wheel::ReadWrite->new(
312 Handle => $socket,
313 Filter => POE::Filter::Stomp->new(),
314 InputEvent => 'server_message',
315 ErrorEvent => 'server_error',
316 );
317  
318 0 ▶ if ($self->config('EnableKeepAlive')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config("EnableKeepAlive")) 0 0 0
319  
320 0 $self->log($kernel, 'debug', "$alias: keepalive activated");
321  
322 # turn keepalive on, this should send a keepalive
323 # packet once every 2 hours according to the RFC.
324  
325 0 setsockopt($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
326  
327 0 ▶ if (($os eq 'linux') or ($os eq 'aix')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "linux" or $os eq "aix") 0 0 0
Condition0%0/3
$os eq "linux" or $os eq "aix"
l!l&&r!l&&!r
000
MC/DC0%0/2
$os eq "linux" or $os eq "aix"
$os eq "linux" $os eq "aix"
Truth table0%0/3
A or B
A= $os eq "linux"B= $os eq "aix"
ABresult
1X1
011
000
328  
329 0 $self->log($kernel, 'debug', "$alias: adjusting keepalive activity");
330  
331 # adjust from system defaults, all values are in seconds.
332 # so this does the following:
333 # every 15 minutes send upto 3 packets at 5 second intervals
334 # if no reply, the connection is down.
335  
336 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPIDLE, 900); # 15 minutes
337 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPINTVL, 5); #
338 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPCNT, 3); #
339  
340 }
341  
342 }
343  
344 0 my $host = gethostbyaddr($peeraddr, AF_INET);
345  
346 0 $self->{attempts} = 0;
347 0 $self->{Wheel} = $wheel;
348 0 $self->{Host} = $host;
349 0 $self->{Port} = $peerport;
350  
351 0 $kernel->yield('handle_connection');
352  
353 }
354  
355 0 ▶sub _server_connection_failed {
Subroutine _server_connection_failed: not called CC=3 SCAR=24.8
356 0 my ($kernel, $self, $operation, $errnum, $errstr, $wheel_id) =
357 @_[KERNEL, OBJECT, ARG0 .. ARG3];
358  
359 0 my $alias = $self->config('Alias');
360  
361 0 $self->log($kernel, 'debug', "$alias: _server_connection_failed()");
362 0 $self->log($kernel, 'error', "$alias: operation: $operation; reason: $errnum - $errstr");
363  
364 0 delete $self->{Listner};
365 0 delete $self->{Wheel};
366  
367 0 foreach my $error (@errors) {
368  
369 0 ▶ $self->_reconnect($kernel) if ($errnum == $error);
Branch: 1 branch
BranchTrueFalseTotal
if $errnum == $error 0 0 0
370  
371 }
372  
373 }
374  
375 0 ▶sub _server_error {
Subroutine _server_error: not called CC=3 SCAR=24.8
376 0 my ($kernel, $self, $operation, $errnum, $errstr, $wheel_id) =
377 @_[KERNEL, OBJECT, ARG0 .. ARG3];
378  
379 0 my $alias = $self->config('Alias');
380  
381 0 $self->log($kernel, 'debug', "$alias: _server_error()");
382 0 $self->log($kernel, 'error', "$alias: operation: $operation; reason: $errnum - $errstr");
383  
384 0 delete $self->{Listner};
385 0 delete $self->{Wheel};
386  
387 0 $kernel->yield('connection_down');
388  
389 0 foreach my $error (@errors) {
390  
391 0 ▶ $self->_reconnect($kernel) if ($errnum == $error);
Branch: 1 branch
BranchTrueFalseTotal
if $errnum == $error 0 0 0
392  
393 }
394  
395 }
396  
397 0 ▶sub _server_message {
Subroutine _server_message: not called CC=5 SCAR=34.0
398 0 my ($kernel, $self, $frame, $wheel_id) = @_[KERNEL, OBJECT, ARG0, ARG1];
399  
400 0 my $alias = $self->config('Alias');
401  
402 0 $self->log($kernel, 'debug' , "$alias: _server_message()");
403  
404 0 ▶ if ($frame->command eq 'CONNECTED') {
Branch: 4 branches
BranchTrueFalseTotal
if ($frame->command eq 'CONNECTED') { } 0 0 0
elsif ($frame->command eq 'MESSAGE') { } 0 0 0
elsif ($frame->command eq 'RECEIPT') { } 0 0 0
elsif ($frame->command eq 'ERROR') { } 0 0 0
405  
406 0 $self->log($kernel, 'debug' , "$alias: received a \"CONNECTED\" message");
407 0 $kernel->yield('handle_connected', $frame);
408  
409 } elsif ($frame->command eq 'MESSAGE') {
410  
411 0 $self->log($kernel, 'debug' , "$alias: received a \"MESSAGE\" message");
412 0 $kernel->yield('handle_message', $frame);
413  
414 } elsif ($frame->command eq 'RECEIPT') {
415  
416 0 $self->log($kernel, 'debug' , "$alias: received a \"RECEIPT\" message");
417 0 $kernel->yield('handle_receipt', $frame);
418  
419 } elsif ($frame->command eq 'ERROR') {
420  
421 0 $self->log($kernel, 'debug' , "$alias: received an \"ERROR\" message");
422 0 $kernel->yield('handle_error', $frame);
423  
424 } else {
425  
426 0 $self->log($kernel, 'warn', "$alias: unknown message type: $frame->command");
427  
428 }
429  
430 }
431  
432 # ---------------------------------------------------------------------
433 # Private Methods
434 # ---------------------------------------------------------------------
435  
436 0 ▶sub _reconnect {
Subroutine _reconnect: not called CC=4 SCAR=30.0
437 0 my ($self, $kernel) = @_;
438  
439 0 my $retry;
440 0 my $alias = $self->config('Alias');
441  
442 0 $self->log($kernel, 'debug', "$alias: attempts: $self->{attempts}, count: $self->{count}");
443  
444 0 ▶ if ($self->{attempts} < $self->{count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attempts'} < $self->{'count'}) { } 0 0 0
445  
446 0 my $delay = $reconnections[$self->{attempts}];
447 0 $self->log($kernel, 'warn', "$alias: attempting reconnection: $self->{attempts}, waiting: $delay seconds");
448 0 $self->{attempts}++;
449 0 $kernel->delay('server_reconnect', $delay);
450  
451 } else {
452  
453 0 ▶ $retry = $self->config('RetryReconnect') || 0;
Condition0%0/2
$self->config('RetryReconnect') || 0
l!l
00
MC/DC0%0/1
$self->config('RetryReconnect') || 0
$self->config('RetryReconnect')
Truth table0%0/2
A
A= $self->config('RetryReconnect')
Aresult
11
00
454  
455 0 ▶ if ($retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry) { } 0 0 0
456  
457 0 $self->log($kernel, 'warn', "$alias: cycling reconnection attempts, but not shutting down...");
458 0 $self->{attempts} = 0;
459 0 $kernel->yield('server_reconnect');
460  
461 } else {
462  
463 0 $self->log($kernel, 'warn', "$alias: shutting down, to many reconnection attempts");
464 0 $kernel->yield('shutdown');
465  
466 }
467  
468 }
469  
470 }
471  
472 1 1;
473  
474 __END__
↑ Summary