ZeroEQ  0.6.0
ZeroEQ - Zero Event Queue
server.h
1 
2 /* Copyright (c) 2016, Human Brain Project
3  * Stefan.Eilemann@epfl.ch
4  */
5 
6 #ifndef ZEROEQ_HTTP_SERVER_H
7 #define ZEROEQ_HTTP_SERVER_H
8 
9 #include <zeroeq/receiver.h> // base class
10 #include <zeroeq/log.h>
11 
12 namespace zeroeq
13 {
15 namespace http
16 {
31 class Server : public zeroeq::Receiver
32 {
33 public:
48  ZEROEQ_API Server( const URI& uri, Receiver& shared );
49  ZEROEQ_API explicit Server( const URI& uri );
50  ZEROEQ_API explicit Server( Receiver& shared );
51  ZEROEQ_API explicit Server();
52  ZEROEQ_API explicit Server( Server& shared )
53  : Server( static_cast< Receiver& >( shared )) {}
54  ZEROEQ_API virtual ~Server();
55 
65  ZEROEQ_API static std::unique_ptr< Server > parse( int argc,
66  const char* const* argv);
67  ZEROEQ_API static std::unique_ptr< Server > parse( int argc,
68  const char* const* argv,
69  Receiver& shared );
78  ZEROEQ_API const URI& getURI() const;
79 
89  ZEROEQ_API SocketDescriptor getSocketDescriptor() const;
91 
95  bool add( servus::Serializable& object )
96  { return subscribe( object ) && register_( object );}
97 
99  bool remove( const servus::Serializable& object )
100  { return unsubscribe( object ) && unregister( object );}
101 
115  ZEROEQ_API bool subscribe( servus::Serializable& object );
116 
126  ZEROEQ_API bool subscribe( const std::string& event, const PUTFunc& func );
127 
137  ZEROEQ_API bool subscribe( const std::string& event,
138  const PUTPayloadFunc& func );
139 
141  ZEROEQ_API bool unsubscribe( const servus::Serializable& object );
142 
144  ZEROEQ_API bool unsubscribe( const std::string& event );
145 
158  ZEROEQ_API bool register_( servus::Serializable& object );
159 
170  ZEROEQ_API bool register_( const std::string& event, const GETFunc& func );
171 
173  ZEROEQ_API bool unregister( const servus::Serializable& object );
174 
176  ZEROEQ_API bool unregister( const std::string& event );
178 
179 private:
180  class Impl;
181  std::unique_ptr< Impl > _impl;
182 
183  // Receiver API
184  void addSockets( std::vector< detail::Socket >& entries ) final;
185  void process( detail::Socket& socket, uint32_t timeout ) final;
186  void addConnection( const std::string& ) final { ZEROEQDONTCALL; } // LCOV_EXCL_LINE
187 };
188 }
189 }
190 
191 #endif
Enhances servus::URI to guarantee the existance of a schema and to allow construction of [host][:port...
Definition: uri.h:20
static std::unique_ptr< Server > parse(int argc, const char *const *argv)
Create a new Server when requested.
bool subscribe(servus::Serializable &object)
Subscribe a serializable object to receive updates from HTTP PUT requests.
bool add(servus::Serializable &object)
Subscribe and register the given object.
Definition: server.h:95
bool register_(servus::Serializable &object)
Subscribe a serializable object to serve HTTP GET requests.
SocketDescriptor getSocketDescriptor() const
Get the underlying socket descriptor.
std::function< bool() > PUTFunc
HTTP PUT callback w/o payload, return reply success.
Definition: types.h:52
const URI & getURI() const
Get the publisher URI.
std::function< std::string() > GETFunc
HTTP GET callback to return JSON reply.
Definition: types.h:58
bool unregister(const servus::Serializable &object)
Unsubscribe the given object for GET requests.
Serves HTTP GET and PUT requests for servus::Serializable objects.
Definition: server.h:31
std::function< bool(const std::string &) > PUTPayloadFunc
HTTP PUT callback w/ JSON payload, return reply success.
Definition: types.h:55
Base class for entities receiving data.
Definition: receiver.h:33
Publish-subscribe classes for typed events.
Definition: broker.h:12
bool unsubscribe(const servus::Serializable &object)
Unsubscribe the given object to stop applying updates.