Skip to content

Commit c046a5e

Browse files
committed
Rewrite connecting to the router to only print when it fails.
This simplifies the code, and makes it less noisy. Signed-off-by: Chris Lalancette <[email protected]>
1 parent afe1ff9 commit c046a5e

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ class rmw_context_impl_s::Data final
9696
throw std::runtime_error("Error setting up zenoh session. ");
9797
}
9898

99-
rmw_ret_t ret = RMW_RET_ERROR;
100-
10199
// Verify if the zenoh router is running if configured.
102100
const std::optional<uint64_t> configured_connection_attempts =
103101
rmw_zenoh_cpp::zenoh_router_check_attempts();
@@ -106,25 +104,23 @@ class rmw_context_impl_s::Data final
106104
// Retry until the connection is successful.
107105
constexpr std::chrono::milliseconds sleep_time(1000);
108106
constexpr int64_t ticks_between_print(std::chrono::milliseconds(1000) / sleep_time);
109-
while (ret != RMW_RET_OK && connection_attempts < configured_connection_attempts.value()) {
107+
do {
108+
zenoh::ZResult result;
109+
this->session_->get_routers_z_id(&result);
110+
if (result == Z_OK) {
111+
break;
112+
}
110113
if ((connection_attempts % ticks_between_print) == 0) {
111114
RMW_ZENOH_LOG_WARN_NAMED(
112115
"rmw_zenoh_cpp",
113116
"Unable to connect to a Zenoh router. "
114117
"Have you started a router with `ros2 run rmw_zenoh_cpp rmw_zenohd`?");
115118
}
116-
zenoh::ZResult result;
117-
this->session_->get_routers_z_id(&result);
118-
if (result != Z_OK) {
119-
++connection_attempts;
120-
} else {
121-
ret = RMW_RET_OK;
122-
}
123119
if (++connection_attempts >= configured_connection_attempts.value()) {
124120
break;
125121
}
126122
std::this_thread::sleep_for(sleep_time);
127-
}
123+
} while(connection_attempts < configured_connection_attempts.value());
128124
}
129125

130126
// Initialize the graph cache.

0 commit comments

Comments
 (0)