NAME

JSON::RPC2::AnyEvent::Client - Asynchronous nonblocking JSON RPC2 client with method mapping

SYNOPSIS

use JSON::RPC2::AnyEvent::Client;

# create tcp connection
my $rpc = JSON::RPC2::AnyEvent::Client->new(
    host     => "127.0.0.1",
    port     => 5555,
    on_error => sub{ die $_[0] } 
);

# call
$rpc->sum( 1, 2, sub{
    my ( $failed, $result, $error ) = @_;
    print $result unless $failed || $error;
})

# call remote function with simple configure
$rpc->service('agent')->listed()->remote_function( 'param1', 'param2', sub{
    my ( $failed, $result, $error ) = @_;
})

# some more constructor arguments
my $rpc = JSON::RPC2::AnyEvent::Client->new(
    url     => "https://$host:$port/api", # http/https transport
    service => 'agent',
    call    => 'listed' || 'named',
    service => '_service',  # rename any this module methods
);

# destroy rpc connection when done
$rpc->destroy;

DESCRIPTION

JSON::RPC2::AnyEvent::Client is JSON RPC2 client, with tcp/http/https transport. Remote functions is mapped to local client object methods. For example remote function fn(...) is called as $c->fn(...,cb). Params of function is params of remote functions with additional one at the end of param list. Additional last param is result handler soubroutine.

Implementation is based on JSON RPC2 implementation JSON::RPC2::Client. Transport implementation is based on AnyEvent::Handle for tcp, and on AnyEvent::HTTP for http/https.

The 'tcp' implementation use persistent connection, that make tcp connection at object creation and use it all object life time. The http/https persistence is AnyEvent::HTTP implementation dependent and currently it is not persistent for idempotent requsests (JSON RPC2 need POST requset). See description of 'persistent' and 'keepalive' params of AnyEvent::HTTP.

METHODS

RESULT HANDLER CALLBACK

The result callback handler is a soubroutine that called when rpc function is called and result is arrived or an error occured. There three param of callback is ( $fail, $result, $error );

The $fail is transport error. It is string that contain description of communication or data decoding error.

The $result is server responce, valid only when there is no fail or error.

The $error is described in rpc protocol standart remote server error responce. It is valid only when no fail.

There is special case for simple applications enabled by simplify_errors constructor argument. The result callback at this case have only two params. First param is transport error if any or text error message arrived from remote service. Simplified callback prototype is: ( $error, $result );

DEPENDENCIES

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Serguei Okladnikov oklaspec@gmail.com