AMQP/RPC/Server.pm

filter: Statementstmt 43.5% 17 / 39 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 41.6% 5 / 12 Podpod 100% 4 / 4 totaltotal 31.3% 26 / 83 CC 11 SCAR 27.6
CC
11
Cov
28%
process_message
42.0
json
12.0
configure
12.0
CRAP 15.8
« AMQP/Utils.pm ↑ Summary AMQP/Queue.pm »
1 package Net::Async::AMQP::RPC::Server;
2 1 $Net::Async::AMQP::RPC::Server::VERSION = '2.000';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use parent qw(Net::Async::AMQP::RPC::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Async::AMQP::RPC::Server - server RPC handling
11  
12 =head1 VERSION
13  
14 version 2.000
15  
16 =head1 DESCRIPTION
17  
18 Provides a basic server implementation for RPC handling.
19  
20 =over 4
21  
22 =item * Declare a queue
23  
24 =item * Declare the RPC exchange
25  
26 =item * Bind our queue to the exchange
27  
28 =item * Start a consumer on the queue
29  
30 =item * For each message, process via subclass-defined handlers and send a reply to the default ('') exchange with the reply_to as the routing key
31  
32 =back
33  
34 =cut
35  
36 1 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head2 queue
41  
42 Returns the server L<Net::Async::AMQP::Queue> instance.
43  
44 =cut
45  
46 0 ▶sub queue { shift->server_queue }
Subroutine queue: not called CC=1 SCAR=6.9
Pod documented
47  
48 =head2 json
49  
50 Returns a L<JSON::MaybeXS> object, for ->encode and ->decode support. This will load L<JSON::MaybeXS> on first call.
51  
52 =cut
53  
54 0 ▶sub json {
Subroutine json: not called CC=3 SCAR=24.8
Pod documented
55 0 ▶ shift->{json} //= do {
Condition0%0/3
+(shift())->{"json"} //= do { eval { do { require JSON::MaybeXS } } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@"; "JSON::MaybeXS"->new }
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"json"} //= do { eval { do { require JSON::MaybeXS } } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@"; "JSON::MaybeXS"->new }
+(shift())->{"json"} do { eval { do { require JSON::MaybeXS } } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@"; "JSON::MaybeXS"->new }
Truth table0%0/3
A //= B
A= +(shift())->{"json"}B= do { eval { do { require JSON::MaybeXS } } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@"; "JSON::MaybeXS"->new }
ABresult
1X1
011
000
56 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require JSON::MaybeXS } } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@" 0 0 0
57 0 require JSON::MaybeXS;
58 } or die "JSON RPC support requires the JSON::MaybeXS module, which could not be loaded:\n$@";
59 0 JSON::MaybeXS->new
60 }
61 }
62  
63 =head2 process_message
64  
65 Called when there is a message to process. Receives several named parameters:
66  
67 =cut
68  
69 0 ▶sub process_message {
Subroutine process_message: not called CC=6 SCAR=37.4
Pod documented
70 0 my ($self, %args) = @_;
71 0 $log->debugf("Have message: %s", join ' ', %args);
72 0 ▶ if(my $code = $self->{json_handler}{$args{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->{'json_handler'}{$args{'type'}}) { } 0 0 0
73 # Run the code, and upgrade to a Future if necessary - we accept immediate values,
74 # or Futures for deferred responses
75 0 ▶ my $f = eval {
Condition0%0/3
eval { do { &$code(%args) or die 'expected hashref or arrayref, had false' } } || 'Future'->fail($@)
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { &$code(%args) or die 'expected hashref or arrayref, had false' } } || 'Future'->fail($@)
eval { do { &$code(%args) or die 'expected hashref or arrayref, had false' } } 'Future'->fail($@)
Truth table0%0/3
A || B
A= eval { do { &$code(%args) or die 'expected hashref or arrayref, had false' } }B= 'Future'->fail($@)
ABresult
1X1
011
000
76 0 ▶ $code->(%args) || die 'expected hashref or arrayref, had false'
Condition0%0/2
&$code(%args) || die('expected hashref or arrayref, had false')
l!l
00
MC/DC0%0/1
&$code(%args) || die('expected hashref or arrayref, had false')
&$code(%args)
Truth table0%0/2
A
A= &$code(%args)
Aresult
11
00
77 } || Future->fail($@);
78 0 ▶ $f = Future->done($f) unless Scalar::Util::blessed($f) && $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("Future") 0 0 0
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
79  
80 # Once our response is ready, send the reply
81 retain_future(
82 0 ▶ $f->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
83 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "Future"->done($self->json->encode(shift())) } } or "Future"->fail({"error", "failed to encode output"}) 0 0 0
84 0 Future->done($self->json->encode(shift))
85 } or Future->fail({ error => 'failed to encode output' })
86 0 ▶ }, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $self->json->encode({ error => shift })
88 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 my $v = shift;
90 $self->reply(
91 reply_to => $args{reply_to},
92 correlation_id => $args{id},
93 type => $args{type},
94 0 content_type => 'application/json',
95 payload => $v
96 )
97 })
98 0 );
99 } else {
100 $self->reply(
101 reply_to => $args{reply_to},
102 correlation_id => $args{id},
103 type => $args{type},
104 0 content_type => 'text/plain',
105 payload => 'no handler defined',
106 );
107 }
108 }
109  
110 =head2 configure
111  
112 Applies configuration:
113  
114 =over 4
115  
116 =item * json_handler - defines the JSON handlers for each type
117  
118 =item * handler - defines default handlers
119  
120 =back
121  
122 =cut
123  
124 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
125 0 my ($self, %args) = @_;
126 0 for (qw(json_handler handler)) {
127 0 ▶ $self->{$_} = delete $args{$_} if exists $args{$_}
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 0 0
128 }
129 0 $self->SUPER::configure(%args)
130 }
131  
132 1 1;
133  
134 __END__
« AMQP/Utils.pm ↑ Summary AMQP/Queue.pm »