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

filter: Statementstmt 20.2% 17 / 84 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 5 / 15 Podpod 100% 9 / 9 totaltotal 25.0% 31 / 124 CC 9 SCAR 26.3
CC
9
Cov
17%
_server_message
42.0
init
12.0
handle_connected
6.0
CRAP 13.9
« blib/lib/XAS/Lib/Modules/Log.pm ↑ Summary blib/lib/XAS/Lib/Process/Win32.pm »
1 package XAS::Lib::Stomp::POE::Client;
2  
3 1 our $VERSION = '0.04';
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 XAS::Lib::Stomp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XAS::Lib::Stomp::POE::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use XAS::Class
11 1 debug => 0,
12 version => $VERSION,
13 base => 'XAS::Lib::Net::POE::Client',
14 mixins => 'XAS::Lib::Mixins::Keepalive',
15 accessors => 'stomp',
16 vars => {
17 PARAMS => {
18 -host => { optional => 1, default => undef },
19 -port => { optional => 1, default => undef },
20 -alias => { optional => 1, default => 'stomp-client' },
21 -login => { optional => 1, default => 'guest' },
22 -passcode => { optional => 1, default => 'guest' },
23 }
24 }
25 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #use Data::Dumper;
28  
29 # ----------------------------------------------------------------------
30 # Public Events
31 # ----------------------------------------------------------------------
32  
33 # ---------------------------------------------------------------------
34 # Public Methods
35 # ---------------------------------------------------------------------
36  
37 sub session_initialize {
38 0 ▶ my $self = shift;
Subroutine session_initialize: not called CC=1 SCAR=6.9
Pod documented
39  
40 0 my $alias = $self->alias;
41  
42 0 $self->log->debug("$alias: session_initialize()");
43  
44 # public events
45  
46 0 $self->log->debug("$alias: doing public events");
47  
48 0 $poe_kernel->state('handle_noop', $self);
49 0 $poe_kernel->state('handle_error', $self);
50 0 $poe_kernel->state('handle_message', $self);
51 0 $poe_kernel->state('handle_receipt', $self);
52 0 $poe_kernel->state('handle_connected', $self);
53  
54 # walk the chain
55  
56 0 $self->SUPER::session_initialize();
57  
58 0 $self->log->debug("$alias: leaving session_initialize()");
59  
60 }
61  
62 sub session_shutdown {
63 0 ▶ my $self = shift;
Subroutine session_shutdown: not called CC=1 SCAR=6.9
Pod documented
64  
65 0 my $alias = $self->alias;
66 0 my $frame = $self->stomp->disconnect(
67 -receipt => 'disconnecting'
68 );
69  
70 0 $self->log->debug("$alias: entering session_shutdown()");
71  
72 0 $poe_kernel->call($alias, 'write_data', $frame);
73  
74 # walk the chain
75  
76 0 $self->SUPER::session_shutdown();
77  
78 0 $self->log->debug("$alias: leaving session_shutdown()");
79  
80 }
81  
82 # ---------------------------------------------------------------------
83 # Public Events
84 # ---------------------------------------------------------------------
85  
86 sub handle_connection {
87 0 ▶ my ($self) = $_[OBJECT];
Subroutine handle_connection: not called CC=1 SCAR=6.9
Pod documented
88  
89 0 my $alias = $self->alias;
90 0 my $frame = $self->stomp->connect(
91 -login => $self->login,
92 -passcode => $self->passcode
93 );
94  
95 0 $self->log->debug("$alias: entering handle_connection()");
96  
97 0 $poe_kernel->post($alias, 'write_data', $frame);
98  
99 0 $self->log->debug("$alias: leaving handle_connection()");
100  
101 }
102  
103 sub handle_connected {
104 0 ▶ my ($self, $frame) = @_[OBJECT, ARG0];
Subroutine handle_connected: not called CC=2 SCAR=17.9
Pod documented
105  
106 0 my $alias = $self->alias;
107  
108 0 $self->log->debug("$alias: entering handle_connected()");
109  
110 0 ▶ if ($self->tcp_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tcp_keepalive) 0 0 0
111  
112 0 $self->log->info('tcp_keepalive_enabled', $alias);
113  
114 0 $self->init_keepalive();
115 0 $self->enable_keepalive($self->socket);
116  
117 }
118  
119 0 $self->log->info_msg('net_server_connected', $alias, $self->host, $self->port);
120  
121 0 $poe_kernel->post($alias, 'connection_up');
122  
123 0 $self->log->debug("$alias: leaving handle_connected()");
124  
125 }
126  
127 sub handle_message {
128 0 ▶ my ($self, $frame) = @_[OBJECT, ARG0];
Subroutine handle_message: not called CC=1 SCAR=6.9
Pod documented
129  
130 }
131  
132 sub handle_receipt {
133 0 ▶ my ($self, $frame) = @_[OBJECT, ARG0];
Subroutine handle_receipt: not called CC=1 SCAR=6.9
Pod documented
134  
135 }
136  
137 sub handle_error {
138 0 ▶ my ($self, $frame) = @_[OBJECT, ARG0];
Subroutine handle_error: not called CC=1 SCAR=6.9
Pod documented
139  
140 0 my $alias = $self->alias;
141  
142 0 $self->log->error_msg('stomp_errors',
143 $alias,
144 $frame->header->message_id,
145 $frame->header->message,
146 $frame->body
147 );
148  
149 }
150  
151 sub handle_noop {
152 0 ▶ my ($self, $frame) = @_[OBJECT, ARG0];
Subroutine handle_noop: not called CC=1 SCAR=6.9
Pod documented
153  
154 0 my $alias = $self->alias;
155  
156 0 $self->log->debug("$alias: handle_noop()");
157  
158 }
159  
160 # ---------------------------------------------------------------------
161 # Private Events
162 # ---------------------------------------------------------------------
163  
164 sub _server_message {
165 0 ▶ my ($self, $frame, $wheel_id) = @_[OBJECT, ARG0, ARG1];
Subroutine _server_message: not called CC=6 SCAR=37.4
166  
167 0 my $alias = $self->alias;
168  
169 0 $self->log->debug("$alias: entering _server_message()");
170  
171 0 ▶ if ($frame->command eq 'CONNECTED') {
Branch: 5 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
elsif ($frame->command eq 'NOOP') { } 0 0 0
172  
173 0 $self->log->debug("$alias: received a \"CONNECTED\" message");
174 0 $poe_kernel->post($alias, 'handle_connected', $frame);
175  
176 } elsif ($frame->command eq 'MESSAGE') {
177  
178 0 $self->log->debug("$alias: received a \"MESSAGE\" message");
179 0 $poe_kernel->post($alias, 'handle_message', $frame);
180  
181 } elsif ($frame->command eq 'RECEIPT') {
182  
183 0 $self->log->debug("$alias: received a \"RECEIPT\" message");
184 0 $poe_kernel->post($alias, 'handle_receipt', $frame);
185  
186 } elsif ($frame->command eq 'ERROR') {
187  
188 0 $self->log->debug("$alias: received an \"ERROR\" message");
189 0 $poe_kernel->post($alias, 'handle_error', $frame);
190  
191 } elsif ($frame->command eq 'NOOP') {
192  
193 0 $self->log->debug("$alias: received an \"NOOP\" message");
194 0 $poe_kernel->post($alias, 'handle_noop', $frame);
195  
196 } else {
197  
198 0 $self->log->warn_msg('stomp_unknown_type', $alias, $frame->command);
199  
200 }
201  
202 0 $self->log->debug("$alias: leaving _server_message()");
203  
204 }
205  
206 # ---------------------------------------------------------------------
207 # Private Methods
208 # ---------------------------------------------------------------------
209  
210 sub init {
211 0 ▶ my $class = shift;
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
212  
213 0 my $self = $class->SUPER::init(@_);
214  
215 0 ▶ unless (defined($self->{host})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"host"}) 0 0 0
216  
217 0 $self->{host} = $self->env->mqserver;
218  
219 }
220  
221 0 ▶ unless (defined($self->{port})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"port"}) 0 0 0
222  
223 0 $self->{port} = $self->env->mqport;
224  
225 }
226  
227 0 $self->{stomp} = XAS::Lib::Stomp::Utils->new();
228 0 $self->{filter} = XAS::Lib::Stomp::POE::Filter->new();
229  
230 0 return $self;
231  
232 }
233  
234 1 1;
235  
236 __END__
237  
238 =head1 NAME
239  
240 XAS::Lib::Stomp::POE::Client - A STOMP client for the POE Environment
241  
242 =head1 SYNOPSIS
243  
244 This module is a class used to create clients that need to access a
245 message server that communicates with the STOMP protocol. Your program could
246 look as follows:
247  
248 package Client;
249  
250 use POE;
251 use XAS::Class
252 version => '1.0',
253 base => 'XAS::Lib::Stomp::POE::Client',
254 ;
255  
256 package main;
257  
258 use POE;
259 use strict;
260  
261 Client->new(
262 -alias => 'testing',
263 -queue => '/queue/testing',
264 );
265  
266 $poe_kernel->run();
267  
268 exit 0;
269  
270 =head1 DESCRIPTION
271  
272 This module handles the nitty-gritty details of setting up the communications
273 channel to a message queue server. You will need to sub-class this module
274 with your own for it to be useful.
275  
276 When messages are received, specific events are generated. Those events are
277 based on the message type. If you are interested in those events you should
278 override the default behavior for those events. The default behavior is to
279 do nothing. This module inherits from L<XAS::Lib::Net::POE::Client|XAS::Lib::Net::POE::Client>.
280  
281 =head1 METHODS
282  
283 =head2 new
284  
285 This method initializes the class and starts a session to handle the
286 communications channel. It takes the following additional parameters:
287  
288 =over 4
289  
290 =item B<-alias>
291  
292 Sets the alias for this client, defaults to 'stomp-client'.
293  
294 =item B<-host>
295  
296 Sets the host to attach too. defaults to 'localhost'.
297  
298 =item B<-port>
299  
300 Sets the port to use, defaults to 61613.
301  
302 =item B<-login>
303  
304 Sets the login name for this server, defaults to 'guest'.
305  
306 =item B<-passcode>
307  
308 Sets the passcode for this server, defaults to 'guest'.
309  
310 =back
311  
312 =head2 handle_connection(OBJECT)
313  
314 This event is signaled and the corresponding method is called upon initial
315 connection to the message server. I accepts these parameters:
316  
317 =over 4
318  
319 =item B<OBJECT>
320  
321 The current class object.
322  
323 =back
324  
325 =head2 handle_connected(OBJECT, ARG0)
326  
327 This event and corresponding method is called when a "CONNECT" frame is
328 received from the server. It posts the frame to the 'connection_up' event.
329 It accepts these parameters:
330  
331 =over 4
332  
333 =item B<OBJECT>
334  
335 The current class object.
336  
337 =item B<ARG0>
338  
339 The current STOMP frame.
340  
341 =back
342  
343 =head2 handle_message(OBJECT, ARG0)
344  
345 This event and corresponding method is used to process "MESSAGE" frames.
346  
347 It accepts these parameters:
348  
349 =over 4
350  
351 =item B<OBJECT>
352  
353 The current class object.
354  
355 =item B<ARG0>
356  
357 The current STOMP frame.
358  
359 =back
360  
361 Example
362  
363 sub handle_message {
364 my ($self, $frame) = @_[OBJECT,ARG0];
365
366 my $nframe = $self->stomp->ack(
367 -message_id => $frame->header->message_id
368 );
369  
370 $poe_kernel->yield('write_data', $nframe);
371  
372 }
373  
374 This example really doesn't do much other then "ack" the messages that are
375 received.
376  
377 =head2 handle_receipt(OBJECT, ARG0)
378  
379 This event and corresponding method is used to process "RECEIPT" frames.
380 It accepts these parameters:
381  
382 =over 4
383  
384 =item B<OBJECT>
385  
386 The current class object.
387  
388 =item B<ARG0>
389  
390 The current STOMP frame.
391  
392 =back
393  
394 Example
395  
396 sub handle_receipt {
397 my ($self, $frame) = @_[OBJECT,ARG0];
398  
399 my $receipt = $frame->header->receipt;
400  
401 }
402  
403 This example really doesn't do much, and you really don't need to worry about
404 receipts unless you ask for one when you send a frame to the server. So this
405 method could be safely left with the default.
406  
407 =head2 handle_error(OBJECT, ARG0)
408  
409 This event and corresponding method is used to process "ERROR" frames.
410 It accepts these parameters:
411  
412 =over 4
413  
414 =item B<OBJECT>
415  
416 The current class object.
417  
418 =item B<ARG0>
419  
420 The current STOMP frame.
421  
422 =back
423  
424 =head2 handle_noop(OBJECT, ARG0)
425  
426 This event and corresponding method is used to process "NOOP" frames.
427 It accepts these parameters:
428  
429 =over 4
430  
431 =item B<OBJECT>
432  
433 The current class object.
434  
435 =item B<ARG0>
436  
437 The current STOMP frame.
438  
439 =back
440  
441 =head1 ACCESSORS
442  
443 =head2 stomp
444  
445 This returns an object to the internal L<XAS::Lib::Stomp::Utils|XAS::Lib::Stomp::Utils>
446 object. This is very useful for creating STOMP frames.
447  
448 Example
449  
450 $frame = $self->stomp->connect(
451 -login => 'testing',
452 -passcode => 'testing'
453 );
454  
455 $poe_kernel->yield('write_data', $frame);
456  
457 =head1 SEE ALSO
458  
459 =over 4
460  
461 =item L<XAS|XAS>
462  
463 =back
464  
465 For details on the protocol see L<http://stomp.github.io/>.
466  
467 =head1 AUTHOR
468  
469 Kevin L. Esteb, E<lt>=[@kesteb.usE<gt>
470  
471 =head1 COPYRIGHT AND LICENSE
472  
473 Copyright (C) 2014 Kevin L. Esteb
474  
475 This is free software; you can redistribute it and/or modify it under
476 the terms of the Artistic License 2.0. For details, see the full text
477 of the license at http://www.perlfoundation.org/artistic_license_2_0.
478  
479 =cut
« blib/lib/XAS/Lib/Modules/Log.pm ↑ Summary blib/lib/XAS/Lib/Process/Win32.pm »