TwoWay/Connection.pm

filter: Statementstmt 77.2% 85 / 110 Branchbran 60.6% 40 / 66 Conditioncond 54.2% 19 / 35 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 80.0% 16 / 20 Podpod 77.7% 7 / 9 totaltotal 66.6% 174 / 261 SCAR 47.3
CC
46
Cov
68%
callraw
30.0
notify
20.0
_handle_response
18.9
CRAP 113.7
↑ Summary TwoWay.pm »
1 package JSON::RPC2::TwoWay::Connection;
2  
3 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.06'; # VERSION
8  
9 # standard perl
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # cpan
16 2 ▶use JSON::MaybeXS qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use constant ERR_REQ => -32600;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new {
21 2 ▶ my ($class, %opt) = @_;
Subroutine new: called CC=6 SCAR=19.1
Pod documented
22 2 ▶ croak 'no rpc?' unless $opt{rpc} and $opt{rpc}->isa('JSON::RPC2::TwoWay');
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"rpc"} and $opt{"rpc"}->isa("JSON::RPC2::TwoWay") 0 2 2
Condition33%1/3
$opt{"rpc"} and $opt{"rpc"}->isa("JSON::RPC2::TwoWay")
!ll&&!rl&&r
002
MC/DC0%0/2
$opt{"rpc"} and $opt{"rpc"}->isa("JSON::RPC2::TwoWay")
$opt{"rpc"} $opt{"rpc"}->isa("JSON::RPC2::TwoWay")
Truth table33%1/3
A and B
A= $opt{"rpc"}B= $opt{"rpc"}->isa("JSON::RPC2::TwoWay")
ABresult
0X0
100
111
23 #croak 'no stream?' unless $opt->{stream} and $opt->{stream}->can('write');
24 2 ▶ croak 'no write?' unless $opt{write} and ref $opt{write} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"write"} and ref $opt{"write"} eq "CODE" 1 1 2
Condition66%2/3
$opt{"write"} and ref $opt{"write"} eq "CODE"
!ll&&!rl&&r
101
MC/DC50%1/2
$opt{"write"} and ref $opt{"write"} eq "CODE"
$opt{"write"} ref $opt{"write"} eq "CODE"
Truth table66%2/3
A and B
A= $opt{"write"}B= ref $opt{"write"} eq "CODE"
ABresult
0X0
100
111
25 my $self = {
26 calls => {},
27 debug => $opt{debug} // 0,
28 next_id => 1,
29 owner => $opt{owner},
30 request => undef,
31 rpc => $opt{rpc},
32 json => $opt{rpc}->{json},
33 state => undef,
34 write => $opt{write},
35 1 ▶ };
Condition50%1/2
$opt{"debug"} // 0
l!l
10
MC/DC0%0/1
$opt{"debug"} // 0
$opt{"debug"}
Truth table50%1/2
A
A= $opt{"debug"}
Aresult
11
00
36 1 weaken $self->{owner};
37 1 return bless $self, $class;
38 }
39  
40 sub call {
41 5 ▶ my ($self, $name, $args, $cb) = @_;
Subroutine call: called CC=7 SCAR=19.6
Pod documented
42 5 ▶ croak 'no self?' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 5 5
43 5 ▶ croak 'args should be a array or hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" or ref $args eq "HASH" 1 4 5
Condition66%2/3
ref $args eq "ARRAY" or ref $args eq "HASH"
l!l&&r!l&&!r
041
MC/DC50%1/2
ref $args eq "ARRAY" or ref $args eq "HASH"
ref $args eq "ARRAY" ref $args eq "HASH"
Truth table66%2/3
A or B
A= ref $args eq "ARRAY"B= ref $args eq "HASH"
ABresult
1X1
011
000
44 unless ref $args eq 'ARRAY' or ref $args eq 'HASH';
45 4 ▶ croak 'no callback?' unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 1 3 4
46 3 ▶ croak 'callback should be a code reference' if ref $cb ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cb ne "CODE" 1 2 3
47 2 my $id = md5_base64($self->{next_id}++ . $name . $self->{json}->encode($args) . refaddr($cb));
48 2 ▶ croak 'duplicate call id' if $self->{calls}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"calls"}{$id} 0 2 2
49 my $request = $self->{json}->encode({
50 2 jsonrpc => '2.0',
51 method => $name,
52 params => $args,
53 id => $id,
54 });
55 2 $self->{calls}->{$id} = [ $cb, 0 ]; # not raw
56 #say STDERR "call: $request" if $self->{debug};
57 2 $self->write($request);
58 2 return;
59 }
60  
61 sub callraw {
62 0 ▶ my ($self, $request, $cb) = @_;
Subroutine callraw: not called CC=5 SCAR=34.0
Pod documented
63 0 ▶ croak 'no self?' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
64 0 ▶ croak 'request should be a array or hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $request eq "HASH" 0 0 0
65 unless ref $request eq 'HASH';
66 0 ▶ croak 'callback should be a code reference' if ref $cb ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cb ne "CODE" 0 0 0
67 0 my $id = md5_base64($self->{next_id}++ . $self->{json}->encode($request) . refaddr($cb));
68 0 ▶ croak 'duplicate call id' if $self->{calls}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"calls"}{$id} 0 0 0
69 0 $request->{jsonrpc} = '2.0';
70 0 $request->{id} = $id;
71 0 $request = $self->{json}->encode($request);
72 0 $self->{calls}->{$id} = [ $cb, 1 ]; # raw
73 #say STDERR "callraw: $request" if $self->{debug};
74 0 $self->write($request);
75 0 return;
76 }
77  
78 sub notify {
79 0 ▶ my ($self, $name, $args, $cb) = @_;
Subroutine notify: not called CC=4 SCAR=30.0
Pod documented
80 0 ▶ croak 'no self?' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
81 0 ▶ croak 'args should be a array of hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" or ref $args eq "HASH" 0 0 0
Condition0%0/3
ref $args eq "ARRAY" or ref $args eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $args eq "ARRAY" or ref $args eq "HASH"
ref $args eq "ARRAY" ref $args eq "HASH"
Truth table0%0/3
A or B
A= ref $args eq "ARRAY"B= ref $args eq "HASH"
ABresult
1X1
011
000
82 unless ref $args eq 'ARRAY' or ref $args eq 'HASH';
83 my $request = $self->{json}->encode({
84 0 jsonrpc => '2.0',
85 method => $name,
86 params => $args,
87 });
88 #say STDERR "notify: $request" if $self->{debug};
89 0 $self->write($request);
90 0 return;
91 }
92  
93 sub handle {
94 17 ▶ my ($self, $json) = @_;
Subroutine handle: called CC=2 SCAR=6.9
Pod documented
95 17 my @err = $self->_handle(\$json);
96 17 ▶ $self->{rpc}->_error($self, undef, ERR_REQ, 'Invalid Request: ' . $err[0]) if $err[0];
Branch: 1 branch
BranchTrueFalseTotal
if $err[0] 5 12 17
97 17 return @err;
98 }
99  
100 sub _handle {
101 17 ▶ my ($self, $jsonr) = @_;
Subroutine _handle: called CC=12 SCAR=24.9
102 17 ▶ say STDERR ' handle: ', $$jsonr if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 17 17
103 17 local $@;
104 17 my $r = eval { $self->{json}->decode($$jsonr) };
105 17 ▶ return "json decode failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 16 17
106 16 ▶ return 'not a json object' if ref $r ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $r ne "HASH" 1 15 16
107 15 ▶ return 'expected jsonrpc version 2.0' unless defined $r->{jsonrpc} and $r->{jsonrpc} eq '2.0';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"jsonrpc"} and $r->{"jsonrpc"} eq "2.0" 1 14 15
Condition66%2/3
defined $r->{"jsonrpc"} and $r->{"jsonrpc"} eq "2.0"
!ll&&!rl&&r
0114
MC/DC50%1/2
defined $r->{"jsonrpc"} and $r->{"jsonrpc"} eq "2.0"
defined $r->{"jsonrpc"} $r->{"jsonrpc"} eq "2.0"
Truth table66%2/3
A and B
A= defined $r->{"jsonrpc"}B= $r->{"jsonrpc"} eq "2.0"
ABresult
0X0
100
111
108 # id can be null in the error case
109 14 ▶ return 'id is not a string or number' if exists $r->{id} and ref $r->{id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{"id"} and ref $r->{"id"} 1 13 14
Condition100%3/3
exists $r->{"id"} and ref $r->{"id"}
!ll&&!rl&&r
3101
MC/DC100%2/2
exists $r->{"id"} and ref $r->{"id"}
exists $r->{"id"} ref $r->{"id"}
Truth table100%3/3
A and B
A= exists $r->{"id"}B= ref $r->{"id"}
ABresult
0X0
100
111
110 13 ▶ if (defined $r->{method}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r->{'method'}) { } 9 4 13
elsif (exists $r->{'id'} and exists $r->{'result'} || defined $r->{'error'}) { } 3 1 4
111 9 return $self->{rpc}->_handle_request($self, $r);
112 ▶ } elsif (exists $r->{id} and (exists $r->{result} or defined $r->{error})) {
Condition83%5/6
exists $r->{"id"} and exists $r->{"result"} || defined $r->{"error"}
!ll&&!rl&&r
013
exists $r->{"result"} or defined $r->{"error"}
l!l&&r!l&&!r
211
MC/DC66%2/3
exists $r->{"id"} and exists $r->{"result"} || defined $r->{"error"}
exists $r->{"id"} exists $r->{"result"} defined $r->{"error"}
Truth table75%3/4
A and B or C
A= exists $r->{"id"}B= exists $r->{"result"}C= defined $r->{"error"}
ABCresult
0XX0
1000
11X1
1011
113 3 return $self->_handle_response($r);
114 } else {
115 1 return 'invalid jsonnrpc object';
116 }
117 }
118  
119 sub _handle_response {
120 3 ▶ my ($self, $r) = @_;
Subroutine _handle_response: called CC=13 SCAR=29.4
121 #say STDERR '_handle_response: ', Dumper($r) if $self->{debug};
122 3 my $id = $r->{id};
123 3 my ($cb, $raw);
124 3 ▶ $cb = delete $self->{calls}->{$id} if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 3 0 3
125 3 ▶ return unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 1 2 3
126 2 ($cb, $raw) = @$cb;
127 2 ▶ if (defined $r->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{'error'}) { } 1 1 2
128 1 my $e = $r->{error};
129 1 ▶ return 'error is not an object' unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 1 1
130 1 ▶ return 'error code is not a integer' unless defined $e->{code} and $e->{code} =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e->{"code"} and $e->{"code"} =~ /^-?\d+$/ 0 1 1
Condition33%1/3
defined $e->{"code"} and $e->{"code"} =~ /^-?\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $e->{"code"} and $e->{"code"} =~ /^-?\d+$/
defined $e->{"code"} $e->{"code"} =~ /^-?\d+$/
Truth table33%1/3
A and B
A= defined $e->{"code"}B= $e->{"code"} =~ /^-?\d+$/
ABresult
0X0
100
111
131 1 ▶ return 'error message is not a string' if ref $e->{message};
Branch: 1 branch
BranchTrueFalseTotal
if ref $e->{"message"} 0 1 1
132 1 ▶ return 'extra members in error object' if (keys %$e == 3 and !exists $e->{data}) or (keys %$e > 2);
Branch: 1 branch
BranchTrueFalseTotal
if keys %$e == 3 and not exists $e->{"data"} or keys %$e > 2 0 1 1
Condition33%2/6
keys %$e == 3 and not exists $e->{"data"} or keys %$e > 2
l!l&&r!l&&!r
001
keys %$e == 3 and not exists $e->{"data"}
!ll&&!rl&&r
100
MC/DC0%0/3
keys %$e == 3 and not exists $e->{"data"} or keys %$e > 2
keys %$e == 3 not exists $e->{"data"} keys %$e > 2
Truth table20%1/5
A and not(B) or C
A= keys %$e == 3B= not exists $e->{"data"}C= keys %$e > 2
ABCresult
11X1
0X11
1011
0X00
1000
133 1 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 1 1
134 0 $cb->($r);
135 } else {
136 1 $cb->($e);
137 }
138 } else {
139 1 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) { } 0 1 1
140 0 $cb->(0, $r);
141 } else {
142 1 $cb->(0, $r->{result});
143 }
144 }
145 2 return;
146 }
147  
148 sub write {
149 16 ▶ my $self = shift;
Subroutine write: called CC=2 SCAR=7.1
Pod undocumented
150 16 ▶ say STDERR ' writing: ', @_ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 16 16
151 16 $self->{write}->(@_);
152 }
153  
154 sub owner {
155 0 ▶ my $self = shift;
Subroutine owner: not called CC=2 SCAR=17.9
Pod documented
156 0 ▶ weaken ($self->{owner} = shift) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
157 0 return $self->{owner};
158 }
159  
160 sub state {
161 4 ▶ my $self = shift;
Subroutine state: called CC=2 SCAR=6.9
Pod undocumented
162 4 ▶ $self->{state} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 3 4
163 4 return $self->{state};
164 }
165  
166  
167 sub close {
168 0 ▶ my $self = shift;
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
169 0 %$self = (); # nuke'm all
170 }
171  
172 #sub DESTROY {
173 # my $self = shift;
174 # say STDERR 'destroying ', $self;
175 #}
176  
177 2 1;
178  
179 =encoding utf8
180  
181 =head1 NAME
182  
183 JSON::RPC2::TwoWay::Connection - Transport-independent bidirectional JSON-RPC 2.0 connection
184  
185 =head1 SYNOPSIS
186  
187 $rpc = JSON::RPC2::TwoWay->new();
188 $rpc->register('ping', \&handle_ping);
189  
190 $con = $rpc->newconnection(
191 owner => $owner,
192 write => sub { $stream->write(@_) }
193 );
194 @err = $con->handle($stream->read);
195 die $err[-1] if @err;
196  
197 =head1 DESCRIPTION
198  
199 L<JSON::RPC2::TwoWay::Connection> is a connection containter for
200 L<JSON::RPC2::TwoWay>.
201  
202 =head1 METHODS
203  
204 =head2 new
205  
206 $con = JSON::RPC2::TwoWay::Connection->new(option => ...);
207  
208 Class method that returns a new JSON::RPC2::TwoWay::Connection object.
209 Use newconnection() on a L<JSON::RPC2::TwoWay> object instead.
210  
211 Valid arguments are:
212  
213 =over 4
214  
215 =item - debug: print debugging to STDERR
216  
217 (default false)
218  
219 =item - owner: 'owner' object of this connection.
220  
221 When provided this object will be asked for the 'state' of the connection.
222 Otherwise state will always be 0.
223  
224 =item - rpc: the L<JSON::RPC2::TwoWay> object to handle incoming method calls
225  
226 (required)
227  
228 =item - write: a coderef called for writing
229  
230 This coderef will be called for all output: both requests and responses.
231 (required)
232  
233 =back
234  
235 =head2 call
236  
237 $con->call('method', { arg => 'foo' }, $cb);
238  
239 Calls the remote method indicated in the first argument.
240  
241 The second argument should either be a arrayref or hashref, depending on
242 wether the remote method requires positional of by-name arguments. Pass a
243 empty reference when there are no arguments.
244  
245 The third argument is a callback: this callback will
246 be called with the results of the called method.
247  
248 Call throws an error in case of missing arguments, otherwise it returns
249 immediately with no return value.
250  
251 =head2 callraw
252  
253 $con->callraw({ method => 'method', params => {..} }, $cb);
254  
255 Enhances the first argument (which should be a hashref) to a full JSON-RPC
256 2.0 request object and sends the request. This allows for manipulating and
257 extending the actual request.
258  
259 The third argument is a callback: this callback will
260 be called with the results of the called method.
261  
262 Callraw throws an error in case of missing arguments, otherwise it returns
263 immediately with no return value.
264  
265 =head3 the result callback
266  
267 The result callback is called with 1 or 2 arguments. The first argument is
268 a protocol-error-flag, it contains a error message when there was some kind
269 of protocol error like calling a normal method as a notification.
270  
271 If there are 2 arguments the first one is always false, the second one will
272 contain the results from the remote method, see "REGISTERED CALLBACK CALLING
273 CONVENTION" in "L<JSON::RPC2::TwoWay>. The full response will be passed for
274 access to any extra fields.
275  
276 =head2 notify
277  
278 $con->notify('notify_me', { baz => 'foo' })
279  
280 Calls the remote method as a notification, i.e. no response will be
281 expected. Notify throws an error in case of missing arguments, otherwise it
282 returns immediately with no return value.
283  
284 =head2 handle
285  
286 $con->handle($jsonblob)
287  
288 Handle the incoming request or response. Requests (if valid) are passed on
289 to the registered callback for that method. Repsonses (if valid) are passed
290 on to the callback provided in the call.
291  
292 Handle returns 0, 1 or 2 values. If no value is returned there were no
293 errors during processing. If 1 value is returned there was a 'fatal' error,
294 and the value is the error message. If 2 values are returned there was a
295 'normal' error, the first value is false, the second value is the error
296 message.
297  
298 In case of an error, handle will call the provided write callback with a
299 appropriate error response to be sent to the other side. The application
300 using the JSON::RPC2::TwoWay::Connection is advised to close the underlying
301 connection in case of fatal errors.
302  
303 =head2 close
304  
305 $con->close()
306  
307 Closes the connection. Recommended to be used to avoid memory leaks due to
308 circular references.
309  
310 =head2 owner
311  
312 Getter-setter to allow the application to connect the
313 JSON::RPC2::TwoWay::Connection to some internal connection concept.
314  
315 -head2 state
316  
317 Getter-setter for the connection state. Evaluated by JSON::RPC2::TwoWay
318 when a method was registered with a state option.
319  
320 =head1 SEE ALSO
321  
322 =over
323  
324 =item *
325  
326 L<JSON::RPC2::TwoWay>
327  
328 =item *
329  
330 L<http://www.jsonrpc.org/specification>: JSON-RPC 2.0 Specification
331  
332 =back
333  
334 =head1 ACKNOWLEDGEMENT
335  
336 This software has been developed with support from L<STRATO|https://www.strato.com/>.
337 In German: Diese Software wurde mit Unterstützung von L<STRATO|https://www.strato.de/> entwickelt.
338  
339 =head1 AUTHORS
340  
341 =over 4
342  
343 =item *
344  
345 Wieger Opmeer <wiegerop@cpan.org>
346  
347 =back
348  
349 =head1 COPYRIGHT AND LICENSE
350  
351 This software is copyright (c) 2016-2019 by Wieger Opmeer.
352  
353 This is free software; you can redistribute it and/or modify it under
354 the same terms as the Perl 5 programming language system itself.
355  
356 =cut
357  
↑ Summary TwoWay.pm »