TwoWay.pm

filter: Statementstmt 85.1% 86 / 101 Branchbran 59.6% 37 / 62 Conditioncond 67.8% 19 / 28 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 87.5% 21 / 24 Podpod 100% 4 / 4 totaltotal 74.5% 176 / 236 CC 42 SCAR 28.9
CC
42
Cov
74%
_handle_request
41.9
unregister
20.0
register
7.0
CRAP 18.0
↑ Summary TwoWay/Connection.pm »
1 package JSON::RPC2::TwoWay;
2 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.07'; # VERSION
7  
8 # standard perl
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # cpan
13 2 ▶use JSON::MaybeXS qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # us
16 2 ▶use JSON::RPC2::TwoWay::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use constant ERR_NOTNOT => -32000;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use constant ERR_ERR => -32001;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use constant ERR_BADSTATE => -32002;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant ERR_REQ => -32600;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant ERR_METHOD => -32601;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant ERR_PARAMS => -32602;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use constant ERR_PARSE => -32700;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub new {
27 1 ▶ my ($class, %opt) = @_;
Subroutine new: called CC=4 SCAR=17.1
Pod documented
28 my $self = {
29 debug => $opt{debug} ? 1 : 0,
30 0 ▶ log => ref $opt{debug} eq 'CODE' ? $opt{debug} : sub { say STDERR @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 1 ▶ json => $opt{json} // JSON::MaybeXS->new(utf8 => 1),
Branch: 2 branches
BranchTrueFalseTotal
$opt{'debug'} ? : 0 1 1
ref $opt{'debug'} eq 'CODE' ? : 0 1 1
Condition33%1/3
$opt{"json"} // "JSON::MaybeXS"->new("utf8", 1)
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt{"json"} // "JSON::MaybeXS"->new("utf8", 1)
$opt{"json"} "JSON::MaybeXS"->new("utf8", 1)
Truth table33%1/3
A // B
A= $opt{"json"}B= "JSON::MaybeXS"->new("utf8", 1)
ABresult
1X1
011
000
32 methods => {},
33 };
34 1 return bless $self, $class;
35 }
36  
37 sub newconnection {
38 2 ▶ my ($self, %opt) = @_;
Subroutine newconnection: called CC=2 SCAR=7.1
Pod documented
39 my $conn = JSON::RPC2::TwoWay::Connection->new(
40 rpc => $self,
41 owner => $opt{owner},
42 write => $opt{write},
43 debug => $self->{debug} ? $self->{log} : 0,
44 json => $self->{json},
45 2 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 0 2 2
46 1 return $conn
47 }
48  
49 sub register {
50 7 ▶ my ($self, $name, $cb, %opts) = @_;
Subroutine register: called CC=7 SCAR=19.5
Pod documented
51 7 my %defaults = (
52 by_name => 1,
53 non_blocking => 0,
54 notification => 0,
55 raw => 0,
56 state => undef,
57 );
58 7 ▶ croak 'no self?' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 7 7
59 7 ▶ croak 'no name?' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 7 7
60 7 ▶ croak 'no callback?' unless ref $cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 6 7
61 6 %opts = (%defaults, %opts);
62 croak 'a non_blocking notification is not sensible'
63 6 ▶ if $opts{non_blocking} and $opts{notification};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"non_blocking"} and $opts{"notification"} 1 5 6
Condition100%3/3
$opts{"non_blocking"} and $opts{"notification"}
!ll&&!rl&&r
411
MC/DC100%2/2
$opts{"non_blocking"} and $opts{"notification"}
$opts{"non_blocking"} $opts{"notification"}
Truth table100%3/3
A and B
A= $opts{"non_blocking"}B= $opts{"notification"}
ABresult
0X0
100
111
64 5 ▶ croak "method $name already registered" if $self->{methods}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"methods"}{$name} 1 4 5
65 $self->{methods}->{$name} = {
66 name => $name,
67 cb => $cb,
68 by_name => $opts{by_name},
69 non_blocking => $opts{non_blocking},
70 notification => $opts{notification},
71 raw => $opts{raw},
72 state => $opts{state},
73 4 };
74 }
75  
76 sub unregister {
77 0 ▶ my ($self, $name) = @_;
Subroutine unregister: not called CC=4 SCAR=30.0
Pod documented
78 0 ▶ croak 'no self?' unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
79 0 ▶ croak 'no name?' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
80 0 my $method = delete $self->{methods}->{$name};
81 0 ▶ croak "method $name already registered" unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
82 }
83  
84  
85 sub _handle_request {
86 9 ▶ my ($self, $c, $r) = @_;
Subroutine _handle_request: called CC=23 SCAR=37.3
87 9 ▶ $self->{log}->(' in handle_request') if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 9 9
88 #print Dumper(\@_);
89 9 my $m = $self->{methods}->{$r->{method}};
90 9 my $id = $r->{id};
91 9 ▶ return $self->_error($c, $id, ERR_METHOD, 'Method not found.') unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 1 8 9
92 8 ▶ return $self->_error($c, $id, ERR_NOTNOT, 'Method is not a notification.') if !$id and !$m->{notification};
Branch: 1 branch
BranchTrueFalseTotal
unless $id or $m->{"notification"} 1 7 8
Condition100%3/3
$id or $m->{"notification"}
l!l&&r!l&&!r
521
MC/DC100%2/2
$id or $m->{"notification"}
$id $m->{"notification"}
Truth table100%3/3
A or B
A= $idB= $m->{"notification"}
ABresult
1X1
011
000
93  
94 return $self->_error($c, $id, ERR_REQ, 'Invalid Request: params should be array or object.')
95 7 ▶ if ref $r->{params} ne 'ARRAY' and ref $r->{params} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $r->{"params"} ne "ARRAY" and ref $r->{"params"} ne "HASH" 1 6 7
Condition100%3/3
ref $r->{"params"} ne "ARRAY" and ref $r->{"params"} ne "HASH"
!ll&&!rl&&r
331
MC/DC100%2/2
ref $r->{"params"} ne "ARRAY" and ref $r->{"params"} ne "HASH"
ref $r->{"params"} ne "ARRAY" ref $r->{"params"} ne "HASH"
Truth table100%3/3
A and B
A= ref $r->{"params"} ne "ARRAY"B= ref $r->{"params"} ne "HASH"
ABresult
0X0
100
111
96  
97 return $self->_error($c, $id, ERR_PARAMS, 'This method expects '.($m->{by_name} ? 'named' : 'positional').' params.')
98 6 ▶ if ref $r->{params} ne ($m->{by_name} ? 'HASH' : 'ARRAY');
Branch: 3 branches
BranchTrueFalseTotal
if ref $r->{"params"} ne ($m->{"by_name"} ? "HASH" : "ARRAY") 2 4 6
$m->{'by_name'} ? : 3 3 6
$m->{'by_name'} ? : 1 1 2
99
100 return $self->_error($c, $id, ERR_BADSTATE, 'This method requires connection state ' . ($m->{state} // 'undef'))
101 4 ▶ if $m->{state} and not ($c->state and $m->{state} eq $c->state);
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"state"} and not $c->state && $m->{"state"} eq $c->state 1 3 4
Condition75%6/8
$m->{"state"} and not $c->state && $m->{"state"} eq $c->state
!ll&&!rl&&r
211
$c->state && $m->{'state'} eq $c->state
!ll&&!rl&&r
101
$m->{"state"} // "undef"
l!l
10
MC/DC66%2/3
$m->{"state"} and not $c->state && $m->{"state"} eq $c->state
$m->{"state"} $c->state $m->{'state'} eq $c->state
MC/DC0%0/1
$m->{"state"} // "undef"
$m->{"state"}
Truth table75%3/4
A and not(B && C)
A= $m->{"state"}B= $c->stateC= $m->{'state'} eq $c->state
ABCresult
0XX0
1110
10X1
1101
Truth table50%1/2
A
A= $m->{"state"}
Aresult
11
00
102  
103 3 ▶ if ($m->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"raw"}) 0 3 3
104 0 my $cb;
105 0 ▶ $cb = sub { $c->write($self->{json}->encode($_[0])) if $id } if $m->{non_blocking};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
if $m->{"non_blocking"} 0 0 0
if $id 0 0 0
106  
107 0 local $@;
108 #my @ret = eval { $m->{cb}->($c, $jsonr, $r, $cb)};
109 0 my @ret = eval { $m->{cb}->($c, $r, $cb)};
110 0 ▶ return $self->_error($c, $id, ERR_ERR, "Method threw error: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
111 #$self->{log}->('method returned: ' . Dumper(\@ret)) if $self->{debug};
112  
113 0 ▶ $c->write($self->{json}->encode($ret[0])) if !$cb and $id;
Branch: 1 branch
BranchTrueFalseTotal
if not $cb and $id 0 0 0
Condition0%0/3
not $cb and $id
!ll&&!rl&&r
000
MC/DC0%0/2
not $cb and $id
not $cb $id
Truth table0%0/3
not(A) and B
A= not $cbB= $id
ABresult
0X0
100
111
114 return
115 0 }
116  
117 3 my $cb;
118 3 ▶ $cb = sub { $self->_result($c, $id, \@_) if $id; } if $m->{non_blocking};
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if $m->{"non_blocking"} 1 2 3
if $id 1 0 1
119  
120 3 local $@;
121 3 my @ret = eval { $m->{cb}->($c, $r->{params}, $cb)};
122 3 ▶ return $self->_error($c, $id, ERR_ERR, "Method threw error: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
123 #$self->{log}->('method returned: ' . Dumper(\@ret)) if $self->{debug};
124
125 3 ▶ return $self->_result($c, $id, \@ret) if !$cb and $id;
Branch: 1 branch
BranchTrueFalseTotal
if not $cb and $id 2 1 3
Condition66%2/3
not $cb and $id
!ll&&!rl&&r
102
MC/DC50%1/2
not $cb and $id
not $cb $id
Truth table66%2/3
not(A) and B
A= not $cbB= $id
ABresult
0X0
100
111
126 1 return;
127 }
128  
129 sub _error {
130 11 ▶ my ($self, $c, $id, $code, $message, $data) = @_;
Subroutine _error: called CC=4 SCAR=14.6
131 11 ▶ my $err = "error: $code " . $message // '';
Condition50%1/2
"error: $code " . $message // ""
l!l
110
MC/DC0%0/1
"error: $code " . $message // ""
"error: $code " . $message
Truth table50%1/2
A
A= "error: $code " . $message
Aresult
11
00
132 11 ▶ $self->{log}->($err) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 11 11
133 $c->write($self->{json}->encode({
134 11 ▶ jsonrpc => '2.0',
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 11 11
135 id => $id,
136 error => {
137 code => $code,
138 message => $message,
139 (defined $data ? ( data => $data ) : ()),
140 },
141 }));
142 11 return 0, $err;
143 }
144  
145 sub _result {
146 3 ▶ my ($self, $c, $id, $result) = @_;
Subroutine _result: called CC=2 SCAR=6.9
147 3 ▶ $result = $$result[0] if scalar(@$result) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$result == 1 2 1 3
148 #$self->{log}->(Dumper($result)) if $self->{debug};
149 $c->write($self->{json}->encode({
150 3 jsonrpc => '2.0',
151 id => $id,
152 result => $result,
153 }));
154 3 return;
155 }
156  
157 #sub DESTROY {
158 # my $self = shift;
159 # $self->{log}->('destroying ' . $self) if $self->{debug};
160 #}
161  
162 2 1;
163  
164 =encoding utf8
165  
166 =head1 NAME
167  
168 JSON::RPC2::TwoWay - Transport-independent bidirectional JSON-RPC 2.0
169  
170 =head1 SYNOPSIS
171  
172 $rpc = JSON::RPC2::TwoWay->new();
173 $rpc->register('ping', \&handle_ping);
174  
175 $con = $rpc->newconnection(
176 owner => $owner,
177 write => sub { $stream->write(@_) }
178 );
179 @err = $con->handle($stream->read);
180 die $err[-1] if @err;
181  
182 =head1 DESCRIPTION
183  
184 L<JSON::RPC2::TwoWay> is a base class to implement bidirectional (a.k.a.
185 twoway) communication using JSON-RPC 2.0 remote procedure calls: both sides
186 can operate as Clients and Servers simultaneously. This class is
187 transport-independent.
188  
189 =head1 METHODS
190  
191 =head2 new
192  
193 $rpc = JSON::RPC2::TwoWay->new();
194  
195 Class method that returns a new JSON::RPC2::TwoWay object.
196  
197 Valid arguments are:
198  
199 =over 4
200  
201 =item - debug: print debugging to STDERR, or if coderef is given call that with
202 the debugging line.
203  
204 =item - json: json encoder/decoder object to use. Defaults to JSON::MaybeXS->new().
205  
206 =back
207  
208 =head2 newconnection
209  
210 my $con = $rpc->newconnection(owner => $owner, write = $write);
211  
212 Creates a L<JSON::RPC2::TwoWay::Connection> with owner $owner and writer $write.
213  
214 See L<JSON::RPC2::TwoWay::Connection> for details.
215  
216 =head2 register
217  
218 $rpc->register('my_method', sub { ... }, option => ... );
219  
220 Register a new method to be callable. Calls are passed to the callback.
221  
222 Valid options are:
223  
224 =over 4
225  
226 =item - by_name
227  
228 When true the arguments to the method will be passed in as a hashref,
229 otherwise as a arrayref. (default true)
230  
231 =item - non_blocking
232  
233 When true the method callback will receive a callback as its last argument
234 for passing back the results (default false)
235  
236 =item - notification
237  
238 When true the method is a notification and no return value is expected by
239 the caller. (Any returned values will be discarded in the handler.)
240  
241 =item - state
242  
243 When defined must be a string value defining the state the connection (see
244 L<newconnection>) must be in for this call to be accepted.
245  
246 =back
247  
248 =head2 unregister
249  
250 $rpc->unregister('my_method')
251  
252 Unregister a method.
253  
254 =head1 REGISTERED CALLBACK CALLING CONVENTION
255  
256 The method callback passed as the second argument of register is called with
257 2 or 3 arguments: the first argument is the JSON::RPC2::TwoWay::Connection
258 object on which the request came in. The second argument is a arrayref or
259 hashref depending on if the method was registered as by-position or by-name.
260 The third argument, if present is a result callback that needs to be called
261 with the results of the method:
262  
263 sub mymethod {
264 ($c, $i, $cb) = @_;
265 $foo = $i->{foo};
266 }
267  
268 some time later;
269  
270 $cb->("you sent $foo");
271  
272 If the method callback returns a scalar value the JSON-RPC 2.0 result member
273 value will be a JSON string, number, or null value. If the method returns a
274 hashref the result member value will be an object. If the method returns
275 multiple values or an arrayref the result member value will be an array.
276  
277 =head1 SEE ALSO
278  
279 =over
280  
281 =item *
282  
283 L<JSON::RPC2::TwoWay::Connection>
284  
285 =item *
286  
287 L<http://www.jsonrpc.org/specification>: JSON-RPC 2.0 Specification
288  
289 =back
290  
291 =head1 ACKNOWLEDGEMENT
292  
293 This software has been developed with support from L<STRATO|https://www.strato.com/>.
294 In German: Diese Software wurde mit Unterstützung von L<STRATO|https://www.strato.de/> entwickelt.
295  
296 =head1 THANKS
297  
298 =over 4
299  
300 =item *
301  
302 'greencoloured' for multiple PRs
303  
304 =back
305  
306 =head1 AUTHORS
307  
308 =over 4
309  
310 =item *
311  
312 Wieger Opmeer <wiegerop@cpan.org>
313  
314 =back
315  
316 =head1 COPYRIGHT AND LICENSE
317  
318 This software is copyright (c) 2016-2022 by Wieger Opmeer.
319  
320 This is free software; you can redistribute it and/or modify it under
321 the same terms as the Perl 5 programming language system itself.
322  
323 =cut
324  
↑ Summary TwoWay/Connection.pm »