forked from philips-software/amp-embedded-infra-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rpc example (philips-software#141)
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |