1414#include < unistd.h>
1515#include < up-cpp/communication/RpcClient.h>
1616#include < up-transport-zenoh-cpp/ZenohUTransport.h>
17+ #include < uprotocol/v1/ustatus.pb.h>
1718
1819#include < chrono>
1920#include < csignal>
2021#include < iostream>
2122
2223#include " common.h"
2324
24- using namespace uprotocol ::v1;
25- using namespace uprotocol ::communication;
26- using namespace uprotocol ::datamodel::builder;
25+ constexpr uint32_t METHOD_RPC_RESOURCE_ID = 12 ;
26+ constexpr std::chrono::milliseconds RPCCLIENT_TTL (500 );
27+
28+ using UMessage = uprotocol::v1::UMessage;
29+ using UStatus = uprotocol::v1::UStatus;
30+ using UPayloadFormat = uprotocol::v1::UPayloadFormat;
31+ using RpcClient = uprotocol::communication::RpcClient;
2732using ZenohUTransport = uprotocol::transport::ZenohUTransport;
33+ using UUri = uprotocol::v1::UUri;
2834
29- bool gTerminate = false ;
35+ bool g_terminate = false ;
3036
3137void signalHandler (int signal) {
3238 if (signal == SIGINT) {
3339 std::cout << " Ctrl+C received. Exiting..." << std::endl;
34- gTerminate = true ;
40+ g_terminate = true ;
3541 }
3642}
3743
3844void OnReceive (RpcClient::MessageOrStatus expected) {
3945 if (!expected.has_value ()) {
40- UStatus status = expected.error ();
46+ const UStatus& status = expected.error ();
4147 spdlog::error (" Expected value not found. -- Status: {}" ,
4248 status.DebugString ());
4349 return ;
@@ -62,34 +68,35 @@ void OnReceive(RpcClient::MessageOrStatus expected) {
6268 // sequence number, current time, and random value
6369 spdlog::debug (" (Client) Received message: {}" , message.DebugString ());
6470
65- const uint64_t * pdata = (uint64_t *)message.payload ().data ();
71+ const size_t num_bytes = message.payload ().size ();
72+ std::vector<uint64_t > pdata (num_bytes / sizeof (uint64_t ));
73+ memcpy (pdata.data (), message.payload ().data (), num_bytes);
6674 spdlog::info (" Received payload: {} - {}, {}" , pdata[0 ], pdata[1 ], pdata[2 ]);
6775}
6876
6977/* The sample RPC client applications demonstrates how to send RPC requests and
7078 * wait for the response
7179 */
72- int main (int argc, char ** argv) {
73- (void )argc;
74- (void )argv;
80+ int main (int argc, char * argv[]) {
81+ std::vector<std::string> args (argv, argv + argc);
7582
7683 if (argc < 2 ) {
7784 std::cout << " No Zenoh config has been provided" << std::endl;
7885 std::cout << " Usage: rpc_client <config_file>" << std::endl;
7986 return 1 ;
8087 }
8188
82- signal (SIGINT, signalHandler);
89+ ( void ) signal (SIGINT, signalHandler);
8390
8491 UUri source = getRpcUUri (0 );
85- UUri method = getRpcUUri (12 );
86- auto transport = std::make_shared<ZenohUTransport>(source, argv[ 1 ] );
87- auto client =
88- RpcClient (transport, std::move (method), UPriority::UPRIORITY_CS4,
89- std::chrono::milliseconds (500 ));
92+ UUri method = getRpcUUri (METHOD_RPC_RESOURCE_ID );
93+ auto transport = std::make_shared<ZenohUTransport>(source, args. at ( 1 ) );
94+ auto client = RpcClient (transport, std::move (method),
95+ uprotocol::v1:: UPriority::UPRIORITY_CS4,
96+ std::chrono::milliseconds (RPCCLIENT_TTL ));
9097 RpcClient::InvokeHandle handle;
9198
92- while (!gTerminate ) {
99+ while (!g_terminate ) {
93100 handle = client.invokeMethod (OnReceive);
94101 sleep (1 );
95102 }
0 commit comments