5#ifndef CARLA_ROS2_MIDDLEWARE_TESTING
6#ifdef CARLA_ROS2_MIDDLEWARE_ZENOH
19std::string compute_zid_string(
const z_loaned_session_t* session) {
20 z_id_t zid = z_info_zid(session);
22 z_id_to_string(&zid, &s);
23 const z_loaned_string_t* ls = z_loan(s);
24 std::string result(z_string_data(ls), z_string_len(ls));
35std::string default_config_path() {
37 if (dladdr(
reinterpret_cast<const void*
>(&zenoh_get_shared_session), &info) &&
38 info.dli_fname !=
nullptr) {
39 const std::string so{info.dli_fname};
40 const size_t slash = so.find_last_of(
'/');
41 if (slash != std::string::npos) {
42 return so.substr(0, slash) +
"/zenoh_session_config.json5";
46 return "zenoh_session_config.json5";
51void apply_config_overrides(z_loaned_config_t* cfg) {
52 const char* env = std::getenv(
"ZENOH_CONFIG_OVERRIDE");
53 if (env ==
nullptr || env[0] ==
'\0')
return;
57 while (pos < s.size()) {
58 size_t semi = s.find(
';', pos);
59 if (semi == std::string::npos) semi = s.size();
60 std::string pair = s.substr(pos, semi - pos);
61 size_t eq = pair.find(
'=');
62 if (eq != std::string::npos) {
63 std::string key = pair.substr(0, eq);
64 std::string val = pair.substr(eq + 1);
65 if (zc_config_insert_json5(cfg, key.c_str(), val.c_str()) != Z_OK) {
66 log_warning(
"ZENOH_CONFIG_OVERRIDE: failed to apply '", pair,
"'");
75const z_loaned_session_t* zenoh_get_shared_session() {
76 static z_owned_session_t session = [] {
80 const char* uri = std::getenv(
"ZENOH_SESSION_CONFIG_URI");
81 const std::string path = (uri !=
nullptr && uri[0] !=
'\0')
82 ? std::string(uri) : default_config_path();
83 if (zc_config_from_file(&cfg, path.c_str()) != Z_OK) {
84 log_error(
"ZenohSharedSession: failed to read config at '", path,
85 "'; falling back to zenoh defaults");
86 z_config_default(&cfg);
88 apply_config_overrides(z_loan_mut(cfg));
91 if (z_open(&s, z_move(cfg),
nullptr) != Z_OK) {
92 log_error(
"ZenohSharedSession: z_open failed");
100 static z_owned_liveliness_token_t node_token = [] {
101 const z_loaned_session_t* loaned = z_session_loan(&session);
102 const std::string ke_str = zenoh_make_node_liveliness_keyexpr(
103 zenoh_ros_domain_id(), compute_zid_string(loaned));
105 z_view_keyexpr_from_str(&ke, ke_str.c_str());
106 z_owned_liveliness_token_t tok;
107 if (z_liveliness_declare_token(loaned, &tok, z_loan(ke),
nullptr) != Z_OK) {
108 log_error(
"ZenohSharedSession: z_liveliness_declare_token failed");
114 return z_session_loan(&session);
117const std::string& zenoh_session_zid() {
118 static const std::string cached =
119 compute_zid_string(zenoh_get_shared_session());
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
static void log_warning(Args &&... args)