Client.pm

filter: Statementstmt 35.9% 119 / 331 Branchbran 23.4% 22 / 94 Conditioncond 20.0% 8 / 40 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 37.3% 28 / 75 Podpod 80.0% 8 / 10 totaltotal 32.5% 187 / 574 CC 71 SCAR 29.7
CC
71
Cov
32%
add_channel
90.0
_parse_frames
72.0
__ANON__
56.0
CRAP 19.6
« Client/Method/Publish.pm ↑ Summary Client/Channel.pm »
1 package Mojo::RabbitMQ::Client;
2 5 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Mojo::Util qw(url_unescape dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::ShareDir qw(dist_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Net::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Net::AMQP::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use Mojo::RabbitMQ::Client::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Mojo::RabbitMQ::Client::LocalQueue;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 require Mojo::RabbitMQ::Client::Consumer;
22 5 require Mojo::RabbitMQ::Client::Publisher;
23  
24 5 our $VERSION = "0.3.1";
25  
26 5 ▶use constant DEBUG => $ENV{MOJO_RABBITMQ_DEBUG} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"MOJO_RABBITMQ_DEBUG"} // 0
l!l
05
MC/DC0%0/1
$ENV{"MOJO_RABBITMQ_DEBUG"} // 0
$ENV{"MOJO_RABBITMQ_DEBUG"}
Truth table50%1/2
A
A= $ENV{"MOJO_RABBITMQ_DEBUG"}
Aresult
11
00
27  
28 5 has is_open => 0;
29 5 has url => undef;
30 13 ▶has tls => sub { shift->_uri_handler('tls') };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 ▶has user => sub { shift->_uri_handler('user') };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 ▶has pass => sub { shift->_uri_handler('pass') };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 ▶has host => sub { shift->_uri_handler('host') };
Subroutine __ANON__: called CC=1 SCAR=0.0
34 0 ▶has port => sub { shift->_uri_handler('port') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 0 ▶has vhost => sub { shift->_uri_handler('vhost') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 3 ▶has params => sub { shift->_uri_handler('params') // Mojo::Parameters->new };
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition66%2/3
(shift())->_uri_handler("params") // "Mojo::Parameters"->new
l!l&&r!l&&!r
210
MC/DC0%0/2
(shift())->_uri_handler("params") // "Mojo::Parameters"->new
(shift())->_uri_handler("params") "Mojo::Parameters"->new
Truth table66%2/3
A // B
A= (shift())->_uri_handler("params")B= "Mojo::Parameters"->new
ABresult
1X1
011
000
37 0 ▶has connect_timeout => sub { $ENV{MOJO_CONNECT_TIMEOUT} // 10 };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{"MOJO_CONNECT_TIMEOUT"} // 10
l!l
00
MC/DC0%0/1
$ENV{"MOJO_CONNECT_TIMEOUT"} // 10
$ENV{"MOJO_CONNECT_TIMEOUT"}
Truth table0%0/2
A
A= $ENV{"MOJO_CONNECT_TIMEOUT"}
Aresult
11
00
38 5 has heartbeat_timeout => 60;
39 5 has heartbeat_received => 0; # When did we receive last heartbeat
40 5 has heartbeat_sent => 0; # When did we sent last heartbeat
41 20 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
42 5 has max_buffer_size => 16384;
43 5 has max_channels => 0;
44 0 ▶has queue => sub { Mojo::RabbitMQ::Client::LocalQueue->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 0 ▶has channels => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46 5 has stream_id => undef;
47  
48 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50 0 $self->{buffer} = '';
51  
52 0 my $id;
53 0 ▶ $id = $self->_connect(sub { $self->_connected($id) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 $self->stream_id($id);
55  
56 0 return $id;
57 }
58  
59 0 ▶sub connect_p {
Subroutine connect_p: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my $self = shift;
61 0 my $promise = Mojo::Promise->new;
62  
63 0 my $id;
64  
65 0 weaken $self;
66 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
67 0 my ($err) = @_;
68 0 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 0 0
69 0 return $promise->reject($err);
70 }
71  
72 0 return $promise->resolve($self);
73 0 };
74  
75 0 ▶ $id = $self->_connect(sub { $self->_connected($id, $handler) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $self->stream_id($id);
77  
78 0 return $promise;
79 }
80  
81 2 ▶sub consumer {
Subroutine consumer: called CC=2 SCAR=6.9
Pod documented
82 2 my ($class, @params) = @_;
83 2 ▶ croak "consumer is a static method" if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 1 2
84  
85 1 return Mojo::RabbitMQ::Client::Consumer->new(@params);
86 }
87  
88 2 ▶sub publisher {
Subroutine publisher: called CC=2 SCAR=6.9
Pod documented
89 2 my ($class, @params) = @_;
90 2 ▶ croak "publisher is a static method" if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 1 2
91  
92 1 return Mojo::RabbitMQ::Client::Publisher->new(@params);
93 }
94  
95 6 ▶sub param {
Subroutine param: called CC=2 SCAR=7.1
Pod documented
96 6 my $self = shift;
97 6 ▶ return undef unless defined $self->params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->params 0 6 6
98 6 return $self->params->param(@_);
99 }
100  
101 0 ▶sub add_channel {
Subroutine add_channel: not called CC=9 SCAR=45.0
Pod documented
102 0 my $self = shift;
103 0 my $channel = shift;
104  
105 0 my $id = $channel->id;
106 0 ▶ if ($id and $self->channels->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $self->channels->{$id}) 0 0 0
Condition0%0/3
$id and $self->channels->{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$id and $self->channels->{$id}
$id $self->channels->{$id}
Truth table0%0/3
A and B
A= $idB= $self->channels->{$id}
ABresult
0X0
100
111
107 0 return $channel->emit(
108 error => 'Channel with id: ' . $id . ' already defined');
109 }
110  
111 0 ▶ if ($self->max_channels > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_channels > 0 and scalar keys %{$self->channels;} >= $self->max_channels) 0 0 0
Condition0%0/3
$self->max_channels > 0 and scalar keys %{$self->channels;} >= $self->max_channels
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_channels > 0 and scalar keys %{$self->channels;} >= $self->max_channels
$self->max_channels > 0 scalar keys %{$self->channels;} >= $self->max_channels
Truth table0%0/3
A and B
A= $self->max_channels > 0B= scalar keys %{$self->channels;} >= $self->max_channels
ABresult
0X0
100
111
112 0 and scalar keys %{$self->channels} >= $self->max_channels)
113 {
114 0 return $channel->emit(error => 'Maximum number of channels reached');
115 }
116  
117 0 ▶ if (not $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
118 0 for my $candidate_id (1 .. (2**16 - 1)) {
119 0 ▶ next if defined $self->channels->{$candidate_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->channels->{$candidate_id} 0 0 0
120 0 $id = $candidate_id;
121 0 last;
122 }
123 0 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
124 0 return $channel->emit(error => 'Ran out of channel ids');
125 }
126 }
127  
128 0 $self->channels->{$id} = $channel->id($id)->client($self);
129 0 weaken $channel->{client};
130  
131 0 return $channel;
132 }
133  
134 0 ▶sub acquire_channel_p {
Subroutine acquire_channel_p: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my $self = shift;
136  
137 0 my $promise = Mojo::Promise->new;
138  
139 0 my $channel = Mojo::RabbitMQ::Client::Channel->new();
140 0 ▶ $channel->catch(sub { $promise->reject(@_); undef $promise });
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 ▶ $channel->on(close => sub { warn "Channel closed" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ $channel->on(open => sub { $promise->resolve(@_); undef $promise });
Subroutine __ANON__: not called CC=1 SCAR=6.9
143  
144 0 $self->open_channel($channel);
145  
146 0 return $promise;
147 }
148  
149 0 ▶sub open_channel {
Subroutine open_channel: not called CC=2 SCAR=17.9
Pod documented
150 0 my $self = shift;
151 0 my $channel = shift;
152  
153 0 ▶ return $channel->emit(error => 'Client connection not opened')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 0 0
154 unless $self->is_open;
155  
156 0 $self->add_channel($channel)->open;
157  
158 0 return $self;
159 }
160  
161 0 ▶sub delete_channel {
Subroutine delete_channel: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 return delete $self->channels->{shift};
164 }
165  
166 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self = shift;
168  
169 0 weaken $self;
170 $self->_write_expect(
171 'Connection::Close' => {},
172 0 ▶ 'Connection::CloseOk' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 warn "-- Connection::CloseOk\n" if DEBUG;
174 0 $self->emit('close');
175 0 $self->_close;
176 },
177 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 $self->_close;
179 }
180 0 );
181 }
182  
183 0 ▶sub _loop { $_[0]->ioloop }
Subroutine _loop: not called CC=1 SCAR=6.9
184  
185 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
186 0 my ($self, $id, $err) = @_;
187  
188 0 $self->emit(error => $err);
189 }
190  
191 20 ▶sub _uri_handler {
Subroutine _uri_handler: called CC=15 SCAR=27.2
192 20 my $self = shift;
193 20 my $attr = shift;
194  
195 20 ▶ return undef unless defined $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->url 2 18 20
196  
197 18 ▶ $self->url(Mojo::URL->new($self->url))
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self->url) and $self->url->isa("Mojo::URL") 18 0 18
Condition33%1/3
builtin::blessed($self->url) and $self->url->isa("Mojo::URL")
!ll&&!rl&&r
1800
MC/DC0%0/2
builtin::blessed($self->url) and $self->url->isa("Mojo::URL")
builtin::blessed($self->url) $self->url->isa("Mojo::URL")
Truth table33%1/3
A and B
A= builtin::blessed($self->url)B= $self->url->isa("Mojo::URL")
ABresult
0X0
100
111
198 unless blessed $self->url && $self->url->isa('Mojo::URL');
199  
200 # Set some defaults
201 18 my %defaults = (
202 tls => 0,
203 user => undef,
204 pass => undef,
205 host => 'localhost',
206 port => 5672,
207 vhost => '/',
208 params => undef
209 );
210  
211 # Check secure scheme in url
212 18 ▶ $defaults{tls} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->url->scheme =~ /^(amqp|rabbitmq)s$/u 2 16 18
213 if $self->url->scheme
214 =~ /^(amqp|rabbitmq)s$/; # Fallback support for rabbitmq scheme name
215 18 ▶ $defaults{port} = 5671 if $defaults{tls};
Branch: 1 branch
BranchTrueFalseTotal
if $defaults{"tls"} 2 16 18
216  
217 # Get host & port
218 18 ▶ $defaults{host} = $self->url->host
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->url->host and $self->url->host ne "" 7 11 18
Condition66%2/3
defined $self->url->host and $self->url->host ne ""
!ll&&!rl&&r
0117
MC/DC50%1/2
defined $self->url->host and $self->url->host ne ""
defined $self->url->host $self->url->host ne ""
Truth table66%2/3
A and B
A= defined $self->url->hostB= $self->url->host ne ""
ABresult
0X0
100
111
219 if defined $self->url->host && $self->url->host ne '';
220 18 ▶ $defaults{port} = $self->url->port if defined $self->url->port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->url->port 4 14 18
221  
222 # Get user & password
223 18 my $userinfo = $self->url->userinfo;
224 18 ▶ if (defined $userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $userinfo) 5 13 18
225 5 my ($user, $pass) = split /:/, $userinfo;
226 5 $defaults{user} = $user;
227 5 $defaults{pass} = $pass;
228 }
229  
230 18 my $vhost = url_unescape $self->url->path;
231 18 $vhost =~ s|^/(.+)$|$1|;
232 18 ▶ $defaults{vhost} = $vhost if defined $vhost && $vhost ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $vhost and $vhost ne "" 9 9 18
Condition66%2/3
defined $vhost and $vhost ne ""
!ll&&!rl&&r
099
MC/DC50%1/2
defined $vhost and $vhost ne ""
defined $vhost $vhost ne ""
Truth table66%2/3
A and B
A= defined $vhostB= $vhost ne ""
ABresult
0X0
100
111
233  
234 # Query params
235 18 my $params = $defaults{params} = $self->url->query;
236  
237 # Handle common aliases to internal names
238 18 my %aliases = (
239 cacertfile => 'ca',
240 certfile => 'cert',
241 keyfile => 'key',
242 fail_if_no_peer_cert => 'verify',
243 connection_timeout => 'timeout'
244 );
245 $params->param($aliases{$_}, $params->param($_))
246 18 foreach grep { defined $params->param($_) } keys %aliases;
247  
248 # Some query parameters are translated to attribute values
249 18 my %attributes = (
250 heartbeat_timeout => 'heartbeat',
251 connect_timeout => 'timeout',
252 max_channels => 'channel_max'
253 );
254 $self->$_($params->param($attributes{$_}))
255 18 foreach grep { defined $params->param($attributes{$_}) } keys %attributes;
256  
257 # Set all
258 18 $self->$_($defaults{$_}) foreach keys %defaults;
259  
260 18 return $self->$attr;
261 }
262  
263 0 ▶sub _close {
Subroutine _close: not called CC=1 SCAR=6.9
264 0 my $self = shift;
265 0 $self->_loop->stream($self->stream_id)->close_gracefully;
266 }
267  
268 0 ▶sub _handle {
Subroutine _handle: not called CC=1 SCAR=6.9
269 0 my ($self, $id, $close) = @_;
270  
271 0 $self->emit('disconnect');
272  
273 0 $self->_loop->remove($id);
274 }
275  
276 0 ▶sub _read {
Subroutine _read: not called CC=1 SCAR=6.9
277 0 my ($self, $id, $chunk) = @_;
278  
279 0 warn "<- @{[dumper $chunk]}" if DEBUG;
280 0 $self->{buffer} .= $chunk;
281 0 $self->_parse_frames;
282  
283 0 return;
284 }
285  
286 0 ▶sub _parse_frames {
Subroutine _parse_frames: not called CC=8 SCAR=42.8
287 0 my $self = shift;
288  
289 0 for my $frame (Net::AMQP->parse_raw_frames(\$self->{buffer})) {
290  
291 0 ▶ if ($frame->isa('Net::AMQP::Frame::Heartbeat')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($frame->isa('Net::AMQP::Frame::Heartbeat')) { } 0 0 0
elsif ($frame->isa('Net::AMQP::Frame::Method') and $frame->method_frame->isa('Net::AMQP::Protocol::Connection::Close')) { } 0 0 0
elsif ($frame->channel == 0) { } 0 0 0
292 0 $self->heartbeat_received(time());
293 }
294 ▶ elsif ($frame->isa('Net::AMQP::Frame::Method')
Condition0%0/3
$frame->isa("Net::AMQP::Frame::Method") and $frame->method_frame->isa("Net::AMQP::Protocol::Connection::Close")
!ll&&!rl&&r
000
MC/DC0%0/2
$frame->isa("Net::AMQP::Frame::Method") and $frame->method_frame->isa("Net::AMQP::Protocol::Connection::Close")
$frame->isa("Net::AMQP::Frame::Method") $frame->method_frame->isa("Net::AMQP::Protocol::Connection::Close")
Truth table0%0/3
A and B
A= $frame->isa("Net::AMQP::Frame::Method")B= $frame->method_frame->isa("Net::AMQP::Protocol::Connection::Close")
ABresult
0X0
100
111
295 and $frame->method_frame->isa('Net::AMQP::Protocol::Connection::Close'))
296 {
297 0 $self->is_open(0);
298  
299 0 $self->_write_frame(Net::AMQP::Protocol::Connection::CloseOk->new());
300 $self->emit(disconnect => "Server side disconnection: "
301 0 . $frame->method_frame->{reply_text});
302 }
303 elsif ($frame->channel == 0) {
304 0 $self->queue->push($frame);
305 }
306 else {
307 0 my $channel = $self->channels->{$frame->channel};
308 0 ▶ if (defined $channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) { } 0 0 0
309 0 $channel->_push_queue_or_consume($frame);
310 }
311 else {
312 0 ▶ $self->emit(
Condition0%0/2
$frame->channel // "(undef)"
l!l
00
MC/DC0%0/1
$frame->channel // "(undef)"
$frame->channel
Truth table0%0/2
A
A= $frame->channel
Aresult
11
00
313 error => "Unknown channel id received: "
314 . ($frame->channel // '(undef)'),
315 $frame
316 );
317 }
318 }
319 }
320 }
321  
322 0 ▶sub _connect {
Subroutine _connect: not called CC=2 SCAR=17.9
323 0 my ($self, $cb) = @_;
324  
325 # Options
326 # Parse according to (https://www.rabbitmq.com/uri-spec.html)
327 0 my $options = {
328 address => $self->host,
329 port => $self->port,
330 timeout => $self->connect_timeout,
331 tls => $self->tls,
332 tls_ca => scalar $self->param('ca'),
333 tls_cert => scalar $self->param('cert'),
334 tls_key => scalar $self->param('key')
335 };
336 0 my $verify = $self->param('verify');
337 0 ▶ $options->{tls_verify} = hex $verify if defined $verify;
Branch: 1 branch
BranchTrueFalseTotal
if defined $verify 0 0 0
338  
339 # Connect
340 0 weaken $self;
341 0 my $id;
342 return $id = $self->_loop->client(
343 0 ▶ $options => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
344 0 my ($loop, $err, $stream) = @_;
345  
346 # Connection error
347 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
348 0 ▶ return $self->_error($id, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
349  
350 0 $self->emit(connect => $stream);
351  
352 # Connection established
353 0 ▶ $stream->on(timeout => sub { $self->_error($id, 'Inactivity timeout') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 ▶ $stream->on(close => sub { $self && $self->_handle($id, 1) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_handle($id, 1) 0 0 0
355 0 ▶ $stream->on(error => sub { $self && $self->_error($id, pop) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_error($id, pop()) 0 0 0
356 0 ▶ $stream->on(read => sub { $self && $self->_read($id, pop) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_read($id, pop()) 0 0 0
357 0 $cb->();
358 }
359 0 );
360 }
361  
362 0 ▶sub _connected {
Subroutine _connected: not called CC=2 SCAR=17.9
363 0 my ($self, $id, $cb) = @_;
364  
365 # Inactivity timeout
366 0 my $stream = $self->_loop->stream($id)->timeout(0);
367  
368 # Store connection information in transaction
369 0 my $handle = $stream->handle;
370  
371 # Detect that xml spec was already loaded
372 0 my $loaded = eval { Net::AMQP::Protocol::Connection::StartOk->new; 1 };
373 0 ▶ unless ($loaded) { # Load AMQP specs
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 0 0 0
374 0 my $file = "amqp0-9-1.stripped.extended.xml";
375  
376 # Original spec is in "fixed_amqp0-8.xml"
377 0 my $share = dist_file('Mojo-RabbitMQ-Client', $file);
378 0 Net::AMQP::Protocol->load_xml_spec($share);
379 }
380  
381 0 $self->_write($id => Net::AMQP::Protocol->header);
382  
383 0 weaken $self;
384 $self->_expect(
385 0 ▶ 'Connection::Start' => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
386 0 my $frame = shift;
387  
388 0 my @server_mechanisms = split /\s/, $frame->method_frame->mechanisms;
389 0 ▶ my $param_mechanism = $self->param('auth_mechanism') // '';
Condition0%0/2
$self->param("auth_mechanism") // ""
l!l
00
MC/DC0%0/1
$self->param("auth_mechanism") // ""
$self->param("auth_mechanism")
Truth table0%0/2
A
A= $self->param("auth_mechanism")
Aresult
11
00
390 0 my @client_mechanisms = ('AMQPLAIN', 'EXTERNAL');
391 0 ▶ @client_mechanisms = ($param_mechanism) if ($param_mechanism);
Branch: 1 branch
BranchTrueFalseTotal
if $param_mechanism 0 0 0
392 0 warn "-- Server mechanisms: @server_mechanisms\n" if DEBUG;
393 0 warn "-- Client mechanisms: @client_mechanisms\n" if DEBUG;
394 0 my $mechanism;
395 0 for my $cand (@client_mechanisms) {
396 0 ▶ if (grep { $_ eq $cand } @server_mechanisms) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $cand;} @server_mechanisms) 0 0 0
397 0 $mechanism = $cand;
398 0 last;
399 }
400 }
401 0 ▶ return $self->emit(error => 'No authentication mechanism could be negotiated')
Branch: 1 branch
BranchTrueFalseTotal
unless $mechanism 0 0 0
402 unless $mechanism;
403  
404 0 my @locales = split /\s/, $frame->method_frame->locales;
405 return $self->emit(error => 'en_US is not found in locales')
406 0 ▶ if none { $_ eq 'en_US' } @locales;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq "en_US"; } , @locales) 0 0 0
407  
408 0 $self->{_server_properties} = $frame->method_frame->server_properties;
409  
410 0 warn "-- Connection::Start {product: " . $self->{_server_properties}->{product} . ", version: " . $self->{_server_properties}->{version} . "}\n" if DEBUG;
411 0 $self->_write_frame(
412 Net::AMQP::Protocol::Connection::StartOk->new(
413 client_properties => {
414 platform => 'Perl',
415 product => __PACKAGE__,
416 information => 'https://github.com/inway/mojo-rabbitmq-client',
417 version => __PACKAGE__->VERSION,
418 },
419 mechanism => $mechanism,
420 response => {LOGIN => $self->user, PASSWORD => $self->pass},
421 locale => 'en_US',
422 ),
423 );
424  
425 0 $self->_tune($id, $cb);
426 },
427 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
428 0 $self->emit(error => 'Unable to start connection: ' . shift);
429 }
430 0 );
431 }
432  
433 0 ▶sub _tune {
Subroutine _tune: not called CC=1 SCAR=6.9
434 0 my ($self, $id, $cb) = @_;
435  
436 0 weaken $self;
437 $self->_expect(
438 0 ▶ 'Connection::Tune' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
439 0 my $frame = shift;
440  
441 0 my $method_frame = $frame->method_frame;
442 0 $self->max_buffer_size($method_frame->frame_max);
443  
444 0 ▶ my $heartbeat = $self->heartbeat_timeout || $method_frame->heartbeat;
Condition0%0/3
$self->heartbeat_timeout || $method_frame->heartbeat
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->heartbeat_timeout || $method_frame->heartbeat
$self->heartbeat_timeout $method_frame->heartbeat
Truth table0%0/3
A || B
A= $self->heartbeat_timeoutB= $method_frame->heartbeat
ABresult
1X1
011
000
445  
446 0 warn "-- Connection::Tune {frame_max: " . $method_frame->frame_max . ", heartbeat: " . $method_frame->heartbeat . "}\n" if DEBUG;
447 # Confirm
448 0 $self->_write_frame(
449 Net::AMQP::Protocol::Connection::TuneOk->new(
450 channel_max => $method_frame->channel_max,
451 frame_max => $method_frame->frame_max,
452 heartbeat => $heartbeat,
453 ),
454 );
455  
456 # According to https://www.rabbitmq.com/amqp-0-9-1-errata.html
457 # The client should start sending heartbeats after receiving a Connection.Tune
458 # method, and start monitoring heartbeats after sending Connection.Open.
459 # -and-
460 # Heartbeat frames are sent about every timeout / 2 seconds. After two missed
461 # heartbeats, the peer is considered to be unreachable.
462 $self->{heartbeat_tid} = $self->_loop->recurring(
463 0 ▶ $heartbeat / 2 => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
464 0 ▶ return unless time() - $self->heartbeat_sent > $heartbeat / 2;
Branch: 1 branch
BranchTrueFalseTotal
unless time - $self->heartbeat_sent > $heartbeat / 2 0 0 0
465 0 $self->_write_frame(Net::AMQP::Frame::Heartbeat->new());
466 0 $self->heartbeat_sent(time());
467 }
468 0 ▶ ) if $heartbeat;
Branch: 1 branch
BranchTrueFalseTotal
if $heartbeat 0 0 0
469  
470 $self->_write_expect(
471 'Connection::Open' =>
472 {virtual_host => $self->vhost, capabilities => '', insist => 1,},
473 0 ▶ 'Connection::OpenOk' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
474 0 warn "-- Connection::OpenOk\n" if DEBUG;
475  
476 0 $self->is_open(1);
477 0 $self->emit('open');
478 0 ▶ $cb->() if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 0 0 0
479 },
480 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
481 0 my $err = shift;
482 0 $self->emit(error => 'Unable to open connection: ' . $err);
483 0 ▶ $cb->($err) if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 0 0 0
484 }
485 0 );
486 },
487 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
488 0 $self->emit(error => 'Unable to tune connection: ' . shift);
489 }
490 0 );
491 }
492  
493 0 ▶sub _write_expect {
Subroutine _write_expect: not called CC=2 SCAR=17.9
494 0 my $self = shift;
495 0 my ($method, $args, $exp, $cb, $failure_cb, $channel_id) = @_;
496 0 $method = 'Net::AMQP::Protocol::' . $method;
497  
498 0 ▶ $channel_id ||= 0;
Condition0%0/2
$channel_id ||= 0
l!l
00
MC/DC0%0/1
$channel_id ||= 0
$channel_id
Truth table0%0/2
A
A= $channel_id
Aresult
11
00
499  
500 0 my $method_frame = Net::AMQP::Frame::Method->new(
501 method_frame => $method->new(%$args)
502 );
503  
504 0 $self->_write_frame(
505 $method_frame,
506 $channel_id
507 );
508  
509 0 return $self->_expect($exp, $cb, $failure_cb, $channel_id);
510 }
511  
512 0 ▶sub _expect {
Subroutine _expect: not called CC=7 SCAR=40.3
513 0 my $self = shift;
514 0 my ($exp, $cb, $failure_cb, $channel_id) = @_;
515 0 ▶ my @expected = ref($exp) eq 'ARRAY' ? @$exp : ($exp);
Branch: 1 branch
BranchTrueFalseTotal
ref $exp eq 'ARRAY' ? : 0 0 0
516  
517 0 ▶ $channel_id ||= 0;
Condition0%0/2
$channel_id ||= 0
l!l
00
MC/DC0%0/1
$channel_id ||= 0
$channel_id
Truth table0%0/2
A
A= $channel_id
Aresult
11
00
518  
519 0 my $queue;
520 0 ▶ if (!$channel_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $channel_id) { } 0 0 0
521 0 $queue = $self->queue;
522 }
523 else {
524 0 my $channel = $self->channels->{$channel_id};
525 0 ▶ if (defined $channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) { } 0 0 0
526 0 $queue = $channel->queue;
527 }
528 else {
529 0 ▶ $failure_cb->(
Condition0%0/2
$channel_id // "(undef)"
l!l
00
MC/DC0%0/1
$channel_id // "(undef)"
$channel_id
Truth table0%0/2
A
A= $channel_id
Aresult
11
00
530 "Unknown channel id received: " . ($channel_id // '(undef)'));
531 }
532 }
533  
534 0 ▶ return unless $queue;
Branch: 1 branch
BranchTrueFalseTotal
unless $queue 0 0 0
535  
536 $queue->get(
537 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
538 0 my $frame = shift;
539  
540 0 ▶ return $failure_cb->("Received data is not method frame")
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->isa("Net::AMQP::Frame::Method") 0 0 0
541 if not $frame->isa("Net::AMQP::Frame::Method");
542  
543 0 my $method_frame = $frame->method_frame;
544 0 for my $exp (@expected) {
545 0 ▶ return $cb->($frame)
Branch: 1 branch
BranchTrueFalseTotal
if $method_frame->isa('Net::AMQP::Protocol::' . $exp) 0 0 0
546 if $method_frame->isa("Net::AMQP::Protocol::" . $exp);
547 }
548  
549 0 $failure_cb->("Method is not "
550 . join(', ', @expected)
551 . ". It's "
552 . ref($method_frame));
553 }
554 0 );
555 }
556  
557 0 ▶sub _write_frame {
Subroutine _write_frame: not called CC=3 SCAR=24.8
558 0 my $self = shift;
559 0 my $id = $self->stream_id;
560 0 my ($out, $channel, $cb) = @_;
561  
562 0 ▶ if ($out->isa('Net::AMQP::Protocol::Base')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out->isa("Net::AMQP::Protocol::Base")) 0 0 0
563 0 $out = $out->frame_wrap;
564 }
565 0 ▶ $out->channel($channel // 0);
Condition0%0/2
$channel // 0
l!l
00
MC/DC0%0/1
$channel // 0
$channel
Truth table0%0/2
A
A= $channel
Aresult
11
00
566  
567 0 return $self->_write($id, $out->to_raw_frame, $cb);
568 }
569  
570 0 ▶sub _write {
Subroutine _write: not called CC=2 SCAR=17.9
571 0 my $self = shift @_;
572 0 my $id = shift @_;
573 0 my $frame = shift @_;
574 0 my $cb = shift @_;
575  
576 0 warn "-> @{[dumper $frame]}" if DEBUG;
577  
578 0 utf8::downgrade($frame);
579 0 ▶ $self->_loop->stream($id)->write($frame => $cb)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_loop->stream($id) 0 0 0
580 if defined $self->_loop->stream($id);
581 }
582  
583 20 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.4
584 20 my $self = shift;
585 20 ▶ my $ioloop = $self->ioloop or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ioloop = $self->ioloop or return 20 0 20
586 20 my $heartbeat_tid = $self->{heartbeat_tid};
587  
588 20 ▶ $ioloop->remove($heartbeat_tid) if $heartbeat_tid;
Branch: 1 branch
BranchTrueFalseTotal
if $heartbeat_tid 0 20 20
589 }
590  
591 5 1;
592  
593 =encoding utf8
594  
595 =head1 NAME
596  
597 Mojo::RabbitMQ::Client - Mojo::IOLoop based RabbitMQ client
598  
599 =head1 SYNOPSIS
600  
601 use Mojo::RabbitMQ::Client;
602  
603 # Supply URL according to (https://www.rabbitmq.com/uri-spec.html)
604 my $client = Mojo::RabbitMQ::Client->new(
605 url => 'amqp://guest:guest@127.0.0.1:5672/');
606  
607 # Catch all client related errors
608 $client->catch(sub { warn "Some error caught in client"; });
609  
610 # When connection is in Open state, open new channel
611 $client->on(
612 open => sub {
613 my ($client) = @_;
614  
615 # Create a new channel with auto-assigned id
616 my $channel = Mojo::RabbitMQ::Client::Channel->new();
617  
618 $channel->catch(sub { warn "Error on channel received"; });
619  
620 $channel->on(
621 open => sub {
622 my ($channel) = @_;
623 $channel->qos(prefetch_count => 1)->deliver;
624  
625 # Publish some example message to test_queue
626 my $publish = $channel->publish(
627 exchange => 'test',
628 routing_key => 'test_queue',
629 body => 'Test message',
630 mandatory => 0,
631 immediate => 0,
632 header => {}
633 );
634 # Deliver this message to server
635 $publish->deliver;
636  
637 # Start consuming messages from test_queue
638 my $consumer = $channel->consume(queue => 'test_queue');
639 $consumer->on(message => sub { say "Got a message" });
640 $consumer->deliver;
641 }
642 );
643 $channel->on(close => sub { $log->error('Channel closed') });
644  
645 $client->open_channel($channel);
646 }
647 );
648  
649 # Start connection
650 $client->connect();
651  
652 # Start Mojo::IOLoop if not running already
653 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
654  
655 =head2 CONSUMER
656  
657 use Mojo::RabbitMQ::Client;
658 my $consumer = Mojo::RabbitMQ::Client->consumer(
659 url => 'amqp://guest:guest@127.0.0.1:5672/?exchange=mojo&queue=mojo',
660 defaults => {
661 qos => {prefetch_count => 1},
662 queue => {durable => 1},
663 consumer => {no_ack => 0},
664 }
665 );
666  
667 $consumer->catch(sub { die "Some error caught in Consumer" } );
668 $consumer->on('success' => sub { say "Consumer ready" });
669 $consumer->on(
670 'message' => sub {
671 my ($consumer, $message) = @_;
672  
673 $consumer->channel->ack($message)->deliver;
674 }
675 );
676 $consumer->start();
677  
678 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
679  
680 =head2 PUBLISHER
681  
682 use Mojo::RabbitMQ::Client;
683 my $publisher = Mojo::RabbitMQ::Client->publisher(
684 url => 'amqp://guest:guest@127.0.0.1:5672/?exchange=mojo&routing_key=mojo'
685 );
686  
687 $publisher->publish('plain text');
688  
689 $publisher->publish(
690 {encode => { to => 'json'}},
691 routing_key => 'mojo_mq'
692 )->then(sub {
693 say "Message published";
694 })->catch(sub {
695 die "Publishing failed"
696 })->wait;
697  
698 =head1 DESCRIPTION
699  
700 L<Mojo::RabbitMQ::Client> is a rewrite of L<AnyEvent::RabbitMQ> to work on top of L<Mojo::IOLoop>.
701  
702 =head1 EVENTS
703  
704 L<Mojo::RabbitMQ::Client> inherits all events from L<Mojo::EventEmitter> and can emit the
705 following new ones.
706  
707 =head2 connect
708  
709 $client->on(connect => sub {
710 my ($client, $stream) = @_;
711 ...
712 });
713  
714 Emitted when TCP/IP connection with RabbitMQ server is established.
715  
716 =head2 open
717  
718 $client->on(open => sub {
719 my ($client) = @_;
720 ...
721 });
722  
723 Emitted AMQP protocol Connection.Open-Ok method is received.
724  
725 =head2 close
726  
727 $client->on(close => sub {
728 my ($client) = @_;
729 ...
730 });
731  
732 Emitted on reception of Connection.Close-Ok method.
733  
734 =head2 disconnect
735  
736 $client->on(close => sub {
737 my ($client) = @_;
738 ...
739 });
740  
741 Emitted when TCP/IP connection gets disconnected.
742  
743 =head1 ATTRIBUTES
744  
745 L<Mojo::RabbitMQ::Client> has following attributes.
746  
747 =head2 tls
748  
749 my $tls = $client->tls;
750 $client = $client->tls(1)
751  
752 Force secure connection. Default is disabled (C<0>).
753  
754 =head2 user
755  
756 my $user = $client->user;
757 $client = $client->user('guest')
758  
759 Sets username for authorization, by default it's not defined.
760  
761 =head2 pass
762  
763 my $pass = $client->pass;
764 $client = $client->pass('secret')
765  
766 Sets user password for authorization, by default it's not defined.
767  
768 =head2 host
769  
770 my $host = $client->host;
771 $client = $client->host('localhost')
772  
773 Hostname or IP address of RabbitMQ server. Defaults to C<localhost>.
774  
775 =head2 port
776  
777 my $port = $client->port;
778 $client = $client->port(1234)
779  
780 Port on which RabbitMQ server listens for new connections.
781 Defaults to C<5672>, which is standard RabbitMQ server listen port.
782  
783 =head2 vhost
784  
785 my $vhost = $client->vhost;
786 $client = $client->vhost('/')
787  
788 RabbitMQ virtual server to user. Default is C</>.
789  
790 =head2 params
791  
792 my $params = $client->params;
793 $client = $client->params(Mojo::Parameters->new('verify=1'))
794  
795 Sets additional parameters for connection. Default is not defined.
796  
797 For list of supported parameters see L</"SUPPORTED QUERY PARAMETERS">.
798  
799 =head2 url
800  
801 my $url = $client->url;
802 $client = $client->url('amqp://...');
803  
804 Sets all connection parameters in one string, according to specification from
805 L<https://www.rabbitmq.com/uri-spec.html>.
806  
807 amqp_URI = "amqp[s]://" amqp_authority [ "/" vhost ] [ "?" query ]
808  
809 amqp_authority = [ amqp_userinfo "@" ] host [ ":" port ]
810  
811 amqp_userinfo = username [ ":" password ]
812  
813 username = *( unreserved / pct-encoded / sub-delims )
814  
815 password = *( unreserved / pct-encoded / sub-delims )
816  
817 vhost = segment
818  
819 =head2 heartbeat_timeout
820  
821 my $timeout = $client->heartbeat_timeout;
822 $client = $client->heartbeat_timeout(180);
823  
824 Heartbeats are use to monitor peer reachability in AMQP.
825 Default value is C<60> seconds, if set to C<0> no heartbeats will be sent.
826  
827 =head2 connect_timeout
828  
829 my $timeout = $client->connect_timeout;
830 $client = $client->connect_timeout(5);
831  
832 Connection timeout used by L<Mojo::IOLoop::Client>.
833 Defaults to environment variable C<MOJO_CONNECT_TIMEOUT> or C<10> seconds
834 if nothing else is set.
835  
836 =head2 max_channels
837  
838 my $max_channels = $client->max_channels;
839 $client = $client->max_channels(10);
840  
841 Maximum number of channels allowed to be active. Defaults to C<0> which
842 means no implicit limit.
843  
844 When you try to call C<add_channel> over limit an C<error> will be
845 emitted on channel saying that: I<Maximum number of channels reached>.
846  
847 =head1 STATIC METHODS
848  
849 =head2 consumer
850  
851 my $client = Mojo::RabbitMQ::Client->consumer(...)
852  
853 Shortcut for creating L<Mojo::RabbitMQ::Client::Consumer>.
854  
855 =head2 publisher
856  
857 my $client = Mojo::RabbitMQ::Client->publisher(...)
858  
859 Shortcut for creating L<Mojo::RabbitMQ::Client::Publisher>.
860  
861 =head1 METHODS
862  
863 L<Mojo::RabbitMQ::Client> inherits all methods from L<Mojo::EventEmitter> and implements
864 the following new ones.
865  
866 =head2 connect
867  
868 $client->connect();
869  
870 Tries to connect to RabbitMQ server and negotiate AMQP protocol.
871  
872 =head2 close
873  
874 $client->close();
875  
876 =head2 param
877  
878 my $param = $client->param('name');
879 $client = $client->param(name => 'value');
880  
881 =head2 add_channel
882  
883 my $channel = Mojo::RabbitMQ::Client::Channel->new();
884 ...
885 $channel = $client->add_channel($channel);
886 $channel->open;
887  
888 =head2 open_channel
889  
890 my $channel = Mojo::RabbitMQ::Client::Channel->new();
891 ...
892 $client->open_channel($channel);
893  
894 =head2 delete_channel
895  
896 my $removed = $client->delete_channel($channel->id);
897  
898 =head1 SUPPORTED QUERY PARAMETERS
899  
900 There's no formal specification, nevertheless a list of common parameters
901 recognized by officially supported RabbitMQ clients is maintained here:
902 L<https://www.rabbitmq.com/uri-query-parameters.html>.
903  
904 Some shortcuts are also supported, you'll find them in parenthesis.
905  
906 Aliases are less significant, so when both are specified only primary
907 value will be used.
908  
909 =head2 cacertfile (I<ca>)
910  
911 Path to Certificate Authority file for TLS.
912  
913 =head2 certfile (I<cert>)
914  
915 Path to the client certificate file for TLS.
916  
917 =head2 keyfile (I<key>)
918  
919 Path to the client certificate private key file for TLS.
920  
921 =head2 fail_if_no_peer_cert (I<verify>)
922  
923 TLS verification mode, defaults to 0x01 on the client-side if a certificate
924 authority file has been provided, or 0x00 otherwise.
925  
926 =head2 auth_mechanism
927  
928 Sets the AMQP authentication mechanism. Defaults to AMQPLAIN. AMQPLAIN and
929 EXTERNAL are supported; EXTERNAL will only work if L<Mojo::RabbitMQ::Client> does not need
930 to do anything beyond passing along a username and password if specified.
931  
932 =head2 heartbeat
933  
934 Sets requested heartbeat timeout, just like C<heartbeat_timeout> attribute.
935  
936 =head2 connection_timeout (I<timeout>)
937  
938 Sets connection timeout - see L<connection_timeout> attribute.
939  
940 =head2 channel_max
941  
942 Sets maximum number of channels - see L<max_channels> attribute.
943  
944 =head1 SEE ALSO
945  
946 L<Mojo::RabbitMQ::Client::Channel>, L<Mojo::RabbitMQ::Client::Consumer>, L<Mojo::RabbitMQ::Client::Publisher>
947  
948 =head1 COPYRIGHT AND LICENSE
949  
950 Copyright (C) 2015-2019, Sebastian Podjasek and others
951  
952 Based on L<AnyEvent::RabbitMQ> - Copyright (C) 2010 Masahito Ikuta, maintained by C<< bobtfish@bobtfish.net >>
953  
954 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
955  
956 Contains AMQP specification (F<shared/amqp0-9-1.stripped.extended.xml>) licensed under BSD-style license.
957  
958 =cut
« Client/Method/Publish.pm ↑ Summary Client/Channel.pm »