Skip to content

Commit

Permalink
Merge pull request #1 from billpittman/feature/upZenohClient
Browse files Browse the repository at this point in the history
examples: Updated to use upZenohClient
  • Loading branch information
MelamudMichael authored Mar 27, 2024
2 parents 8aa402c + 52e561c commit b428cd3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 52 deletions.
20 changes: 6 additions & 14 deletions pubsub/src/main_pub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <unistd.h> // For sleep

#include <spdlog/spdlog.h>
#include <up-client-zenoh-cpp/transport/zenohUTransport.h>
#include <up-client-zenoh-cpp/client/upZenohClient.h>
#include <up-cpp/uuid/factory/Uuidv8Factory.h>
#include <up-cpp/uri/serializer/LongUriSerializer.h>
#include <up-core-api/ustatus.pb.h>
Expand Down Expand Up @@ -81,7 +81,7 @@ std::uint8_t* getCounter() {
return &counter;
}

UCode sendMessage(ZenohUTransport *transport,
UCode sendMessage(std::shared_ptr<upZenohClient> transport,
UUri &uri,
std::uint8_t *buffer,
size_t size) {
Expand Down Expand Up @@ -112,12 +112,11 @@ int main(int argc,
signal(SIGINT, signalHandler);

UStatus status;
ZenohUTransport *transport = &ZenohUTransport::instance();
std::shared_ptr<upZenohClient> transport = upZenohClient::instance();

/* Initialize zenoh utransport */
status = transport->init();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport init failed");
if (nullptr == transport) {
spdlog::error("upZenohClientinit failed");
return -1;
}

Expand Down Expand Up @@ -148,12 +147,5 @@ int main(int argc,
sleep(1);
}

/* Terminate zenoh utransport */
status = transport->term();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport term failed");
return -1;
}

return 0;
}
}
16 changes: 4 additions & 12 deletions pubsub/src/main_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <iostream>
#include <spdlog/spdlog.h>
#include <unistd.h> // For sleep
#include <up-client-zenoh-cpp/transport/zenohUTransport.h>
#include <up-client-zenoh-cpp/client/upZenohClient.h>
#include <up-cpp/uri/serializer/LongUriSerializer.h>

using namespace uprotocol::utransport;
Expand Down Expand Up @@ -96,12 +96,11 @@ int main(int argc,
signal(SIGINT, signalHandler);

UStatus status;
ZenohUTransport *transport = &ZenohUTransport::instance();
std::shared_ptr<upZenohClient> transport = upZenohClient::instance();

/* init zenoh utransport */
status = transport->init();
if (UCode::OK != status.code()){
spdlog::error("ZenohUTransport init failed");
if (nullptr == transport){
spdlog::error("upZenohClient init failed");
return -1;
}

Expand Down Expand Up @@ -143,12 +142,5 @@ int main(int argc,
}
}

/* term zenoh utransport */
status = transport->term();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport term failed");
return -1;
}

return 0;
}
16 changes: 4 additions & 12 deletions rpc/src/main_rpc_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <csignal>
#include <unistd.h>
#include <spdlog/spdlog.h>
#include <up-client-zenoh-cpp/rpc/zenohRpcClient.h>
#include <up-client-zenoh-cpp/client/upZenohClient.h>
#include <up-cpp/uuid/factory/Uuidv8Factory.h>
#include <up-cpp/uri/serializer/LongUriSerializer.h>

Expand Down Expand Up @@ -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<UPayload> result = ZenohRpcClient::instance().invokeMethod(uri, payload, attributes);
std::future<UPayload> result = upZenohClient::instance()->invokeMethod(uri, payload, attributes);

if (!result.valid()) {
spdlog::error("Future is invalid");
Expand All @@ -78,11 +78,10 @@ int main(int argc,
signal(SIGINT, signalHandler);

UStatus status;
ZenohRpcClient *rpcClient = &ZenohRpcClient::instance();
std::shared_ptr<upZenohClient> rpcClient = upZenohClient::instance();

/* init RPC client */
status = rpcClient->init();
if (UCode::OK != status.code()) {
if (nullptr == rpcClient) {
spdlog::error("init failed");
return -1;
}
Expand All @@ -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;
}
21 changes: 7 additions & 14 deletions rpc/src/main_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <chrono>
#include <csignal>
#include <unistd.h>
#include <up-client-zenoh-cpp/transport/zenohUTransport.h>
#include <up-client-zenoh-cpp/client/upZenohClient.h>
#include <up-cpp/uuid/factory/Uuidv8Factory.h>
#include <up-cpp/uri/serializer/LongUriSerializer.h>
#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -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);
}
};

Expand All @@ -80,19 +80,19 @@ int main(int argc,
signal(SIGINT, signalHandler);

UStatus status;
ZenohUTransport *transport = &ZenohUTransport::instance();
std::shared_ptr<upZenohClient> transport = upZenohClient::instance();

/* init zenoh utransport */
status = transport->init();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport init failed");
if (nullptr == transport) {
spdlog::error("upZenohClient init failed");
return -1;
}

auto rpcUri = LongUriSerializer::deserialize("/test_rpc.app/1/rpc.milliseconds");

/* register listener to handle RPC requests */
status = transport->registerListener(rpcUri, listener);

if (UCode::OK != status.code()) {
spdlog::error("registerListener failed");
return -1;
Expand All @@ -108,12 +108,5 @@ int main(int argc,
return -1;
}

/* term zenoh utransport */
status = transport->term();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport term failed");
return -1;
}

return 0;
}
}

0 comments on commit b428cd3

Please sign in to comment.