line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::RPC2::AnyEvent; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1586
|
use JSON::RPC2::AnyEvent::Server; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
43
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
1; |
8
|
|
|
|
|
|
|
__END__ |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=encoding utf-8 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
JSON::RPC2::AnyEvent - Yet-another, transport-independent and asynchronous JSON-RPC 2.0 implementation |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use JSON::RPC2::AnyEvent::Server; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $srv = JSON::RPC2::AnyEvent::Server->new( |
21
|
|
|
|
|
|
|
hello => "[family_name, first_name]" => sub{ |
22
|
|
|
|
|
|
|
my ($cv, $args, $original_args) = @_; |
23
|
|
|
|
|
|
|
my ($family, $given) = @$args; |
24
|
|
|
|
|
|
|
do_some_async_task(sub{ |
25
|
|
|
|
|
|
|
# Done! |
26
|
|
|
|
|
|
|
$cv->send("Hello, $given $family!"); |
27
|
|
|
|
|
|
|
}); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $cv = $srv->dispatch({ |
32
|
|
|
|
|
|
|
jsonrpc => "2.0", |
33
|
|
|
|
|
|
|
id => 1, |
34
|
|
|
|
|
|
|
method => 'hello', |
35
|
|
|
|
|
|
|
params => [qw(Sogoru Kyo)], |
36
|
|
|
|
|
|
|
}); |
37
|
|
|
|
|
|
|
my $res = $cv->recv; # { jsonrpc => "2.0", id => 1, result => "Hello, Kyo Sogoru!" } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $cv = $srv->dispatch({ |
40
|
|
|
|
|
|
|
jsonrpc => "2.0", |
41
|
|
|
|
|
|
|
id => 2, |
42
|
|
|
|
|
|
|
method => 'hello', |
43
|
|
|
|
|
|
|
params => {first_name => 'Ryoko', family_name => 'Kaminagi'} # You can pass a hash as well! |
44
|
|
|
|
|
|
|
}); |
45
|
|
|
|
|
|
|
my $res = $cv->recv; # { jsonrpc => "2.0", id => 2, result => "Hello, Ryoko Kaminagi!" } |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# For Notification Request, just returns undef. |
48
|
|
|
|
|
|
|
my $cv = $srv->dispatch({ |
49
|
|
|
|
|
|
|
jsonrpc => "2.0", |
50
|
|
|
|
|
|
|
method => "hello", |
51
|
|
|
|
|
|
|
params => ["Misaki", "Shizuno"] |
52
|
|
|
|
|
|
|
}); # notification request when "id" is omitted. |
53
|
|
|
|
|
|
|
not defined $cv; # true |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 DESCRIPTION |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
JSON::RPC2::AnyEvent is yet-another JSON-RPC 2.0 implementation. This module is very similar to L<JSON::RPC2> and |
59
|
|
|
|
|
|
|
actually shares the main goals. That is, transport independent, asynchronous, and light-weight. |
60
|
|
|
|
|
|
|
However, this module is designed so that it works with L<AnyEvent>, especially with L<AnyEvent::Handle>. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 THINK SIMPLE |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
JSON::RPC2::AnyEvent considers JSON-RPC as simple as possible. For example, L<JSON::RPC2::Server> abstracts JSON-RPC |
66
|
|
|
|
|
|
|
server as a kind of hash filter. Unlike L<JSON::RPC2::Server> accepts and outputs serialized JSON text, |
67
|
|
|
|
|
|
|
L<JSON::RPC2::AnyEvent::Server> accepts and outputs Perl hash: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
+----------+ |
70
|
|
|
|
|
|
|
| | |
71
|
|
|
|
|
|
|
Inuput | JSON-RPC | Output |
72
|
|
|
|
|
|
|
request ---------->| Server |----------> response |
73
|
|
|
|
|
|
|
(as a hash) | | (as a hash) |
74
|
|
|
|
|
|
|
+----------+ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Actually, it accepts any kind of Perl data (array, hash, and scalar!), then, outputs a JSON-like hash. Response hash can |
77
|
|
|
|
|
|
|
be either of successful response or error response. Anyway, it's a hash! |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
What you need to do is just to make or retrieve a JSON-like data structure in some way, and input it into the server, |
80
|
|
|
|
|
|
|
then, get the result as a hash. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Actually, JSON::RPC2::AnyEvent just treats Perl data structures instead of JSON, and has nothing to with serializing |
83
|
|
|
|
|
|
|
Perl data or deserializing JSON text. This concept allows you to use JSON-RPC on any kind of transport layer. |
84
|
|
|
|
|
|
|
In particular, this way is excellent with L<AnyEvent::Handle>, such as C<$h-E<gt>push_read(json =E<gt> sub{...})> and |
85
|
|
|
|
|
|
|
C<$h-E<gt>push_write(json =E<gt> ...)>. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
If you are interested in a "real" solution, you should look at L<JSON::RPC2::AnyEvent::Server::Handle>, which is an |
88
|
|
|
|
|
|
|
example to use this module on stream protocol like TCP. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SEE ALSO |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=over |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item L<JSON::RPC2::AnyEvent::Server> |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item L<JSON::RPC2::AnyEvent::Server::Handle> |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 LICENSE |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Copyright (C) Daisuke (yet another) Maki. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
107
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 AUTHOR |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Daisuke (yet another) Maki E<lt>maki.daisuke@gmail.comE<gt> |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|