blib/lib/XAS/Lib/Mixins/JSON/Server.pm

filter: Statementstmt 22.5% 21 / 93 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 6 / 18 Podpod 100% 4 / 4 totaltotal 22.6% 31 / 137 CC 13 SCAR 30.0
CC
13
Cov
18%
_exception_handler
56.0
__ANON__
30.0
__ANON__
12.0
CRAP 20.1
« blib/lib/XAS/Lib/Stomp/Frame.pm ↑ Summary blib/lib/XAS/Lib/Mixins/Keepalive.pm »
1 package XAS::Lib::Mixins::JSON::Server;
2  
3 1 our $VERSION = '0.04';
4  
5 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Set::Light;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Params::Validate 'ARRAYREF';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use XAS::Class
11 1 debug => 0,
12 version => $VERSION,
13 base => 'XAS::Base',
14 mixin => 'XAS::Lib::Mixins::Handlers',
15 accessors => 'methods',
16 codec => 'JSON',
17 constants => 'HASH ARRAY :jsonrpc',
18 mixins => 'process_request process_response process_errors
19 methods init_json_server',
20 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 my $errors = {
23 '-32700' => 'Parse Error',
24 '-32600' => 'Invalid Request',
25 '-32601' => 'Method not Found',
26 '-32602' => 'Invalid Params',
27 '-32603' => 'Internal Error',
28 '-32099' => 'Server Error',
29 '-32001' => 'App Error',
30 };
31  
32 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # ----------------------------------------------------------------------
35 # Public Methods
36 # ----------------------------------------------------------------------
37  
38 sub init_json_server {
39 0 ▶ my $self = shift;
Subroutine init_json_server: not called CC=1 SCAR=6.9
Pod documented
40 0 my ($methods) = $self->validate_params(\@_, [
41 { type => ARRAYREF }
42 ]);
43  
44 0 $self->{methods} = Set::Light->new(@$methods);
45  
46 }
47  
48 # ----------------------------------------------------------------------
49 # Public Events
50 # ----------------------------------------------------------------------
51  
52 sub process_request {
53 0 ▶ my ($self, $input, $ctx) = @_[OBJECT,ARG0,ARG1];
Subroutine process_request: not called CC=1 SCAR=6.9
Pod documented
54  
55 0 my $request;
56 0 my $alias = $self->alias;
57  
58 0 $self->log->debug("$alias: entering process_request");
59  
60 try {
61  
62 0 ▶ $request = decode($input);
Subroutine __ANON__: not called CC=3 SCAR=24.8
63  
64 0 ▶ if (ref($request) eq ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request eq 'ARRAY') { } 0 0 0
65  
66 0 foreach my $r (@$request) {
67  
68 0 _rpc_request($self, $r, $ctx);
69  
70 }
71  
72 } else {
73  
74 0 _rpc_request($self, $request, $ctx);
75  
76 }
77  
78 } catch {
79  
80 0 ▶ my $ex = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
81  
82 0 $self->exception_handler($ex);
83 0 $self->log->error(Dumper($input));
84  
85 0 };
86  
87 }
88  
89 sub process_response {
90 0 ▶ my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1];
Subroutine process_response: not called CC=1 SCAR=6.9
Pod documented
91  
92 0 my $json;
93 0 my $alias = $self->alias;
94  
95 0 $self->log->debug("$alias: entering process_response");
96  
97 0 $json = _rpc_result($self, $ctx->{id}, $output);
98  
99 0 $poe_kernel->post($alias, 'client_output', encode($json), $ctx);
100  
101 }
102  
103 sub process_errors {
104 0 ▶ my ($self, $output, $ctx) = @_[OBJECT,ARG0,ARG1];
Subroutine process_errors: not called CC=1 SCAR=6.9
Pod documented
105  
106 0 my $json;
107 0 my $alias = $self->alias;
108  
109 0 $self->log->debug("$alias: entering process_errors");
110  
111 0 $json = _rpc_error($self, $ctx->{id}, $output->{code}, $output->{message});
112  
113 0 $poe_kernel->post($alias, 'client_output', encode($json), $ctx);
114  
115 }
116  
117 # ----------------------------------------------------------------------
118 # Private Methods
119 # ----------------------------------------------------------------------
120  
121 sub _exception_handler {
122 0 ▶ my ($self, $ex, $id) = @_;
Subroutine _exception_handler: not called CC=7 SCAR=40.3
123  
124 0 my $packet;
125 0 my $ref = ref($ex);
126  
127 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
128  
129 0 ▶ if ($ex->isa('XAS::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ex->isa('XAS::Exception')) { } 0 0 0
130  
131 0 my $type = $ex->type;
132 0 my $info = $ex->info;
133  
134 0 ▶ if ($type eq ('xas.lib.mixins.json.server.rpc_method')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'xas.lib.mixins.json.server.rpc_method') { } 0 0 0
elsif ($type eq 'xas.lib.mixins.json.server.rpc_version') { } 0 0 0
elsif ($type eq 'xas.lib.mixins.json.server.rpc_format') { } 0 0 0
elsif ($type eq 'xas.lib.mixins.json.server.rpc_notify') { } 0 0 0
135  
136 0 $packet = _rpc_error($self, $id, RPC_ERR_METHOD, $info);
137  
138 } elsif ($type eq ('xas.lib.mixins.json.server.rpc_version')) {
139  
140 0 $packet = _rpc_error($self, $id, RPC_ERR_REQ, $info);
141  
142 } elsif ($type eq ('xas.lib.mixins.json.server.rpc_format')) {
143  
144 0 $packet = _rpc_error($self, $id, RPC_ERR_PARSE, $info);
145  
146 } elsif ($type eq ('xas.lib.mixins.json.server.rpc_notify')) {
147  
148 0 $packet = _rpc_error($self, $id, RPC_ERR_INTERNAL, $info);
149  
150 } else {
151  
152 0 my $msg = $type . ' - ' . $info;
153 0 $packet = _rpc_error($self, $id, RPC_ERR_APP, $msg);
154  
155 }
156  
157 0 $self->log->error_msg('exception', $type, $info);
158  
159 } else {
160  
161 0 my $msg = sprintf("%s", $ex);
162  
163 0 $packet = _rpc_error($self, $id, RPC_ERR_SERVER, $msg);
164 0 $self->log->error_msg('unexpected', $msg);
165  
166 }
167  
168 } else {
169  
170 0 my $msg = sprintf("%s", $ex);
171  
172 0 $packet = _rpc_error($self, $id, RPC_ERR_APP, $msg);
173 0 $self->log->error_msg('unexpected', $msg);
174  
175 }
176  
177 0 return $packet;
178  
179 }
180  
181 sub _rpc_request {
182 0 ▶ my ($self, $request, $ctx) = @_;
Subroutine _rpc_request: not called CC=1 SCAR=6.9
183  
184 0 my $method;
185 0 my $alias = $self->alias;
186
187 try {
188  
189 0 ▶ if (ref($request) ne HASH) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request ne "HASH") 0 0 0
190  
191 0 $self->throw_msg(
192 'xas.lib.mixins.json.server.format',
193 'json_rpc_format'
194 );
195  
196 }
197  
198 0 ▶ if ($request->{jsonrpc} ne RPC_JSON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"jsonrpc"} ne "2.0") 0 0 0
199  
200 0 $self->throw_msg(
201 'xas.lib.mixins.json.server.rpc_version',
202 'json_rpc_version'
203 );
204  
205 }
206  
207 0 ▶ unless (defined($request->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $request->{"id"}) 0 0 0
208  
209 0 $self->throw_msg(
210 'xas.lib.mixins.json.server.nonotifications',
211 'json_rpc_notify'
212 );
213  
214 }
215  
216 0 ▶ if ($self->methods->has($request->{method})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->methods->has($request->{'method'})) { } 0 0 0
217  
218 0 $ctx->{id} = $request->{id};
219 0 $self->log->debug("$alias: performing \"" . $request->{method} . '"');
220  
221 0 $poe_kernel->post($alias, $request->{method}, $request->{params}, $ctx);
222  
223 } else {
224  
225 $self->throw_msg(
226 'xas.lib.mixins.json.server.rpc_method',
227 'json_rpc_method',
228 $request->{method}
229 0 );
230  
231 }
232  
233 } catch {
234  
235 0 ▶ my $ex = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
236  
237 0 my $output = _exception_handler($self, $ex, $request->{id});
238 0 $poe_kernel->post($alias, 'client_output', encode($output), $ctx);
239  
240 0 };
241  
242 }
243  
244 sub _rpc_error {
245 0 ▶ my ($self, $id, $code, $message) = @_;
Subroutine _rpc_error: not called CC=1 SCAR=6.9
246  
247 return {
248 jsonrpc => RPC_JSON,
249 id => $id,
250 error => {
251 code => $code,
252 0 message => $errors->{$code},
253 data => $message
254 }
255 };
256  
257 }
258  
259 sub _rpc_result {
260 0 ▶ my ($self, $id, $result) = @_;
Subroutine _rpc_result: not called CC=1 SCAR=6.9
261  
262 return {
263 0 jsonrpc => RPC_JSON,
264 id => $id,
265 result => $result
266 };
267  
268 }
269  
270 1 1;
271  
272 __END__
273  
274 =head1 NAME
275  
276 XAS::Lib::Mixins::JSON::Server - A mixin for a simple JSON RPC server
277  
278 =head1 SYNOPSIS
279  
280 package Echo;
281  
282 use POE;
283 use XAS::Class
284 debug => 0,
285 version => '0.01',
286 base => 'XAS::Lib::Net::Server'
287 mixin => 'XAS::Lib::Mixins::JSON::Server XAS::Lib::Mixins::Keepalive',
288 vars => {
289 PARAMS => {
290 -port => { optional => 1, default => 9500 },
291 -tcp_keepalive => { optional => 1, default => 0 }
292 }
293 }
294 ;
295  
296 sub handle_connection {
297 my ($self, $wheel) = @_[OBJECT, ARG0];
298  
299 if (my $socket = $self->{clients}->{$wheel}->{socket}) {
300  
301 if ($self->tcp_keepalive) {
302  
303 $self->log->info("keepalive enabled");
304 $self->init_keepalive();
305 $self->enable_keepalive($socket);
306  
307 }
308  
309 }
310  
311 }
312  
313 sub echo {
314 my ($self, $params, $ctx) = @_[OBJECT, ARGO, ARG1];
315  
316 my $alias = $self->alias;
317 my $line = $params->{line};
318  
319 $poe_kernel->post($alias, 'process_response', $line, $ctx);
320  
321 }
322  
323 sub init {
324 my $class = shift;
325  
326 my $self = $class->SUPER::init(@_);
327 my @methods = ['echo'];
328  
329 $self->init_json_server(\@methods);
330  
331 return $self;
332  
333 }
334  
335 package main;
336  
337 my $echo = Echo->new();
338  
339 $echo->run();
340  
341 =head1 DESCRIPTION
342  
343 This modules implements a simple L<JSON RPC v2.0|http://www.jsonrpc.org/specification> server as a mixin. It
344 doesn't support "Notification" calls.
345  
346 =head1 METHODS
347  
348 =head2 init_json_server($methods)
349  
350 This initializes the module.
351  
352 =over 4
353  
354 =item B<$methods>
355  
356 An arrayref of methods that this server can process.
357  
358 =back
359  
360 =head2 methods
361  
362 A handle to a L<Set::Light|https://metacpan.org/pod/Set::Light> object that contains the methods
363 that can be evoked.
364  
365 =head1 EVENTS
366  
367 =head2 process_request(OBJECT, ARG0, ARG1)
368  
369 =head2 process_response(OBJECT, ARG0, ARG1)
370  
371 =head2 process_errors(OBJECT, ARG0, ARG1)
372  
373 =head1 AUTHOR
374  
375 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
376  
377 =head1 COPYRIGHT AND LICENSE
378  
379 Copyright (C) 2014 Kevin L. Esteb
380  
381 This is free software; you can redistribute it and/or modify it under
382 the terms of the Artistic License 2.0. For details, see the full text
383 of the license at http://www.perlfoundation.org/artistic_license_2_0.
384  
385 =cut
« blib/lib/XAS/Lib/Stomp/Frame.pm ↑ Summary blib/lib/XAS/Lib/Mixins/Keepalive.pm »