Skip to content

Commit

Permalink
Merge branch 'rolling' into wip/merge-rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Nov 19, 2024
2 parents b6df66c + 3d771c8 commit 59203df
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 @@ -105,25 +105,24 @@ class rmw_context_impl_s::Data final
z_close(z_loan_mut(session_), NULL);
});

// 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 @@ -97,8 +97,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 @@ -117,10 +117,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 @@ -40,24 +40,19 @@ rmw_ret_t zenoh_router_check(const z_loaned_session_t * session)
(*(static_cast<int *>(ctx)))++;
};

rmw_ret_t ret = RMW_RET_OK;
z_owned_closure_zid_t router_callback;
z_closure(&router_callback, callback, NULL, &context);
if (z_info_routers_zid(session, z_move(router_callback)) != Z_OK) {
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 59203df

Please sign in to comment.