blib/lib/Net/Thumper.pm

filter: Statementstmt 67.1% 192 / 286 Branchbran 42.6% 29 / 68 Conditioncond 54.2% 19 / 35 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 59.6% 34 / 57 Podpod 92.3% 12 / 13 totaltotal 60.6% 290 / 478 CC 52 SCAR 16.7
CC
52
Cov
62%
_read_data
15.9
receive
12.0
_strip_receive_frames
11.0
CRAP 5.3
↑ Summary
1 package Net::Thumper;
2 {
3 1 $Net::Thumper::VERSION = '0.03';
4 }
5  
6 =head1 NAME
7  
8 Net::Thumper - a rudimentary Pure Perl AMQP client
9  
10 =head1 SYNOPSIS
11  
12 my $amqp = Net::Thumper->new(
13 debug => 0,
14 server => 'rabbitmq-host',
15 amqp_definition => 'amqp0-8.xml',
16 );
17  
18 # Connect
19 $amqp->connect();
20 $amqp->open_channel();
21 $amqp->declare_queue('foo');
22  
23 # Publish
24 $amqp->publish('', 'foo', $payload, {}, { reply_to => 'foo', correlation_id => '1' });
25  
26 # Get
27 my $msg = $amqp->get('foo');
28  
29 # Consume
30 my $consumer_tag = $amqp->consume('foo');
31 my $msg = $amqp->get('foo');
32 $amqp->cancel($consumer_tag);
33  
34 # Disconnect
35 $amqp->disconnect();
36
37 =head1 DESCRIPTION
38  
39 This class is a rudimentary AMQP client written in Pure Perl. It has been tested
40 with recent versions of RabbitMQ.
41  
42 There are a few limitations:
43  
44 * No concept of 'channels' is exposed, so only one logical channel is possible
45  
46 * Some parts of the AMQP spec not exposed
47  
48 * API isn't the cleanest is some places - could be tidied up
49  
50 While this module mostly works for my purpose, it might be restrictive in some
51 places. Still, the fundamentals are reasonably solid (the low-level communication)
52 so it could probably be fairly easily modified for other purposes.
53  
54 Patches welcome :)
55  
56 =cut
57  
58 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 ▶use Net::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 $SIG{PIPE} = "IGNORE";
68  
69 # Class variable to indicate whether the AMQP spec has been loaded.
70 # (Only needs to be done once)
71 1 my $spec_loaded = 0;
72  
73 =head1 CONSTRUCTOR
74  
75 =head2 new(debug => $bool, server => $host, port => $port, amqp_definition => $path_to_amqp_xml, debug_hook => \&_debug_hook)
76  
77 Creates a new Net::Thumper instance. Parameters are:
78  
79 * server (required) - the server to connect to
80  
81 * port - server port, defaults to 5672
82  
83 * amqp_definition (required) - path to the AMQP XML definition (as available from the amqp.org website)
84  
85 * debug - boolean to indicate whether debugging information should be output. Defaults to false
86  
87 * debug_hook - optional subroutine reference to call when outputting debug info. If not supplied, output will go to STDERR
88  
89 =cut
90  
91 1 has 'socket' => (
92 is => 'rw',
93 init_arg => undef,
94 lazy_build => 1,
95 );
96  
97 1 has 'debug' => (
98 is => 'rw',
99 isa => 'Bool',
100 default => 0,
101 );
102  
103 1 has 'debug_hook' => (
104 is => 'rw',
105 );
106  
107 1 has 'server' => (
108 is => 'ro',
109 required => 1,
110 isa => 'Str',
111 );
112  
113 1 has 'port' => (
114 is => 'ro',
115 isa => 'Int',
116 default => 5672
117 );
118  
119 1 has 'amqp_definition' => (
120 is => 'ro',
121 isa => 'Str',
122 );
123  
124 has 'receive_cache' => (
125 is => 'rw',
126 isa => 'ArrayRef',
127 init_arg => undef,
128 traits => ['Array'],
129 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
130 1 handles => {
131 'receive_cache_is_empty' => 'is_empty',
132 'receive_cache_shift' => 'shift',
133 'receive_cache_push' => 'push',
134 },
135 );
136  
137 1 ▶sub _build_socket {
Subroutine _build_socket: called CC=4 SCAR=15.5
138 1 my $self = shift;
139
140 1 my $socket;
141
142 {
143 1 $socket = IO::Socket::INET->new(
144 PeerAddr => $self->server,
145 PeerPort => $self->port,
146 Proto => 'tcp',
147 Timeout => 1,
148 );
149
150 1 ▶ if (! defined $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 1 0 1
151 1 my $error = $!;
152 1 ▶ if ($error eq 'Interrupted system call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq "Interrupted system call") 0 1 1
153 0 redo;
154 }
155
156 1 ▶ die "Could not open socket: $error\n" unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 1 0 1
157 }
158 }
159
160 0 $socket->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1);
161
162 0 return $socket;
163 }
164  
165 2 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
166 2 my $self = shift;
167
168 2 ▶ if ($self->amqp_definition && ! $spec_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->amqp_definition and not $spec_loaded) 1 1 2
Condition66%2/3
$self->amqp_definition and not $spec_loaded
!ll&&!rl&&r
011
MC/DC50%1/2
$self->amqp_definition and not $spec_loaded
$self->amqp_definition not $spec_loaded
Truth table66%2/3
A and not(B)
A= $self->amqp_definitionB= not $spec_loaded
ABresult
0X0
100
111
169 1 Net::AMQP::Protocol->load_xml_spec($self->amqp_definition);
170 1 $spec_loaded = 1;
171 }
172
173 }
174  
175 =head1 METHODS
176  
177 =head2 connect( login => 'login', password => 'password' )
178  
179 Connects to the server. Additionally sends all handshake frames necessary to
180 start a connection.
181  
182 Accepts two optional parameters, 'login' and 'password', which both default
183 to 'guest'.
184  
185 =cut
186  
187 2 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.2
Pod documented
188 2 my $self = shift;
189 2 my %params = @_;
190  
191 2 my $greeting = Net::AMQP::Protocol->header;
192 2 $self->_write_data($greeting);
193
194 1 my ($start) = $self->_read_frames();
195
196 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
197 1 $self->_assert_frame_is($start, 'Net::AMQP::Protocol::Connection::Start');
198 }
199 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 die "Failed opening connection: " . $_;
201 1 };
202
203 # TODO: set params properly
204 my $start_frame = Net::AMQP::Protocol::Connection::StartOk->new(
205 client_properties => {
206 platform => 'Perl',
207 product => 'Net-Thumper',
208 version => 0.0.1,
209 },
210 mechanism => 'AMQPLAIN',
211 response => {
212 LOGIN => $params{login} // 'guest',
213 1 ▶ PASSWORD => $params{password} // 'guest',
Condition50%2/4
$params{"login"} // "guest"
l!l
01
$params{"password"} // "guest"
l!l
01
MC/DC0%0/1
$params{"login"} // "guest"
$params{"login"}
MC/DC0%0/1
$params{"password"} // "guest"
$params{"password"}
Truth table50%1/2
A
A= $params{"login"}
Aresult
11
00
Truth table50%1/2
A
A= $params{"password"}
Aresult
11
00
214 },
215 locale => 'en_US',
216 );
217
218 1 $self->_write_frame($start_frame, 0);
219
220 1 my ($tune_info) = $self->_read_frames();
221
222 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 1 $self->_assert_frame_is($tune_info, 'Net::AMQP::Protocol::Connection::Tune');
224 }
225 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 die "Failed opening connection: " . $_;
227 1 };
228
229 1 my $tune_ok = Net::AMQP::Protocol::Connection::TuneOk->new(
230 channel_max => $tune_info->method_frame->channel_max,
231 frame_max => $tune_info->method_frame->frame_max,
232 heartbeat => $tune_info->method_frame->heartbeat,
233 );
234
235 1 $self->_write_frame($tune_ok, 0);
236
237 1 my $connection_open = Net::AMQP::Protocol::Connection::Open->new(
238 virtual_host => '/',
239 capabilities => '',
240 insist => 1,
241 );
242 1 $self->_write_frame($connection_open, 0);
243
244 1 my ($open_ok) = $self->_read_frames();
245
246 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
247 1 $self->_assert_frame_is($open_ok, 'Net::AMQP::Protocol::Connection::OpenOk');
248 }
249 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 0 die "Failed opening connection: " . $_;
251 1 };
252 }
253  
254 =head2 open_channel()
255  
256 Open a channel. This is hard-coded to channel 1.
257  
258 =cut
259  
260 1 ▶sub open_channel {
Subroutine open_channel: called CC=1 SCAR=0.0
Pod documented
261 1 my $self = shift;
262
263 1 my $channel_open = Net::AMQP::Protocol::Channel::Open->new(
264 channel => 1,
265 );
266
267 1 $self->_write_frame($channel_open);
268
269 1 my ($open_ok) = $self->_read_frames();
270
271 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
272 1 $self->_assert_frame_is($open_ok, 'Net::AMQP::Protocol::Channel::OpenOk');
273 }
274 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 0 die "Failed opening channel: " . $_;
276 1 };
277 }
278  
279 =head2 declare_queue($queue, %params)
280  
281 Declare a queue. The optional %params contains options for the queue declaration.
282  
283 =cut
284  
285 1 ▶sub declare_queue {
Subroutine declare_queue: called CC=1 SCAR=0.0
Pod documented
286 1 my $self = shift;
287 1 my $queue = shift;
288 1 my %args = @_;
289
290 1 my $declare_queue = Net::AMQP::Protocol::Queue::Declare->new(
291 passive => 0,
292 durable => 0,
293 exclusive => 0,
294 auto_delete => 0,
295 no_ack => 1,
296 %args,
297 queue => $queue,
298 ticket => 0,
299 nowait => 0,
300 );
301
302 1 $self->_write_frame($declare_queue);
303
304 1 my ($declare_ok) = $self->_read_frames();
305
306 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
307 1 $self->_assert_frame_is($declare_ok, 'Net::AMQP::Protocol::Queue::DeclareOk');
308 }
309 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
310 0 die "Failed declaring queue: " . $_;
311 1 };
312 }
313  
314 =head2 bind_queue($queue, $exchange, $routing_key, %params)
315  
316 Bind a queue to an exchange using the routing key.
317 The optional %params contains options for the bind
318  
319 =cut
320  
321 0 ▶sub bind_queue {
Subroutine bind_queue: not called CC=1 SCAR=6.9
Pod documented
322 0 my $self = shift;
323 0 my $queue = shift;
324 0 my $exchange = shift;
325 0 my $routing_key = shift;
326 0 my %args = @_;
327
328 0 my $declare_queue = Net::AMQP::Protocol::Queue::Bind->new(
329 %args,
330 queue => $queue,
331 exchange => $exchange,
332 routing_key => $routing_key,
333 ticket => 0,
334 nowait => 0,
335 );
336
337 0 $self->_write_frame($declare_queue);
338
339 0 my ($declare_ok) = $self->_read_frames();
340
341 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
342 0 $self->_assert_frame_is($declare_ok, 'Net::AMQP::Protocol::Queue::BindOk');
343 }
344 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 0 die "Failed binding queue: " . $_;
346 0 };
347 }
348  
349 =head2 declare_exchange($exchange, $type, %params)
350  
351 Declare an exchange
352  
353 =cut
354  
355 0 ▶sub declare_exchange {
Subroutine declare_exchange: not called CC=1 SCAR=6.9
Pod documented
356 0 my $self = shift;
357 0 my $exchange = shift;
358 0 my $type = shift;
359 0 my %args = @_;
360
361 0 my $declare_queue = Net::AMQP::Protocol::Exchange::Declare->new(
362 exchange => $exchange,
363 type => $type,
364 passive => 0,
365 durable => 0,
366 auto_delete => 0,
367 internal => 0,
368 %args,
369 ticket => 0,
370 nowait => 0,
371 );
372
373 0 $self->_write_frame($declare_queue);
374
375 0 my ($declare_ok) = $self->_read_frames();
376
377 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 $self->_assert_frame_is($declare_ok, 'Net::AMQP::Protocol::Exchange::DeclareOk');
379 }
380 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
381 0 die "Failed declaring exchange: " . $_;
382 0 };
383 }
384  
385 =head2 publish($exchange, $routing_key, $body, $params, $props)
386  
387 Publish a message via $exchange with $routing_key. Message contents are
388 passed in $body. $params is a hashref of args to the publish request
389 (such as the mandatory and immediate flags), $props is a hashref of
390 header parameters (such as correlation_id and reply_to)
391  
392 =cut
393  
394 2 ▶sub publish {
Subroutine publish: called CC=4 SCAR=13.9
Pod documented
395 2 my $self = shift;
396 2 my $exchange = shift;
397 2 my $routing_key = shift;
398 2 my $body = shift;
399 2 ▶ my $args = shift // {};
Condition50%1/2
shift() // {}
l!l
20
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
400 2 ▶ my $props = shift // {};
Condition50%1/2
shift() // {}
l!l
20
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
401
402 2 my $publish = Net::AMQP::Protocol::Basic::Publish->new(
403 exchange => $exchange,
404 mandatory => 0,
405 immediate => 0,
406 routing_key => $routing_key,
407 ticket => 0,
408 %$args,
409 );
410
411 2 my $header = Net::AMQP::Frame::Header->new(
412 weight => 0,
413 body_size => length($body),
414 header_frame => Net::AMQP::Protocol::Basic::ContentHeader->new(
415 content_type => 'application/octet-stream',
416 content_encoding => undef,
417 headers => {},
418 delivery_mode => 1,
419 priority => 1,
420 correlation_id => undef,
421 expiration => undef,
422 message_id => undef,
423 timestamp => time,
424 type => undef,
425 user_id => undef,
426 app_id => undef,
427 cluster_id => undef,
428 %$props
429 ),
430 );
431
432 2 $self->_write_frame($publish);
433 2 $self->_write_frame($header);
434  
435 # We split the body into frames of 30000 characters.
436 # TODO: this size should really be based on the max_frame_size set
437 # in the Tune/TuneOk frames during connection
438 2 my @chunks = unpack '(a30000)*', $body;
439
440 2 foreach my $chunk (@chunks) {
441 5 my $body_frame = Net::AMQP::Frame::Body->new(payload => $chunk);
442 5 $self->_write_frame($body_frame);
443 }
444 }
445  
446 =head2 get($queue, %params)
447  
448 Get a message from $queue. Return a hashref with the message details, or undef
449 if there is no message. This is essentially a poll of a given queue. %params is
450 an optional hash containing parameters to the Get request.
451  
452 The message returned in a hashref with the following keys:
453  
454 * body - the body of the message
455  
456 * reply_to - the reply_to header of the message
457  
458 * correlation_id - the correlation_id of the message
459  
460 * delivery_tag - used in acking messages.
461  
462 =cut
463  
464 4 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
465 4 my $self = shift;
466 4 my $queue = shift;
467 4 my %args = @_;
468
469 4 my $get = Net::AMQP::Protocol::Basic::Get->new(
470 no_ack => 1,
471 queue => $queue,
472 %args,
473 ticket => 0,
474 );
475
476 4 $self->_write_frame($get);
477
478 4 my ($get_ok) = $self->_read_frames();
479
480 4 ▶ return unless $get_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $get_ok 0 4 4
481
482 4 ▶ if ($get_ok->method_frame->isa('Net::AMQP::Protocol::Basic::GetEmpty')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($get_ok->method_frame->isa("Net::AMQP::Protocol::Basic::GetEmpty")) 1 3 4
483 1 return;
484 }
485
486 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
487 3 $self->_assert_frame_is($get_ok, 'Net::AMQP::Protocol::Basic::GetOk');
488 }
489 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 0 die "Failed getting message: " . $_;
491 3 };
492
493 3 my ($header, @bodies) = $self->_read_resp();
494
495 3 return $self->_create_resp($get_ok, $header, @bodies);
496 }
497  
498 =head2 consume($queue, %params)
499  
500 Indicate that a given queue should be consumed from. %params contains
501 params to be passed to the Consume request.
502  
503 Returns the consumer tag. Once the client is consuming from a queue,
504 receive() can be called to get any messages.
505  
506 =cut
507  
508 0 ▶sub consume {
Subroutine consume: not called CC=1 SCAR=6.9
Pod documented
509 0 my $self = shift;
510 0 my $queue = shift;
511 0 my %args = @_;
512
513 0 my $consume = Net::AMQP::Protocol::Basic::Consume->new(
514 consumer_tag => '',
515 no_local => 0,
516 no_ack => 1,
517 exclusive => 0,
518 queue => $queue,
519 %args, # queue
520 ticket => 0,
521 nowait => 0,
522 );
523
524 0 $self->_write_frame($consume);
525
526 0 my ($consume_ok) = $self->_read_frames();
527
528 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
529 0 $self->_assert_frame_is($consume_ok, 'Net::AMQP::Protocol::Basic::ConsumeOk');
530 }
531 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
532 0 die "Failed getting message: " . $_;
533 0 };
534
535 0 $self->{consuming} = 1;
536
537 0 return $consume_ok->method_frame->{consumer_tag};
538 }
539  
540 =head2 receive($timeout)
541  
542 Receive a message from a queue that has previously been consumed from. Wait for
543 up to $timeout seconds before giving up and returning undef.
544  
545 The message returned is of the same format as that returned from get()
546  
547 =cut
548  
549 0 ▶sub receive {
Subroutine receive: not called CC=3 SCAR=24.8
Pod documented
550 0 my $self = shift;
551 0 my $timeout = shift;
552  
553 0 ▶ $self->_read_frames($timeout, 1) if $self->receive_cache_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->receive_cache_is_empty 0 0 0
554
555 0 my $resp = $self->receive_cache_shift;
556
557 0 ▶ return unless $resp;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp 0 0 0
558
559 0 return $self->_create_resp($resp->{deliver}, $resp->{header}, @{ $resp->{bodies} });
560
561 }
562  
563 # Strip out any receive frames, and add them to the cache
564 17 ▶sub _strip_receive_frames {
Subroutine _strip_receive_frames: called CC=4 SCAR=24.0
565 17 my $self = shift;
566 17 my $timeout = shift;
567 17 my $expecting_receive = shift;
568 17 my $frame = shift;
569
570 # Only do something if we're consuming
571 17 ▶ return $frame unless $self->{consuming};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"consuming"} 17 0 17
572
573 0 my @messages;
574
575 my @other_frames;
576
577 0 while (1) {
578 0 my $deliver = $frame;
579
580 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
581 0 $self->_assert_frame_is($deliver, 'Net::AMQP::Protocol::Basic::Deliver');
582 }
583 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
584 0 undef $deliver;
585 0 };
586
587 0 ▶ last unless $deliver;
Branch: 1 branch
BranchTrueFalseTotal
unless $deliver 0 0 0
588
589 0 my ($header, @bodies) = $self->_read_resp();
590
591 0 push @messages, {
592 deliver => $deliver,
593 header => $header,
594 bodies => \@bodies
595 };
596
597 # See if there's a new frame to read, unless we were
598 # expecting a receive, in which case there may not be anything
599 # more.
600 0 ▶ if ($expecting_receive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting_receive) { } 0 0 0
601 0 last;
602 }
603 else {
604 0 ($frame) = $self->_read_frames($timeout);
605 }
606 }
607
608 0 $self->receive_cache_push(@messages);
609  
610 0 return $frame;
611 }
612  
613 =head2 cancel($consumer_tag, %params)
614  
615 Cancel consuming a queue. $consumer_tag is the tag returned from
616 the original consume() request.
617  
618 =cut
619  
620 0 ▶sub cancel {
Subroutine cancel: not called CC=1 SCAR=6.9
Pod documented
621 0 my $self = shift;
622 0 my $consumer_tag = shift;
623 0 my %args = @_;
624
625 0 my $consume = Net::AMQP::Protocol::Basic::Cancel->new(
626 consumer_tag => $consumer_tag,
627 %args,
628 nowait => 0,
629 );
630
631 0 $self->_write_frame($consume);
632
633 0 my ($consume_ok) = $self->_read_frames();
634
635 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
636 0 $self->_assert_frame_is($consume_ok, 'Net::AMQP::Protocol::Basic::CancelOk');
637 }
638 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
639 0 die "Failed getting message: " . $_;
640 0 };
641 }
642  
643 =head2 disconnect()
644  
645 Disconnect from the server.
646  
647 =cut
648  
649 1 ▶sub disconnect {
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
650 1 my $self = shift;
651
652 1 my $close = Net::AMQP::Protocol::Connection::Close->new();
653
654 1 $self->_write_frame($close, 0);
655
656 1 my ($close_ok) = $self->_read_frames();
657
658 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
659 1 $self->_assert_frame_is($close_ok, 'Net::AMQP::Protocol::Connection::CloseOk');
660 }
661 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
662 0 die "Failed getting message: " . $_;
663 1 };
664
665 1 $self->socket->close();
666 }
667  
668 =head2 is_connected()
669  
670 Return true if we appear to be connected to the server. This just checks
671 that the TCP socket appears to be connected, so the application may actually
672 have disconnected.
673  
674 =cut
675  
676 0 ▶sub is_connected {
Subroutine is_connected: not called CC=2 SCAR=17.9
Pod documented
677 0 my $self = shift;
678
679 0 ▶ return $self->socket->connected ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->socket->connected ? : 0 0 0
680 }
681  
682 3 ▶sub _read_resp {
Subroutine _read_resp: called CC=2 SCAR=6.9
683 3 my $self = shift;
684
685 3 my ($header) = $self->_read_frames();
686
687 3 my $read_length = 0;
688 3 my @bodies;
689  
690 3 ▶ while ($read_length < $header->body_size()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_length < $header->body_size) 4 3 7
691 4 my ($body) = $self->_read_frames();
692  
693 4 $read_length += length($body->payload);
694  
695 4 push @bodies, $body;
696 }
697
698 3 return ($header, @bodies);
699
700 }
701  
702 3 ▶sub _create_resp {
Subroutine _create_resp: called CC=6 SCAR=19.1
703 3 my $self = shift;
704 3 my ($control, $header, @bodies) = @_;
705  
706 3 ▶ return unless $control && $header && @bodies;
Branch: 1 branch
BranchTrueFalseTotal
unless $control and $header and @bodies 0 3 3
Condition33%2/6
$control and $header and @bodies
!ll&&!rl&&r
003
$control and $header
!ll&&!rl&&r
003
MC/DC0%0/3
$control and $header and @bodies
$control $header @bodies
Truth table25%1/4
A and B and C
A= $controlB= $headerC= @bodies
ABCresult
0XX0
10X0
1100
1111
707
708 3 my $data;
709 3 foreach my $body (@bodies) {
710 4 $data .= $body->payload;
711 }
712
713 3 ▶ my $headers = $header->header_frame->headers // {};
Condition100%2/2
$header->header_frame->headers // {}
l!l
12
MC/DC100%1/1
$header->header_frame->headers // {}
$header->header_frame->headers
Truth table100%2/2
A
A= $header->header_frame->headers
Aresult
11
00
714
715 return {
716 body => $data,
717 delivery_tag => $control->method_frame->delivery_tag,
718 reply_to => $headers->{reply_to},
719 correlation_id => $headers->{correlation_id},
720 }
721 3 }
722  
723 9 ▶sub _assert_frame_is {
Subroutine _assert_frame_is: called CC=5 SCAR=20.9
724 9 my $self = shift;
725 9 my $frame = shift;
726 9 my $expected_class = shift;
727
728 9 ▶ die "Expected $expected_class but got a non-blessed scalar!\nFrame Dump:" . Dumper $frame
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($frame) 0 9 9
729 unless blessed $frame;
730
731 9 ▶ if (! $frame->can('method_frame')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->can("method_frame")) 0 9 9
732 0 die "Exepected $expected_class, but got " . ref($frame) . "\nFrame Dump:" . Dumper $frame;
733 }
734
735 9 ▶ if (! $frame->method_frame->isa($expected_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $frame->method_frame->isa($expected_class)) 0 9 9
736 0 ▶ if (! $frame->method_frame->can('reply_text')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->method_frame->can("reply_text")) 0 0 0
737 0 die "Exepected $expected_class, but got " . ref($frame->method_frame) . "\nFrame Dump:" . Dumper $frame;
738 }
739 0 die $frame->method_frame->reply_text . "\n";
740 }
741 }
742  
743  
744 19 ▶sub _write_frame {
Subroutine _write_frame: called CC=3 SCAR=11.0
745 19 my $self = shift;
746 19 my $frame = shift;
747 19 ▶ my $channel = shift // 1;
Condition100%2/2
shift() // 1
l!l
415
MC/DC100%1/1
shift() // 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
748
749 19 ▶ if ($frame->isa('Net::AMQP::Protocol::Base')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->isa("Net::AMQP::Protocol::Base")) 12 7 19
750 12 $frame = $frame->frame_wrap;
751 }
752 19 $frame->channel($channel);
753
754 19 $self->_debug("Writing frame: " . Dumper $frame);
755
756 19 $self->_write_data($frame->to_raw_frame());
757
758
759 }
760  
761 17 ▶sub _read_frames {
Subroutine _read_frames: called CC=6 SCAR=19.5
762 17 my $self = shift;
763 17 my $timeout = shift;
764 17 ▶ my $expecting_receive = shift // 0;
Condition50%1/2
shift() // 0
l!l
017
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
765
766 17 my $data = $self->_read_data($timeout);
767
768 17 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 17 17
769
770 17 my @frames = Net::AMQP->parse_raw_frames(\$data);
771
772 17 $self->_debug("Read frames: " . Dumper \@frames);
773 17 ▶ $self->_debug("Raw data: $data") if ! @frames;
Branch: 1 branch
BranchTrueFalseTotal
unless @frames 0 17 17
774  
775 17 @frames = $self->_strip_receive_frames($timeout, $expecting_receive, @frames);
776
777 17 ▶ if (length($data) > 0 && ! @frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 0 and not @frames) 0 17 17
Condition33%1/3
length $data > 0 and not @frames
!ll&&!rl&&r
1700
MC/DC0%0/2
length $data > 0 and not @frames
length $data > 0 not @frames
Truth table33%1/3
A and not(B)
A= length $data > 0B= not @frames
ABresult
0X0
100
111
778 0 die "Read " . length($data) . " bytes of data, but it contained no parsable frames\n";
779 }
780
781 17 return @frames;
782 }
783  
784 21 ▶sub _write_data {
Subroutine _write_data: called CC=2 SCAR=7.1
785 21 my $self = shift;
786 21 my $data = shift;
787
788 21 ▶ $self->socket->send($data) || die "Failed in writing data ($!)\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->socket->send($data) or die "Failed in writing data ($!)\n" 20 0 20
789
790 }
791  
792 17 ▶sub _read_data {
Subroutine _read_data: called CC=10 SCAR=27.7
793 17 my $self = shift;
794 17 ▶ my $timeout = shift // 30;
Condition50%1/2
shift() // 30
l!l
017
MC/DC0%0/1
shift() // 30
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
795
796 # Set the timeout
797 17 $self->socket->setsockopt(SOL_SOCKET, SO_RCVTIMEO, pack('L!L!', $timeout, 0));
798
799 17 ▶ my $data = $self->{_pre_read_data} // '';
Condition100%2/2
$self->{"_pre_read_data"} // ""
l!l
161
MC/DC100%1/1
$self->{"_pre_read_data"} // ""
$self->{"_pre_read_data"}
Truth table100%2/2
A
A= $self->{"_pre_read_data"}
Aresult
11
00
800
801 # If we have less than 7 bytes of data, we need to read more so we at least get the header
802 17 ▶ if (length $data < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 7) 17 0 17
803 17 ▶ $data .= $self->_read_from_socket(1024) // '';
Condition50%1/2
$self->_read_from_socket(1024) // ""
l!l
170
MC/DC0%0/1
$self->_read_from_socket(1024) // ""
$self->_read_from_socket(1024)
Truth table50%1/2
A
A= $self->_read_from_socket(1024)
Aresult
11
00
804 }
805  
806 # Read header
807 17 my $header = substr $data, 0, 7, '';
808
809 17 ▶ return unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 0 17 17
810
811 17 my ($type_id, $channel, $size) = unpack 'CnN', $header;
812
813 # Read body
814 17 my $body = substr $data, 0, $size, '';
815
816 # If we haven't got the full body and the footer, we have more to read
817 17 ▶ if (length $body < $size || length $data == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body < $size or length $data == 0) 0 17 17
Condition33%1/3
length $body < $size or length $data == 0
l!l&&r!l&&!r
0017
MC/DC0%0/2
length $body < $size or length $data == 0
length $body < $size length $data == 0
Truth table33%1/3
A or B
A= length $body < $sizeB= length $data == 0
ABresult
1X1
011
000
818 # Add 1 to the size to make sure we get the footer
819 0 my $size_remaining = $size+1 - length $body;
820
821 0 ▶ while ($size_remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_remaining > 0) 0 0 0
822 0 my $chunk = $self->_read_from_socket($size_remaining);
823  
824 0 $size_remaining -= length $chunk;
825 0 $data .= $chunk;
826 }
827
828 0 $body .= substr($data, 0, $size-length($body), '');
829 }
830
831 # Read footer
832 17 my $footer = substr $data, 0, 1, '';
833 17 my $footer_octet = unpack 'C', $footer;
834
835 17 ▶ die "Invalid footer: $footer_octet\n" unless $footer_octet == 206;
Branch: 1 branch
BranchTrueFalseTotal
unless $footer_octet == 206 0 17 17
836
837 17 $self->{_pre_read_data} = $data;
838
839 17 return $header . $body . $footer;
840 }
841  
842 17 ▶sub _read_from_socket {
Subroutine _read_from_socket: called CC=5 SCAR=21.5
843 17 my $self = shift;
844 17 my $read_size = shift;
845
846 17 my $chunk_size;
847 my $chunk;
848
849 17 ▶ while (! defined $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chunk_size) 17 17 34
850 17 $chunk_size = $self->socket->sysread($chunk,$read_size);
851  
852 17 ▶ if (! defined $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chunk_size) 0 17 17
853 0 my $read_error = $!;
854 0 ▶ if ($read_error eq 'Interrupted system call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_error eq "Interrupted system call") 0 0 0
855 # A signal interrupted us... just try again
856 0 next;
857 }
858
859 0 ▶ if ($read_error eq 'Resource temporarily unavailable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_error eq "Resource temporarily unavailable") 0 0 0
860 # We assume this is a timeout...
861 0 return;
862 }
863
864 0 die "Error reading from socket: $read_error\n";
865 }
866 }
867
868 17 return $chunk;
869 }
870  
871 36 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=16.0
872 36 my $self = shift;
873 36 my $lines = shift;
874
875 36 ▶ return unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 36 0 36
876
877 0 ▶ if ($self->debug_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug_hook) { } 0 0 0
878 0 $self->debug_hook->($lines);
879 }
880 else {
881 0 warn $lines;
882 }
883 }
884  
885 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
886 1 __PACKAGE__->meta->make_immutable;
887  
888 =head1 THE FUTURE
889  
890 When I get a chance, I'd like to tidy up the API, improve the tests and
891 split some of the logic into different modules. Could also look at a lighter
892 weight alternative to Moose.
893  
894 Patches are - of course - welcome.
895  
896 =head1 AUTHORS
897  
898 Created by Sam Crawley
899  
900 Contributions by DanC (dconlon)
901  
902 =head1 LICENCE
903  
904 Development commissioned by NZ Registry Services, and carried out by
905 Catalyst IT - L<http://www.catalyst.net.nz/>
906  
907 Copyright 2012 NZ Registry Services. This module is licensed
908 under the Artistic License v2.0, which permits relicensing under other
909 Free Software licenses.
910  
911 =cut
↑ Summary