Servus  1.5.1
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
32 {
33 class Servus;
34 }
35 
46 class Servus
47 {
48 public:
49  enum Interface
50  {
51  IF_ALL = 0,
52  // (uint32_t) -1 == kDNSServiceInterfaceIndexLocalOnly
53  IF_LOCAL = (unsigned)(-1)
54  };
55 
62  class Result : public servus::Result
63  {
64  public:
65  explicit Result(const int32_t code)
66  : servus::Result(code)
67  {
68  }
69  virtual ~Result() {}
70  SERVUS_API std::string getString() const override;
71 
73  static const int32_t PENDING = -1;
75  static const int32_t NOT_SUPPORTED = -2;
77  static const int32_t POLL_ERROR = -3;
78  };
79 
81  SERVUS_API static bool isAvailable();
82 
89  SERVUS_API explicit Servus(const std::string& name);
90 
92  SERVUS_API virtual ~Servus();
93 
95  SERVUS_API const std::string& getName() const;
96 
109  SERVUS_API void set(const std::string& key, const std::string& value);
110 
112  SERVUS_API Strings getKeys() const;
113 
115  SERVUS_API const std::string& get(const std::string& key) const;
116 
125  SERVUS_API Result announce(const unsigned short port,
126  const std::string& instance);
127 
129  SERVUS_API void withdraw();
130 
132  SERVUS_API bool isAnnounced() const;
133 
144  SERVUS_API Strings discover(const Interface addr,
145  const unsigned browseTime);
146 
154  SERVUS_API Result beginBrowsing(const servus::Servus::Interface addr);
155 
163  SERVUS_API Result browse(int32_t timeout = -1);
164 
166  SERVUS_API void endBrowsing();
167 
169  SERVUS_API bool isBrowsing() const;
170 
172  SERVUS_API Strings getInstances() const;
173 
175  SERVUS_API Strings getKeys(const std::string& instance) const;
176 
178  SERVUS_API const std::string& getHost(const std::string& instance) const;
179 
181  SERVUS_API bool containsKey(const std::string& instance,
182  const std::string& key) const;
183 
185  SERVUS_API const std::string& get(const std::string& instance,
186  const std::string& key) const;
187 
194  SERVUS_API void addListener(Listener* listener);
195 
202  SERVUS_API void removeListener(Listener* listener);
203 
205  typedef std::map<std::string, std::map<std::string, std::string> > Data;
206 
208  SERVUS_API void getData(Data& data);
209 
210 private:
211  Servus(const Servus&);
212  Servus& operator=(const Servus&);
213  detail::Servus* const _impl;
214  friend SERVUS_API std::ostream& operator<<(std::ostream&, const Servus&);
215 };
216 
218 SERVUS_API std::string getHostname();
219 
221 SERVUS_API std::ostream& operator<<(std::ostream&, const Servus&);
222 
224 SERVUS_API std::ostream& operator<<(std::ostream&, const Servus::Interface&);
225 }
226 
227 #endif // SERVUS_SERVUS_H
Defines export visibility macros for library Servus.
A result returns an error code and behaves like a boolean.
Definition: result.h:30
virtual ~Result()
Destruct the result.
Definition: servus.h:69
The ZeroConf operation result code.
Definition: servus.h:62
Simple wrapper for ZeroConf key/value pairs.
Definition: servus.h:46
A listener interface which can be implemented to listen on changes of a Servus instance.
Definition: listener.h:31