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 Lunchbox library is compiled without zeroconf support (
#include "test.h"
#include <lunchbox/servus.h>
#include <lunchbox/rng.h>
#include <boost/lexical_cast.hpp>
#ifdef LUNCHBOX_USE_DNSSD
# include <dns_sd.h>
#endif
int main( int, char** )
{
try
{
}
catch( const std::runtime_error& e )
{
if( getenv( "TRAVIS" ))
{
std::cerr << "Bailing, no avahi on a Travis CI setup" << std::endl;
TEST( e.what() ==
std::string( "Can't setup avahi client: Daemon not running"));
return EXIT_SUCCESS;
}
throw e;
}
const uint16_t port = (rng.
get< uint16_t >() % 60000) + 1024;
boost::lexical_cast< std::string >( port ));
{
return EXIT_SUCCESS;
}
#ifdef LUNCHBOX_USE_DNSSD
TEST( lunchbox::Result::SUCCESS == kDNSServiceErr_NoError );
#endif
if( result != lunchbox::Result::SUCCESS )
{
LBWARN <<
"Bailing, got " << result
<< ": looks like a broken zeroconf setup" << std::endl;
return EXIT_SUCCESS;
}
TESTINFO( result, result );
service.withdraw();
service.set( "foo", "bar" );
TEST( service.announce( port, boost::lexical_cast< std::string >( port )));
2000 );
if( hosts.empty() && getenv( "TRAVIS" ))
{
std::cerr << "Bailing, got no hosts on a Travis CI setup" << std::endl;
return EXIT_SUCCESS;
}
TESTINFO( hosts.size() == 1, hosts.size( ));
TESTINFO( hosts.front() == boost::lexical_cast< std::string >( port ),
hosts.front( ));
TEST( service.get( hosts.front(), "foo" ) == "bar" );
service.set( "foobar", "42" );
TESTINFO( hosts.size() == 1, hosts.size( ));
TESTINFO( service.get( hosts.front(), "foobar" ) == "42",
TEST( service.getKeys().size() == 2 );
TEST( !service.isBrowsing( ));
TEST( service.isBrowsing( ));
TEST( service.isBrowsing( ));
TESTINFO( service.browse( 200 ), service.browse( 0 ));
hosts = service.getInstances();
TESTINFO( hosts.size() == 1, hosts.size( ));
TESTINFO( service.get( hosts.front(), "foo" ) == "bar",
service.get( hosts.front(), "foo" ));
TEST( service.getKeys().size() == 2 );
{
TEST( service2.announce( port+1,
boost::lexical_cast< std::string >( port+1 )));
TEST( service.browse( 2000 ));
hosts = service.getInstances();
}
TEST( service.browse( 2000 ));
hosts = service.getInstances();
TEST( service.isBrowsing( ));
service.endBrowsing();
TEST( !service.isBrowsing( ));
hosts = service.getInstances();
TEST( service.get( hosts.front(), "foo" ) == "bar" );
TEST( service.getKeys().size() == 2 );
return EXIT_SUCCESS;
}