AMQP/Server/Protocol.pm

filter: Statementstmt 83.2% 109 / 131 Branchbran 57.1% 16 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.3% 24 / 26 Podpod 90.4% 19 / 21 totaltotal 77.7% 171 / 220 CC 21 SCAR 12.6
CC
21
Cov
76%
remove_pending
12.0
next_pending
7.7
bus
6.0
CRAP 3.5
« AMQP/ConnectionManager/Connection.pm ↑ Summary AMQP/Server/Connection.pm »
1 package Net::Async::AMQP::Server::Protocol;
2 5 $Net::Async::AMQP::Server::Protocol::VERSION = '2.000';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Net::Async::AMQP::Server::Protocol
9  
10 =head1 VERSION
11  
12 version 2.000
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =cut
19  
20 5 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Net::Async::AMQP::Utils qw(amqp_frame_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head2 new
24  
25 =cut
26  
27 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 3 my $class = shift;
29 3 my $self = bless { @_ }, $class;
30 }
31  
32 =head2 write
33  
34 =cut
35  
36 6 ▶sub write { my $self = shift; $self->{write}->(@_) }
Subroutine write: called CC=1 SCAR=0.0
Pod documented
37  
38 =head2 on_read
39  
40 =cut
41  
42 5 ▶sub on_read {
Subroutine on_read: called CC=3 SCAR=11.0
Pod documented
43 5 my ($self, $buffer, $eof) = @_;
44 5 ▶ return 0 unless length $$buffer >= length Net::AMQP::Protocol->header;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$buffer >= length "Net::AMQP::Protocol"->header 2 3 5
45  
46 3 $self->{initial_header} = substr $$buffer, 0, length Net::AMQP::Protocol->header, '';
47 3 ▶ my ($proto, $version) = $self->{initial_header} =~ /^(AMQP)(....)/ or die "Invalid header received: " . sprintf "%v02x", $self->{initial_header};
Branch: 1 branch
BranchTrueFalseTotal
() = $self->{"initial_header"} =~ /^(AMQP)(....)/ or die 'Invalid header received: ' . sprintf("%v02x", $self->{"initial_header"}) 2 1 3
48 2 $self->debug_printf("Protocol $proto, version " . join '.', sprintf '%08x', unpack 'N1', $version);
49 2 $self->can('startup');
50 }
51  
52 =head2 startup
53  
54 =cut
55  
56 2 ▶sub startup {
Subroutine startup: called CC=3 SCAR=11.2
Pod documented
57 2 my ($self, $buffer, $eof) = @_;
58 2 my $frame = Net::AMQP::Frame::Method->new(
59 channel => 0,
60 method_frame => Net::AMQP::Protocol::Connection::Start->new(
61 server_properties => {
62 },
63 mechanisms => $self->auth_mechanisms,
64 locale => $self->locale,
65 ),
66 );
67 2 ▶ $frame = $frame->frame_wrap if $frame->isa("Net::AMQP::Protocol::Base");
Branch: 1 branch
BranchTrueFalseTotal
if $frame->isa("Net::AMQP::Protocol::Base") 0 2 2
68 2 ▶ $frame->channel(0) unless defined $frame->channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame->channel 0 2 2
69 2 $self->write($frame->to_raw_frame);
70 2 $self->push_pending(
71 'Connection::StartOk' => $self->can('start_ok'),
72 'Connection::Close' => $self->can('conn_close'),
73 );
74 2 $self->can('conn_start');
75 }
76  
77 2 ▶sub auth_mechanisms { 'AMQPLAIN' }
Subroutine auth_mechanisms: called CC=1 SCAR=0.0
Pod undocumented
78  
79 2 ▶sub locale { 'en_GB' }
Subroutine locale: called CC=1 SCAR=0.0
Pod undocumented
80  
81 =head2 push_pending
82  
83 =cut
84  
85 6 ▶sub push_pending {
Subroutine push_pending: called CC=2 SCAR=6.9
Pod documented
86 6 my $self = shift;
87 6 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 6 14
88 8 my ($type, $code) = splice @_, 0, 2;
89 8 push @{$self->{pending}{$type}}, $code;
90 }
91 6 return $self;
92 }
93  
94 =head2 remove_pending
95  
96 =cut
97  
98 0 ▶sub remove_pending {
Subroutine remove_pending: not called CC=3 SCAR=24.8
Pod documented
99 0 my $self = shift;
100 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
101 0 my ($type, $code) = splice @_, 0, 2;
102 # This is the same as extract_by { $_ eq $code } @{$self->{pending}{$type}};,
103 # but since we'll be calling it a lot might as well do it inline:
104 splice
105 0 @{$self->{pending}{$type}},
106 $_,
107 0 1 for grep {
108 0 $self->{pending}{$type}[$_] eq $code
109 0 } reverse 0..$#{$self->{pending}{$type}};
110 }
111 0 return $self;
112 }
113  
114 =head2 next_pending
115  
116 =cut
117  
118 5 ▶sub next_pending {
Subroutine next_pending: called CC=5 SCAR=20.4
Pod documented
119 5 my ($self, $type, $frame) = @_;
120 5 $self->debug_printf("Check next pending for %s", $type);
121  
122 5 ▶ if(my $next = shift @{$self->{pending}{$type} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$self->{'pending'}{$type} or [];}) { } 5 0 5
Condition50%1/2
$self->{'pending'}{$type} || []
l!l
50
MC/DC0%0/1
$self->{'pending'}{$type} || []
$self->{'pending'}{$type}
Truth table50%1/2
A
A= $self->{'pending'}{$type}
Aresult
11
00
123 # We have a registered handler for this frame type. This usually
124 # means that we've sent a frame and are awaiting a response.
125 5 ▶ if(ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq 'ARRAY') { } 0 5 5
126 0 my ($f, @args) = @$next;
127 0 ▶ $f->done(@args) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
128 } else {
129 5 $next->($self, $frame, @_);
130 }
131 } else {
132 # It's quite possible we'll see unsolicited frames back from
133 # the server: these will typically be errors, connection close,
134 # or consumer cancellation if the consumer_cancel_notify
135 # option is set (RabbitMQ). We don't expect many so report
136 # them when in debug mode.
137 0 $self->debug_printf("We had no pending handlers for %s, raising as event", $type);
138 0 $self->bus->invoke_event(
139 unexpected_frame => $type, $frame
140 );
141 }
142 5 $self
143 }
144  
145 =head2 process_frame
146  
147 =cut
148  
149 5 ▶sub process_frame {
Subroutine process_frame: called CC=2 SCAR=10.3
Pod documented
150 5 my ($self, $frame) = @_;
151 # if(my $ch = $self->channel_by_id($frame->channel)) {
152 # return $self if $ch->next_pending($frame);
153 # }
154  
155 5 my $frame_type = amqp_frame_type($frame);
156  
157 # Basic::Deliver - we're delivering a message to a ctag
158 # Frame::Header - header part of message
159 # Frame::Body* - body content
160 5 $self->debug_printf("Processing connection frame %s => %s", $self, $frame);
161  
162 5 $self->next_pending($frame_type, $frame);
163 5 return $self;
164  
165 # Any channel errors will be represented as a channel close event
166 0 ▶ if($frame_type eq 'Channel::Close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame_type eq "Channel::Close") 0 0 0
167 0 $self->debug_printf("Channel was %d, calling close", $frame->channel);
168 0 $self->channel_by_id($frame->channel)->on_close(
169 $frame->method_frame
170 );
171 0 return $self;
172 }
173  
174  
175 0 return $self;
176 }
177  
178 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 =head2 conn_start
181  
182 =cut
183  
184 7 ▶sub conn_start {
Subroutine conn_start: called CC=2 SCAR=6.9
Pod documented
185 7 my ($self, $buffer, $eof) = @_;
186 7 $self->debug_printf("Have " . length($$buffer) . " bytes of post-connect data");
187 7 for my $frame (Net::AMQP->parse_raw_frames($buffer)) {
188 5 $self->debug_printf(":: Frame $frame" . Dumper($frame));
189 5 $self->process_frame($frame);
190 }
191 7 0;
192 }
193  
194 =head2 start_ok
195  
196 =cut
197  
198 1 ▶sub start_ok {
Subroutine start_ok: called CC=1 SCAR=0.0
Pod documented
199 1 my ($self, $frame) = @_;
200 1 $self->debug_printf("Start okay:\n");
201 1 my $method_frame = $frame->method_frame;
202 1 $self->debug_printf("Auth: " . $method_frame->mechanism);
203 1 $self->debug_printf("Locale: " . $method_frame->locale);
204 1 $self->debug_printf("Response: " . $method_frame->response);
205 1 $self->send_frame(
206 Net::AMQP::Protocol::Connection::Tune->new(
207 channel_max => 12 || $self->channel_max,
208 frame_max => $self->frame_max,
209 heartbeat => $self->heartbeat_interval,
210 )
211 );
212 1 $self->push_pending(
213 'Connection::TuneOk' => $self->can('tune_ok'),
214 );
215 }
216  
217 =head2 heartbeat_interval
218  
219 =cut
220  
221 1 ▶sub heartbeat_interval { shift->{heartbeat_interval} //= 0 }
Subroutine heartbeat_interval: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"heartbeat_interval"} //= 0
l!l
01
MC/DC0%0/1
+(shift())->{"heartbeat_interval"} //= 0
+(shift())->{"heartbeat_interval"}
Truth table50%1/2
A
A= +(shift())->{"heartbeat_interval"}
Aresult
11
00
222  
223 =head2 send_frame
224  
225 =cut
226  
227 4 ▶sub send_frame {
Subroutine send_frame: called CC=4 SCAR=13.9
Pod documented
228 4 my $self = shift;
229 4 my $frame = shift;
230 4 my %args = @_;
231  
232 # Apply defaults and wrap as required
233 4 ▶ $frame = $frame->frame_wrap if $frame->isa("Net::AMQP::Protocol::Base");
Branch: 1 branch
BranchTrueFalseTotal
if $frame->isa("Net::AMQP::Protocol::Base") 3 1 4
234 4 ▶ $frame->channel($args{channel} // 0) unless defined $frame->channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame->channel 3 1 4
Condition50%1/2
$args{"channel"} // 0
l!l
03
MC/DC0%0/1
$args{"channel"} // 0
$args{"channel"}
Truth table50%1/2
A
A= $args{"channel"}
Aresult
11
00
235  
236 # Get bytes to send across our transport
237 4 my $data = $frame->to_raw_frame;
238 4 $self->write($data);
239  
240 4 $self;
241 }
242  
243 =head2 bus
244  
245 =cut
246  
247 0 ▶sub bus { $_[0]->{bus} ||= Mixin::Event::Dispatch::Bus->new }
Subroutine bus: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"bus"} ||= "Mixin::Event::Dispatch::Bus"->new
$_[0]{"bus"} "Mixin::Event::Dispatch::Bus"->new
Truth table0%0/3
A ||= B
A= $_[0]{"bus"}B= "Mixin::Event::Dispatch::Bus"->new
ABresult
1X1
011
000
248  
249 =head2 frame_max
250  
251 =cut
252  
253 1 ▶sub frame_max {
Subroutine frame_max: called CC=2 SCAR=9.2
Pod documented
254 1 my $self = shift;
255 1 ▶ return $self->{frame_max} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
256  
257 0 $self->{frame_max} = shift;
258 0 $self
259 }
260  
261 =head2 tune_ok
262  
263 =cut
264  
265 1 ▶sub tune_ok {
Subroutine tune_ok: called CC=1 SCAR=0.0
Pod documented
266 1 my ($self, $frame) = @_;
267 1 $self->debug_printf("Tune okay:");
268 1 my $method_frame = $frame->method_frame;
269 1 $self->debug_printf("Channels: " . $method_frame->channel_max);
270 1 $self->debug_printf("Max size: " . $method_frame->frame_max);
271 1 $self->debug_printf("Heartbeat: " . $method_frame->heartbeat);
272 1 $self->push_pending(
273 'Connection::Open' => $self->can('connection_open'),
274 # 'Channel::Open' => $self->can('channel_open'),
275 );
276 }
277  
278 =head2 connection_open
279  
280 =cut
281  
282 1 ▶sub connection_open {
Subroutine connection_open: called CC=1 SCAR=0.0
Pod documented
283 1 my ($self, $frame) = @_;
284 1 $self->push_pending(
285 'Channel::Open' => $self->can('channel_open'),
286 );
287 1 $self->send_frame(
288 Net::AMQP::Protocol::Connection::OpenOk->new(
289 reserved_1 => '',
290 )
291 );
292 }
293  
294 =head2 channel_open
295  
296 =cut
297  
298 1 ▶sub channel_open {
Subroutine channel_open: called CC=2 SCAR=7.2
Pod documented
299 1 my ($self, $frame) = @_;
300 1 $self->push_pending(
301 'Channel::Open' => $self->can('channel_open'),
302 );
303 1 my $method_frame = $frame->method_frame;
304 1 my $id = $frame->channel;
305 1 $self->debug_printf("Channel [%d] open request", $id);
306 1 ▶ if(exists $self->{channels}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"channels"}{$id}) 0 1 1
307 0 $self->debug_printf("Channel [%d] already assigned, rejecting", $id);
308 0 $self->send_frame(
309 Net::AMQP::Protocol::Channel->new(
310 reserved_1 => '',
311 )
312 );
313  
314 }
315  
316 {
317 1 my $frame = Net::AMQP::Frame::Method->new(
318 channel => $id,
319 method_frame => Net::AMQP::Protocol::Channel::OpenOk->new(
320 reserved_1 => '',
321 )
322 );
323 1 $self->send_frame(
324 $frame
325 );
326 }
327 }
328  
329 =head2 conn_close
330  
331 =cut
332  
333 1 ▶sub conn_close {
Subroutine conn_close: called CC=1 SCAR=0.0
Pod documented
334 1 my ($self, $frame) = @_;
335 1 $self->debug_printf("Close request");
336 1 my $method_frame = $frame->method_frame;
337 1 $self->debug_printf("Code: " . $method_frame->reply_code);
338 1 $self->debug_printf("Text: " . $method_frame->reply_text);
339 1 $self->debug_printf("Class: " . $method_frame->class_id);
340 1 $self->debug_printf("Method: " . $method_frame->method_id);
341 1 $self->send_frame(
342 Net::AMQP::Protocol::Connection::CloseOk->new(
343 )
344 );
345 }
346  
347 =head2 debug_printf
348  
349 =cut
350  
351 38 ▶sub debug_printf {
Subroutine debug_printf: called CC=1 SCAR=0.0
Pod documented
352 38 my ($self, $fmt, @args) = @_;
353 # strip CR/LF/FF
354 38 $fmt =~ s/\v+/ /g;
355 # warn sprintf "$fmt\n" => @args;
356 38 $self
357 }
358  
359 5 1;
360  
361 __END__
« AMQP/ConnectionManager/Connection.pm ↑ Summary AMQP/Server/Connection.pm »