nlib
nn::nlib::msgpack::jsonrpc::JsonRpcClient Class Referencefinal

Class to manage client side JSON-RPC. More...

#include "nn/nlib/msgpack/jsonrpc/JsonRpcClient.h"

Public Types

typedef void(* NoTargetResponseHandler) (UniquePtr< JsonRpcResponse > &rhs) NLIB_NOEXCEPT_FUNCPTR
 Function type called when a response could not be sorted. More...
 
typedef ::nlib_ns::threading::Future< UniquePtr< JsonRpcResponse > > FutureType
 A future type set when a response (or timeout) is assigned to each request.
 

Public Member Functions

 JsonRpcClient ()
 Instantiates the object with default parameters (default constructor).
 
 JsonRpcClient (const JsonRpcClient &rhs)
 Copy constructor. More...
 
JsonRpcClientoperator= (const JsonRpcClient &rhs)
 Assignment operator.
 
void swap (JsonRpcClient &rhs)
 A swap. More...
 
reqid_t GenerateId ()
 Creates and returns an ID to be placed in a JSON-RPC request. More...
 
errno_t GetFutureForId (FutureType *future, reqid_t reqid)
 Gets the future that corresponds to the request ID. More...
 
errno_t ResolveResponse (const void *buf, size_t nbytes)
 Receives the response data, and makes the corresponding future valid. More...
 
errno_t Abort (reqid_t reqid)
 Halts the reception of the response to a request. More...
 
errno_t SetNoTargetResponseHandler (NoTargetResponseHandler handler)
 Sets a function called when a response could not be sorted. More...
 
bool operator== (const JsonRpcClient &rhs) const
 Returns true if the client is the same.
 
bool operator!= (const JsonRpcClient &rhs) const
 Returns false if the client is the same.
 
 operator bool () const
 Returns true if the client is valid.
 

Static Public Member Functions

static JsonRpcClient OpenClient (const char *name)
 Opens a client. If a client doesn't exist, it creates one. More...
 
static JsonRpcClient SearchClient (const char *name)
 Searches and returns the opened client. More...
 
static void CloseClient (const JsonRpcClient &client)
 Closes the specified JsonRpcClient. More...
 
static void Shutdown ()
 Close all JsonRpcClient objects and stops using them.
 

Detailed Description

Class to manage client side JSON-RPC.

Description
It performs the following operations.
  • Uses GenerateId to generate the ID attached to the request.
  • Creates the future that will be valid if the response arrives.
  • Sorts the response from the server.

Definition at line 37 of file JsonRpcClient.h.

Member Typedef Documentation

◆ NoTargetResponseHandler

void(* nn::nlib::msgpack::jsonrpc::JsonRpcClient::NoTargetResponseHandler)(UniquePtr< JsonRpcResponse > &rhs)

Function type called when a response could not be sorted.

Description
Set in SetNoTargetResponseHandler.

Definition at line 45 of file JsonRpcClient.h.

Constructor & Destructor Documentation

◆ JsonRpcClient()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::JsonRpcClient ( const JsonRpcClient rhs)
inline

Copy constructor.

Parameters
[in]rhsClient object.

Definition at line 47 of file JsonRpcClient.h.

Member Function Documentation

◆ Abort()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::Abort ( reqid_t  reqid)

Halts the reception of the response to a request.

Parameters
[in]reqidRequest ID.
Return values
0Success.
ESRCHInvalid client.
Description
The request returns a JsonRpcResponse::kClientAbort error code as a response and halts.

◆ CloseClient()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::CloseClient ( const JsonRpcClient client)
static

Closes the specified JsonRpcClient.

Parameters
[in]clientClient object.
Description
For requests waiting for a reply that are managed by JsonRpcClient, the JsonRpcResponse::kClientAbort error code is returned and the process is canceled.

◆ GenerateId()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::GenerateId ( )

Creates and returns an ID to be placed in a JSON-RPC request.

Returns
Returns a nonzero unique ID.

◆ GetFutureForId()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::GetFutureForId ( FutureType future,
reqid_t  reqid 
)

Gets the future that corresponds to the request ID.

Parameters
[out]futurePointer to the future that becomes valid when the response corresponding to the request arrives.
[in]reqidRequest ID.
Return values
0Success.
ESRCHInvalid client.
ENOMEMNot enough memory.

◆ OpenClient()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::OpenClient ( const char *  name)
static

Opens a client. If a client doesn't exist, it creates one.

Parameters
[in]nameName of the client.
Returns
Client object.
Description
In many cases , you create a JsonRpcClient object for each server.
Sample code is provided below.
if (!myClient) { error }

◆ ResolveResponse()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::ResolveResponse ( const void *  buf,
size_t  nbytes 
)

Receives the response data, and makes the corresponding future valid.

Parameters
[in]bufPointer to response data.
[in]nbytesThe size of the response.
Return values
0Success.
EINVALbuf is NULL or nBytes is 0.
ESRCHInvalid client.
ENOMEMNot enough memory.
EILSEQResponse data could not be parsed.
Description
Correctly sorts the response received from the server (via a user defined communications path), and makes the corresponding future valid.
This function must be called from user code. In addition, the response data must be self-contained.

◆ SearchClient()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::SearchClient ( const char *  name)
static

Searches and returns the opened client.

Parameters
[in]nameName of the client.
Returns
Client object.

◆ SetNoTargetResponseHandler()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::SetNoTargetResponseHandler ( NoTargetResponseHandler  handler)

Sets a function called when a response could not be sorted.

Parameters
[in]handlerHandler.
Return values
0Success.
Description
A future that corresponds to a response cannot be made valid if a response is received after a timeout or Abort call is made.
The response can be processed by calling handler in this case.

◆ swap()

nn::nlib::msgpack::jsonrpc::JsonRpcClient::swap ( JsonRpcClient rhs)
inline

A swap.

Parameters
[in]rhsClient object.

Definition at line 52 of file JsonRpcClient.h.


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