Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
servus.h
1 
2 /* Copyright (c) 2012-2014, Stefan Eilemann <eile@eyescale.ch>
3  *
4  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 2.1 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef LUNCHBOX_SERVUS_H
21 #define LUNCHBOX_SERVUS_H
22 
23 #include <lunchbox/api.h>
24 #include <lunchbox/result.h> // nested base class
25 #include <lunchbox/types.h>
26 #include <boost/shared_ptr.hpp>
27 #include <map>
28 
29 namespace lunchbox
30 {
31 namespace detail { class Servus; }
32 
43 class Servus
44 {
45 public:
46  enum Interface
47  {
48  IF_ALL = 0,
49  // (uint32_t) -1 == kDNSServiceInterfaceIndexLocalOnly
50  IF_LOCAL = (unsigned)(-1)
51  };
52 
59  class Result : public lunchbox::Result
60  {
61  public:
62  explicit Result( const int32_t code ) : lunchbox::Result( code ){}
63  virtual ~Result(){}
64  LUNCHBOX_API std::string getString() const override;
65 
67  static const int32_t PENDING = -1;
69  static const int32_t NOT_SUPPORTED = -2;
71  static const int32_t POLL_ERROR = -3;
72  };
73 
75  LUNCHBOX_API static bool isAvailable();
76 
83  LUNCHBOX_API explicit Servus( const std::string& name );
84 
91  LUNCHBOX_API Servus( const Servus& from );
92 
94  LUNCHBOX_API virtual ~Servus();
95 
97  LUNCHBOX_API Servus& operator = ( const Servus& rhs );
98 
111  LUNCHBOX_API void set( const std::string& key, const std::string& value );
112 
114  LUNCHBOX_API Strings getKeys() const;
115 
117  LUNCHBOX_API const std::string& get( const std::string& key ) const;
118 
127  LUNCHBOX_API Result announce( const unsigned short port,
128  const std::string& instance );
129 
131  LUNCHBOX_API void withdraw();
132 
134  LUNCHBOX_API bool isAnnounced() const;
135 
146  LUNCHBOX_API Strings discover( const Interface addr,
147  const unsigned browseTime );
148 
156  LUNCHBOX_API Result beginBrowsing( const lunchbox::Servus::Interface addr );
157 
165  LUNCHBOX_API Result browse( int32_t timeout = -1 );
166 
168  LUNCHBOX_API void endBrowsing();
169 
171  LUNCHBOX_API bool isBrowsing() const;
172 
174  LUNCHBOX_API Strings getInstances() const;
175 
177  LUNCHBOX_API Strings getKeys( const std::string& instance ) const;
178 
180  LUNCHBOX_API bool containsKey( const std::string& instance,
181  const std::string& key ) const;
182 
184  LUNCHBOX_API const std::string& get( const std::string& instance,
185  const std::string& key ) const;
186 
188  typedef std::map< std::string, std::map< std::string, std::string > > Data;
189 
191  LUNCHBOX_API void getData( Data& data );
192 
193 private:
194  boost::shared_ptr< detail::Servus > _impl;
195  friend LUNCHBOX_API std::ostream& operator << ( std::ostream&,
196  const Servus& );
197 };
198 
200 LUNCHBOX_API std::ostream& operator << ( std::ostream&, const Servus& );
201 
203 LUNCHBOX_API std::ostream& operator << (std::ostream&,const Servus::Interface&);
204 }
205 
206 #endif // LUNCHBOX_SERVUS_H
LUNCHBOX_API Result announce(const unsigned short port, const std::string &instance)
Start announcing the registered key/value pairs.
Defines export visibility macros for Lunchbox.
Basic type definitions not provided by the operating system.
virtual LUNCHBOX_API ~Servus()
Destruct this service.
LUNCHBOX_API void withdraw()
Stop announcing the registered key/value pairs.
LUNCHBOX_API Strings discover(const Interface addr, const unsigned browseTime)
Discover all announced key/value pairs.
LUNCHBOX_API void endBrowsing()
Stop a discovery process and return all results.
std::vector< std::string > Strings
A vector of std::strings.
Definition: types.h:221
LUNCHBOX_API Strings getKeys() const
friend LUNCHBOX_API std::ostream & operator<<(std::ostream &, const Servus &)
Output the servus instance in human-readable format.
LUNCHBOX_API Servus & operator=(const Servus &rhs)
Share the ZeroConf service with the rhs argument.
LUNCHBOX_API bool isAnnounced() const
LUNCHBOX_API bool isBrowsing() const
LUNCHBOX_API Result browse(int32_t timeout=-1)
Browse and process discovered key/value pairs.
A result returns an error code and behaves like a boolean.
Definition: result.h:31
virtual ~Result()
Destruct the result.
Definition: servus.h:63
static const int32_t POLL_ERROR
Error during polling for event.
Definition: servus.h:71
static LUNCHBOX_API bool isAvailable()
LUNCHBOX_API Result beginBrowsing(const lunchbox::Servus::Interface addr)
Begin the discovery of announced key/value pairs.
LUNCHBOX_API Servus(const std::string &name)
Create a new service handle.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
static const int32_t PENDING
operation did not complete.
Definition: servus.h:67
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
Definition: array.h:47
static const int32_t NOT_SUPPORTED
Lunchbox compiled without ZeroConf support.
Definition: servus.h:69
The ZeroConf operation result code.
Definition: servus.h:59
LUNCHBOX_API std::string getString() const override
LUNCHBOX_API bool containsKey(const std::string &instance, const std::string &key) const
use all interfaces
Definition: servus.h:48
LUNCHBOX_API Strings getInstances() const
Simple wrapper for ZeroConf key/value pairs.
Definition: servus.h:43
only local interfaces
Definition: servus.h:50
LUNCHBOX_API void set(const std::string &key, const std::string &value)
Set a key/value pair to be announced.