service_discovery
Public Member Functions | Static Public Member Functions | List of all members
servicediscovery::avahi::ServiceDiscovery Class Reference

A wrapper class for the avahi service discovery. More...

#include <ServiceDiscovery.hpp>

Inheritance diagram for servicediscovery::avahi::ServiceDiscovery:
servicediscovery::avahi::ClientObserver

Public Member Functions

 ServiceDiscovery ()
 
virtual ~ServiceDiscovery ()
 
void start (const ServiceConfiguration &conf)
 
void listenOn (const std::vector< std::string > &types)
 
void update (const ServiceDescription &desc)
 
virtual void update (const ClientObserver::Event &event)
 
void stop ()
 
std::vector< ServiceDescriptionfindServices (const SearchPattern &pattern=SearchPattern()) const
 
std::vector< ServiceDescriptionfindServices (const ServicePattern &pattern, const std::string &name_space="*") const
 
std::vector< std::string > getServiceNames ()
 
bool isRunning () const
 
ServiceConfiguration getConfiguration () const
 
void addedComponentConnect (const sigc::slot< void, ServiceEvent > &slot)
 
void removedComponentConnect (const sigc::slot< void, ServiceEvent > &slot)
 
- Public Member Functions inherited from servicediscovery::avahi::ClientObserver
virtual ~ClientObserver ()
 

Static Public Member Functions

static bool update (const std::string &servicename, const ServiceDescription &description)
 
static ServiceDescription getServiceDescription (const std::string &servicename)
 
static std::vector< ServiceDescriptiongetUpdateableServices ()
 
static std::map< std::string, ServiceDescriptiongetVisibleServices (const SearchPattern &pattern=SearchPattern(""))
 
static void registerCallbacks (const std::string &serviceName, const sigc::slot< void, ServiceEvent > &serviceAdded, const sigc::slot< void, ServiceEvent > &serviceRemoved)
 

Additional Inherited Members

- Public Types inherited from servicediscovery::avahi::ClientObserver
enum  EventType { UNKNOWN, DISCONNECTED, RECOVERED }
 

Detailed Description

A wrapper class for the avahi service discovery.

* namespace sd = servicediscovery;
* void removeCallback(dc::ServiceEvent e)
* {
* 
*    // what to perform when component has been added
*    sd::ServiceConfiguration configuration = e.getServiceConfiguration();
*
*    std::string serviceName = configuration.getName();
*    std::string labelData = configuration.getDescription("my-label");
*    ...
* }
* void addCallback(sd::ServiceEvent e)
* {
*   // what to perform when a component has been added
* }
* std::string someServiceData;
* ServiceDiscovery::ServiceDiscovery service;
* std::string serviceName = "ModuleA";
* std::string serviceType = "_module._tcp"
* sd::ServiceConfiguration conf(someName, serviceType);
* conf.setDescription("my-label","data-associated-with-label");
* service.addedComponentConnect(sigc::mem_fun(*this, &addCallback));
* service.removedComponentConnect(sigc::mem_fun(*this, &removeCallback));
* service.start(conf);
*
* std::vector<sd::ServiceDescription> services = 
*     service.findServices(SearchPattern("my-component-name"));
* 
* 

The service discovery takes advantage of the features of avahi and allows you to publish and browse for services based on existing or newly created types.

Depending on the need, the service discovery can be used in a listening mode only or in publishing mode. However, for each service you intend to publish, you have to create a new ServiceDiscovery instance.

This implementation support a avahi-daemon restart after you service discovery instances have been created. At startup of the service discovery a running avahi-daemon is required. Nevertheless, if you need to restart the avahi-daemon after a successful start, all service browsers will be registered. Any attached application has to deal with the newly emitted ServiceEvents.

Implementation details: All service discovery objects within the same process will access the avahi daemon through a single client connection. Thus synchronization is needed and performed using a unique lock on the client instance.

Constructor & Destructor Documentation

◆ ServiceDiscovery()

servicediscovery::avahi::ServiceDiscovery::ServiceDiscovery ( )

◆ ~ServiceDiscovery()

servicediscovery::avahi::ServiceDiscovery::~ServiceDiscovery ( )
virtual

Default deconstructor – will cleanup all underlying resolvers and browsers created for this instance

Member Function Documentation

◆ addedComponentConnect()

void servicediscovery::avahi::ServiceDiscovery::addedComponentConnect ( const sigc::slot< void, ServiceEvent > &  slot)
inline

◆ findServices() [1/2]

std::vector< ServiceDescription > servicediscovery::avahi::ServiceDiscovery::findServices ( const SearchPattern &  pattern = SearchPattern()) const

Search Services using a standard search pattern

Returns
List of service descriptions of all available services, by default of all services seen by this service discovery instance

◆ findServices() [2/2]

std::vector< ServiceDescription > servicediscovery::avahi::ServiceDiscovery::findServices ( const ServicePattern &  pattern,
const std::string &  name_space = "*" 
) const

Search services using a predefined set of service pattern

◆ getConfiguration()

ServiceConfiguration servicediscovery::avahi::ServiceDiscovery::getConfiguration ( ) const

Get the current configuration of this service discovery if in PUBLISH mode and when the service is running

Exceptions
ifthe service discovery is in LISTEN_ONLY mode, where no service configuration exists
Returns
ServiceConfiguration of a published service

◆ getServiceDescription()

ServiceDescription servicediscovery::avahi::ServiceDiscovery::getServiceDescription ( const std::string &  servicename)
static

Retrieve the service description for a given service

Parameters
servicename

◆ getServiceNames()

std::vector< std::string > servicediscovery::avahi::ServiceDiscovery::getServiceNames ( )

Get list of names of services currently seen by this service discovery instance

◆ getUpdateableServices()

std::vector< ServiceDescription > servicediscovery::avahi::ServiceDiscovery::getUpdateableServices ( )
static

Get a list of service descriptions for services that can be updated via the update(const std::string&, const ServiceDescription& ) method.

◆ getVisibleServices()

std::map< std::string, ServiceDescription > servicediscovery::avahi::ServiceDiscovery::getVisibleServices ( const SearchPattern &  pattern = SearchPattern(""))
static

Relying on all running Servicediscovery within a process, this function allows to search for visible services an all existing domains - search pattern allows to limit the search if required

◆ isRunning()

bool servicediscovery::avahi::ServiceDiscovery::isRunning ( ) const

Check if service discovery is running

◆ listenOn()

void servicediscovery::avahi::ServiceDiscovery::listenOn ( const std::vector< std::string > &  types)

Use the service discovery object only to listen for a list of given service types

Parameters
typeslist of types, e.g. whereas types is of format _test._tcp

◆ registerCallbacks()

void servicediscovery::avahi::ServiceDiscovery::registerCallbacks ( const std::string &  serviceName,
const sigc::slot< void, ServiceEvent > &  serviceAdded,
const sigc::slot< void, ServiceEvent > &  serviceRemoved 
)
static

Register callbacks on a specific ServiceDiscovery object running in the current process

◆ removedComponentConnect()

void servicediscovery::avahi::ServiceDiscovery::removedComponentConnect ( const sigc::slot< void, ServiceEvent > &  slot)
inline

◆ start()

void servicediscovery::avahi::ServiceDiscovery::start ( const ServiceConfiguration conf)

Associate the service discovery object with a local service

Parameters
confService configuration

◆ stop()

void servicediscovery::avahi::ServiceDiscovery::stop ( )
virtual

Stop the listening and publishing activities of the service discovery object

Implements servicediscovery::avahi::ClientObserver.

◆ update() [1/3]

void servicediscovery::avahi::ServiceDiscovery::update ( const ServiceDescription desc)

Update a service description that has been associated with the current service discovery

Parameters
descriptionNew service description that will be published

◆ update() [2/3]

void servicediscovery::avahi::ServiceDiscovery::update ( const ClientObserver::Event event)
virtual

Implementation of the observer function in order to react to underlying client connection changes This update is called from the Client and with an acquire avahi client lock

Implements servicediscovery::avahi::ClientObserver.

◆ update() [3/3]

bool servicediscovery::avahi::ServiceDiscovery::update ( const std::string &  servicename,
const ServiceDescription description 
)
static

Update all known services of the given name using the associated description

Parameters
servicenameName of the service
descriptionNew description of the service

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