Servus  1.2.0
A small set of network oriented utilities in C++ including a zeroconf implementation.
servus.h
1 /* Copyright (c) 2012-2015, Human Brain Project
2  * Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Servus <https://github.com/HBPVIS/Servus>
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 3.0 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 SERVUS_SERVUS_H
21 #define SERVUS_SERVUS_H
22 
23 #include <servus/api.h>
24 #include <servus/result.h> // nested base class
25 #include <servus/types.h>
26 
27 #include <map>
28 
29 namespace servus
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 servus::Result
60  {
61  public:
62  explicit Result( const int32_t code ) : servus::Result( code ){}
63  virtual ~Result(){}
64  SERVUS_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  SERVUS_API static bool isAvailable();
76 
83  SERVUS_API explicit Servus( const std::string& name );
84 
86  SERVUS_API virtual ~Servus();
87 
89  SERVUS_API const std::string& getName() const;
90 
103  SERVUS_API void set( const std::string& key, const std::string& value );
104 
106  SERVUS_API Strings getKeys() const;
107 
109  SERVUS_API const std::string& get( const std::string& key ) const;
110 
119  SERVUS_API Result announce( const unsigned short port,
120  const std::string& instance );
121 
123  SERVUS_API void withdraw();
124 
126  SERVUS_API bool isAnnounced() const;
127 
138  SERVUS_API Strings discover( const Interface addr,
139  const unsigned browseTime );
140 
148  SERVUS_API Result beginBrowsing( const servus::Servus::Interface addr );
149 
157  SERVUS_API Result browse( int32_t timeout = -1 );
158 
160  SERVUS_API void endBrowsing();
161 
163  SERVUS_API bool isBrowsing() const;
164 
166  SERVUS_API Strings getInstances() const;
167 
169  SERVUS_API Strings getKeys( const std::string& instance ) const;
170 
172  SERVUS_API bool containsKey( const std::string& instance,
173  const std::string& key ) const;
174 
176  SERVUS_API const std::string& get( const std::string& instance,
177  const std::string& key ) const;
178 
185  SERVUS_API void addListener( Listener* listener );
186 
193  SERVUS_API void removeListener( Listener* listener );
194 
196  typedef std::map< std::string, std::map< std::string, std::string > > Data;
197 
199  SERVUS_API void getData( Data& data );
200 
201 private:
202  Servus( const Servus& );
203  Servus& operator=( const Servus& );
204  detail::Servus* const _impl;
205  friend SERVUS_API std::ostream& operator << ( std::ostream&,
206  const Servus& );
207 };
208 
210 SERVUS_API std::string getHostname();
211 
213 SERVUS_API std::ostream& operator << ( std::ostream&, const Servus& );
214 
216 SERVUS_API std::ostream& operator << ( std::ostream&,const Servus::Interface& );
217 }
218 
219 #endif // SERVUS_SERVUS_H
SERVUS_API void withdraw()
Stop announcing the registered key/value pairs.
Defines export visibility macros for library Servus.
A result returns an error code and behaves like a boolean.
Definition: result.h:31
virtual ~Result()
Destruct the result.
Definition: servus.h:63
friend SERVUS_API std::ostream & operator<<(std::ostream &, const Servus &)
Output the servus instance in human-readable format.
SERVUS_API Servus(const std::string &name)
Create a new service handle.
The ZeroConf operation result code.
Definition: servus.h:59
SERVUS_API Strings getInstances() const
only local interfaces
Definition: servus.h:50
SERVUS_API bool isBrowsing() const
SERVUS_API void removeListener(Listener *listener)
Remove a listener to stop invokation on its supported callbacks.
static const int32_t PENDING
operation did not complete.
Definition: servus.h:67
SERVUS_API void endBrowsing()
Stop a discovery process and return all results.
virtual SERVUS_API ~Servus()
Destruct this service.
SERVUS_API const std::string & getName() const
SERVUS_API void addListener(Listener *listener)
Add a listener which is invoked according to its supported callbacks.
Simple wrapper for ZeroConf key/value pairs.
Definition: servus.h:43
SERVUS_API Strings getKeys() const
static const int32_t POLL_ERROR
Error during polling for event.
Definition: servus.h:71
SERVUS_API void set(const std::string &key, const std::string &value)
Set a key/value pair to be announced.
SERVUS_API bool isAnnounced() const
SERVUS_API std::string getString() const override
A listener interface which can be implemented to listen on changes of a Servus instance.
Definition: listener.h:32
SERVUS_API Strings discover(const Interface addr, const unsigned browseTime)
Discover all announced key/value pairs.
SERVUS_API Result browse(int32_t timeout=-1)
Browse and process discovered key/value pairs.
SERVUS_API Result beginBrowsing(const servus::Servus::Interface addr)
Begin the discovery of announced key/value pairs.
SERVUS_API Result announce(const unsigned short port, const std::string &instance)
Start announcing the registered key/value pairs.
static SERVUS_API bool isAvailable()
static const int32_t NOT_SUPPORTED
Servus compiled without ZeroConf support.
Definition: servus.h:69
use all interfaces
Definition: servus.h:48
SERVUS_API bool containsKey(const std::string &instance, const std::string &key) const