Skip to content

Commit

Permalink
feat: add rpc example (philips-software#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjaegers committed Dec 20, 2022
1 parent 169dd81 commit 0e0d709
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ if (EMIL_HOST_BUILD)
add_subdirectory(http)
add_subdirectory(https)
add_subdirectory(mdns)
add_subdirectory(rpc)
add_subdirectory(serial_net)
add_subdirectory(serial_output)
add_subdirectory(sntp)
Expand Down
4 changes: 4 additions & 0 deletions examples/rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(examples.rpc_server)
target_sources(examples.rpc_server PRIVATE Server.cpp)
target_link_libraries(examples.rpc_server PRIVATE services.network_instantiations hal.generic)
protocol_buffer_echo_cpp(examples.rpc_server Console.proto)
19 changes: 19 additions & 0 deletions examples/rpc/Console.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

import "EchoAttributes.proto";

package examples;
option java_package = "com.philips.emil.protobufEcho";
option java_outer_classname = "ConsoleProto";

message Message
{
string message = 2 [(string_size) = 128];
}

service Console
{
option (service_id) = 1;

rpc Write(Message) returns (Nothing) { option (method_id) = 1; }
}
65 changes: 65 additions & 0 deletions examples/rpc/Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "generated/echo/Console.pb.hpp"
#include "hal/generic/TimerServiceGeneric.hpp"
#include "protobuf/echo/Echo.hpp"
#include "services/network_instantiations/NetworkAdapter.hpp"
#include <iostream>

class Console
: public examples::Console
{
public:
Console(services::Echo& echo)
: examples::Console(echo)
{}

void Write(infra::BoundedConstString message) override
{
std::cout << infra::AsStdString(message) << std::endl;
MethodDone();
}
};

class EchoConnection
: public services::EchoOnConnection
{
public:
EchoConnection()
: console(*this)
{}

private:
Console console;
};

class RpcServer
: public services::ServerConnectionObserverFactory
{
public:
RpcServer(services::ConnectionFactory& factory)
: listener(factory.Listen(1234, *this))
{}

void ConnectionAccepted(infra::AutoResetFunction<void(infra::SharedPtr<services::ConnectionObserver> connectionObserver)>&& createdObserver, services::IPAddress address) override
{
if (connection)
connection->::services::EchoOnConnection::Subject().AbortAndDestroy();

if (connection.Allocatable())
createdObserver(connection.Emplace());
}

private:
infra::SharedPtr<void> listener;
infra::SharedOptional<EchoConnection> connection;
};

int main(int argc, const char* argv[], const char* env[])
{
static hal::TimerServiceGeneric timerService;
static main_::NetworkAdapter network;
static RpcServer rpc(network.ConnectionFactory());

network.Run();

return 0;
}

0 comments on commit 0e0d709

Please sign in to comment.