Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ configure_file(
# Install the yaml configuration files in an unversioned location.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${GZ_DESIGNATION}${PROJECT_VERSION_MAJOR}.yaml
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/gz/)

# Install default Zenoh config files.
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/gz-${GZ_DESIGNATION}
FILES_MATCHING PATTERN "*.json5")
803 changes: 803 additions & 0 deletions conf/gz_zenoh_router_config.json5

Large diffs are not rendered by default.

810 changes: 810 additions & 0 deletions conf/gz_zenoh_session_config.json5

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions include/gz/transport/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@

namespace gz::transport
{
#ifdef _WIN32
# define GZ_HOMEDIR "USERPROFILE"
#else
# define GZ_HOMEDIR "HOME"
#endif

// Inline bracket to help doxygen filtering.
inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
//
Expand Down
3 changes: 3 additions & 0 deletions include/gz/transport/config.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

#define GZ_TRANSPORT_VERSION_HEADER "Gazebo Transport, version ${PROJECT_VERSION_FULL}\nCopyright (C) 2017 Open Source Robotics Foundation.\nReleased under the Apache 2.0 License.\n\n"

#define GZ_TRANSPORT_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}"
#define GZ_TRANSPORT_INSTALL_SHARE_PATH "${CMAKE_INSTALL_DATAROOTDIR}"

#cmakedefine HAVE_IFADDRS 1
#cmakedefine UBUNTU_FOCAL 1
#cmakedefine HAVE_ZENOH 1
Expand Down
111 changes: 104 additions & 7 deletions src/NodeShared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <chrono>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <map>
#include <mutex>
Expand All @@ -35,17 +36,20 @@
#include <zmq.hpp>

#include "gz/transport/AdvertiseOptions.hh"
#include "gz/transport/config.hh"
#include "gz/transport/Helpers.hh"
#include "gz/transport/NodeShared.hh"
#include "gz/transport/RepHandler.hh"
#include "gz/transport/ReqHandler.hh"
#include "gz/transport/SubscriptionHandler.hh"
#include "gz/transport/TransportTypes.hh"
#include "gz/transport/Uuid.hh"
#include <gz/utils/Environment.hh>

#include "Discovery.hh"
#include "NodeSharedPrivate.hh"

namespace fs = std::filesystem;
using namespace std::chrono_literals;

const char kGzAuthDomain[] = "gz-auth";
Expand Down Expand Up @@ -187,13 +191,6 @@ NodeShared *NodeShared::Instance()
NodeShared::NodeShared()
: dataPtr(new NodeSharedPrivate)
{
// If GZ_VERBOSE=1 enable the verbose mode.
std::string gzVerbose;
if (env("GZ_VERBOSE", gzVerbose) && !gzVerbose.empty())
{
this->dataPtr->verbose = (gzVerbose == "1");
}

// Set the multicast IP used for discovery.
std::string envDiscoveryIp;
if (env("GZ_DISCOVERY_MULTICAST_IP", envDiscoveryIp) &&
Expand Down Expand Up @@ -1823,6 +1820,106 @@ int NodeSharedPrivate::NonNegativeEnvVar(const std::string &_envVar,
return numVal;
}

#ifdef HAVE_ZENOH
/////////////////////////////////////////////////
zenoh::Config NodeSharedPrivate::ZenohConfig()
{
// Check if the ZENOH_CONFIG env variable exists.
try
{
zenoh::ZResult result;
zenoh::Config config = zenoh::Config::from_env(&result);
if (result == Z_OK)
{
if (this->verbose)
{
std::cout << "Zenoh config file loaded from ZENOH_CONFIG env"
<< std::endl;
}
return config;
}
} catch (zenoh::ZException &_e)
{
std::cerr << "Error parsing Zenoh config file: " << _e.what() << "\n";
}

// Check if the default user config file exists.
std::string userConfigDir;
if (gz::utils::env(GZ_HOMEDIR, userConfigDir) && !userConfigDir.empty())
{
fs::path userConfigDirPath = fs::path(userConfigDir) / ".gz" / "transport";
fs::path userConfigFilePath =
userConfigDirPath / fs::path("gz_zenoh_session_config.json5");
if (!fs::exists(userConfigFilePath))
{
// Let's create default user config files.
fs::path installedConfigDir =
fs::path(GZ_TRANSPORT_INSTALL_PREFIX) /
fs::path(GZ_TRANSPORT_INSTALL_SHARE_PATH) /
"gz-transport" / "conf";

// Create directories if needed.
std::error_code ec;
fs::create_directories(userConfigDirPath, ec);

if (fs::exists(userConfigDirPath) && fs::is_directory(userConfigDirPath))
{
for (auto name : {"gz_zenoh_router_config.json5",
"gz_zenoh_session_config.json5"})
{
const auto copyOptions = fs::copy_options::skip_existing;
fs::path installedConfigFilePath = installedConfigDir / name;
fs::path newUserConfigFilePath = userConfigDirPath / name;

auto ret = fs::copy_file(installedConfigFilePath,
newUserConfigFilePath, copyOptions, ec);
if (ret)
{
std::cout << "New user config file created ["
<< newUserConfigFilePath << "]" << std::endl;
}
else
{
std::cerr << "Unable to create user config file ["
<< newUserConfigFilePath << "]. Reason: "
<< ec.message() << std::endl;
}
}
}
}

if (fs::exists(userConfigFilePath) &&
fs::is_regular_file(userConfigFilePath))
{
try
{
zenoh::ZResult result;
zenoh::Config config = zenoh::Config::from_file(
userConfigFilePath.c_str(), &result);
if (result == Z_OK)
{
if (this->verbose)
{
std::cout << "Zenoh config file loaded from [" << userConfigFilePath
<< "]" << std::endl;
}
return config;
}
} catch (zenoh::ZException &_e)
{
std::cerr << "Error parsing Zenoh config file:" << _e.what() << "\n";
}
}
}

// Configuration file not found.
if (this->verbose)
std::cout << "Zenoh default config loaded" << std::endl;

return zenoh::Config::create_default();
}
#endif

/////////////////////////////////////////////////
void NodeShared::AddGlobalRelay(const std::string& _relayAddress)
{
Expand Down
21 changes: 20 additions & 1 deletion src/NodeSharedPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <zmq.hpp>
Expand Down Expand Up @@ -66,6 +67,13 @@ namespace gz::transport
responseReceiver(new zmq::socket_t(*context, ZMQ_ROUTER)),
replier(new zmq::socket_t(*context, ZMQ_ROUTER))
{
// If GZ_VERBOSE=1 enable the verbose mode.
std::string gzVerbose;
if (env("GZ_VERBOSE", gzVerbose) && !gzVerbose.empty())
{
this->verbose = (gzVerbose == "1");
}

// Set the Gz Transport implementation (ZeroMQ, Zenoh, ...).
std::string gzImpl;
if (env("GZ_TRANSPORT_IMPLEMENTATION", gzImpl) && !gzImpl.empty())
Expand All @@ -83,8 +91,9 @@ namespace gz::transport
#ifdef HAVE_ZENOH
if (this->gzImplementation == "zenoh")
{
auto config = ZenohConfig();
this->session = std::make_shared<zenoh::Session>(
zenoh::Session::open(zenoh::Config::create_default()));
zenoh::Session::open(std::move(config)));
}
#endif
}
Expand All @@ -110,6 +119,16 @@ namespace gz::transport
int _defaultValue) const;

#ifdef HAVE_ZENOH
/// \brief Get the path to the Zenoh config file.
/// We check a few different options from higher to lower priority:
/// 1. If the environment variable GZ_ZENOH_CONFIG_PATH is set.
/// 2. If the default configuration file exists at:
/// $HOME / .gz / transport / gz_zenoh_session/json5
/// If none of the previous options succeed, no configuration file is used.
/// \return The path to the Zenoh configuration file or empty string if
/// no config file was found.
public: zenoh::Config ZenohConfig();

/// \Pointer to the Zenoh session.
public: std::shared_ptr<zenoh::Session> session;
#endif
Expand Down
Loading