From a70109751a6c1b823e5f05b5c08a429c7145f30d Mon Sep 17 00:00:00 2001 From: CihatAltiparmak Date: Mon, 5 Aug 2024 21:56:56 +0300 Subject: [PATCH 1/2] Added SIGTERM handler for linux and closed session when reading error is caught (#252) * Added SIGTERM handler for linux and closed session when reading error from tty * Added scope_exit for closing session * Moved scope_exit right after checking session * Minor tweak to scope_exit name Signed-off-by: Yadunund --------- Signed-off-by: Yadunund Co-authored-by: Yadunund --- rmw_zenoh_cpp/src/zenohd/main.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/rmw_zenoh_cpp/src/zenohd/main.cpp b/rmw_zenoh_cpp/src/zenohd/main.cpp index fd749d4b..fe6241be 100644 --- a/rmw_zenoh_cpp/src/zenohd/main.cpp +++ b/rmw_zenoh_cpp/src/zenohd/main.cpp @@ -36,6 +36,8 @@ #include "rmw/error_handling.h" +#include "rcpputils/scope_exit.hpp" + static bool running = true; class KeyboardReader final @@ -192,18 +194,24 @@ int main(int argc, char ** argv) return 1; } - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { printf("Unable to open router session!\n"); return 1; } + auto always_close_session = rcpputils::make_scope_exit( + [&session]() { + z_close(z_move(session)); + }); + printf( "Started Zenoh router with id %s.\n", - rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&s))).c_str()); + rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&session))).c_str()); #ifdef _WIN32 SetConsoleCtrlHandler(quit, TRUE); #else signal(SIGINT, quit); + signal(SIGTERM, quit); #endif KeyboardReader keyreader; @@ -227,7 +235,5 @@ int main(int argc, char ** argv) std::this_thread::sleep_for(std::chrono::milliseconds(100)); } - z_close(z_move(s)); - return 0; } From b78d6a15ca47681ddd11613c085fe41420fa9168 Mon Sep 17 00:00:00 2001 From: Yadu Date: Wed, 7 Aug 2024 13:51:57 +0200 Subject: [PATCH 2/2] Const qualify function to check if service is avaialble (#260) Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/detail/graph_cache.cpp | 6 +++--- rmw_zenoh_cpp/src/detail/graph_cache.hpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/graph_cache.cpp b/rmw_zenoh_cpp/src/detail/graph_cache.cpp index 79bff430..00e78be6 100644 --- a/rmw_zenoh_cpp/src/detail/graph_cache.cpp +++ b/rmw_zenoh_cpp/src/detail/graph_cache.cpp @@ -1202,13 +1202,13 @@ rmw_ret_t GraphCache::get_entities_info_by_topic( rmw_ret_t GraphCache::service_server_is_available( const char * service_name, const char * service_type, - bool * is_available) + bool * is_available) const { *is_available = false; std::lock_guard lock(graph_mutex_); - GraphNode::TopicMap::iterator service_it = graph_services_.find(service_name); + GraphNode::TopicMap::const_iterator service_it = graph_services_.find(service_name); if (service_it != graph_services_.end()) { - GraphNode::TopicTypeMap::iterator type_it = service_it.value().find(service_type); + GraphNode::TopicTypeMap::const_iterator type_it = service_it->second.find(service_type); if (type_it != service_it->second.end()) { for (const auto & [_, topic_data] : type_it->second) { if (topic_data->subs_.size() > 0) { diff --git a/rmw_zenoh_cpp/src/detail/graph_cache.hpp b/rmw_zenoh_cpp/src/detail/graph_cache.hpp index 47d33765..8d2b6802 100644 --- a/rmw_zenoh_cpp/src/detail/graph_cache.hpp +++ b/rmw_zenoh_cpp/src/detail/graph_cache.hpp @@ -167,7 +167,7 @@ class GraphCache final rmw_ret_t service_server_is_available( const char * service_name, const char * service_type, - bool * is_available); + bool * is_available) const; /// @brief Signature for a function that will be invoked by the GraphCache when a QoS /// event is detected.