| 1 | package POE::Component::Server::JSONRPC; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use POE qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | Filter::Line | ||||||||||||||||||||||||||||
| 10 | 2 | /; | |||||||||||||||||||||||||||
| 11 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | POE::Component::Server::JSONRPC - POE tcp or http based JSON-RPC server | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #http version: | ||||||||||||||||||||||||||||
| 20 | POE::Component::Server::JSONRPC::Http->new( | ||||||||||||||||||||||||||||
| 21 | # consider using something like JSON::MaybeXS for better performance | ||||||||||||||||||||||||||||
| 22 | json => JSON::MaybeXS->new->utf8->allow_nonref, | ||||||||||||||||||||||||||||
| 23 | Port => 3000, | ||||||||||||||||||||||||||||
| 24 | Handler => { | ||||||||||||||||||||||||||||
| 25 | 'echo' => 'echo', | ||||||||||||||||||||||||||||
| 26 | 'sum' => 'sum', | ||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||
| 28 | SslKey => '/path/to/the/server.key', | ||||||||||||||||||||||||||||
| 29 | SslCert => '/path/to/the/server.crt', | ||||||||||||||||||||||||||||
| 30 | Authenticate => \&authentication_handler, | ||||||||||||||||||||||||||||
| 31 | # authentication_handler must be a function that takes two parameters login and password, | ||||||||||||||||||||||||||||
| 32 | # and returns true if connection is successful, false otherwise | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #tcp version: | ||||||||||||||||||||||||||||
| 36 | POE::Component::Server::JSONRPC::Tcp->new( | ||||||||||||||||||||||||||||
| 37 | Port => 3000, | ||||||||||||||||||||||||||||
| 38 | Handler => { | ||||||||||||||||||||||||||||
| 39 | 'echo' => 'echo', | ||||||||||||||||||||||||||||
| 40 | 'sum' => 'sum', | ||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub echo { | ||||||||||||||||||||||||||||
| 45 | my ($kernel, $jsonrpc, $id, @params) = @_[KERNEL, ARG0..$#_ ]; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $kernel->post( $jsonrpc => 'result' => $id, @params ); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub sum { | ||||||||||||||||||||||||||||
| 51 | my ($kernel, $jsonrpc, $id, @params) = @_[KERNEL, ARG0..$#_ ]; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | $kernel->post( $jsonrpc => 'result' => $id, $params[0] + $params[1] ); | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This module is a POE component for tcp or http based JSON-RPC Server. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | The specification is defined on http://json-rpc.org/ and this module use JSON-RPC 1.0 spec (1.1 does not cover tcp streams) | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 METHODS | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head2 new | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Create JSONRPC component session and return the session id. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Parameters: | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =over | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item Port | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Port number for listen. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =item Handler | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Hash variable contains handler name as key, handler poe state name as value. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Handler name (key) is used as JSON-RPC method name. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | So if you send {"method":"echo"}, this module call the poe state named "echo". | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $self = shift->SUPER::new( @_ > 1 ? {@_} : $_[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $self->{parent} = $poe_kernel->get_active_session->ID; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $self->{json} ||= JSON->new; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"json"} ||= "JSON"->new
MC/DC0%0/2
$self->{"json"} ||= "JSON"->new
$self->{"json"}
"JSON"->new
Truth table0%0/3
A ||= B
A= $self->{"json"}B= "JSON"->new
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | my $session = POE::Session->create( | ||||||||||||||||||||||||||||
| 95 | object_states => [ | ||||||||||||||||||||||||||||
| 96 | $self => { | ||||||||||||||||||||||||||||
| 97 | 0 | map { ( $_ => "poe_$_", ) } | |||||||||||||||||||||||||||
| 98 | qw/_start init_server input_handler result error send/ | ||||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | ], | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $session->ID; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 HANDLER PARAMETERS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =over | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =item ARG0 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | A session id of PoCo::Server::JSONRPC itself. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =item ARG1 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | The id of the client you're treating, send that back in result/error. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item ARG2 .. ARGN | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | JSONRPC argguments | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | ex) If you send following request | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | {"method":"echo", "params":["foo", "bar"]} | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | then, "echo" handler is called and parameters is that ARG0 is component session id, ARG1 is client id, ARG2 "foo", ARG3 "bar". | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 HANDLER RESPONSE | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | You must call either "result" or "error" state in your handlers to response result or error. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | ex: | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | $kernel->post( $component_session_id => "result" => $client_id, "result value" ) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $component_session_id is ARG0 in handler. If you do above, response is: | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | {"result":"result value", "error":""} | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 POE METHODS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Inner method for POE states. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 poe__start | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub poe__start { | ||||||||||||||||||||||||||
|
Subroutine
poe__start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ($self, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | $heap->{clients} = {}; | |||||||||||||||||||||||||||
| 155 | 0 | $heap->{id} = 0; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | $kernel->yield('init_server'); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 poe_init_server | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Should be defined in Http or Tcp | ||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub poe_init_server { print "error init_server\n"; } | ||||||||||||||||||||||||||
|
Subroutine
poe_init_server: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 poe_input_handler | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub poe_input_handler { | ||||||||||||||||||||||||||
|
Subroutine
poe_input_handler: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 0 | my ($self, $kernel, $session, $heap, $request, $response, $dirmatch) = @_[OBJECT, KERNEL, SESSION, HEAP, ARG0..$#_ ]; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | $heap->{clients}->{$heap->{id}} = {json_id => undef, response => $response}; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | my $json; | |||||||||||||||||||||||||||
| 177 | 0 | eval { | |||||||||||||||||||||||||||
| 178 | 0 | $json = $self->{json}->decode( $request->content ); | |||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $kernel->yield('error', $heap->{id}, q{invalid json request}); | |||||||||||||||||||||||||||
| 182 | 0 | return; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | $heap->{clients}->{$heap->{id}} = {json_id => $json->{id}, response => $response}; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | unless ($json and $json->{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$json and $json->{"method"}
MC/DC0%0/2
$json and $json->{"method"}
$json
$json->{"method"}
Truth table0%0/3
A and B
A= $jsonB= $json->{"method"}
| |||||||||||||||||||||||||||||
| 188 | 0 | $kernel->yield('error', $heap->{id}, q{parameter "method" is required}); | |||||||||||||||||||||||||||
| 189 | 0 | return; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | unless ($self->{Handler}{ $json->{method} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $kernel->yield('error', $heap->{id}, qq{no such method "$json->{method}"}); | |||||||||||||||||||||||||||
| 194 | 0 | return; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | my $handler = $self->{Handler}{ $json->{method} }; | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | my @params = @{ $json->{params} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$json->{'params'} || []
MC/DC0%0/1
$json->{'params'} || []
$json->{'params'}
Truth table0%0/2
A
A= $json->{'params'}
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | $kernel->post($self->{parent}, $handler, $session->ID, $heap->{id}, @params); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | $heap->{id}++; | |||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ($heap->{id}>=65535) { # limit to 2 bytes | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 0 | $heap->{id} = 0; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head2 poe_result | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub poe_result { | ||||||||||||||||||||||||||
|
Subroutine
poe_result: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | 0 | my ($self, $kernel, $heap, $id, @results) = @_[OBJECT, KERNEL, HEAP, ARG0..$#_ ]; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | #~ print "answering to ".$id."\n"; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | my $client = $heap->{clients}->{$id}; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $json_content = $self->{json}->encode( | ||||||||||||||||||||||||||||
| 220 | { id => $client->{json_id} || undef, | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | error => undef, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$client->{'json_id'} || undef
MC/DC0%0/1
$client->{'json_id'} || undef
$client->{'json_id'}
Truth table0%0/2
A
A= $client->{'json_id'}
| |||||||||||||||||||||||||||||
| 222 | result => (@results > 1 ? \@results : $results[0]), | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | ); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | #~ print "json content : ".$json_content."\n"; | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | $kernel->yield('send',$client->{response},$json_content); | |||||||||||||||||||||||||||
| 229 | 0 | delete $heap->{clients}->{$id}; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head2 poe_error | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =cut | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub poe_error { | ||||||||||||||||||||||||||
|
Subroutine
poe_error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 0 | my ($self, $kernel, $heap, $id, $error) = @_[OBJECT, KERNEL, HEAP, ARG0..$#_]; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | my $client = $heap->{clients}->{$id}; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | my $json_error_content = $self->{json}->encode( | ||||||||||||||||||||||||||||
| 242 | { id => $client->{json_id} || undef, | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | error => $error, | ||||||||||||||||||||||||||
|
Condition0%0/2
$client->{'json_id'} || undef
MC/DC0%0/1
$client->{'json_id'} || undef
$client->{'json_id'}
Truth table0%0/2
A
A= $client->{'json_id'}
| |||||||||||||||||||||||||||||
| 244 | result => undef, | ||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | #~ print "json content : ".$json_error_content."\n"; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | $kernel->yield('send',$client->{response},$json_error_content); | |||||||||||||||||||||||||||
| 251 | 0 | delete $heap->{clients}->{$id}; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head2 poe_send | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Should be defined in Http or Tcp | ||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | sub poe_send { print "error poe_send\n"; } | ||||||||||||||||||||||||||
|
Subroutine
poe_send: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 262 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Côme Chilliet <mcmic@cpan.org> | ||||||||||||||||||||||||||||
| 265 | Daisuke Murase <typester@cpan.org> | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 270 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 273 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 2 | 1; | |||||||||||||||||||||||||||