Skip to content

Commit

Permalink
Merge branch 'ros2:rolling' into rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
imstevenpmwork authored Nov 20, 2024
2 parents c92ca58 + 3d771c8 commit c8f6827
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
25 changes: 12 additions & 13 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,24 @@ class rmw_context_impl_s::Data final
z_close(z_move(session_));
});

// TODO(Yadunund) Move this check into a separate thread.
// Verify if the zenoh router is running if configured.
const std::optional<uint64_t> configured_connection_attempts =
rmw_zenoh_cpp::zenoh_router_check_attempts();
if (configured_connection_attempts.has_value()) {
ret = RMW_RET_ERROR;
uint64_t connection_attempts = 0;
// Retry until the connection is successful.
while (ret != RMW_RET_OK && connection_attempts < configured_connection_attempts.value()) {
if ((ret = rmw_zenoh_cpp::zenoh_router_check(z_loan(session_))) != RMW_RET_OK) {
++connection_attempts;
constexpr std::chrono::milliseconds sleep_time(1000);
constexpr int64_t ticks_between_print(std::chrono::milliseconds(1000) / sleep_time);
while ((ret = rmw_zenoh_cpp::zenoh_router_check(z_loan(session_))) != RMW_RET_OK) {
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`?");
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (ret != RMW_RET_OK) {
throw std::runtime_error(
"Unable to connect to a Zenoh router after " +
std::to_string(configured_connection_attempts.value()) +
" retries.");
if (++connection_attempts >= configured_connection_attempts.value()) {
break;
}
std::this_thread::sleep_for(sleep_time);
}
}

Expand Down
8 changes: 4 additions & 4 deletions rmw_zenoh_cpp/src/detail/zenoh_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ rmw_ret_t get_z_config(const ConfigurableEntity & entity, z_owned_config_t * con
std::optional<uint64_t> zenoh_router_check_attempts()
{
const char * envar_value;
// The default value is to check indefinitely.
uint64_t default_value = std::numeric_limits<uint64_t>::max();
// The default is to check only once.
uint64_t default_value = 1;

if (NULL != rcutils_get_env(router_check_attempts_envar, &envar_value)) {
// NULL is returned if everything is ok.
Expand All @@ -120,10 +120,10 @@ std::optional<uint64_t> zenoh_router_check_attempts()
return std::nullopt;
}
// If the value is 0, check indefinitely.
return default_value;
return std::numeric_limits<uint64_t>::max();
}

// If unset, check indefinitely.
// If unset, use the default.
return default_value;
}
} // namespace rmw_zenoh_cpp
11 changes: 3 additions & 8 deletions rmw_zenoh_cpp/src/detail/zenoh_router_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,18 @@ rmw_ret_t zenoh_router_check(z_session_t session)
(*(static_cast<int *>(ctx)))++;
};

rmw_ret_t ret = RMW_RET_OK;
z_owned_closure_zid_t router_callback = z_closure(callback, nullptr /* drop */, &context);
if (z_info_routers_zid(session, z_move(router_callback))) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Failed to evaluate if Zenoh routers are connected to the session.");
ret = RMW_RET_ERROR;
return RMW_RET_ERROR;
} else {
if (context == 0) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to connect to a Zenoh router. "
"Have you started a router with `ros2 run rmw_zenoh_cpp rmw_zenohd`?");
ret = RMW_RET_ERROR;
return RMW_RET_ERROR;
}
}

return ret;
return RMW_RET_OK;
}
} // namespace rmw_zenoh_cpp

0 comments on commit c8f6827

Please sign in to comment.