line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::RPC::Dispatcher::Procedure; |
2
|
|
|
|
|
|
|
$JSON::RPC::Dispatcher::Procedure::VERSION = '0.0507'; |
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
JSON::RPC::Dispatcher::Procedure - The data holder between RPC requests and responses. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
version 0.0507 |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use JSON::RPC::Dispatcher::Procedure; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $proc = JSON::RPC::Dispatcher::Procedure->new; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$proc->error_code(300); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $method = $proc->method; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Something needs to act as an intermediary to hold the data and state of requests coming in, RPC being called, and responses going out. THis module fits that bill. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
2
|
|
12
|
use Moose; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
17
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#-------------------------------------------------------- |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 error_code ( [ code ] ) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Returns the current error code. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head3 code |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
An integer. Sets an error code. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 has_error_code ( ) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Returns a boolean indicating whether an error code has been set. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has error_code => ( |
47
|
|
|
|
|
|
|
is => 'rw', |
48
|
|
|
|
|
|
|
predicate => 'has_error_code', |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
#-------------------------------------------------------- |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 error_message ( [ message ] ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Returns the current error message. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head3 message |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
A string. Sets an error message. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has error_message => ( |
64
|
|
|
|
|
|
|
is => 'rw', |
65
|
|
|
|
|
|
|
default => undef, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#-------------------------------------------------------- |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 error_data ( [ data ] ) |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Returns the current error data. Error data is entirely defined by the application (e.g. detailed error information, nested errors etc.). |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head3 data |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
A scalar or reference. Sets an error data. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
has error_data => ( |
81
|
|
|
|
|
|
|
is => 'rw', |
82
|
|
|
|
|
|
|
default => undef, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
#-------------------------------------------------------- |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 error ( code, message, [ data ] ) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub error { |
92
|
0
|
|
|
0
|
1
|
|
my ($self, $code, $message, $data) = @_; |
93
|
0
|
|
|
|
|
|
$self->error_code($code); |
94
|
0
|
|
|
|
|
|
$self->error_message($message); |
95
|
0
|
|
|
|
|
|
$self->error_data($data); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
#-------------------------------------------------------- |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 invalid_request ( [ data ] ) |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Sets an Invalid Request error as defined by the JSON-RPC 2.0 spec. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head3 data |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Optionally set some error data for the error. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub invalid_request { |
111
|
0
|
|
|
0
|
1
|
|
my ($self, $msg) = @_; |
112
|
0
|
|
|
|
|
|
$self->error_code(-32600); |
113
|
0
|
|
|
|
|
|
$self->error_message('Invalid Request.'); |
114
|
0
|
|
|
|
|
|
$self->error_data($msg); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
#-------------------------------------------------------- |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 method_not_found ( [ data ] ) |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Sets a Method Not Found error as defined by the JSON-RPC 2.0 spec. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head3 data |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Optionally set some error data for the error. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=cut |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub method_not_found { |
130
|
0
|
|
|
0
|
1
|
|
my ($self, $msg) = @_; |
131
|
0
|
|
|
|
|
|
$self->error_code(-32601); |
132
|
0
|
|
|
|
|
|
$self->error_message('Method not found.'); |
133
|
0
|
|
|
|
|
|
$self->error_data($msg); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
#-------------------------------------------------------- |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 invalid_params ( [ data ] ) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Sets an Invalid Params error as defined by the JSON-RPC 2.0 spec. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head3 data |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Optionally set some error data for the error. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub invalid_params { |
149
|
0
|
|
|
0
|
1
|
|
my ($self, $msg) = @_; |
150
|
0
|
|
|
|
|
|
$self->error_code(-32602); |
151
|
0
|
|
|
|
|
|
$self->error_message('Invalid params.'); |
152
|
0
|
|
|
|
|
|
$self->error_data($msg); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#-------------------------------------------------------- |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 internal_error ( [ data ] ) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Sets an Internal Error as defined by the JSON-RPC 2.0 spec. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head3 data |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Optionally set some error data for the error. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub internal_error { |
168
|
0
|
|
|
0
|
1
|
|
my ($self, $msg) = @_; |
169
|
0
|
|
|
|
|
|
$self->error_code(-32603); |
170
|
0
|
|
|
|
|
|
$self->error_message('Internal error.'); |
171
|
0
|
|
|
|
|
|
$self->error_data($msg); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
#-------------------------------------------------------- |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 method ( [ name ] ) |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Returns the name of the procedure to be called. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head3 name |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
An alphanumeric string. Sets the method name. Will set an error if the method name is not alpha-numeric. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
has method => ( |
187
|
|
|
|
|
|
|
is => 'rw', |
188
|
|
|
|
|
|
|
default => undef, |
189
|
|
|
|
|
|
|
trigger => sub { |
190
|
|
|
|
|
|
|
my ($self, $new, $old) = @_; |
191
|
|
|
|
|
|
|
if (defined $new && $new !~ m{^[A-Za-z0-9_]+$}xms) { |
192
|
|
|
|
|
|
|
$self->invalid_request($new.' is not a valid method name.'); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
}, |
195
|
|
|
|
|
|
|
); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
#-------------------------------------------------------- |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 params ( [ data ] ) |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Returns the parameters to be passed into the procedure. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head3 data |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
An array or hashref. Sets the parameters. Will set an error if the params are not an array ref or hash ref. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
has params => ( |
210
|
|
|
|
|
|
|
is => 'rw', |
211
|
|
|
|
|
|
|
default => undef, |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
#-------------------------------------------------------- |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 id ( [ id ] ) |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Returns the id of the request. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head3 id |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Sets the id of the request. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=cut |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
has id => ( |
228
|
|
|
|
|
|
|
is => 'rw', |
229
|
|
|
|
|
|
|
default => undef, |
230
|
|
|
|
|
|
|
); |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
#-------------------------------------------------------- |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 result ( [ data ] ) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Returns the data that will be sent back to the client. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head3 data |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Sets the data that will be sent back to the client. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=cut |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
has result => ( |
245
|
|
|
|
|
|
|
is => 'rw', |
246
|
|
|
|
|
|
|
default => undef, |
247
|
|
|
|
|
|
|
); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
#-------------------------------------------------------- |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head2 response ( ) |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Formats the data stored in this object into the data structure expected by L<JSON::RPC::Dispatcher>, which will ultimately be returned to the client. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub response { |
258
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
259
|
0
|
|
|
|
|
|
my $error; |
260
|
0
|
0
|
|
|
|
|
if ($self->has_error_code) { |
261
|
0
|
|
|
|
|
|
$error = { |
262
|
|
|
|
|
|
|
code => $self->error_code, |
263
|
|
|
|
|
|
|
message => $self->error_message, |
264
|
|
|
|
|
|
|
data => $self->error_data, |
265
|
|
|
|
|
|
|
}; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
return { |
268
|
0
|
|
|
|
|
|
jsonrpc => '2.0', |
269
|
|
|
|
|
|
|
id => $self->id, |
270
|
|
|
|
|
|
|
result => $self->result, |
271
|
|
|
|
|
|
|
error => $error, |
272
|
|
|
|
|
|
|
}; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 LEGAL |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
JSON::RPC::Dispatcher is Copyright 2009-2010 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=cut |
280
|
|
|
|
|
|
|
|
281
|
2
|
|
|
2
|
|
15109
|
no Moose; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
11
|
|
282
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |