Class MdnsDiscovery

Nested Relationships

Nested Types

Class Documentation

class MdnsDiscovery

mDNS-based discovery for auto-discovering peer gateways

Uses the mjansson/mdns header-only C library to announce this gateway’s presence on the local network and browse for other gateways. Peers are discovered via the configured service type (default: _medkit._tcp.local).

Thread safety: start()/stop() are not thread-safe with respect to each other. The running_ flag is atomic and used for clean shutdown of background threads.

Public Types

using ErrorCallback = std::function<void(const std::string &message)>

Configuration for mDNS discovery.

Callback invoked when an mDNS operation encounters an error (e.g., socket failure)

using LogCallback = std::function<void(const std::string &message)>

Callback for diagnostic logging (wired to RCLCPP_DEBUG in gateway)

using PeerFoundCallback = std::function<void(const std::string &url, const std::string &name)>

Callback invoked when a peer gateway is discovered.

using PeerRemovedCallback = std::function<void(const std::string &name)>

Callback invoked when a peer gateway is removed (goodbye received)

Public Functions

explicit MdnsDiscovery(const Config &config)

Construct an MdnsDiscovery instance.

Parameters:

config – Configuration controlling announce/discover behavior

~MdnsDiscovery()

Destructor calls stop() to ensure clean thread shutdown.

MdnsDiscovery(const MdnsDiscovery&) = delete
MdnsDiscovery &operator=(const MdnsDiscovery&) = delete
MdnsDiscovery(MdnsDiscovery&&) = delete
MdnsDiscovery &operator=(MdnsDiscovery&&) = delete
void start(PeerFoundCallback on_found, PeerRemovedCallback on_removed)

Start mDNS announce and/or browse threads.

Starts background threads based on config flags:

  • If announce is true, starts a thread that responds to mDNS queries

  • If discover is true, starts a thread that sends mDNS queries and invokes callbacks when peers are found or removed

Parameters:
  • on_found – Callback for when a peer is discovered

  • on_removed – Callback for when a peer sends a goodbye

void stop()

Stop all mDNS threads.

Sets running_ to false and joins all background threads. Safe to call multiple times (idempotent).

bool is_announcing() const

Check if the announce thread is running.

bool is_discovering() const

Check if the discover/browse thread is running.

inline const std::string &instance_name() const

Get the resolved mDNS instance name (hostname if not explicitly set)

struct Config

Public Members

bool announce = {false}

Broadcast presence via mDNS.

bool discover = {false}

Browse for peers via mDNS.

std::string service = {"_medkit._tcp.local"}

Service type to announce/browse.

int port = {8080}

Port this gateway listens on.

std::string name

Instance name for announcement.

std::string peer_scheme = {"http"}

URL scheme for discovered peers (“http” or “https”)

ErrorCallback on_error

Optional error reporting callback.

LogCallback on_log

Optional diagnostic log callback.