Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/noncopyable.hpp>
27 #include <map>
28 
29 namespace lunchbox
30 {
31 namespace detail { class Servus; }
32 
43 class Servus : public boost::noncopyable
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 
86  LUNCHBOX_API virtual ~Servus();
87 
100  LUNCHBOX_API void set( const std::string& key, const std::string& value );
101 
103  LUNCHBOX_API Strings getKeys() const;
104 
106  LUNCHBOX_API const std::string& get( const std::string& key ) const;
107 
116  LUNCHBOX_API Result announce( const unsigned short port,
117  const std::string& instance );
118 
120  LUNCHBOX_API void withdraw();
121 
123  LUNCHBOX_API bool isAnnounced() const;
124 
135  LUNCHBOX_API Strings discover( const Interface addr,
136  const unsigned browseTime );
137 
145  LUNCHBOX_API Result beginBrowsing( const lunchbox::Servus::Interface addr );
146 
154  LUNCHBOX_API Result browse( int32_t timeout = -1 );
155 
157  LUNCHBOX_API void endBrowsing();
158 
160  LUNCHBOX_API bool isBrowsing() const;
161 
163  LUNCHBOX_API Strings getInstances() const;
164 
166  LUNCHBOX_API Strings getKeys( const std::string& instance ) const;
167 
169  LUNCHBOX_API bool containsKey( const std::string& instance,
170  const std::string& key ) const;
171 
173  LUNCHBOX_API const std::string& get( const std::string& instance,
174  const std::string& key ) const;
175 
177  typedef std::map< std::string, std::map< std::string, std::string > > Data;
178 
180  LUNCHBOX_API void getData( Data& data );
181 
182 private:
183  detail::Servus* const _impl;
184  friend LUNCHBOX_API std::ostream& operator << ( std::ostream&,
185  const Servus& );
186 };
187 
189 LUNCHBOX_API std::ostream& operator << ( std::ostream&, const Servus& );
190 
192 LUNCHBOX_API std::ostream& operator << (std::ostream&,const Servus::Interface&);
193 }
194 
195 #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 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.
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.