Skip to content

Commit

Permalink
Allow programmatic configuration of unicast relays.
Browse files Browse the repository at this point in the history
This change allows users to configure relays from code without having to
`setenv(GZ_RELAY)`.

Signed-off-by: Michael Beardsworth <beardsworth@intrinsic.ai>
  • Loading branch information
mbeards committed May 6, 2024
1 parent e53d1d6 commit cfd75cf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
51 changes: 31 additions & 20 deletions include/gz/transport/Discovery.hh
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,37 @@ namespace gz
}
}

/// \brief Register a new relay address.
/// \param[in] _ip New IP address.
public: void AddRelayAddress(const std::string &_ip)
{
// Sanity check: Make sure that this IP address is not already saved.
for (auto const &addr : this->relayAddrs)
{
if (addr.sin_addr.s_addr == inet_addr(_ip.c_str()))
return;
}

sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(_ip.c_str());
addr.sin_port = htons(static_cast<u_short>(this->port));

this->relayAddrs.push_back(addr);
}

public: std::vector<std::string> RelayAddresses() const
{
std::vector<std::string> result;

for (auto const &addr : this->relayAddrs) {
result.push_back(inet_ntoa(addr.sin_addr));
}

return result;
}

/// \brief Broadcast periodic heartbeats.
private: void UpdateHeartbeat()
{
Expand Down Expand Up @@ -1420,26 +1451,6 @@ namespace gz
return true;
}

/// \brief Register a new relay address.
/// \param[in] _ip New IP address.
private: void AddRelayAddress(const std::string &_ip)
{
// Sanity check: Make sure that this IP address is not already saved.
for (auto const &addr : this->relayAddrs)
{
if (addr.sin_addr.s_addr == inet_addr(_ip.c_str()))
return;
}

sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(_ip.c_str());
addr.sin_port = htons(static_cast<u_short>(this->port));

this->relayAddrs.push_back(addr);
}

/// \brief Default activity interval value (ms.).
/// \sa ActivityInterval.
/// \sa SetActivityInterval.
Expand Down
3 changes: 3 additions & 0 deletions include/gz/transport/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ namespace gz
public: std::optional<TopicStatistics> TopicStats(
const std::string &_topic) const;

public: void AddGlobalRelay(const std::string& relay_address);
public: std::vector<std::string> GlobalRelays() const;

/// \brief Get a pointer to the shared node (singleton shared by all the
/// nodes).
/// \return The pointer to the shared node.
Expand Down
3 changes: 3 additions & 0 deletions include/gz/transport/NodeShared.hh
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ namespace gz
public: std::optional<TopicStatistics> TopicStats(
const std::string &_topic) const;

public: void AddGlobalRelay(const std::string& relay_address);
public: std::vector<std::string> GlobalRelays() const;

/// \brief Constructor.
protected: NodeShared();

Expand Down
8 changes: 8 additions & 0 deletions src/Node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1139,3 +1139,11 @@ bool Node::RequestRaw(const std::string &_topic,
bool executed = this->Request(_topic, *req, _timeout, *res, _result);
return executed && res->SerializeToString(&_response);
}

void Node::AddGlobalRelay(const std::string& relay_address) {
Shared()->AddGlobalRelay(relay_address);
}

std::vector<std::string> Node::GlobalRelays() const {
return Shared()->GlobalRelays();
}
19 changes: 19 additions & 0 deletions src/NodeShared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1941,3 +1941,22 @@ int NodeSharedPrivate::NonNegativeEnvVar(const std::string &_envVar,
}
return numVal;
}

void NodeShared::AddGlobalRelay(const std::string& relay_address) {
dataPtr->msgDiscovery->AddRelayAddress(relay_address);
dataPtr->srvDiscovery->AddRelayAddress(relay_address);
}

std::vector<std::string> NodeShared::GlobalRelays() const {
// Merge relays from message and service discovery. They should be identical
// since they're typically build from the same sources.
//
// This is confusing - do we want to add different handling here?
auto msgRelays = dataPtr->msgDiscovery->RelayAddresses();
std::set<std::string> msgRelaySet(msgRelays.cbegin(), msgRelays.cend());
auto srvRelays = dataPtr->srvDiscovery->RelayAddresses();
std::set<std::string> srvRelaySet(srvRelays.cbegin(), srvRelays.cend());
srvRelaySet.merge(msgRelaySet);

return std::vector<std::string>(srvRelaySet.cbegin(), srvRelaySet.cend());
}

0 comments on commit cfd75cf

Please sign in to comment.