blib/lib/XAS/Lib/Stomp/Frame.pm

filter: Statementstmt 23.0% 29 / 126 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 42.1% 8 / 19 Podpod 100% 2 / 2 totaltotal 21.2% 39 / 184 CC 20 SCAR 30.4
CC
20
Cov
18%
as_string
56.0
_decode_headers
42.0
init
20.0
CRAP 20.8
« blib/lib/XAS/Lib/Mixins/JSON/Client.pm ↑ Summary blib/lib/XAS/Lib/Mixins/JSON/Server.pm »
1 package XAS::Lib::Stomp::Frame;
2  
3 1 our $VERSION = '0.02';
4  
5 use XAS::Class
6 1 debug => 0,
7 version => $VERSION,
8 base => 'XAS::Base',
9 accessors => 'eol header',
10 mutators => 'command body',
11 constants => 'CRLF',
12 codec => 'unicode',
13 constant => {
14 LF => "\n",
15 EOF => "\000",
16 },
17 vars => {
18 PARAMS => {
19 -body => { optional => 1, default => undef },
20 -command => { optional => 1, default => undef },
21 -headers => { optional => 1, default => undef },
22 }
23 }
24 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our %ENCODE_MAP = (
27 "\r" => "\\r",
28 "\n" => "\\n",
29 ":" => "\\c",
30 "\\" => "\\\\",
31 );
32  
33 1 our %DECODE_MAP = reverse %ENCODE_MAP;
34  
35 #use Data::Dumper;
36 #use Data::Hexdumper;
37  
38 # ----------------------------------------------------------------------
39 # Public Methods
40 # ----------------------------------------------------------------------
41  
42 sub as_string {
43 0 ▶ my $self = shift;
Subroutine as_string: not called CC=7 SCAR=40.3
Pod documented
44  
45 # protocol spec is unclear about the case of the command,
46 # so uppercase the command, Why, just because I can.
47  
48 0 my $frame;
49 0 my $command = uc($self->command);
50 0 my $headers = $self->header->devolve;
51 0 my $body = $self->body;
52  
53 # special handling for NOOPs
54  
55 0 ▶ if ($command eq 'NOOP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "NOOP") 0 0 0
56  
57 0 $command = '';
58 0 $headers = {};
59 0 $body = '';
60  
61 }
62  
63 0 ▶ if ($self->env->mqlevel > 1.1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->env->mqlevel > 1.1) { } 0 0 0
64  
65 0 $frame = encode('utf8', $command) . $self->eol;
66  
67 } else {
68  
69 0 $frame = $command . $self->eol;
70  
71 }
72  
73 # v1.0 and v1.1 is unclear about spaces between headers and values
74 # nor the case of the header.
75 #
76 # v1.2 says there should be no 'padding' in headers and values, not
77 # sure what 'padding' means. It also adds the capability to 'escape'
78 # certain values. Please see %ENCODE_MAP and %DECODE_MAP for those
79 # values.
80 #
81 # So add a space and lowercase the header. Why, just because I can.
82  
83 0 ▶ if (keys %{$headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$headers;}) { } 0 0 0
84  
85 0 ▶ $self->_encode_headers(\$headers) if ($self->env->mqlevel > 1.1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->env->mqlevel > "1.1" 0 0 0
86  
87 0 ▶ while (my ($key, $value) = each(%{$headers})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$headers;}) 0 0 0
88  
89 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
90  
91 0 $frame .= lc($key) . ': ' . $value . $self->eol();
92  
93 }
94  
95 }
96  
97 } else {
98  
99 0 $frame .= $self->eol();
100  
101 }
102  
103 0 $frame .= $self->eol();
104 0 $frame .= $body;
105 0 $frame .= EOF;
106  
107 0 return $frame;
108  
109 }
110  
111 # ----------------------------------------------------------------------
112 # Private Methods
113 # ----------------------------------------------------------------------
114  
115 sub init {
116 0 ▶ my $class = shift;
Subroutine init: not called CC=4 SCAR=30.0
Pod documented
117  
118 0 my $self = $class->SUPER::init(@_);
119
120 0 ▶ my $headers = $self->headers || {};
Condition0%0/2
$self->headers || {}
l!l
00
MC/DC0%0/1
$self->headers || {}
$self->headers
Truth table0%0/2
A
A= $self->headers
Aresult
11
00
121  
122 0 ▶ $self->{eol} = ($self->env->mqlevel > 1.1) ? CRLF : LF;
Branch: 1 branch
BranchTrueFalseTotal
$self->env->mqlevel > 1.1 ? : 0 0 0
123  
124 0 ▶ $self->_decode_headers(\$headers) if ($self->env->mqlevel > 1.1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->env->mqlevel > "1.1" 0 0 0
125 0 $self->{header} = XAS::Lib::Stomp::Frame::Headers->new($headers);
126  
127 0 return $self;
128  
129 }
130  
131 sub _encode_headers {
132 0 ▶ my $self = shift;
Subroutine _encode_headers: not called CC=2 SCAR=17.9
133 0 my $headers = shift; # a pointer to a reference of a hash, oh my...
134  
135 0 my $ENCODE_KEYS = '['.join('', map(sprintf('\\x%02x', ord($_)), keys(%ENCODE_MAP))).']';
136  
137 0 ▶ while (my ($k, $v) = each(%$$headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$$headers) 0 0 0
138  
139 0 $k = encode('utf8', $k);
140 0 $v = encode('utf8', $v);
141  
142 0 $v =~ s/($ENCODE_KEYS)/$ENCODE_MAP{$1}/ego;
143 0 $k =~ s/($ENCODE_KEYS)/$ENCODE_MAP{$1}/ego;
144  
145 0 $$headers->{$k} = $v;
146  
147 }
148  
149 }
150  
151 sub _decode_headers {
152 0 ▶ my $self = shift;
Subroutine _decode_headers: not called CC=6 SCAR=37.4
153 0 my $headers = shift; # a pointer to a reference of a hash, oh my...
154  
155 0 ▶ while (my ($k, $v) = each(%$$headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$$headers) 0 0 0
156  
157 0 $k = decode('utf8', $k);
158 0 $v = decode('utf8', $v);
159  
160 0 ▶ if ($v =~ m/(\\.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /(\\.)/) 0 0 0
161  
162 0 ▶ unless ($v =~ s/(\\.)/$DECODE_MAP{$1}/eg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v =~ s/(\\.)/$XAS::Lib::Stomp::Frame::DECODE_MAP{$1};/eg) 0 0 0
163  
164 0 $self->throw_msg(
165 'xas.lib.stomp.frame.decode_header.badval',
166 'stomp_badval',
167 );
168  
169 }
170  
171 }
172  
173 0 ▶ if ($k =~ m/(\\.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /(\\.)/) 0 0 0
174  
175 0 ▶ unless ($k =~ s/(\\.)/$DECODE_MAP{$1}/eg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ s/(\\.)/$XAS::Lib::Stomp::Frame::DECODE_MAP{$1};/eg) 0 0 0
176  
177 0 $self->throw_msg(
178 'xas.lib.stomp.frame.decode_header.badkey',
179 'stomp_badkey'
180 );
181  
182 }
183  
184 }
185  
186 0 $$headers->{$k} = $v;
187  
188 }
189  
190 }
191  
192 package # hide from cpan...
193 XAS::Lib::Stomp::Frame::Headers;
194  
195 1 our $VERSION = '0.01';
196  
197 use XAS::Class
198 1 debug => 0,
199 version => $VERSION,
200 base => 'XAS::Base',
201 constants => 'REFS',
202 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
203  
204 #use Data::Dumper;
205  
206 sub remove {
207 0 ▶ my ($self, $key) = @_;
Subroutine remove: not called CC=1 SCAR=1.5
208  
209 0 my @x = grep { $_ ne $key } @{$self->{_methods}};
210 0 $self->{_methods} = \@x;
211  
212 0 delete($self->{$key});
213  
214 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217 0 *$key = undef;
218  
219 }
220  
221 sub add {
222 0 ▶ my ($self, $key, $value) = @_;
Subroutine add: not called CC=1 SCAR=2.4
223  
224 0 $key =~ s/-/_/g;
225  
226 0 $self->{$key} = $value;
227 0 push(@{$self->{_methods}}, $key);
228  
229 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 1 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 *$key = sub {
233 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
234 0 ▶ $self->{$key} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
235 0 return $self->{$key};
236 0 };
237  
238 }
239  
240 sub introspect {
241 0 ▶ my $self = shift;
Subroutine introspect: not called CC=1 SCAR=6.9
242  
243 0 return $self->{_methods};
244  
245 }
246  
247 sub devolve {
248 0 ▶ my $self = shift;
Subroutine devolve: not called CC=2 SCAR=17.9
249  
250 0 my $value;
251 0 my $header = {};
252  
253 0 foreach my $key (@{$self->{_methods}}) {
254  
255 0 $value = $self->{$key};
256 0 $key =~ s/_/-/g;
257 0 $header->{$key} = $value;
258  
259 }
260  
261 0 return $header;
262  
263 }
264  
265 sub init {
266 0 ▶ my $self = shift;
Subroutine init: not called CC=2 SCAR=13.0
267 0 my $configs = shift;
268  
269 0 $self->{config} = $configs;
270 0 $self->{_methods} = undef;
271  
272 # turn frame headers into mutators of there values
273  
274 0 ▶ while (my ($key, $value) = each(%$configs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$configs) 0 0 0
275  
276 0 $key =~ s/-/_/g;
277  
278 0 $self->{$key} = $value;
279 0 push(@{$self->{_methods}}, $key);
280  
281 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
282 1 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 *$key = sub {
285 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
286 0 ▶ $self->{$key} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
287 0 return $self->{$key};
288 0 };
289  
290 }
291  
292 0 return $self;
293  
294 }
295  
296 1 1;
297  
298 __END__
299  
300 =head1 NAME
301  
302 XAS::Lib::Stomp::Frame - A STOMP Frame
303  
304 =head1 SYNOPSIS
305  
306 use XAS::Lib::Stomp::Frame;
307  
308 my $frame = XAS::Lib::Stomp::Frame->new(
309 -command => $command,
310 -headers => $headers,
311 -body => $body,
312 );
313  
314 ... or ...
315  
316 my $frame = XAS:::Lib::Stomp::Frame->new();
317  
318 $frame->command('MESSAGE');
319 $frame->header->add('destination', '/queue/foo');
320 $frame->body('this is the body');
321  
322 ... stringification ...
323  
324 my $string = $frame->as_string;
325  
326 =head1 DESCRIPTION
327  
328 This module encapsulates a STOMP frame. STOMP is the Streaming Text
329 Orientated Messaging Protocol (or the Protocol Briefly Known as TTMP
330 and Represented by the symbol :ttmp). It's a simple and easy to
331 implement protocol for working with Message Orientated Middleware from
332 any language.
333  
334 A STOMP frame consists of a command, a series of headers and a body.
335  
336 =head1 METHODS
337  
338 =head2 new
339  
340 Create a new XAS::Lib::Stomp::Frame object:
341  
342 my $frame = XAS::Lib::Stomp::Frame->new(
343 -command => $command,
344 -headers => $headers,
345 -body => $body,
346 );
347  
348 It can take the following parameters:
349  
350 =over 4
351  
352 =item B<-command>
353  
354 The command verb.
355  
356 =item B<-headers>
357  
358 Headers for this command. This supports the 'bytes_message' header which
359 indicates a binary body.
360  
361 =item B<-body>
362  
363 A body for the command.
364  
365 =back
366  
367 =head2 as_string
368  
369 Create a buffer from the serialized frame.
370  
371 my $buffer = $frame->as_string;
372  
373 =head2 header
374  
375 This returns a XAS::Lib::Stomp::Frame::Headers object. This object contains
376 auto generated mutators of the header fields in a STOMP frame.
377  
378 =head1 MUTATORS
379  
380 =head2 command
381  
382 This get/sets STOMP frames command verb.
383  
384 =head2 body
385  
386 This get/sets the body of the STOMP frame.
387  
388 =head1 XAS::Lib::Stomp::Frame::Headers
389  
390 This is an internal class that auto generates mutators for the headers in a
391 STOMP frame. Any dashes in the header names are converted to underscores
392 for the mutators name.
393  
394 Example, a header of:
395  
396 content-type: test/plain
397  
398 Will become the mutator content_type().
399  
400 The usual way to access the headers is as follows:
401  
402 my $type = $frame->header->content_type;
403  
404 $frame->header->content_type('text/plain');
405  
406 The following methods are also available.
407  
408 =head2 devolve
409  
410 This will create a hash with header/value pairs. Any underscores are
411 converted to dashes in the headers name. Primarily used during
412 stringification of the STOMP frame.
413  
414 =head2 introspect
415  
416 This will return a list of headers.
417  
418 =head2 add($name, $value)
419  
420 This will add a header.
421  
422 =over 4
423  
424 =item B<$name>
425  
426 The name of the header.
427  
428 =item B<$value>
429  
430 The value for the header.
431  
432 =back
433  
434 =head2 remove($name)
435  
436 This will remove a header.
437  
438 =over 4
439  
440 =item B<$name>
441  
442 The name of the header to remove.
443  
444 =back
445  
446 =head1 ACKNOWLEDGEMENTS
447  
448 This module is based on L<Net::Stomp::Frame|https://metacpan.org/pod/Net::Stomp::Frame> by Leon Brocard <acme@astray.com>.
449  
450 =head1 SEE ALSO
451  
452 =over 4
453  
454 =item L<XAS|XAS>
455  
456 =back
457  
458 For more information on the STOMP protocol, please refer to: L<http://stomp.github.io/> .
459  
460 =head1 AUTHOR
461  
462 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
463  
464 =head1 COPYRIGHT AND LICENSE
465  
466 Copyright (C) 2014 Kevin L. Esteb
467  
468 This is free software; you can redistribute it and/or modify it under
469 the terms of the Artistic License 2.0. For details, see the full text
470 of the license at http://www.perlfoundation.org/artistic_license_2_0.
471  
472 =cut
« blib/lib/XAS/Lib/Mixins/JSON/Client.pm ↑ Summary blib/lib/XAS/Lib/Mixins/JSON/Server.pm »