ZeroEQ  0.9.0
ZeroEQ - Zero Event Queue
http/server.h
1 
2 /* Copyright (c) 2016-2017, Human Brain Project
3  * Stefan.Eilemann@epfl.ch
4  * Raphael.Dumusc@epfl.ch
5  */
6 
7 #ifndef ZEROEQ_HTTP_SERVER_H
8 #define ZEROEQ_HTTP_SERVER_H
9 
10 #include <zeroeq/http/api.h>
11 #include <zeroeq/http/request.h>
12 #include <zeroeq/http/types.h>
13 
14 #include <zeroeq/log.h>
15 #include <zeroeq/receiver.h> // base class
16 
17 namespace zeroeq
18 {
19 namespace http
20 {
38 class Server : public zeroeq::Receiver
39 {
40 public:
56  ZEROEQHTTP_API Server(const URI& uri, Receiver& shared);
57  ZEROEQHTTP_API explicit Server(const URI& uri);
58  ZEROEQHTTP_API explicit Server(Receiver& shared);
59  explicit Server(Server& shared)
60  : Server(static_cast<Receiver&>(shared))
61  {
62  }
63  ZEROEQHTTP_API Server();
64 
66  ZEROEQHTTP_API ~Server();
67 
77  ZEROEQHTTP_API
78  static std::unique_ptr<Server> parse(int argc, const char* const* argv);
79  ZEROEQHTTP_API
80  static std::unique_ptr<Server> parse(int argc, const char* const* argv,
81  Receiver& shared);
90  ZEROEQHTTP_API const URI& getURI() const;
91 
103  ZEROEQHTTP_API SocketDescriptor getSocketDescriptor() const;
105 
115  bool handle(Method method, const std::string& endpoint, RESTFunc func);
116 
125  bool handle(servus::Serializable& object)
126  {
127  return handlePUT(object) && handleGET(object);
128  }
129 
136  ZEROEQHTTP_API bool handle(const std::string& endpoint,
137  servus::Serializable& object);
138 
140  ZEROEQHTTP_API bool remove(const servus::Serializable& object);
141 
143  ZEROEQHTTP_API bool remove(const std::string& endpoint);
144 
158  ZEROEQHTTP_API bool handlePUT(servus::Serializable& object);
159 
166  ZEROEQHTTP_API bool handlePUT(const std::string& endpoint,
167  servus::Serializable& object);
168 
179  ZEROEQHTTP_API
180  bool handlePUT(const std::string& endpoint, const PUTFunc& func);
181 
188  ZEROEQHTTP_API bool handlePUT(const std::string& endpoint,
189  const std::string& schema,
190  const PUTFunc& func);
191 
202  ZEROEQHTTP_API bool handlePUT(const std::string& endpoint,
203  const PUTPayloadFunc& func);
204 
211  ZEROEQHTTP_API bool handlePUT(const std::string& endpoint,
212  const std::string& schema,
213  const PUTPayloadFunc& func);
226  ZEROEQHTTP_API bool handleGET(const servus::Serializable& object);
227 
234  ZEROEQHTTP_API bool handleGET(const std::string& endpoint,
235  const servus::Serializable& object);
236 
247  ZEROEQHTTP_API
248  bool handleGET(const std::string& endpoint, const GETFunc& func);
249 
256  ZEROEQHTTP_API
257  bool handleGET(const std::string& endpoint, const std::string& schema,
258  const GETFunc& func);
259 
264  ZEROEQHTTP_API
265  std::string getSchema(const servus::Serializable& object) const;
266 
268  ZEROEQHTTP_API std::string getSchema(const std::string& endpoint) const;
270 
271 protected:
280  virtual std::future<Response> respondTo(Request& request) const;
281 
282 private:
283  class Impl;
284  std::unique_ptr<Impl> _impl;
285 
286  // Receiver API
287  void addSockets(std::vector<detail::Socket>& entries) final;
288  bool process(detail::Socket& socket) final;
289 };
290 }
291 }
292 
293 #endif
Enhances servus::URI to guarantee the existance of a schema and to allow construction of [host][:port...
Definition: uri.h:19
ZEROEQHTTP_API bool handlePUT(servus::Serializable &object)
Subscribe a serializable object to receive updates from HTTP PUT requests.
ZEROEQHTTP_API const URI & getURI() const
Get the publisher URI.
std::function< bool()> PUTFunc
HTTP PUT callback w/o payload, return reply success.
Definition: http/types.h:27
std::function< std::string()> GETFunc
HTTP GET callback to return JSON reply.
Definition: http/types.h:33
static ZEROEQHTTP_API std::unique_ptr< Server > parse(int argc, const char *const *argv)
Create a new Server when requested.
Method
HTTP method used in a Request.
Definition: request.h:17
std::function< bool(const std::string &)> PUTPayloadFunc
HTTP PUT callback w/ JSON payload, return reply success.
Definition: http/types.h:30
ZEROEQHTTP_API std::string getSchema(const servus::Serializable &object) const
bool handle(servus::Serializable &object)
Handle PUT and GET for the given object.
Definition: http/server.h:125
std::function< std::future< Response >(const Request &)> RESTFunc
HTTP REST callback with Request parameter returning a Response future.
Definition: http/types.h:36
HTTP Request with method, path and body.
Definition: request.h:50
ZEROEQHTTP_API bool handleGET(const servus::Serializable &object)
Subscribe a serializable object to serve HTTP GET requests.
Serves HTTP GET and PUT requests for servus::Serializable objects.
Definition: http/server.h:38
virtual std::future< Response > respondTo(Request &request) const
Respond to a request.
bool handle(Method method, const std::string &endpoint, RESTFunc func)
Handle a single method on a given endpoint.
Base class for entities receiving data.
Definition: receiver.h:38
Publish-subscribe and request-reply.
Definition: client.h:10
ZEROEQHTTP_API ~Server()
Destruct this http server.
Defines export visibility macros for library ZeroEQHTTP.
ZEROEQHTTP_API SocketDescriptor getSocketDescriptor() const
Get the underlying socket descriptor.