Skip to content

Commit

Permalink
Rewrite connecting to the router to only print when it fails.
Browse files Browse the repository at this point in the history
This simplifies the code, and makes it less noisy.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Dec 17, 2024
1 parent 894005d commit b2b76e0
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class rmw_context_impl_s::Data final
throw std::runtime_error("Error setting up zenoh session. ");
}

rmw_ret_t ret = RMW_RET_ERROR;

// Verify if the zenoh router is running if configured.
const std::optional<uint64_t> configured_connection_attempts =
rmw_zenoh_cpp::zenoh_router_check_attempts();
Expand All @@ -106,25 +104,23 @@ class rmw_context_impl_s::Data final
// Retry until the connection is successful.
constexpr std::chrono::milliseconds sleep_time(1000);
constexpr int64_t ticks_between_print(std::chrono::milliseconds(1000) / sleep_time);
while (ret != RMW_RET_OK && connection_attempts < configured_connection_attempts.value()) {
do {
zenoh::ZResult result;
this->session_->get_routers_z_id(&result);
if (result == Z_OK) {
break;
}
if ((connection_attempts % ticks_between_print) == 0) {
RMW_ZENOH_LOG_WARN_NAMED(
"rmw_zenoh_cpp",
"Unable to connect to a Zenoh router. "
"Have you started a router with `ros2 run rmw_zenoh_cpp rmw_zenohd`?");
}
zenoh::ZResult result;
this->session_->get_routers_z_id(&result);
if (result != Z_OK) {
++connection_attempts;
} else {
ret = RMW_RET_OK;
}
if (++connection_attempts >= configured_connection_attempts.value()) {
break;
}
std::this_thread::sleep_for(sleep_time);
}
} while(connection_attempts < configured_connection_attempts.value());
}

// Initialize the graph cache.
Expand Down

0 comments on commit b2b76e0

Please sign in to comment.