Servus  1.1.0
A small set of network oriented utilities in C++ including a zeroconf implementation.
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
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 
82  SERVUS_API explicit Servus( const std::string& name );
83 
85  SERVUS_API virtual ~Servus();
86 
88  SERVUS_API const std::string& getName() const;
89 
100  SERVUS_API void set( const std::string& key, const std::string& value );
101 
103  SERVUS_API Strings getKeys() const;
104 
106  SERVUS_API const std::string& get( const std::string& key ) const;
107 
115  SERVUS_API Result announce( const unsigned short port,
116  const std::string& instance );
117 
119  SERVUS_API void withdraw();
120 
122  SERVUS_API bool isAnnounced() const;
123 
133  SERVUS_API Strings discover( const Interface addr,
134  const unsigned browseTime );
135 
142  SERVUS_API Result beginBrowsing( const servus::Servus::Interface addr );
143 
150  SERVUS_API Result browse( int32_t timeout = -1 );
151 
153  SERVUS_API void endBrowsing();
154 
156  SERVUS_API bool isBrowsing() const;
157 
159  SERVUS_API Strings getInstances() const;
160 
162  SERVUS_API Strings getKeys( const std::string& instance ) const;
163 
165  SERVUS_API bool containsKey( const std::string& instance,
166  const std::string& key ) const;
167 
169  SERVUS_API const std::string& get( const std::string& instance,
170  const std::string& key ) const;
171 
173  typedef std::map< std::string, std::map< std::string, std::string > > Data;
174 
176  SERVUS_API void getData( Data& data );
177 
178 private:
179  Servus( const Servus& );
180  Servus& operator=( const Servus& );
181  detail::Servus* const _impl;
182  friend SERVUS_API std::ostream& operator << ( std::ostream&,
183  const Servus& );
184 };
185 
187 SERVUS_API std::string getHostname();
188 
190 SERVUS_API std::ostream& operator << ( std::ostream&, const Servus& );
191 
193 SERVUS_API std::ostream& operator << (std::ostream&,const Servus::Interface&);
194 }
195 
196 #endif // SERVUS_SERVUS_H
SERVUS_API void withdraw()
Stop announcing the registered key/value pairs.
Defines export visibility macros for 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
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
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
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