From e2fc839535946082941eda8655acffe24c82526f Mon Sep 17 00:00:00 2001 From: Bill Pittman Date: Thu, 14 Mar 2024 12:28:00 -0500 Subject: [PATCH 1/2] examples: Updated to use upZenohClient Signed-off-by: Bill Pittman --- pubsub/src/main_pub.cpp | 12 ++++++------ pubsub/src/main_sub.cpp | 8 ++++---- rpc/src/main_rpc_client.cpp | 6 +++--- rpc/src/main_rpc_server.cpp | 12 ++++++------ 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pubsub/src/main_pub.cpp b/pubsub/src/main_pub.cpp index ea13c17..1a9fc5f 100644 --- a/pubsub/src/main_pub.cpp +++ b/pubsub/src/main_pub.cpp @@ -29,7 +29,7 @@ #include // For sleep #include -#include +#include #include #include #include @@ -81,7 +81,7 @@ std::uint8_t* getCounter() { return &counter; } -UCode sendMessage(ZenohUTransport *transport, +UCode sendMessage(upZenohClient *transport, UUri &uri, std::uint8_t *buffer, size_t size) { @@ -112,12 +112,12 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - ZenohUTransport *transport = &ZenohUTransport::instance(); + upZenohClient *transport = &upZenohClient::instance(); /* Initialize zenoh utransport */ status = transport->init(); if (UCode::OK != status.code()) { - spdlog::error("ZenohUTransport init failed"); + spdlog::error("upZenohClientinit failed"); return -1; } @@ -151,9 +151,9 @@ int main(int argc, /* Terminate zenoh utransport */ status = transport->term(); if (UCode::OK != status.code()) { - spdlog::error("ZenohUTransport term failed"); + spdlog::error("upZenohClient term failed"); return -1; } return 0; -} \ No newline at end of file +} diff --git a/pubsub/src/main_sub.cpp b/pubsub/src/main_sub.cpp index a4642f8..0e7034b 100644 --- a/pubsub/src/main_sub.cpp +++ b/pubsub/src/main_sub.cpp @@ -27,7 +27,7 @@ #include #include #include // For sleep -#include +#include #include using namespace uprotocol::utransport; @@ -96,12 +96,12 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - ZenohUTransport *transport = &ZenohUTransport::instance(); + upZenohClient *transport = &upZenohClient::instance(); /* init zenoh utransport */ status = transport->init(); if (UCode::OK != status.code()){ - spdlog::error("ZenohUTransport init failed"); + spdlog::error("upZenohClient init failed"); return -1; } @@ -146,7 +146,7 @@ int main(int argc, /* term zenoh utransport */ status = transport->term(); if (UCode::OK != status.code()) { - spdlog::error("ZenohUTransport term failed"); + spdlog::error("upZenohClient term failed"); return -1; } diff --git a/rpc/src/main_rpc_client.cpp b/rpc/src/main_rpc_client.cpp index 48dd26d..797204b 100644 --- a/rpc/src/main_rpc_client.cpp +++ b/rpc/src/main_rpc_client.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include @@ -55,7 +55,7 @@ UPayload sendRPC(UUri& uri) { UPayload payload(buffer, sizeof(buffer), UPayloadType::VALUE); /* send the RPC request , a future is returned from invokeMethod */ - std::future result = ZenohRpcClient::instance().invokeMethod(uri, payload, attributes); + std::future result = upZenohClient::instance().invokeMethod(uri, payload, attributes); if (!result.valid()) { spdlog::error("Future is invalid"); @@ -78,7 +78,7 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - ZenohRpcClient *rpcClient = &ZenohRpcClient::instance(); + upZenohClient *rpcClient = &upZenohClient::instance(); /* init RPC client */ status = rpcClient->init(); diff --git a/rpc/src/main_rpc_server.cpp b/rpc/src/main_rpc_server.cpp index 9e52011..4654c73 100644 --- a/rpc/src/main_rpc_server.cpp +++ b/rpc/src/main_rpc_server.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include @@ -63,7 +63,7 @@ class RpcListener : public UListener { UAttributes responseAttributes = builder.build(); /* Send the response */ - return ZenohUTransport::instance().send(uri, responsePayload, responseAttributes); + return upZenohClient::instance().send(uri, responsePayload, responseAttributes); } }; @@ -80,12 +80,12 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - ZenohUTransport *transport = &ZenohUTransport::instance(); + upZenohClient *transport = &upZenohClient::instance(); /* init zenoh utransport */ status = transport->init(); if (UCode::OK != status.code()) { - spdlog::error("ZenohUTransport init failed"); + spdlog::error("upZenohClient init failed"); return -1; } @@ -111,9 +111,9 @@ int main(int argc, /* term zenoh utransport */ status = transport->term(); if (UCode::OK != status.code()) { - spdlog::error("ZenohUTransport term failed"); + spdlog::error("upZenohClient term failed"); return -1; } return 0; -} \ No newline at end of file +} From 52e561ce28540763b1d837fb95b19c5875548a56 Mon Sep 17 00:00:00 2001 From: Bill Pittman Date: Wed, 20 Mar 2024 11:41:59 -0500 Subject: [PATCH 2/2] examples: Use shared_ptr for reference counting Use a shared_ptr for reference counting and remove the explicit calls to init/term. Signed-off-by: Bill Pittman --- pubsub/src/main_pub.cpp | 14 +++----------- pubsub/src/main_sub.cpp | 12 ++---------- rpc/src/main_rpc_client.cpp | 14 +++----------- rpc/src/main_rpc_server.cpp | 15 ++++----------- 4 files changed, 12 insertions(+), 43 deletions(-) diff --git a/pubsub/src/main_pub.cpp b/pubsub/src/main_pub.cpp index 1a9fc5f..d682b1b 100644 --- a/pubsub/src/main_pub.cpp +++ b/pubsub/src/main_pub.cpp @@ -81,7 +81,7 @@ std::uint8_t* getCounter() { return &counter; } -UCode sendMessage(upZenohClient *transport, +UCode sendMessage(std::shared_ptr transport, UUri &uri, std::uint8_t *buffer, size_t size) { @@ -112,11 +112,10 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - upZenohClient *transport = &upZenohClient::instance(); + std::shared_ptr transport = upZenohClient::instance(); /* Initialize zenoh utransport */ - status = transport->init(); - if (UCode::OK != status.code()) { + if (nullptr == transport) { spdlog::error("upZenohClientinit failed"); return -1; } @@ -148,12 +147,5 @@ int main(int argc, sleep(1); } - /* Terminate zenoh utransport */ - status = transport->term(); - if (UCode::OK != status.code()) { - spdlog::error("upZenohClient term failed"); - return -1; - } - return 0; } diff --git a/pubsub/src/main_sub.cpp b/pubsub/src/main_sub.cpp index 0e7034b..eac19cc 100644 --- a/pubsub/src/main_sub.cpp +++ b/pubsub/src/main_sub.cpp @@ -96,11 +96,10 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - upZenohClient *transport = &upZenohClient::instance(); + std::shared_ptr transport = upZenohClient::instance(); /* init zenoh utransport */ - status = transport->init(); - if (UCode::OK != status.code()){ + if (nullptr == transport){ spdlog::error("upZenohClient init failed"); return -1; } @@ -143,12 +142,5 @@ int main(int argc, } } - /* term zenoh utransport */ - status = transport->term(); - if (UCode::OK != status.code()) { - spdlog::error("upZenohClient term failed"); - return -1; - } - return 0; } diff --git a/rpc/src/main_rpc_client.cpp b/rpc/src/main_rpc_client.cpp index 797204b..98a531d 100644 --- a/rpc/src/main_rpc_client.cpp +++ b/rpc/src/main_rpc_client.cpp @@ -55,7 +55,7 @@ UPayload sendRPC(UUri& uri) { UPayload payload(buffer, sizeof(buffer), UPayloadType::VALUE); /* send the RPC request , a future is returned from invokeMethod */ - std::future result = upZenohClient::instance().invokeMethod(uri, payload, attributes); + std::future result = upZenohClient::instance()->invokeMethod(uri, payload, attributes); if (!result.valid()) { spdlog::error("Future is invalid"); @@ -78,11 +78,10 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - upZenohClient *rpcClient = &upZenohClient::instance(); + std::shared_ptr rpcClient = upZenohClient::instance(); /* init RPC client */ - status = rpcClient->init(); - if (UCode::OK != status.code()) { + if (nullptr == rpcClient) { spdlog::error("init failed"); return -1; } @@ -103,12 +102,5 @@ int main(int argc, sleep(1); } - /* term RPC client */ - status = rpcClient->term(); - if (UCode::OK != status.code()) { - spdlog::error("term failed"); - return -1; - } - return 0; } diff --git a/rpc/src/main_rpc_server.cpp b/rpc/src/main_rpc_server.cpp index 4654c73..f24e2fd 100644 --- a/rpc/src/main_rpc_server.cpp +++ b/rpc/src/main_rpc_server.cpp @@ -63,7 +63,7 @@ class RpcListener : public UListener { UAttributes responseAttributes = builder.build(); /* Send the response */ - return upZenohClient::instance().send(uri, responsePayload, responseAttributes); + return upZenohClient::instance()->send(uri, responsePayload, responseAttributes); } }; @@ -80,11 +80,10 @@ int main(int argc, signal(SIGINT, signalHandler); UStatus status; - upZenohClient *transport = &upZenohClient::instance(); + std::shared_ptr transport = upZenohClient::instance(); /* init zenoh utransport */ - status = transport->init(); - if (UCode::OK != status.code()) { + if (nullptr == transport) { spdlog::error("upZenohClient init failed"); return -1; } @@ -93,6 +92,7 @@ int main(int argc, /* register listener to handle RPC requests */ status = transport->registerListener(rpcUri, listener); + if (UCode::OK != status.code()) { spdlog::error("registerListener failed"); return -1; @@ -108,12 +108,5 @@ int main(int argc, return -1; } - /* term zenoh utransport */ - status = transport->term(); - if (UCode::OK != status.code()) { - spdlog::error("upZenohClient term failed"); - return -1; - } - return 0; }