Lim/RPC/Call.pm

filter: Statementstmt 24.0% 44 / 183 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 70.0% 14 / 20 Podpod 100% 4 / 4 totaltotal 19.0% 62 / 326 CC 52 SCAR 67.8
CC
52
Cov
15%
new
1560.0
__ANON__
156.0
DESTROY
6.0
CRAP 883.9
↑ Summary Lim/RPC/Server.pm »
1 package Lim::RPC::Call;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use URI::Split ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Lim::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Lim::RPC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Lim::RPC::Protocols ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Lim::RPC::Transport::Clients ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 ...
26  
27 =head1 VERSION
28  
29 See L<Lim> for version.
30  
31 =over 4
32  
33 =item OK
34  
35 =item ERROR
36  
37 =back
38  
39 =cut
40  
41 10 our $VERSION = $Lim::VERSION;
42  
43 sub OK (){ 1 }
44 sub ERROR (){ -1 }
45  
46 =head1 SYNOPSIS
47  
48 ...
49  
50 =head1 SUBROUTINES/METHODS
51  
52 =head2 new
53  
54 =cut
55  
56 0 ▶sub new {
Subroutine new: not called CC=39 SCAR=73.5
Pod documented
57 0 my $this = shift;
58 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
59 0 my $self = {
60 logger => Log::Log4perl->get_logger($class),
61 status => 0
62 };
63 0 bless $self, $class;
64 0 weaken($self->{logger});
65 0 my $real_self = $self;
66 0 weaken($self);
67  
68 0 $self->{plugin} = shift;
69 0 $self->{call} = shift;
70 0 $self->{call_def} = shift;
71 0 $self->{component} = shift;
72 0 my ($data, $cb, $args, $method, $uri);
73  
74 0 $args = {};
75 0 ▶ if (scalar @_ == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
elsif (scalar @_ == 2) { } 0 0 0
elsif (scalar @_ == 3) { } 0 0 0
elsif (scalar @_ > 3) { } 0 0 0
76 0 ▶ unless (ref($_[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0] eq "CODE") 0 0 0
77 0 confess __PACKAGE__, ': Given one argument but its not a CODE callback';
78 }
79  
80 0 $cb = $_[0];
81 }
82 elsif (scalar @_ == 2) {
83 0 ▶ if (ref($_[0]) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 0 0 0
elsif (ref $_[1] eq 'CODE') { } 0 0 0
84 0 $cb = $_[0];
85 0 $args = $_[1];
86 }
87 elsif (ref($_[1]) eq 'CODE') {
88 0 $data = $_[0];
89 0 $cb = $_[1];
90 }
91 else {
92 0 confess __PACKAGE__, ': Given two arguments but non are CODE callback';
93 }
94 }
95 elsif (scalar @_ == 3) {
96 0 ▶ unless (ref($_[1]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[1] eq "CODE") 0 0 0
97 0 confess __PACKAGE__, ': Given three argument but second its not a CODE callback';
98 }
99  
100 0 $data = $_[0];
101 0 $cb = $_[1];
102 0 $args = $_[2];
103 }
104 elsif (scalar @_ > 3) {
105 0 ▶ unless (ref($_[1]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[1] eq "CODE") 0 0 0
106 0 confess __PACKAGE__, ': Given three argument but second its not a CODE callback';
107 }
108  
109 0 $data = shift;
110 0 $cb = shift;
111 0 $args = { @_ };
112 }
113 else {
114 0 confess __PACKAGE__, ': Too many arguments';
115 }
116  
117 0 ▶ unless (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args eq "HASH") 0 0 0
118 0 confess __PACKAGE__, ': Given an arguments argument but its not an hash';
119 }
120  
121 0 ▶ unless (defined $self->{call}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"call"}) 0 0 0
122 0 confess __PACKAGE__, ': No call specified';
123 }
124 0 ▶ unless (defined $self->{call_def} and ref($self->{call_def}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"call_def"} and ref $self->{"call_def"} eq "HASH") 0 0 0
Condition0%0/3
defined $self->{"call_def"} and ref $self->{"call_def"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"call_def"} and ref $self->{"call_def"} eq "HASH"
defined $self->{"call_def"} ref $self->{"call_def"} eq "HASH"
Truth table0%0/3
A and B
A= defined $self->{"call_def"}B= ref $self->{"call_def"} eq "HASH"
ABresult
0X0
100
111
125 0 confess __PACKAGE__, ': No call definition specified or invalid';
126 }
127 0 ▶ unless (blessed $self->{component} and $self->{component}->isa('Lim::Component::Client')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self->{"component"}) and $self->{"component"}->isa("Lim::Component::Client")) 0 0 0
Condition0%0/3
builtin::blessed($self->{"component"}) and $self->{"component"}->isa("Lim::Component::Client")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self->{"component"}) and $self->{"component"}->isa("Lim::Component::Client")
builtin::blessed($self->{"component"}) $self->{"component"}->isa("Lim::Component::Client")
Truth table0%0/3
A and B
A= builtin::blessed($self->{"component"})B= $self->{"component"}->isa("Lim::Component::Client")
ABresult
0X0
100
111
128 0 confess __PACKAGE__, ': No component specified or not a Lim::Component::Client';
129 }
130 0 ▶ unless (defined $cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cb) 0 0 0
131 0 confess __PACKAGE__, ': No cb specified';
132 }
133  
134 0 ▶ if (defined $args->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'uri'}) { } 0 0 0
135 0 my ($scheme, $auth, $path) = URI::Split::uri_split(delete $args->{uri});
136 0 my ($transport, $protocol);
137  
138 0 ▶ if ($scheme =~ /^([a-z0-9_\-\.]+)\+([a-z0-9_\-\.\+]+)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /^([a-z0-9_\-\.]+)\+([a-z0-9_\-\.\+]+)/ou) { } 0 0 0
139 0 ($transport, $protocol) = ($1, $2);
140 }
141 else {
142 0 confess __PACKAGE__, ': Invalid schema in uri '.$uri;
143 }
144  
145 0 $uri = URI->new('', 'http');
146 0 $uri->path($path);
147 0 $uri->authority($auth);
148 0 my ($user, $pass) = split(/:/o, $uri->userinfo);
149  
150 0 $self->{host} = $uri->host;
151 0 $self->{port} = $uri->_port;
152 0 $self->{user} = $user;
153 0 $self->{pass} = $pass;
154 0 $self->{path} = $uri->path;
155 0 $self->{transport} = $transport;
156 0 $self->{protocol} = $protocol;
157 }
158 else {
159 0 foreach (qw(host port user pass path transport protocol)) {
160 0 ▶ $self->{$_} = defined $args->{$_} ? delete $args->{$_} : Lim::Config->{cli}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{$_} ? : 0 0 0
161 }
162 }
163 0 $self->{cb} = $cb;
164  
165 0 foreach (qw(host transport protocol)) {
166 0 ▶ unless (defined $self->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$_}) 0 0 0
167 0 confess __PACKAGE__, ': No '.$_.' specified';
168 }
169 }
170  
171 0 ▶ unless (defined ($self->{protocol_obj} = Lim::RPC::Protocols->instance->protocol($self->{protocol}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($self->{"protocol_obj"} = "Lim::RPC::Protocols"->instance->protocol($self->{"protocol"}))) 0 0 0
172 0 confess __PACKAGE__, ': Unsupported protocol '.$self->{protocol};
173 }
174 0 ▶ unless (defined ($self->{transport_obj} = Lim::RPC::Transport::Clients->instance->transport($self->{transport}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($self->{"transport_obj"} = "Lim::RPC::Transport::Clients"->instance->transport($self->{"transport"}))) 0 0 0
175 0 confess __PACKAGE__, ': Unsupported transport '.$self->{transport};
176 }
177  
178 0 ▶ if (defined $data and ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and ref $data ne "HASH") 0 0 0
Condition0%0/3
defined $data and ref $data ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and ref $data ne "HASH"
defined $data ref $data ne "HASH"
Truth table0%0/3
A and B
A= defined $dataB= ref $data ne "HASH"
ABresult
0X0
100
111
179 0 confess __PACKAGE__, ': Data is not a hash';
180 }
181 0 ▶ if (exists $self->{call_def}->{in}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'call_def'}{'in'}) { } 0 0 0
elsif (defined $data and %$data) { } 0 0 0
182 0 undef $@;
183 0 eval {
184 0 ▶ Lim::RPC::V(defined $data ? $data : {}, $self->{call_def}->{in});
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 0 0
185 };
186 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
187 0 undef $@;
188 0 eval {
189 0 eval 'use Data::Dumper;';
190 confess __PACKAGE__, ': Unable to verify data ', "\n",
191 Dumper(defined $data ? $data : {}), "\n",
192 0 ▶ Dumper($self->{call_def}->{in}), "\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 0 0
193 $@;
194 };
195 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
196 0 confess __PACKAGE__, ': Unable to verify data';
197 }
198 }
199 }
200 ▶ elsif (defined $data and %$data) {
Condition0%0/3
defined $data and %$data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and %$data
defined $data %$data
Truth table0%0/3
A and B
A= defined $dataB= %$data
ABresult
0X0
100
111
201 0 confess __PACKAGE__, ': Data given without in parameter definition';
202 }
203  
204 0 my $request;
205 0 eval {
206 $request = $self->{protocol_obj}->request(
207 0 (map { $_ => $self->{$_} } qw(plugin call path)),
208 data => $data
209 );
210 };
211 0 ▶ if ($@ or !defined $request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $request) 0 0 0
Condition0%0/3
$@ or not defined $request
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not defined $request
$@ not defined $request
Truth table0%0/3
A or not(B)
A= $@B= not defined $request
ABresult
1X1
011
000
212 0 ▶ confess __PACKAGE__, ': Protocol request creation failed: '.(($@) ? $@ : 'Unknown');
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
213 }
214  
215 0 $self->{component}->_addCall($self);
216 $self->{transport_obj}->request(
217 %$args,
218 0 (map { $_ => $self->{$_} } qw(plugin call host port user pass)),
219 request => $request,
220 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
221 0 my (undef, $response) = @_;
222 0 my $data;
223  
224 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
225 0 return;
226 }
227  
228 0 ▶ unless (blessed $response) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($response)) { } 0 0 0
if (do { $response->isa('Lim::Error') }) { } 0 0 0
elsif ($response->isa('HTTP::Response')) { } 0 0 0
229 0 $self->{error} = Lim::Error->new(
230 message => 'Transport returned invalid response',
231 module => $self
232 );
233 0 $self->{status} = ERROR;
234 }
235 elsif ($response->isa('Lim::Error')) {
236 0 $self->{error} = $response;
237 0 $self->{status} = ERROR;
238 }
239 elsif ($response->isa('HTTP::Response')) {
240 0 eval {
241 0 $data = $self->{protocol_obj}->response($response);
242 };
243  
244 0 ▶ if ($@) {
Branch: 3 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (builtin::blessed($data) and $data->isa('Lim::Error')) { } 0 0 0
elsif (ref $data ne 'HASH') { } 0 0 0
245 0 $self->{error} = Lim::Error->new(
246 message => 'Protocol response failure: '.$@,
247 module => $self
248 );
249 0 $self->{status} = ERROR;
250 0 $data = undef;
251 }
252 ▶ elsif (blessed $data and $data->isa('Lim::Error')) {
Condition0%0/3
builtin::blessed($data) and $data->isa("Lim::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Lim::Error")
builtin::blessed($data) $data->isa("Lim::Error")
Truth table0%0/3
A and B
A= builtin::blessed($data)B= $data->isa("Lim::Error")
ABresult
0X0
100
111
253 0 $self->{error} = $data;
254 0 $self->{status} = ERROR;
255 0 $data = undef;
256 }
257 elsif (ref($data) ne 'HASH') {
258 0 $self->{error} = Lim::Error->new(
259 message => 'Protocol returned invalid data from response',
260 module => $self
261 );
262 0 $self->{status} = ERROR;
263 0 $data = undef;
264 }
265 else {
266 0 ▶ if (exists $self->{call_def}->{out}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'call_def'}{'out'}) { } 0 0 0
elsif (%$data) { } 0 0 0
267 0 eval {
268 0 Lim::RPC::V($data, $self->{call_def}->{out});
269 };
270 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
271 0 $self->{error} = Lim::Error->new(
272 message => $@,
273 module => $self
274 );
275 0 $self->{status} = ERROR;
276 0 $data = undef;
277 }
278 else {
279 0 $self->{status} = OK;
280 }
281 }
282 elsif (%$data) {
283 0 $self->{error} = Lim::Error->new(
284 message => 'Invalid data return, does not match definition',
285 module => $self
286 );
287 0 $self->{status} = ERROR;
288 0 $data = undef;
289 }
290 else {
291 0 $self->{status} = OK;
292 }
293 }
294 }
295 else {
296 0 $self->{error} = Lim::Error->new(
297 message => 'Transport returned invalid response',
298 module => $self
299 );
300 0 $self->{status} = ERROR;
301 }
302  
303 0 $self->{cb}->($self, $data);
304 0 $self->{component}->_deleteCall($self);
305 }
306 0 );
307  
308 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 0 0 0
309 0 $real_self;
310 }
311  
312 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
313 0 my ($self) = @_;
314 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 0 0 0
315 }
316  
317 =head2 Successful
318  
319 =cut
320  
321 0 ▶sub Successful {
Subroutine Successful: not called CC=1 SCAR=6.9
Pod documented
322 0 $_[0]->{status} == OK;
323 }
324  
325 =head2 Error
326  
327 =cut
328  
329 0 ▶sub Error {
Subroutine Error: not called CC=1 SCAR=6.9
Pod documented
330 0 $_[0]->{error};
331 }
332  
333 =head2 ResetTimeout
334  
335 =cut
336  
337 0 ▶sub ResetTimeout {
Subroutine ResetTimeout: not called CC=2 SCAR=17.9
Pod documented
338 0 ▶ if (exists $_[0]->{client}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{"client"}) 0 0 0
339 0 $_[0]->{client}->reset_timeout;
340 }
341 }
342  
343 =head1 AUTHOR
344  
345 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
346  
347 =head1 BUGS
348  
349 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
350  
351 =head1 SUPPORT
352  
353 You can find documentation for this module with the perldoc command.
354  
355 perldoc Lim
356  
357 You can also look for information at:
358  
359 =over 4
360  
361 =item * Lim issue tracker (report bugs here)
362  
363 L<https://github.com/jelu/lim/issues>
364  
365 =back
366  
367 =head1 ACKNOWLEDGEMENTS
368  
369 =head1 LICENSE AND COPYRIGHT
370  
371 Copyright 2012-2013 Jerry Lundström.
372  
373 This program is free software; you can redistribute it and/or modify it
374 under the terms of either: the GNU General Public License as published
375 by the Free Software Foundation; or the Artistic License.
376  
377 See http://dev.perl.org/licenses/ for more information.
378  
379  
380 =cut
381  
382 10 1; # End of Lim::RPC::Call
↑ Summary Lim/RPC/Server.pm »