Skip to content

Commit

Permalink
add example hello
Browse files Browse the repository at this point in the history
  • Loading branch information
ptesavol committed Aug 9, 2024
1 parent 7a56536 commit 755c169
Show file tree
Hide file tree
Showing 7 changed files with 924 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/streamr-proto-rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,16 @@ target_link_libraries(streamr-proto-rpc-test-integration
include(GoogleTest)
gtest_discover_tests(streamr-proto-rpc-test-integration)

add_executable(streamr-proto-rpc-example-hello
examples/hello/hello.cpp
examples/hello/proto/HelloRpc.pb.cc
)

target_link_libraries(streamr-proto-rpc-example-hello
PUBLIC streamr-proto-rpc
PUBLIC Folly::folly
)

target_include_directories(streamr-proto-rpc-example-hello
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/examples/hello/proto>
)
69 changes: 69 additions & 0 deletions packages/streamr-proto-rpc/examples/hello/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <folly/coro/BlockingWait.h>
#include "HelloRpc.client.pb.h"
#include "HelloRpc.pb.h"
#include "HelloRpc.server.pb.h"
#include "streamr-proto-rpc/ProtoCallContext.hpp"
#include "streamr-proto-rpc/RpcCommunicator.hpp"

using streamr::protorpc::HelloRpcService;
using streamr::protorpc::HelloRpcServiceClient;
using streamr::protorpc::ProtoCallContext;
using streamr::protorpc::RpcCommunicator;
using streamr::protorpc::RpcMessage;

class HelloService : public HelloRpcService {
public:
HelloResponse sayHello(
const HelloRequest& request,
const ProtoCallContext& /* callContext */) override {
HelloResponse response;
response.set_greeting("Hello, " + request.myname());
return response;
}
};

int main() {
// Setup server
RpcCommunicator communicator1;
HelloService helloService;

communicator1.registerRpcMethod<HelloRequest, HelloResponse>(
"sayHello",
std::bind( // NOLINT(modernize-avoid-bind)
&HelloService::sayHello,
&helloService,
std::placeholders::_1,
std::placeholders::_2));

// Setup client
RpcCommunicator communicator2;
HelloRpcServiceClient helloClient(communicator2);

// Simulate a network connection between the client and server
communicator1.setOutgoingMessageCallback(
[&communicator2](
const RpcMessage& message,
const std::string& /* requestId */,
const ProtoCallContext& /* context */) -> void {
communicator2.handleIncomingMessage(message, ProtoCallContext());
});

communicator2.setOutgoingMessageCallback(
[&communicator1](
const RpcMessage& message,
const std::string& /* requestId */,
const ProtoCallContext& /* context */) -> void {
communicator1.handleIncomingMessage(message, ProtoCallContext());
});

// Make the RPC call

HelloRequest request;
request.set_myname("Alice");
auto response = folly::coro::blockingWait(
helloClient.sayHello(request, ProtoCallContext()));
std::cout << response.greeting() << "\n";

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// generated by the protocol buffer streamr pluging. DO NOT EDIT!
// generated from protobuf file "HelloRpc.proto"

#ifndef STREAMR_PROTORPC_HELLORPC_CLIENT_PB_H
#define STREAMR_PROTORPC_HELLORPC_CLIENT_PB_H

#include <folly/experimental/coro/Task.h>
#include "HelloRpc.pb.h" // NOLINT
#include "streamr-proto-rpc/ProtoCallContext.hpp"
#include "streamr-proto-rpc/RpcCommunicator.hpp"


namespace streamr::protorpc {
class HelloRpcServiceClient {
private:
RpcCommunicator& communicator;
public:
HelloRpcServiceClient(RpcCommunicator& communicator) : communicator(communicator) {}
folly::coro::Task<HelloResponse> sayHello(const HelloRequest& request, const ProtoCallContext& callContext) {
return communicator.callRemote<HelloResponse, HelloRequest>("sayHello", request, callContext);
}
}; // class HelloRpcServiceClient
}; // namespace streamr::protorpc

#endif // STREAMR_PROTORPC_HELLORPC_CLIENT_PB_H

270 changes: 270 additions & 0 deletions packages/streamr-proto-rpc/examples/hello/proto/HelloRpc.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 755c169

Please sign in to comment.