Skip to content

Commit

Permalink
iox-eclipse-iceoryx#65 Log warning and use defaults when failing to p…
Browse files Browse the repository at this point in the history
…arse gateway config.

Signed-off-by: Ithier Jeff (CC-AD/EYF1) <[email protected]>
  • Loading branch information
orecham committed Jul 6, 2020
1 parent 514dd67 commit f6cb3cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace iox
{
namespace dds
{

enum TomlGatewayConfigParseError
{
FILE_NOT_FOUND,
Expand All @@ -34,10 +35,15 @@ enum TomlGatewayConfigParseError
INVALID_SERVICE_DESCRIPTION
};

static constexpr const char DEFAULT_CONFIG_FILE_PATH[] = "/etc/iceoryx/gateway_config.toml";
constexpr int32_t MAX_ENUM_STRING_SIZE = 64;
constexpr char TomlGatewayConfigParseErrorString[][MAX_ENUM_STRING_SIZE] = {"FILE_NOT_FOUND",
"INCOMPLETE_CONFIGURATION",
"INCOMPLETE_SERVICE_DESCRIPTION",
"INVALID_SERVICE_DESCRIPTION"};

static constexpr const char REGEX_VALID_CHARACTERS[] = "^[a-zA-Z_][a-zA-Z0-9_]*$";

static constexpr const char DEFAULT_CONFIG_FILE_PATH[] = "/etc/iceoryx/gateway_config.toml";
static constexpr const char GATEWAY_CONFIG_SERVICE_TABLE_NAME[] = "services";
static constexpr const char GATEWAY_CONFIG_SERVICE_NAME[] = "service";
static constexpr const char GATEWAY_CONFIG_SERVICE_INSTANCE_NAME[] = "instance";
Expand Down
10 changes: 10 additions & 0 deletions iceoryx_dds/source/dds2iceoryx_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "iceoryx_dds/dds/data_reader.hpp"

#include "iceoryx_dds/internal/log/logging.hpp"
#include "iceoryx_dds/gateway/dds_to_iox.hpp"
#include "iceoryx_dds/gateway/toml_gateway_config_parser.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
Expand Down Expand Up @@ -65,6 +66,15 @@ int main(int argc, char* argv[])
{
gw.loadConfiguration(result.get_value());
}
else
{
iox::dds::LogWarn() << "[Main] Failed to parse gateway config with error: " << iox::dds::TomlGatewayConfigParseErrorString[result.get_error()];
iox::dds::LogWarn() << "[Main] Using default configuration.";
iox::dds::GatewayConfig defaultConfig;
defaultConfig.setDefaults();
gw.loadConfiguration(defaultConfig);
}

gw.runMultithreaded();

// Run until SIGINT or SIGTERM
Expand Down
9 changes: 9 additions & 0 deletions iceoryx_dds/source/iceoryx2dds_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "iceoryx_dds/gateway/gateway_config.hpp"
#include "iceoryx_dds/gateway/iox_to_dds.hpp"
#include "iceoryx_dds/gateway/toml_gateway_config_parser.hpp"
#include "iceoryx_dds/internal/log/logging.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
Expand Down Expand Up @@ -57,6 +58,14 @@ int main(int argc, char* argv[])
{
gw.loadConfiguration(result.get_value());
}
else
{
iox::dds::LogWarn() << "[Main] Failed to parse gateway config with error: " << iox::dds::TomlGatewayConfigParseErrorString[result.get_error()];
iox::dds::LogWarn() << "[Main] Using default configuration.";
iox::dds::GatewayConfig defaultConfig;
defaultConfig.setDefaults();
gw.loadConfiguration(defaultConfig);
}

gw.runMultithreaded();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ iox::dds::TomlGatewayConfigParser::parse(ConfigFilePathString_t path)
return iox::cxx::success<iox::dds::GatewayConfig>(config);
}

LogInfo() << "[TomlGatewayConfigParser] Using gateway config at: " << path;
LogInfo() << "[TomlGatewayConfigParser] Attempting to load gateway config at: " << path;

// Load the file
auto parsedToml = cpptoml::parse_file(path.c_str());
auto result = validate(*parsedToml);
if (result.has_error())
{
LogWarn() << "[TomlGatewayConfigParser] Unable to parse configuration file";
return iox::cxx::error<TomlGatewayConfigParseError>(result.get_error());
}

Expand Down Expand Up @@ -75,7 +74,6 @@ iox::dds::TomlGatewayConfigParser::validate(const cpptoml::table& parsedToml) no
auto serviceArray = parsedToml.get_table_array(GATEWAY_CONFIG_SERVICE_TABLE_NAME);
if (!serviceArray)
{
LogError() << "[TomlGatewayConfigParser] Incomplete configuration provided.";
return iox::cxx::error<TomlGatewayConfigParseError>(TomlGatewayConfigParseError::INCOMPLETE_CONFIGURATION);
}

Expand All @@ -92,22 +90,19 @@ iox::dds::TomlGatewayConfigParser::validate(const cpptoml::table& parsedToml) no
// check for incomplete service descriptions
if (!serviceName || !instance || !event)
{
LogError() << "[TomlGatewayConfigParser] Incomplete service description at entry: " << count;
return iox::cxx::error<TomlGatewayConfigParseError>(
TomlGatewayConfigParseError::INCOMPLETE_SERVICE_DESCRIPTION);
}
// check for incomplete service descriptions
if (!sampleSize)
{
LogError() << "[TomlGatewayConfigParser] Incomplete data description at entry: " << count;
return iox::cxx::error<TomlGatewayConfigParseError>(
TomlGatewayConfigParseError::INCOMPLETE_SERVICE_DESCRIPTION);
}

// check for invalid characters in strings
if (hasInvalidCharacter(*serviceName) || hasInvalidCharacter(*instance) || hasInvalidCharacter(*event))
{
LogError() << "[TomlGatewayConfigParser] Invalid service description at entry: " << count;
return iox::cxx::error<TomlGatewayConfigParseError>(
TomlGatewayConfigParseError::INVALID_SERVICE_DESCRIPTION);
}
Expand All @@ -121,9 +116,5 @@ bool iox::dds::TomlGatewayConfigParser::hasInvalidCharacter(std::string s) noexc
// See: https://design.ros2.org/articles/topic_and_service_names.html
const std::regex regex(REGEX_VALID_CHARACTERS);
auto isInvalid = !std::regex_match(s, regex);
if (isInvalid)
{
LogError() << "[TomlGatewayConfigParser] Invalid character in name: " + s;
}
return isInvalid;
}

0 comments on commit f6cb3cb

Please sign in to comment.