Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transport agnostic interfaces #34

Open
tinu73 opened this issue May 28, 2022 · 1 comment
Open

Transport agnostic interfaces #34

tinu73 opened this issue May 28, 2022 · 1 comment

Comments

@tinu73
Copy link

tinu73 commented May 28, 2022

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;
@cinemast
Copy link
Contributor

Hi!

I am not sure I fully understood your issue with the synchronous interface. If you actually need async semantics, you can always wrap it into future/promises, right? Just introducing them on the connector level does not really make sense in my opinions, since the calling semantics through the RPC client will be synchronous again.

Could you explain in a bit more detail, maybe even using the TCP connector example what exactly would be easier/better with async interfaces?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants