From eb68c0d7a113a957e6a296359acef89f9ddcad53 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Wed, 28 Feb 2024 11:49:17 +0800 Subject: [PATCH] Bundle envar and default config into the same map Signed-off-by: Yadunund --- README.md | 2 +- rmw_zenoh_cpp/src/detail/zenoh_config.cpp | 17 ++++------------- rmw_zenoh_cpp/src/detail/zenoh_config.hpp | 14 +++++++++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7650fcd2..448ad928 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,4 @@ In this example, the `router` will connect to the `router` running on a second h } ``` -> Note: To connect multiple hosts, include the endpoints of all routers in the network. \ No newline at end of file +> Note: To connect multiple hosts, include the endpoints of all routers in the network. diff --git a/rmw_zenoh_cpp/src/detail/zenoh_config.cpp b/rmw_zenoh_cpp/src/detail/zenoh_config.cpp index 5a477b37..91be773e 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_config.cpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_config.cpp @@ -25,12 +25,6 @@ ///============================================================================== namespace { -/// The name of the default configuration file for the default zenoh session configuration. -static const std::unordered_map default_config_filenames = { - {ConfigurableEntity::Session, "DEFAULT_RMW_ZENOH_SESSION_CONFIG.json5"}, - {ConfigurableEntity::Router, "DEFAULT_RMW_ZENOH_ROUTER_CONFIG.json5"} -}; - rmw_ret_t _get_z_config( const char * envar_name, const char * default_uri, @@ -72,11 +66,8 @@ rmw_ret_t _get_z_config( ///============================================================================== rmw_ret_t get_z_config(const ConfigurableEntity & entity, z_owned_config_t * config) { - auto entity_envar_it = envar_map.find(entity); - auto default_filename_it = default_config_filenames.find(entity); - if (entity_envar_it == envar_map.end() || - default_filename_it == default_config_filenames.end() ) - { + auto envar_map_it = envar_map.find(entity); + if (envar_map_it == envar_map.end()) { RCUTILS_LOG_ERROR_NAMED( "rmw_zenoh_cpp", "get_z_config called with invalid ConfigurableEntity."); return RMW_RET_ERROR; @@ -84,7 +75,7 @@ rmw_ret_t get_z_config(const ConfigurableEntity & entity, z_owned_config_t * con // Get the absolute path to the default configuration file. static const std::string path_to_config_folder = ament_index_cpp::get_package_share_directory("rmw_zenoh_cpp") + "/config/"; - const std::string default_config_path = path_to_config_folder + default_filename_it->second; + const std::string default_config_path = path_to_config_folder + envar_map_it->second.second; - return _get_z_config(entity_envar_it->second, default_config_path.c_str(), config); + return _get_z_config(envar_map_it->second.first, default_config_path.c_str(), config); } diff --git a/rmw_zenoh_cpp/src/detail/zenoh_config.hpp b/rmw_zenoh_cpp/src/detail/zenoh_config.hpp index 215ee0dd..a7f1a9e8 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_config.hpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_config.hpp @@ -18,6 +18,7 @@ #include #include +#include #include "rmw/ret_types.h" @@ -30,11 +31,14 @@ enum class ConfigurableEntity : uint8_t }; ///============================================================================== -/// Environment variable name that stores the absolute path to the Zenoh config -/// for respective entities. -static const std::unordered_map envar_map = { - {ConfigurableEntity::Session, "ZENOH_SESSION_CONFIG_URI"}, - {ConfigurableEntity::Router, "ZENOH_ROUTER_CONFIG_URI"} +/// Map the configurable entity to a pair of environment variable name that +/// stores the absolute path to the Zenoh config and the default config filename. +/// Note: The default config file should be located within rmw_zenoh_cpp/config/. +static const std::unordered_map> envar_map = { + {ConfigurableEntity::Session, + {"ZENOH_SESSION_CONFIG_URI", "DEFAULT_RMW_ZENOH_SESSION_CONFIG.json5"}}, + {ConfigurableEntity::Router, {"ZENOH_ROUTER_CONFIG_URI", "DEFAULT_RMW_ZENOH_ROUTER_CONFIG.json5"}} }; /// Get the zenoh configuration for a configurable entity.