Servus  1.4.0
C++ network oriented utilities 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 const std::string& getHost( const std::string& instance ) const;
173 
175  SERVUS_API bool containsKey( const std::string& instance,
176  const std::string& key ) const;
177 
179  SERVUS_API const std::string& get( const std::string& instance,
180  const std::string& key ) const;
181 
188  SERVUS_API void addListener( Listener* listener );
189 
196  SERVUS_API void removeListener( Listener* listener );
197 
199  typedef std::map< std::string, std::map< std::string, std::string > > Data;
200 
202  SERVUS_API void getData( Data& data );
203 
204 private:
205  Servus( const Servus& );
206  Servus& operator=( const Servus& );
207  detail::Servus* const _impl;
208  friend SERVUS_API std::ostream& operator << ( std::ostream&,
209  const Servus& );
210 };
211 
213 SERVUS_API std::string getHostname();
214 
216 SERVUS_API std::ostream& operator << ( std::ostream&, const Servus& );
217 
219 SERVUS_API std::ostream& operator << ( std::ostream&,const Servus::Interface& );
220 }
221 
222 #endif // SERVUS_SERVUS_H
Result browse(int32_t timeout=-1)
Browse and process discovered key/value pairs.
Defines export visibility macros for library Servus.
void addListener(Listener *listener)
Add a listener which is invoked according to its supported callbacks.
A result returns an error code and behaves like a boolean.
Definition: result.h:31
virtual ~Result()
Destruct the result.
Definition: servus.h:63
void removeListener(Listener *listener)
Remove a listener to stop invokation on its supported callbacks.
The ZeroConf operation result code.
Definition: servus.h:59
bool isBrowsing() const
virtual ~Servus()
Destruct this service.
only local interfaces
Definition: servus.h:50
Strings getKeys() const
Result beginBrowsing(const servus::Servus::Interface addr)
Begin the discovery of announced key/value pairs.
static bool isAvailable()
static const int32_t PENDING
operation did not complete.
Definition: servus.h:67
bool isAnnounced() const
friend std::ostream & operator<<(std::ostream &, const Servus &)
Output the servus instance in human-readable format.
Simple wrapper for ZeroConf key/value pairs.
Definition: servus.h:43
Strings discover(const Interface addr, const unsigned browseTime)
Discover all announced key/value pairs.
static const int32_t POLL_ERROR
Error during polling for event.
Definition: servus.h:71
void endBrowsing()
Stop a discovery process and return all results.
Result announce(const unsigned short port, const std::string &instance)
Start announcing the registered key/value pairs.
const std::string & getHost(const std::string &instance) const
Servus(const std::string &name)
Create a new service handle.
void set(const std::string &key, const std::string &value)
Set a key/value pair to be announced.
A listener interface which can be implemented to listen on changes of a Servus instance.
Definition: listener.h:32
std::string getString() const override
void withdraw()
Stop announcing the registered key/value pairs.
Strings getInstances() const
bool containsKey(const std::string &instance, const std::string &key) const
static const int32_t NOT_SUPPORTED
Servus compiled without ZeroConf support.
Definition: servus.h:69
const std::string & getName() const
use all interfaces
Definition: servus.h:48