MIRA
Classes | Public Member Functions | Protected Types | List of all members
RPCClient Class Reference

The RPCClient is responsible for handling the client-side of an rpc call. More...

#include <rpc/RPCClient.h>

Inheritance diagram for RPCClient:
Inheritance graph
[legend]

Classes

struct  PendingResponse
 PendingResponse template class. More...
 
struct  PendingResponseBase
 Interface for PendingResponse. More...
 

Public Member Functions

template<typename Backend , typename R , typename... ARGS>
RPCFuture< R > call (typename Backend::ClientRequest &request, const std::string &service, const std::string &method, ARGS &&... args)
 Generates an RPCRequest for an RPC call. More...
 
template<typename Backend , typename R >
RPCFuture< R > addPendingResponse (const std::string &callId)
 Adds a new "PendingResponse" object for a call (with the specified callid) that was either generated by the above call() method or by a call that was created manually (e.g. More...
 
template<typename Backend >
std::string handleResponse (typename Backend::ClientResponse &response)
 Is called after the RPCServer has sent the response of the RPC call to handle the response and to set the return value or return state of the RPCFuture that was returned by the call method. More...
 
void handleServiceRemoved (const std::string &callId)
 
virtual void onDestructFuture (const std::string &callId)
 Is called by the RPCFutures that are created by the call() method upon the destruction of the RPCFutures. More...
 

Protected Types

typedef boost::shared_ptr< PendingResponseBasePendingResponsePtr
 

Detailed Description

The RPCClient is responsible for handling the client-side of an rpc call.

The client side view of an rpc call is as follows:

  1. the call() method is called and generates an RPCRequest based on the desired service and method strings and the parameters of the method.
    • the RPCRequest can be a realization of multiple possible types, like: binary requests, json rpc request, etc.
    • the RPCRequest can be thought of some kind of container that contains all information about the desired call (for binary rpc calls it is a buffer with serialized data, with serialized parameters etc.)
    • the result of the call() method is an RPCFuture which allows the caller to wait until the call has finished and to access the result value of the call as soon as it becomes available.
    • the calls are identified by IDs which are generated randomly based on UUIDs
    • internally a PendingResponse object is created for each call and stored in an internal map together with the call id. The PendingResponse object is kept until the RPCFuture of the call is destroyed (e.g. since the caller is not interested in the response) or until the actual response was received.
  2. the data of the RPCRequest generated by the call method is shipped as RPCClientRequest to the remote side where it is processed as RPCServerRequest by the RPCServer and the actual call is performed. As result the RPCServer will return an RPCServerResponse.
  3. the response is received as RPCClientResponse at the client side
  4. the handleResponse method of the RPCClient is called with the received response. It will extract the return value (if any) and set that value to the RPCFuture in order to signal the caller that the desired call has finished and to return the result.
    • internally the handleResponse method looks for the corresponding PendingResponse object (that was created in step 1) using the ID of the call, which is included in the response header
    • the PendingResponse object is responsible for handling the response
See also
Services and Remote Procedure Calls, RPCServer

Member Typedef Documentation

◆ PendingResponsePtr

typedef boost::shared_ptr<PendingResponseBase> PendingResponsePtr
protected

Member Function Documentation

◆ call()

RPCFuture<R> call ( typename Backend::ClientRequest &  request,
const std::string &  service,
const std::string &  method,
ARGS &&...  args 
)
inline

Generates an RPCRequest for an RPC call.

This method takes the name of the service, the name of the RPC method and a variable number of optional parameters p1 ... pn to generate an RPC request that is returned in the first parameter. The request type can be a BinaryRPCClientRequest, JsonRPCClientRequest, etc.

Parameters
[out]requestThe request object which will be filled with all information of the request
serviceThe name of the service
methodThe method name of the requested call
p1The first optional parameter. See pn.
pnVariable number of optional parameters. The number and type depends on the requested RPC method. Both will be checked by the RPC server. If the parameter types do not match, the server will return an error.
Returns
Returns an RPCFuture object. The type of the RPCFuture depends on the return type of the RPC method. It allows the caller to wait for the RPC call to finish and to retrieve the return value. The return value for the RPCFuture will be set by handleResponse().

Example:

RPCFuture<float> result = rpcclient.call<BinaryRPCBackend, float>(request, "myservice", "mymethod", 123, 1.23f);
result.wait(); // wait for the result
float r = result.get(); // get the result value

◆ addPendingResponse()

RPCFuture<R> addPendingResponse ( const std::string &  callId)
inline

Adds a new "PendingResponse" object for a call (with the specified callid) that was either generated by the above call() method or by a call that was created manually (e.g.

JSON calls).

This method is internally used by the call() method.

Returns
Returns an RPCFuture object. The type of the RPCFuture depends on the return type of the RPC method. It allows the caller to wait for the RPC call to finish and to retrieve the return value. The return value for the RPCFuture will be set by handleResponse().

◆ handleResponse()

std::string handleResponse ( typename Backend::ClientResponse &  response)
inline

Is called after the RPCServer has sent the response of the RPC call to handle the response and to set the return value or return state of the RPCFuture that was returned by the call method.

Returns
callId of the response

◆ handleServiceRemoved()

void handleServiceRemoved ( const std::string &  callId)
inline

◆ onDestructFuture()

virtual void onDestructFuture ( const std::string &  callId)
inlinevirtual

Is called by the RPCFutures that are created by the call() method upon the destruction of the RPCFutures.

Internally, the pending responses are removed since no one is longer interested in the response.

Implements AbstractRPCClient.


The documentation for this class was generated from the following file: