Skip to content

Commit

Permalink
Bundle envar and default config into the same map
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Feb 28, 2024
1 parent 4f61113 commit eb68c0d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
> Note: To connect multiple hosts, include the endpoints of all routers in the network.
17 changes: 4 additions & 13 deletions rmw_zenoh_cpp/src/detail/zenoh_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
///==============================================================================
namespace
{
/// The name of the default configuration file for the default zenoh session configuration.
static const std::unordered_map<ConfigurableEntity, const char *> 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,
Expand Down Expand Up @@ -72,19 +66,16 @@ 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;
}
// 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);
}
14 changes: 9 additions & 5 deletions rmw_zenoh_cpp/src/detail/zenoh_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <zenoh.h>

#include <unordered_map>
#include <utility>

#include "rmw/ret_types.h"

Expand All @@ -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<ConfigurableEntity, const char *> 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<ConfigurableEntity,
std::pair<const char *, const char *>> 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.
Expand Down

0 comments on commit eb68c0d

Please sign in to comment.