blib/lib/JSONRPC/Transport/TCP.pm

filter: Statementstmt 30.7% 28 / 91 Branchbran 2.5% 1 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 61.5% 8 / 13 Podpod 100% 4 / 4 totaltotal 22.5% 41 / 182 CC 30 SCAR 46.5
CC
30
Cov
19%
connect
182.0
call
156.0
new
30.0
CRAP 105.1
↑ Summary
1 package JSONRPC::Transport::TCP;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 __PACKAGE__->mk_accessors(qw/result error/);
7  
8 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.04';
14 3 our $XS_AVAILABLE = 1;
15  
16 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
17 3 eval { require JSON::XS };
18 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
19 0 $XS_AVAILABLE = 0;
20 0 require JSON;
21 }
22 }
23  
24 =for stopwords Hostname Str tcp ip unix
25  
26 =head1 NAME
27  
28 JSONRPC::Transport::TCP - Client component for TCP JSONRPC
29  
30 =head1 SYNOPSIS
31  
32 use JSONRPC::Transport::TCP;
33
34 my $rpc = JSONRPC::Transport::TCP->new( host => '127.0.0.1', port => 3000 );
35 my $res = $rpc->call('echo', 'arg1', 'arg2' )
36 or die $rpc->error;
37
38 print $res->result;
39  
40 =head1 DESCRIPTION
41  
42 This module is a simple client side implementation about JSONRPC via TCP.
43  
44 This module doesn't support continual tcp streams, and so it open/close connection on each request.
45  
46 =head1 METHODS
47  
48 =head2 new
49  
50 Create new client object.
51  
52 Parameters:
53  
54 =over
55  
56 =item host => 'Str'
57  
58 Hostname or ip address to connect.
59  
60 This should be set 'unix/' when you want to connect to unix socket.
61  
62 =item port => 'Int | Str'
63  
64 Port number or unix socket path to connect
65  
66 =back
67  
68 =cut
69  
70 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
71 0 ▶ my $self = shift->SUPER::new( @_ > 1 ? {@_} : $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
72  
73 0 $self->{id} = 0;
74 0 ▶ $self->{json} ||= $XS_AVAILABLE ? JSON::XS->new->utf8 : JSON->new->utf8;
Branch: 1 branch
BranchTrueFalseTotal
$JSONRPC::Transport::TCP::XS_AVAILABLE ? : 0 0 0
Condition0%0/3
$self->{"json"} ||= $JSONRPC::Transport::TCP::XS_AVAILABLE ? "JSON::XS"->new->utf8 : "JSON"->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"json"} ||= $JSONRPC::Transport::TCP::XS_AVAILABLE ? "JSON::XS"->new->utf8 : "JSON"->new->utf8
$self->{"json"} $JSONRPC::Transport::TCP::XS_AVAILABLE ? "JSON::XS"->new->utf8 : "JSON"->new->utf8
Truth table0%0/3
A ||= B
A= $self->{"json"}B= $JSONRPC::Transport::TCP::XS_AVAILABLE ? "JSON::XS"->new->utf8 : "JSON"->new->utf8
ABresult
1X1
011
000
75 0 ▶ $self->{delimiter} ||= q[];
Condition0%0/2
$self->{"delimiter"} ||= ""
l!l
00
MC/DC0%0/1
$self->{"delimiter"} ||= ""
$self->{"delimiter"}
Truth table0%0/2
A
A= $self->{"delimiter"}
Aresult
11
00
76  
77 0 $self;
78 }
79  
80 =head2 connect
81  
82 Connect remote host.
83  
84 This module automatically connect on following "call" method, so you have not to call this method.
85  
86 =cut
87  
88 0 ▶sub connect {
Subroutine connect: not called CC=13 SCAR=52.0
Pod documented
89 0 my $self = shift;
90 0 ▶ my $params = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
91  
92 0 ▶ $self->disconnect if $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
93  
94 0 my $socket;
95 0 eval {
96 # unix socket
97 0 ▶ my $host = $params->{host} || $self->{host};
Condition0%0/3
$params->{'host'} || $self->{'host'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'host'} || $self->{'host'}
$params->{'host'} $self->{'host'}
Truth table0%0/3
A || B
A= $params->{'host'}B= $self->{'host'}
ABresult
1X1
011
000
98 0 ▶ my $port = $params->{port} || $self->{port};
Condition0%0/3
$params->{'port'} || $self->{'port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'port'} || $self->{'port'}
$params->{'port'} $self->{'port'}
Truth table0%0/3
A || B
A= $params->{'port'}B= $self->{'port'}
ABresult
1X1
011
000
99  
100 0 ▶ if ($host eq 'unix/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($host eq 'unix/') { } 0 0 0
101 $socket = IO::Socket::UNIX->new(
102 Peer => $port,
103 0 ▶ Timeout => $self->{timeout} || 30,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::UNIX"->new("Peer", $port, "Timeout", $self->{"timeout"} || 30) or croak(qq[Unable to connect to unix socket "$port": $!]) 0 0 0
Condition0%0/2
$self->{'timeout'} || 30
l!l
00
MC/DC0%0/1
$self->{'timeout'} || 30
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
104 ) or croak qq/Unable to connect to unix socket "$port": $!/;
105 }
106 else {
107 $socket = IO::Socket::INET->new(
108 PeerAddr => $host,
109 PeerPort => $port,
110 Proto => 'tcp',
111 0 ▶ Timeout => $self->{timeout} || 30,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Proto", "tcp", "Timeout", $self->{"timeout"} || 30) or croak(qq[Unable to connect to "@{[$params->{"host"} || $self->{"host"}];}:@{[$params->{"port"} || $self->{"port"}];}": $!]) 0 0 0
Condition0%0/2
$self->{'timeout'} || 30
l!l
00
MC/DC0%0/1
$self->{'timeout'} || 30
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
112 )
113 or croak
114 0 ▶ qq/Unable to connect to "@{[ $params->{host} || $self->{host} ]}:@{[ $params->{port} || $self->{port} ]}": $!/;
Condition0%0/6
$params->{'host'} || $self->{'host'}
l!l&&r!l&&!r
000
$params->{'port'} || $self->{'port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'host'} || $self->{'host'}
$params->{'host'} $self->{'host'}
MC/DC0%0/2
$params->{'port'} || $self->{'port'}
$params->{'port'} $self->{'port'}
Truth table0%0/3
A || B
A= $params->{'host'}B= $self->{'host'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $params->{'port'}B= $self->{'port'}
ABresult
1X1
011
000
115 }
116  
117 0 $socket->autoflush(1);
118  
119 0 $self->{socket} = $socket;
120 };
121 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
122 0 $self->{error} = $@;
123 0 return;
124 }
125  
126 0 1;
127 }
128  
129 =head2 disconnect
130  
131 Disconnect the connection
132  
133 =cut
134  
135 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
136 0 my $self = shift;
137 0 ▶ delete $self->{socket} if $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
138 }
139  
140 =head2 call($method_name, @params)
141  
142 Call remote method.
143  
144 When remote method is success, it returns self object that contains result as ->result accessor.
145  
146 If some error are occurred, it returns undef, and you can check the error by ->error accessor.
147  
148 Parameters:
149  
150 =over
151  
152 =item $method_name
153  
154 Remote method name to call
155  
156 =item @params
157  
158 Remote method parameters.
159  
160 =back
161  
162 =cut
163  
164 0 ▶sub call {
Subroutine call: not called CC=12 SCAR=50.5
Pod documented
165 0 my ($self, $method, @params) = @_;
166  
167 0 ▶ $self->connect unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 0 0
168 0 ▶ return unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 0 0
169  
170 my $request = {
171 id => ++$self->{id},
172 0 method => $method,
173 params => \@params,
174 };
175 0 $self->{socket}->print($self->{json}->encode($request) . $self->{delimiter});
176  
177 0 my $timeout = $self->{socket}->timeout;
178 0 my $limit = time + $timeout;
179  
180 0 ▶ my $select = IO::Select->new or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
my $select = "IO::Select"->new or croak($!) 0 0 0
181 0 $select->add($self->{socket});
182  
183 0 my $buf = '';
184  
185 0 ▶ while ($limit >= time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit >= time) 0 0 0
186 0 ▶ my @ready = $select->can_read( $limit - time )
Branch: 1 branch
BranchTrueFalseTotal
my(@ready) = $select->can_read($limit - time) or last 0 0 0
187 or last;
188  
189 0 for my $s (@ready) {
190 0 ▶ croak qq/$s isn't $self->{socket}/ unless $s eq $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $s eq $self->{"socket"} 0 0 0
191 }
192  
193 0 ▶ unless (my $l = $self->{socket}->sysread( $buf, 512, length($buf) )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $l = $self->{"socket"}->sysread($buf, 512, length $buf)) 0 0 0
194 0 my $e = $!;
195 0 $self->disconnect;
196 0 croak qq/Error reading: $e/;
197 }
198  
199 0 my $json = eval { $self->{json}->incr_parse($buf) };
200  
201 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($json) { } 0 0 0
202 0 $self->{error} = $@;
203 0 $self->disconnect;
204 0 return;
205 }
206 elsif ($json) {
207 0 ▶ if ($json->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'error'}) { } 0 0 0
208 0 $self->{error} = $json->{error};
209 0 $self->disconnect;
210 0 return;
211 }
212 else {
213 0 $self->{result} = $json->{result};
214 0 $self->disconnect;
215 0 return $self;
216 }
217 }
218 else {
219 0 $buf = '';
220 0 next;
221 }
222 }
223  
224 0 croak "request timeout";
225 }
226  
227 =head2 DESTROY
228  
229 Automatically disconnect when object destroy.
230  
231 =cut
232  
233 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
234 0 my $self = shift;
235 0 $self->disconnect;
236 }
237  
238 =head1 ACCESSORS
239  
240 =head2 result
241  
242 Contains result of remote method
243  
244 =head2 error
245  
246 Contains error of remote method
247  
248 =head1 AUTHOR
249  
250 Daisuke Murase <typester@cpan.org>
251  
252 =head1 COPYRIGHT
253  
254 This program is free software; you can redistribute
255 it and/or modify it under the same terms as Perl itself.
256  
257 The full text of the license can be found in the
258 LICENSE file included with this module.
259  
260 =cut
261  
262 3 1;
↑ Summary