Servus  1.0.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 #include <memory>
29 
30 namespace servus
31 {
32 namespace detail { class Servus; }
33 
44 class Servus
45 {
46 public:
47  enum Interface
48  {
49  IF_ALL = 0,
50  // (uint32_t) -1 == kDNSServiceInterfaceIndexLocalOnly
51  IF_LOCAL = (unsigned)(-1)
52  };
53 
60  class Result : public servus::Result
61  {
62  public:
63  explicit Result( const int32_t code ) : servus::Result( code ){}
64  virtual ~Result(){}
65  SERVUS_API std::string getString() const override;
66 
68  static const int32_t PENDING = -1;
70  static const int32_t NOT_SUPPORTED = -2;
72  static const int32_t POLL_ERROR = -3;
73  };
74 
76  SERVUS_API static bool isAvailable();
77 
83  SERVUS_API explicit Servus( const std::string& name );
84 
90  SERVUS_API Servus( const Servus& from );
91 
93  SERVUS_API virtual ~Servus();
94 
96  SERVUS_API Servus& operator = ( const Servus& rhs );
97 
99  SERVUS_API const std::string& getName() const;
100 
111  SERVUS_API void set( const std::string& key, const std::string& value );
112 
114  SERVUS_API Strings getKeys() const;
115 
117  SERVUS_API const std::string& get( const std::string& key ) const;
118 
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 
144  SERVUS_API Strings discover( const Interface addr,
145  const unsigned browseTime );
146 
153  SERVUS_API Result beginBrowsing( const servus::Servus::Interface addr );
154 
161  SERVUS_API Result browse( int32_t timeout = -1 );
162 
164  SERVUS_API void endBrowsing();
165 
167  SERVUS_API bool isBrowsing() const;
168 
170  SERVUS_API Strings getInstances() const;
171 
173  SERVUS_API Strings getKeys( const std::string& instance ) const;
174 
176  SERVUS_API bool containsKey( const std::string& instance,
177  const std::string& key ) const;
178 
180  SERVUS_API const std::string& get( const std::string& instance,
181  const std::string& key ) const;
182 
184  typedef std::map< std::string, std::map< std::string, std::string > > Data;
185 
187  SERVUS_API void getData( Data& data );
188 
189 private:
190  std::shared_ptr< detail::Servus > _impl;
191  friend SERVUS_API std::ostream& operator << ( std::ostream&,
192  const Servus& );
193 };
194 
196 SERVUS_API std::string getHostname();
197 
199 SERVUS_API std::ostream& operator << ( std::ostream&, const Servus& );
200 
202 SERVUS_API std::ostream& operator << (std::ostream&,const Servus::Interface&);
203 }
204 
205 #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:64
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:60
SERVUS_API Strings getInstances() const
only local interfaces
Definition: servus.h:51
SERVUS_API bool isBrowsing() const
static const int32_t PENDING
operation did not complete.
Definition: servus.h:68
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:44
SERVUS_API Strings getKeys() const
static const int32_t POLL_ERROR
Error during polling for event.
Definition: servus.h:72
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 Servus & operator=(const Servus &rhs)
Share the ZeroConf service with the rhs argument.
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:70
use all interfaces
Definition: servus.h:49
SERVUS_API bool containsKey(const std::string &instance, const std::string &key) const