-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathzenoh_config.cpp
More file actions
236 lines (207 loc) · 8.32 KB
/
zenoh_config.cpp
File metadata and controls
236 lines (207 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2023 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "zenoh_config.hpp"
#include <rcutils/env.h>
#include <limits>
#include <sstream>
#include <string>
#include <zenoh.hxx>
#include <zenoh/api/config.hxx>
#include "logging_macros.hpp"
#include <ament_index_cpp/get_package_share_path.hpp>
#include <ament_index_cpp/get_package_prefix.hpp> /* for PackageNotFoundError */
#include <rmw/impl/cpp/macros.hpp>
///=============================================================================
namespace rmw_zenoh_cpp
{
namespace
{
/// 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"}}
};
static const char * router_check_attempts_envar = "ZENOH_ROUTER_CHECK_ATTEMPTS";
static const char * zenoh_shm_alloc_size_envar = "ZENOH_SHM_ALLOC_SIZE";
static const char * zenoh_shm_message_size_threshold_envar = "ZENOH_SHM_MESSAGE_SIZE_THRESHOLD";
/// Allow users to override the configuration using key-value pairs.
/// The supporting syntax is "key1=value2;key2=value2;...". For instance,
/// ZENOH_CONFIG_OVERRIDE='listen/endpoints=["tcp/127.0.0.1:7448"];scouting/multicast/enabled=true'
static const char * zenoh_config_override = "ZENOH_CONFIG_OVERRIDE";
std::optional<zenoh::Config> _get_z_config(
const char * envar_name,
const std::filesystem::path & default_uri)
{
std::filesystem::path configured_uri;
const char * envar_uri;
// Get the path to the zenoh configuration file from the environment variable.
if (NULL != rcutils_get_env(envar_name, &envar_uri)) {
// NULL is returned if everything is ok.
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read.", envar_name);
return std::nullopt;
}
std::filesystem::path envar_uri_p(envar_uri);
// If the environment variable is set, try to read the configuration from the file,
// if the environment variable is not set use internal configuration
configured_uri = envar_uri[0] != '\0' ? envar_uri_p : default_uri;
// Try to read the configuration
zenoh::ZResult result;
zenoh::Config config = zenoh::Config::from_file(configured_uri.string(), &result);
if (result != Z_OK) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Invalid configuration file %s", configured_uri.c_str());
return std::nullopt;
}
// Override the zenoh config by key/value pairs.
const char * key_val_pairs_str;
if (NULL != rcutils_get_env(zenoh_config_override, &key_val_pairs_str)) {
// NULL is returned if everything is ok.
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read. Ignoring override...", zenoh_config_override);
return config;
}
if (key_val_pairs_str[0] != '\0') {
std::stringstream ss(key_val_pairs_str);
std::string pair;
while (std::getline(ss, pair, ';')) {
std::stringstream pair_stream(pair);
std::string key, val;
if (std::getline(pair_stream, key, '=') && std::getline(pair_stream, val)) {
config.insert_json5(key, val, &result);
if (result != Z_OK) {
RMW_ZENOH_LOG_WARN_NAMED(
"rmw_zenoh_cpp",
"Ignore the invalid configuration key-value pair: (%s, %s)", key.c_str(), val.c_str());
}
}
}
}
RMW_ZENOH_LOG_DEBUG_NAMED(
"rmw_zenoh_cpp",
"configured using configuration file %s", configured_uri.c_str());
return config;
}
} // namespace
///=============================================================================
std::optional<zenoh::Config> get_z_config(const ConfigurableEntity & entity)
{
auto envar_map_it = envar_map.find(entity);
if (envar_map_it == envar_map.end()) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "get_z_config called with invalid ConfigurableEntity.");
return std::nullopt;
}
std::filesystem::path default_config_path;
try {
// Get the absolute path to the default configuration file.
std::filesystem::path path_to_config_folder =
ament_index_cpp::get_package_share_path("rmw_zenoh_cpp") / "config";
default_config_path = path_to_config_folder / envar_map_it->second.second;
} catch (const ament_index_cpp::PackageNotFoundError& e) {
RMW_ZENOH_LOG_WARN_NAMED(
"rmw_zenoh_cpp",
"Failed to find rmw_zenoh_cpp package in ament_index (%s). "
"Relying on 'ZENOH_*_CONFIG_URI' ENV vars.", e.what());
}
return _get_z_config(envar_map_it->second.first, default_config_path);
}
///=============================================================================
std::optional<uint64_t> zenoh_router_check_attempts()
{
const char * envar_value;
// 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.
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read. Report this bug.",
router_check_attempts_envar);
return default_value;
}
// If the environment variable contains a value, handle it accordingly.
if (envar_value[0] != '\0') {
const int64_t read_value = std::strtoll(envar_value, nullptr, 10);
if (read_value > 0) {
return read_value;
} else if (read_value < 0) {
// If less than 0, we skip the check.
return std::nullopt;
}
// If the value is 0, check indefinitely.
return std::numeric_limits<uint64_t>::max();
}
// If unset, use the default.
return default_value;
}
///=============================================================================
std::optional<size_t> zenoh_shm_alloc_size()
{
const char * envar_value;
if (NULL != rcutils_get_env(zenoh_shm_alloc_size_envar, &envar_value)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read. Report this bug.",
zenoh_shm_alloc_size_envar);
return std::nullopt;
}
// If the environment variable contains a value, handle it accordingly.
if (envar_value[0] != '\0') {
const auto read_value = std::strtoull(envar_value, nullptr, 10);
if (read_value > 0) {
if (read_value > std::numeric_limits<size_t>::max()) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s: value is too large!",
zenoh_shm_alloc_size_envar);
} else {
return read_value;
}
}
}
return std::nullopt;
}
///=============================================================================
std::optional<size_t> zenoh_shm_message_size_threshold()
{
const char * envar_value;
if (NULL != rcutils_get_env(zenoh_shm_message_size_threshold_envar, &envar_value)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read. Report this bug.",
zenoh_shm_message_size_threshold_envar);
return std::nullopt;
}
// If the environment variable contains a value, handle it accordingly.
if (envar_value[0] != '\0') {
const auto read_value = std::strtoull(envar_value, nullptr, 10);
if (read_value > 0) {
if (read_value > std::numeric_limits<size_t>::max()) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s: value is too large!",
zenoh_shm_message_size_threshold_envar);
} else if ((read_value & (read_value - 1)) != 0) { // power of 2 check
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s: value must be power of 2!",
zenoh_shm_message_size_threshold_envar);
} else {
return read_value;
}
}
}
return std::nullopt;
}
} // namespace rmw_zenoh_cpp