Skip to content

Transport agnostic interfaces #34

Open
@tinu73

Description

@tinu73

First of all this is really a great and useful framework.

I would like to raise the question or rather a discussion weather the interfaces are transport agnostic yes or no. If i am right in my conclusion they are not. Why?

  • The http client client.hpp and iclientconnector.hpp are implemented in a synchronous request/response pattern, therefore offering a Send method with return type std::string
  • A tcp client is preferably implemented in asynchronous send/receive pattern, therefore client.hpp and iclientconnector.hpp cannot be used 'agnostically' out of the box
  • For using json-rpc-cxx with a tcp client/server, the iclientconnector.hpp and client.hpp have to be extended by offering, e.g. a Post method without a return value and a MessageHandler, e.g.

iclientconnector.hpp

using MessageHandler = std::function<void(const std::string)>;
virtual void Post(const std::string &request) = 0;
MessageHandler OnMessage;

client.hpp

using MessageHandler = std::function<void(const JsonRpcResponse)>;
JsonRpcClient(IClientConnector &connector, version v) : connector(connector), v(v)
{
    connector.OnMessage = [this](const std::string &message)
    {
        json response = json::parse(message);
        JsonRpcResponse rpc_response{};
        ...
        OnMessage(rpc_response);
    }
}
...
MessageHandler OnMessage;

client.cpp

std::future<JsonRpcResponse> future;
std::promise<JsonRpcResponse> promise;
JsonRpcResponse response;
client.OnMessage = [&] (const JsonRpcResponse &response) mutable
{
    promise.set_value(response);
};
future = promise.get_future();
client.CallMethod(1, "GetProduct", {"0xff"});
response = future.get();
std::cout << response.result.dump() << std::endl;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions