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