View on GitHub

Asio Service Discovery

Components for service discovery via udp multicasting. It's using boost::asio for async networking. It's non-blocking and non-locking.

Download this project as a .zip file Download this project as a tar.gz file
betabugs::networking::service_discoverer Class Reference

#include <service_discoverer.hpp>

Classes

struct  service
 

Public Types

typedef std::set< serviceservices
 a set of discovered services
 
typedef std::function< void(const
services &services)> 
on_services_changed_t
 this callback gets called, when ever the set of available services changes
 

Public Member Functions

 service_discoverer (boost::asio::io_service &io_service, const std::string &listen_for_service, const on_services_changed_t on_services_changed, const std::chrono::steady_clock::duration max_idle=std::chrono::seconds(30), const size_t max_services=10, const unsigned short multicast_port=30001, const boost::asio::ip::address &listen_address=boost::asio::ip::address::from_string("0.0.0.0"), const boost::asio::ip::address &multicast_address=boost::asio::ip::address::from_string("239.255.0.1"))
 

Detailed Description

Class to discover services announced by the service_announcer use service_discoverer discoverer(io_service, name_of_my_service, on_service_discovered); on_service_discovered is a std::function, that gets a services set passed as its first and only argument.

example:

boost::asio::io_service io_service;
service_discoverer discoverer(
io_service,
"my_awesome_service",
{
for(auto& service : services)
{
std::cout << "discovered: " service << std::endl;
}
}
);
io_service.run();

Note: In case of error, this class just prints to std::cerr

Constructor & Destructor Documentation

betabugs::networking::service_discoverer::service_discoverer ( boost::asio::io_service &  io_service,
const std::string &  listen_for_service,
const on_services_changed_t  on_services_changed,
const std::chrono::steady_clock::duration  max_idle = std::chrono::seconds(30),
const size_t  max_services = 10,
const unsigned short  multicast_port = 30001,
const boost::asio::ip::address &  listen_address = boost::asio::ip::address::from_string("0.0.0.0"),
const boost::asio::ip::address &  multicast_address = boost::asio::ip::address::from_string("239.255.0.1") 
)
inline

Constructs a service_discoverer.

listen for udp multicast packets announcing listen_for_service on listen_address:multicast_port.

Call on_services_changed each time a service has been discovered or a service has been idle for too long. To protect against malicious announcers, there is a limit (max_services) on how many services will end up in the set of discovered services.

Parameters
io_serviceio_service to use
listen_for_servicethe service to watch out for
on_services_changedgets called when ever the set of discovered services changes
max_idleservices not seen for this amount of time will be removed from the set
max_servicesmaximum number of services to hold
multicast_portthe udp multicast port to listen on
listen_addressaddress to listen on
multicast_addressmust match the one used in service_announcer

The documentation for this class was generated from the following file: