Servus  1.4.0
C++ network oriented utilities including a zeroconf implementation
servus::Servus Class Reference

Simple wrapper for ZeroConf key/value pairs. More...

#include <servus.h>

+ Collaboration diagram for servus::Servus:

Classes

class  Result
 The ZeroConf operation result code. More...
 

Public Types

enum  Interface { IF_ALL = 0, IF_LOCAL = (unsigned)(-1) }
 
typedef std::map< std::string, std::map< std::string, std::string > > Data
 

Public Member Functions

 Servus (const std::string &name)
 Create a new service handle. More...
 
virtual ~Servus ()
 Destruct this service. More...
 
const std::string & getName () const
 
void set (const std::string &key, const std::string &value)
 Set a key/value pair to be announced. More...
 
Strings getKeys () const
 
const std::string & get (const std::string &key) const
 
Result announce (const unsigned short port, const std::string &instance)
 Start announcing the registered key/value pairs. More...
 
void withdraw ()
 Stop announcing the registered key/value pairs. More...
 
bool isAnnounced () const
 
Strings discover (const Interface addr, const unsigned browseTime)
 Discover all announced key/value pairs. More...
 
Result beginBrowsing (const servus::Servus::Interface addr)
 Begin the discovery of announced key/value pairs. More...
 
Result browse (int32_t timeout=-1)
 Browse and process discovered key/value pairs. More...
 
void endBrowsing ()
 Stop a discovery process and return all results. More...
 
bool isBrowsing () const
 
Strings getInstances () const
 
Strings getKeys (const std::string &instance) const
 
const std::string & getHost (const std::string &instance) const
 
bool containsKey (const std::string &instance, const std::string &key) const
 
const std::string & get (const std::string &instance, const std::string &key) const
 
void addListener (Listener *listener)
 Add a listener which is invoked according to its supported callbacks. More...
 
void removeListener (Listener *listener)
 Remove a listener to stop invokation on its supported callbacks. More...
 
void getData (Data &data)
 

Static Public Member Functions

static bool isAvailable ()
 

Friends

std::ostream & operator<< (std::ostream &, const Servus &)
 Output the servus instance in human-readable format. More...
 

Detailed Description

Simple wrapper for ZeroConf key/value pairs.

The servus class allows simple announcement and discovery of key/value pairs using ZeroConf networking. The same instance can be used to announce and/or to browse a ZeroConf service. If the Servus library is compiled without zeroconf support (

See also
isAvailable()), this class does not do anything useful.

Example:

/* Copyright (c) 2012-2015, Stefan.Eilemann@epfl.ch
*
* This file is part of Servus <https://github.com/HBPVIS/Servus>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define BOOST_TEST_MODULE servus_servus
#include <boost/test/unit_test.hpp>
#include <servus/servus.h>
#ifdef SERVUS_USE_CXX03
# include <boost/random.hpp>
# include <boost/lexical_cast.hpp>
namespace rnd=boost::random;
#else
# include <random>
namespace rnd=std;
#endif
#ifdef SERVUS_USE_DNSSD
# include <dns_sd.h>
#endif
#ifdef _MSC_VER
# include <windows.h>
# define _sleep Sleep
#else
# define _sleep ::sleep
#endif
static const int _propagationTime = 1000;
static const int _propagationTries = 10;
uint16_t getRandomPort()
{
#ifdef SERVUS_USE_CXX03
static rnd::minstd_rand engine;
#else
static rnd::random_device device;
static rnd::minstd_rand engine( device( ));
#endif
rnd::uniform_int_distribution< uint16_t > generator( 1024u, 65535u );
return generator( engine );
}
template< class T > std::string toString( const T& what )
{
#ifdef SERVUS_USE_CXX03
return boost::lexical_cast< std::string >( what );
#else
return std::to_string( what );
#endif
}
BOOST_AUTO_TEST_CASE(test_servus)
{
const uint32_t port = getRandomPort();
std::string serviceName = "_servustest_" + toString( port ) + "._tcp";
try
{
servus::Servus service( serviceName );
}
catch( const std::runtime_error& e )
{
if( getenv( "TRAVIS" ))
{
std::cerr << "Bailing, no avahi on a Travis CI setup" << std::endl;
BOOST_CHECK_EQUAL(
e.what(), "Can't setup avahi client: Daemon not running" );
return;
}
throw;
}
servus::Servus service( serviceName );
const servus::Servus::Result& result = service.announce( port,
toString( port ));
BOOST_CHECK_EQUAL( service.getName(), serviceName );
{
// BOOST_CHECK_EQUAL gives a link error for Result::NOT_SUPPORTED
BOOST_CHECK( result == servus::Servus::Result::NOT_SUPPORTED );
return;
}
#ifdef SERVUS_USE_DNSSD
BOOST_CHECK( servus::Result::SUCCESS == kDNSServiceErr_NoError );
#endif
if( result != servus::Result::SUCCESS ) // happens on CI VMs
{
std::cerr << "Bailing, got " << result
<< ": looks like a broken zeroconf setup" << std::endl;
return;
}
BOOST_CHECK_EQUAL( result, result );
service.withdraw();
service.set( "foo", "bar" );
BOOST_CHECK_EQUAL( service.get( "foo" ), "bar" );
BOOST_CHECK_EQUAL( service.get( "bar" ), std::string( ));
BOOST_CHECK( service.announce( port, toString( port )));
int nLoops = _propagationTries;
while( --nLoops )
{
const servus::Strings& hosts =
service.discover( servus::Servus::IF_LOCAL, _propagationTime );
if( hosts.empty() && nLoops > 1 )
{
if( getenv( "TRAVIS" ))
{
std::cerr << "Bailing, got no hosts on a Travis CI setup"
<< std::endl;
return;
}
continue;
}
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( hosts.front(), toString( port ));
BOOST_CHECK( service.containsKey( hosts.front(), "foo" ));
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foo" ), "bar" );
BOOST_CHECK_EQUAL( service.get( "bar", "foo" ), std::string( ));
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foobar" ),
std::string( ));
break;
}
service.set( "foobar", "42" );
nLoops = _propagationTries;
while( --nLoops )
{
const servus::Strings& hosts =
service.discover( servus::Servus::IF_LOCAL, _propagationTime );
const bool hasFoobar = !hosts.empty() &&
service.containsKey( hosts.front(), "foobar" );
if(( hosts.empty() || !hasFoobar ) && nLoops > 1 )
continue;
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foobar" ), "42" );
BOOST_CHECK_EQUAL( service.getKeys().size(), 2 );
break;
}
// continuous browse API
BOOST_CHECK( !service.isBrowsing( ));
BOOST_CHECK( service.beginBrowsing( servus::Servus::IF_LOCAL ));
BOOST_CHECK( service.isBrowsing( ));
// BOOST_CHECK_EQUAL gives a link error for Result::PENDING
BOOST_CHECK( service.beginBrowsing( servus::Servus::IF_LOCAL ) ==
BOOST_CHECK( service.isBrowsing( ));
BOOST_CHECK_EQUAL( service.browse( _propagationTime ),
service.browse( 0 ));
servus::Strings hosts = service.getInstances();
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foo" ), "bar" );
BOOST_CHECK_EQUAL( service.getKeys().size(), 2 );
{ // test updates during browsing
servus::Servus service2( serviceName );
BOOST_CHECK( service2.announce( port+1, toString( port+1 )));
nLoops = _propagationTries;
while( --nLoops )
{
BOOST_CHECK( service.browse( _propagationTime ));
hosts = service.getInstances();
if( hosts.size() < 2 && nLoops > 1 )
continue;
BOOST_CHECK_EQUAL( hosts.size(), 2 );
break;
}
}
nLoops = _propagationTries;
while( --nLoops )
{
BOOST_CHECK( service.browse( _propagationTime ));
hosts = service.getInstances();
if( hosts.size() > 1 && nLoops > 1 )
continue;
BOOST_CHECK_EQUAL( hosts.size(), 1 );
break;
}
BOOST_CHECK( service.isBrowsing( ));
service.endBrowsing();
BOOST_CHECK( !service.isBrowsing( ));
hosts = service.getInstances();
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foo" ), "bar" );
BOOST_CHECK_EQUAL( service.getKeys().size(), 2 );
}

Definition at line 43 of file servus.h.

Member Enumeration Documentation

Enumerator
IF_ALL 

use all interfaces

IF_LOCAL 

only local interfaces

Definition at line 46 of file servus.h.

Constructor & Destructor Documentation

servus::Servus::Servus ( const std::string &  name)
explicit

Create a new service handle.

Parameters
namethe service descriptor, e.g., "_hwsd._tcp"
Version
1.1
virtual servus::Servus::~Servus ( )
virtual

Destruct this service.

Version
1.1

Member Function Documentation

void servus::Servus::addListener ( Listener listener)

Add a listener which is invoked according to its supported callbacks.

Parameters
listenerthe listener to be added, must not be nullptr
Version
1.2
Result servus::Servus::announce ( const unsigned short  port,
const std::string &  instance 
)

Start announcing the registered key/value pairs.

Parameters
portthe service IP port in host byte order.
instancea host-unique instance name, hostname is used if empty.
Returns
the success status of the operation.
Version
1.1
Result servus::Servus::beginBrowsing ( const servus::Servus::Interface  addr)

Begin the discovery of announced key/value pairs.

Parameters
addrthe scope of the discovery
Returns
the success status of the operation.
Version
1.1
Result servus::Servus::browse ( int32_t  timeout = -1)

Browse and process discovered key/value pairs.

Parameters
timeoutThe time to spend browsing.
Returns
the success status of the operation.
Version
1.1
bool servus::Servus::containsKey ( const std::string &  instance,
const std::string &  key 
) const
Returns
true if the given key was discovered.
Version
1.1
Strings servus::Servus::discover ( const Interface  addr,
const unsigned  browseTime 
)

Discover all announced key/value pairs.

Parameters
addrthe scope of the discovery
browseTimethe browse time, in milliseconds, to wait for new records.
Returns
all instance names found during discovery.
See also
beginBrowsing(), browse(), endBrowsing()
Version
1.1
void servus::Servus::endBrowsing ( )

Stop a discovery process and return all results.

Version
1.1
const std::string& servus::Servus::get ( const std::string &  key) const
Returns
the value to the given (to be) announced key.
Version
1.1
const std::string& servus::Servus::get ( const std::string &  instance,
const std::string &  key 
) const
Returns
the value of the given key and instance.
Version
1.1
const std::string& servus::Servus::getHost ( const std::string &  instance) const
Returns
the host corresponding to the given instance.
Version
1.3
Strings servus::Servus::getInstances ( ) const
Returns
all instances found during the last discovery.
Version
1.1
Strings servus::Servus::getKeys ( ) const
Returns
all (to be) announced keys.
Version
1.1
Strings servus::Servus::getKeys ( const std::string &  instance) const
Returns
all keys discovered on the given instance.
Version
1.1
const std::string& servus::Servus::getName ( ) const
Returns
the service name.
Version
1.1
bool servus::Servus::isAnnounced ( ) const
Returns
true if the local data is announced.
Version
1.1
static bool servus::Servus::isAvailable ( )
static
Returns
true if a usable implementation is available.
bool servus::Servus::isBrowsing ( ) const
Returns
true if the local data is browsing.
Version
1.1
void servus::Servus::removeListener ( Listener listener)

Remove a listener to stop invokation on its supported callbacks.

Parameters
listenerthe listener to be removed, must not be nullptr
Version
1.2
void servus::Servus::set ( const std::string &  key,
const std::string &  value 
)

Set a key/value pair to be announced.

Keys should be at most eight characters, and values are truncated to 255 characters. The total length of all keys and values cannot exceed 65535 characters. Setting a value on an announced service causes an update which needs some time to propagate after this function returns, that is, calling discover() immediately afterwards will very likely not contain the new key/value pair.

Version
1.1
void servus::Servus::withdraw ( )

Stop announcing the registered key/value pairs.

Version
1.1

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  ,
const Servus  
)
friend

Output the servus instance in human-readable format.


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