line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XAS::Lib::RPC::JSON::Client; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use XAS::Class |
6
|
1
|
|
|
|
|
9
|
debug => 0, |
7
|
|
|
|
|
|
|
version => $VERSION, |
8
|
|
|
|
|
|
|
base => 'XAS::Lib::Net::Client', |
9
|
|
|
|
|
|
|
utils => ':validation dotid', |
10
|
|
|
|
|
|
|
codec => 'JSON', |
11
|
|
|
|
|
|
|
constants => ':jsonrpc HASHREF', |
12
|
1
|
|
|
1
|
|
957
|
; |
|
1
|
|
|
|
|
2
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
894
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
306
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
17
|
|
|
|
|
|
|
# Public Methods |
18
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub call { |
21
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
my $p = validate_params(\@_, { |
23
|
|
|
|
|
|
|
-method => 1, |
24
|
|
|
|
|
|
|
-id => 1, |
25
|
|
|
|
|
|
|
-params => { type => HASHREF } |
26
|
|
|
|
|
|
|
}); |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $params; |
29
|
|
|
|
|
|
|
my $response; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
while (my ($key, $value) = each(%{$p->{'params'}})) { |
|
0
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$key =~ s/^-//; |
34
|
0
|
|
|
|
|
|
$params->{$key} = $value; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $packet = { |
39
|
|
|
|
|
|
|
jsonrpc => RPC_JSON, |
40
|
|
|
|
|
|
|
id => $p->{'id'}, |
41
|
0
|
|
|
|
|
|
method => $p->{'method'}, |
42
|
|
|
|
|
|
|
params => $params |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$self->log->debug(Dumper($packet)); |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
$self->puts(encode($packet)); |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ($response = $self->gets()) { |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
$response = decode($response); |
52
|
0
|
|
|
|
|
|
$self->log->debug(Dumper($response)); |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ($response->{'id'} eq $p->{'id'}) { |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$self->_check_for_errors($response); |
57
|
0
|
|
|
|
|
|
return $response->{'result'}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
} else { |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$self->throw_msg( |
62
|
|
|
|
|
|
|
dotid($self->class) . '.call.invalid_id', |
63
|
|
|
|
|
|
|
'json_rpc_invalid_id', |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
} else { |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$self->throw_msg( |
71
|
|
|
|
|
|
|
dotid($self->class) . '.call.invalid_response', |
72
|
|
|
|
|
|
|
'json_rpc_invalid_response', |
73
|
0
|
|
|
|
|
|
$p->{'method'} |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
81
|
|
|
|
|
|
|
# Private Methods |
82
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _check_for_errors { |
85
|
0
|
|
|
0
|
|
|
my $self = shift; |
86
|
0
|
|
|
|
|
|
my $response = shift; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
if ($response->{'error'}) { |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$self->throw_msg( |
91
|
|
|
|
|
|
|
dotid($self->class) . '.call.rpc_error', |
92
|
|
|
|
|
|
|
'json_rpc_error', |
93
|
|
|
|
|
|
|
$response->{'error'}->{'code'} || '', |
94
|
|
|
|
|
|
|
$response->{'error'}->{'message'} || '', |
95
|
0
|
|
0
|
|
|
|
$response->{'error'}->{'data'} || '', |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
96
|
|
|
|
|
|
|
); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
XAS::Lib::RPC::JSON::Client - A mixin for a JSON RPC interface |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
package Client |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
use XAS::Class |
115
|
|
|
|
|
|
|
debug => 0, |
116
|
|
|
|
|
|
|
version => '0.01', |
117
|
|
|
|
|
|
|
base => 'XAS::Lib::RPC::JSON::Client', |
118
|
|
|
|
|
|
|
; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
package main |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $client = Client->new( |
123
|
|
|
|
|
|
|
-port => 9505, |
124
|
|
|
|
|
|
|
-host => 'localhost', |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$client->connect(); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $data = $client->call( |
130
|
|
|
|
|
|
|
-method => 'test' |
131
|
|
|
|
|
|
|
-id => $id, |
132
|
|
|
|
|
|
|
-params => {} |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$client->disconnect(); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 DESCRIPTION |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This modules implements a simple L<JSON RPC v2.0|http://www.jsonrpc.org/specification> client. |
140
|
|
|
|
|
|
|
It doesn't support "Notification" calls. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 METHODS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 new |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This module inherits from L<XAS::Lib::Net::Client|XAS::Lib::Net::Client>. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 call |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This method is used to format the JSON packet and send it to the server. |
151
|
|
|
|
|
|
|
Any errors returned from the server are parsed and then thrown. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over 4 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item B<-method> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The name of the RPC method to invoke. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item B<-id> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
The id used to identify this method call. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item B<-params> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
A hashref of the parameters to be passed to the method. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=back |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 SEE ALSO |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=over 4 |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item L<XAS::Lib::RPC::JSON::Server|XAS::Lib::RPC::JSON::Server> |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item L<XAS|XAS> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=back |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 AUTHOR |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Copyright (c) 2012-2015 Kevin L. Esteb |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
188
|
|
|
|
|
|
|
the terms of the Artistic License 2.0. For details, see the full text |
189
|
|
|
|
|
|
|
of the license at http://www.perlfoundation.org/artistic_license_2_0. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |