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 (
#define BOOST_TEST_MODULE servus_servus
#include <boost/test/unit_test.hpp>
#include <servus/servus.h>
#include <random>
#include <thread>
#ifdef SERVUS_USE_DNSSD
# include <dns_sd.h>
#endif
uint16_t getRandomPort()
{
static std::random_device device;
static std::minstd_rand engine( device( ));
std::uniform_int_distribution< uint16_t > generator( 1024, 65535u );
return generator( engine );
}
BOOST_AUTO_TEST_CASE(test_servus)
{
const uint32_t port = getRandomPort();
std::string serviceName = "_servustest_" + std::to_string( port ) + "._tcp";
try
{
}
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;
}
std::to_string( port ));
BOOST_CHECK_EQUAL( service.getName(), serviceName );
{
return;
}
#ifdef SERVUS_USE_DNSSD
BOOST_CHECK( servus::Result::SUCCESS == kDNSServiceErr_NoError );
#endif
if( result != servus::Result::SUCCESS )
{
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( service.announce( port, std::to_string( port )));
if( hosts.empty() && getenv( "TRAVIS" ))
{
std::cerr << "Bailing, got no hosts on a Travis CI setup" << std::endl;
return;
}
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( hosts.front(), std::to_string( port ));
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foo" ), "bar" );
std::this_thread::sleep_for( std::chrono::milliseconds( 200 ));
service.set( "foobar", "42" );
std::this_thread::sleep_for( std::chrono::milliseconds( 2000 ));
BOOST_REQUIRE_EQUAL( hosts.size(), 1 );
BOOST_CHECK_EQUAL( service.get( hosts.front(), "foobar" ), "42" );
BOOST_CHECK_EQUAL( service.getKeys().size(), 2 );
BOOST_CHECK( !service.isBrowsing( ));
BOOST_CHECK( service.isBrowsing( ));
BOOST_CHECK( service.isBrowsing( ));
BOOST_CHECK_EQUAL( service.browse( 200 ), service.browse( 0 ));
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 );
{
BOOST_CHECK( service2.announce( port+1, std::to_string( port+1 )));
BOOST_CHECK( service.browse( 2000 ));
hosts = service.getInstances();
BOOST_CHECK_EQUAL( hosts.size(), 2 );
}
std::this_thread::sleep_for( std::chrono::milliseconds( 500 ));
BOOST_CHECK( service.browse( 2000 ));
hosts = service.getInstances();
BOOST_CHECK_EQUAL( hosts.size(), 1 );
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 );
}