Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::Servus Class Reference

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

#include <servus.h>

+ Inheritance diagram for lunchbox::Servus:
+ Collaboration diagram for lunchbox::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

LUNCHBOX_API Servus (const std::string &name)
 Create a new service handle. More...
 
virtual LUNCHBOX_API ~Servus ()
 Destruct this service. More...
 
LUNCHBOX_API void set (const std::string &key, const std::string &value)
 Set a key/value pair to be announced. More...
 
LUNCHBOX_API Strings getKeys () const
 
LUNCHBOX_API const std::string & get (const std::string &key) const
 
LUNCHBOX_API Result announce (const unsigned short port, const std::string &instance)
 Start announcing the registered key/value pairs. More...
 
LUNCHBOX_API void withdraw ()
 Stop announcing the registered key/value pairs. More...
 
LUNCHBOX_API bool isAnnounced () const
 
LUNCHBOX_API Strings discover (const Interface addr, const unsigned browseTime)
 Discover all announced key/value pairs. More...
 
LUNCHBOX_API Result beginBrowsing (const lunchbox::Servus::Interface addr)
 Begin the discovery of announced key/value pairs. More...
 
LUNCHBOX_API Result browse (int32_t timeout=-1)
 Browse and process discovered key/value pairs. More...
 
LUNCHBOX_API void endBrowsing ()
 Stop a discovery process and return all results. More...
 
LUNCHBOX_API bool isBrowsing () const
 
LUNCHBOX_API Strings getInstances () const
 
LUNCHBOX_API Strings getKeys (const std::string &instance) const
 
LUNCHBOX_API bool containsKey (const std::string &instance, const std::string &key) const
 
LUNCHBOX_API const std::string & get (const std::string &instance, const std::string &key) const
 
LUNCHBOX_API void getData (Data &data)
 

Static Public Member Functions

static LUNCHBOX_API bool isAvailable ()
 

Friends

LUNCHBOX_API 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 Lunchbox library is compiled without zeroconf support (

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

Example:

/* Copyright (c) 2012-2014, Stefan.Eilemann@epfl.ch
*
* This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
*
* 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.
*/
#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
{
lunchbox::Servus service( "_servustest._tcp" );
}
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;
}
lunchbox::Servus service( "_servustest._tcp" );
const uint16_t port = (rng.get< uint16_t >() % 60000) + 1024;
const lunchbox::Servus::Result& result = service.announce( port,
boost::lexical_cast< std::string >( port ));
{
TESTINFO( result == lunchbox::Servus::Result::NOT_SUPPORTED, result );
return EXIT_SUCCESS;
}
#ifdef LUNCHBOX_USE_DNSSD
TEST( lunchbox::Result::SUCCESS == kDNSServiceErr_NoError );
#endif
if( result != lunchbox::Result::SUCCESS ) // happens on CI VMs
{
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 )));
lunchbox::Strings hosts = service.discover( lunchbox::Servus::IF_LOCAL,
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" );
lunchbox::sleep( 2000 );
hosts = service.discover( lunchbox::Servus::IF_LOCAL, 2000 );
TESTINFO( hosts.size() == 1, hosts.size( ));
TESTINFO( service.get( hosts.front(), "foobar" ) == "42",
"Keys: " << lunchbox::format( service.getKeys( hosts.front( ))));
TEST( service.getKeys().size() == 2 );
// continuous browse API
TEST( !service.isBrowsing( ));
TESTRESULT( service.beginBrowsing( lunchbox::Servus::IF_LOCAL ),
TEST( service.isBrowsing( ));
TEST( service.beginBrowsing( lunchbox::Servus::IF_LOCAL ) ==
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 updates during browsing
lunchbox::Servus service2( "_servustest._tcp" );
TEST( service2.announce( port+1,
boost::lexical_cast< std::string >( port+1 )));
TEST( service.browse( 2000 ));
hosts = service.getInstances();
TESTINFO( hosts.size() == 2, lunchbox::format( hosts ));
}
TEST( service.browse( 2000 ));
hosts = service.getInstances();
TESTINFO( hosts.size() == 1, lunchbox::format( hosts ));
TEST( service.isBrowsing( ));
service.endBrowsing();
TEST( !service.isBrowsing( ));
hosts = service.getInstances();
TESTINFO( hosts.size() == 1, lunchbox::format( hosts ));
TEST( service.get( hosts.front(), "foo" ) == "bar" );
TEST( service.getKeys().size() == 2 );
return EXIT_SUCCESS;
}

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

LUNCHBOX_API lunchbox::Servus::Servus ( const std::string &  name)
explicit

Create a new service handle.

Parameters
namethe service descriptor, e.g., "_hwsd._tcp"
Version
0.9
virtual LUNCHBOX_API lunchbox::Servus::~Servus ( )
virtual

Destruct this service.

Member Function Documentation

LUNCHBOX_API Result lunchbox::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
0.9
LUNCHBOX_API Result lunchbox::Servus::beginBrowsing ( const lunchbox::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.9.2
LUNCHBOX_API Result lunchbox::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.9.2
LUNCHBOX_API bool lunchbox::Servus::containsKey ( const std::string &  instance,
const std::string &  key 
) const
Returns
true if the given key was discovered.
Version
0.9
LUNCHBOX_API Strings lunchbox::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.
Version
0.9
See Also
beginBrowsing(), browse(), endBrowsing()
LUNCHBOX_API void lunchbox::Servus::endBrowsing ( )

Stop a discovery process and return all results.

Version
1.9.2
LUNCHBOX_API const std::string& lunchbox::Servus::get ( const std::string &  key) const
Returns
the value to the given (to be) announced key.
Version
1.5.1
LUNCHBOX_API const std::string& lunchbox::Servus::get ( const std::string &  instance,
const std::string &  key 
) const
Returns
the value of the given key and instance.
Version
0.9
LUNCHBOX_API Strings lunchbox::Servus::getInstances ( ) const
Returns
all instances found during the last discovery.
Version
0.9
LUNCHBOX_API Strings lunchbox::Servus::getKeys ( ) const
Returns
all (to be) announced keys.
Version
1.5.1
LUNCHBOX_API Strings lunchbox::Servus::getKeys ( const std::string &  instance) const
Returns
all keys discovered on the given instance.
Version
0.9
LUNCHBOX_API bool lunchbox::Servus::isAnnounced ( ) const
Returns
true if the local data is announced.
Version
0.9
static LUNCHBOX_API bool lunchbox::Servus::isAvailable ( )
static
Returns
true if a usable implementation is available.
Version
1.9.2
LUNCHBOX_API bool lunchbox::Servus::isBrowsing ( ) const
Returns
true if the local data is browsing.
Version
1.9.2
LUNCHBOX_API void lunchbox::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
0.9
LUNCHBOX_API void lunchbox::Servus::withdraw ( )

Stop announcing the registered key/value pairs.

Version
0.9

Friends And Related Function Documentation

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

Output the servus instance in human-readable format.

Version
1.5.1

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