1package 2 MyApp; 3 4use 5.006; 5use strict; 6use base qw(JSON::RPC::Procedure); # requires Perl 5.6 or later 7 8use Data::Dumper; 9 10 11sub _allowable_procedure { 12 return { 13 echo => \&echo, 14 sum => \&sum, 15 }; 16} 17 18 19sub echo : Public { 20 my ($s, $args) = @_; 21 return $args->[0]; 22} 23 24 25sub now : Public() { 26 return scalar(localtime); 27} 28 29 30sub sum : Number(a:num, b:num) { 31 my ($s, $obj) = @_; 32 return $obj->{a} + $obj->{b}; 33} 34 35 36sub sum2 : Public { 37 my $s = shift; 38 39 if ($s->version) { # JSONRPC 1.1 40 my $arg = shift; 41 return $arg->[0] + $arg->[1]; 42 } 43 else { # JSONRPC 1.0 44 return $_[0] + $_[1]; 45 } 46 47} 48 49 50sub sum3 : String(a, b) { 51 my $s = shift; 52 return $_[0]->{a} + $_[0]->{b}; 53} 54 55 56sub sum4 : Private { 57 my $s = shift; 58 # This is private... 59} 60 61 62 63 64 65 66package 67 MyApp::system; 68 69sub describe { 70 { 71 sdversion => "1.0", 72 name => 'MyApp', 73 }; 74} 75 76 77 781; 79__END__ 80 81=pod 82 83=head1 NAME 84 85MyApp - sample JSON-RPC server class 86 87=head1 DESCRIPTION 88 89This module is a smple code (for Perl 5.6 or later). 90Please check the source. 91 92 93=head2 PROCEDURES 94 95=over 96 97=item echo 98 99Takes a scalar and returns it as is. 100 101=item now 102 103Returns the current time. 104 105 106=item sum 107 108Takes two numbers and returns the total. 109 110 sum : Number(a:num, b:num) 111 112The two numbers are automatically set into 'a' and 'b'. 113 114=item sum2 115 116Takes two numbers and returns the total. 117 118 sum2 : Public 119 120This routine is a sample for both JSONRPC 1.1 and 1.0 121 122=item sum3 123 124Same as sum3 but its format is difference. 125 126 sum3 : String(a, b) 127 128=item sum4 129 130This is a private procedure, so client can't call this. 131 132 sum4 : Private 133 134 135=back MyApp::system::describe 136 137This is a reserved procedure returns a C<Service Description> object. 138 139See to L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#ServiceDescription>. 140 141=item _allowable_procedure 142 143If you change the name into C<allowable_procedure>, 144clients are able to call C<echo> and C<sum> only. 145 146C<allowable_procedure> is a special name and the method 147returns a hash reference contains procedure names and its code reference. 148 149 sub allowable_procedure { 150 return { 151 echo => \&echo, 152 sum => \&sum, 153 }; 154 } 155 156 157=cut 158 159=head1 AUTHOR 160 161Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt> 162 163 164=head1 COPYRIGHT AND LICENSE 165 166Copyright 2008 by Makamaka Hannyaharamitu 167 168This library is free software; you can redistribute it and/or modify 169it under the same terms as Perl itself. 170 171=cut 172 173