Servus  1.6.0
C++ network oriented utilities including a zeroconf implementation
servus.h
1 /* Copyright (c) 2012-2017, 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 #include <memory>
29 
30 namespace servus
31 {
35 static const std::string TEST_DRIVER{"_servus._test"};
36 
47 class Servus
48 {
49 public:
50  enum Interface
51  {
52  IF_ALL = 0,
53  // (uint32_t) -1 == kDNSServiceInterfaceIndexLocalOnly
54  IF_LOCAL = (unsigned)(-1)
55  };
56 
63  class Result : public servus::Result
64  {
65  public:
66  explicit Result(const int32_t code)
67  : servus::Result(code)
68  {
69  }
70  virtual ~Result() {}
71  SERVUS_API std::string getString() const override;
72 
74  static const int32_t PENDING = -1;
76  static const int32_t NOT_SUPPORTED = -2;
78  static const int32_t POLL_ERROR = -3;
79  };
80 
82  SERVUS_API static bool isAvailable();
83 
90  SERVUS_API explicit Servus(const std::string& name);
91 
93  SERVUS_API virtual ~Servus();
94 
96  SERVUS_API const std::string& getName() const;
97 
110  SERVUS_API void set(const std::string& key, const std::string& value);
111 
113  SERVUS_API Strings getKeys() const;
114 
116  SERVUS_API const std::string& get(const std::string& key) const;
117 
126  SERVUS_API Result announce(const unsigned short port,
127  const std::string& instance);
128 
130  SERVUS_API void withdraw();
131 
133  SERVUS_API bool isAnnounced() const;
134 
145  SERVUS_API Strings discover(const Interface addr,
146  const unsigned browseTime);
147 
155  SERVUS_API Result beginBrowsing(const servus::Servus::Interface addr);
156 
164  SERVUS_API Result browse(int32_t timeout = -1);
165 
167  SERVUS_API void endBrowsing();
168 
170  SERVUS_API bool isBrowsing() const;
171 
173  SERVUS_API Strings getInstances() const;
174 
176  SERVUS_API Strings getKeys(const std::string& instance) const;
177 
179  SERVUS_API const std::string& getHost(const std::string& instance) const;
180 
182  SERVUS_API bool containsKey(const std::string& instance,
183  const std::string& key) const;
184 
186  SERVUS_API const std::string& get(const std::string& instance,
187  const std::string& key) const;
188 
195  SERVUS_API void addListener(Listener* listener);
196 
203  SERVUS_API void removeListener(Listener* listener);
204 
206  typedef std::map<std::string, std::map<std::string, std::string> > Data;
207 
209  SERVUS_API void getData(Data& data);
210 
211  class Impl;
212 
213 private:
214  Servus(const Servus&) = delete;
215  Servus& operator=(const Servus&) = delete;
216 
217  std::unique_ptr<Impl> _impl;
218 
219  friend SERVUS_API std::ostream& operator<<(std::ostream&, const Servus&);
220 };
221 
223 SERVUS_API std::string getHostname();
224 
226 SERVUS_API std::ostream& operator<<(std::ostream&, const Servus&);
227 
229 SERVUS_API std::ostream& operator<<(std::ostream&, const Servus::Interface&);
230 }
231 
232 #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:30
virtual ~Result()
Destruct the result.
Definition: servus.h:70
void removeListener(Listener *listener)
Remove a listener to stop invokation on its supported callbacks.
The ZeroConf operation result code.
Definition: servus.h:63
bool isBrowsing() const
virtual ~Servus()
Destruct this service.
only local interfaces
Definition: servus.h:54
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:74
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:47
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:78
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.
A listener interface which can be implemented to listen on changes of a Servus instance.
Definition: listener.h:31
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:76
const std::string & getName() const
use all interfaces
Definition: servus.h:52