Lunchbox  1.9.1
 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 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)
 

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 (LUNCHBOX_USE_DNSSD is not set), 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>
#ifdef LUNCHBOX_USE_DNSSD
# include <dns_sd.h>
#endif
int main( int, char** )
{
const uint16_t port = rng.get< uint16_t >();
lunchbox::Servus service( "_servustest._tcp" );
std::ostringstream os;
os << port;
const lunchbox::Servus::Result& result = service.announce( port, os.str( ));
#ifdef LUNCHBOX_USE_DNSSD
TEST( lunchbox::Result::SUCCESS == kDNSServiceErr_NoError );
if( result == kDNSServiceErr_Unknown ) // happens on CI VMs
{
std::cerr << "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, os.str( )));
const lunchbox::Strings& hosts =
service.discover( lunchbox::Servus::IF_LOCAL, 500 );
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() == os.str(), hosts.front( ));
TEST( service.get( hosts.front(), "foo" ) == "bar" );
service.set( "foobar", "42" );
service.discover( lunchbox::Servus::IF_LOCAL, 500 );
TEST( service.get( hosts.front(), "foobar" ) == "42" );
TEST( service.getKeys().size() == 2 );
#else
TESTINFO( result == lunchbox::Servus::Result::NOT_SUPPORTED, result );
#endif
return EXIT_SUCCESS;
}

Definition at line 44 of file servus.h.

Member Enumeration Documentation

Enumerator
IF_ALL 

use all interfaces

IF_LOCAL 

only local interfaces

Definition at line 47 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., "_gpu-sd._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 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
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
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

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