runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
RocServer.h
Go to the documentation of this file.
1 #ifndef ROC_SERVER_H
2 #define ROC_SERVER_H
3 
4 #include <zmq.hpp>
5 #include <json/json.h>
6 
7 #include <string>
8 #include <list>
9 #include <thread>
10 #include <exception>
11 
12 #define ROC_JSON_PARSE_ERROR (-32700)
13 #define ROC_JSON_INVALID_REQUEST (-32600)
14 #define ROC_JSON_METHOD_NOT_FOUND (-32601)
15 #define ROC_JSON_INVALID_PARAMS (-32602)
16 #define ROC_JSON_INTERNAL_ERROR (-32603)
17 
18 class RocNotImplementedError: public std::exception {
19 };
20 
21 class RocIsCallable: public std::exception {
22 };
23 
24 class RocServer {
25 public:
26  RocServer(zmq::context_t & _ctx,
27  const std::string & _brokerEndpoint,
28  const std::string & _serviceName);
29  virtual ~RocServer();
30 protected:
33  virtual void startup();
36  virtual void teardown();
42  virtual void onRequest(std::list<zmq::message_t*>* envelope, std::list<zmq::message_t*>* data);
53  virtual Json::Value call(const std::string & name, const Json::Value & arguments);
63  virtual Json::Value getattr(const std::string & name);
72  virtual void setattr(const std::string & name, const Json::Value & value);
73  void reply(std::list<zmq::message_t*>* envelope, std::list<zmq::message_t*>* data = nullptr);
74  void replyValue(std::list<zmq::message_t*>* envelope, const Json::Value & value, const std::string & id = "");
75  void replyError(std::list<zmq::message_t*>* envelope, int errorNo, const std::string & id = "");
76 
77  zmq::context_t & ctx;
78 private:
79  void serve();
80 
81  void send(char _command, const std::string & _option = "");
89  void _onRequest();
99  Json::Value rocCall(const std::string & name, const Json::Value & arguments);
100 
101  zmq::socket_t * sock;
102  std::string brokerEndpoint;
103  std::string serviceName;
104  std::thread serverThread;
105 
106  long timeout;
107  volatile bool keepGoing;
108  int liveness;
109 };
110 
111 #endif /* ROC_SERVER_H */
virtual void onRequest(std::list< zmq::message_t * > *envelope, std::list< zmq::message_t * > *data)
The (not anymore) "to-be-overloaded" request handler.
Definition: RocServer.cpp:87
virtual ~RocServer()
Definition: RocServer.cpp:32
zmq::context_t & ctx
Definition: RocServer.h:77
RocServer(zmq::context_t &_ctx, const std::string &_brokerEndpoint, const std::string &_serviceName)
Definition: RocServer.cpp:20
virtual Json::Value call(const std::string &name, const Json::Value &arguments)
The "to-be-overloaded" call handler.
Definition: RocServer.cpp:49
void replyValue(std::list< zmq::message_t * > *envelope, const Json::Value &value, const std::string &id="")
Definition: RocServer.cpp:288
void reply(std::list< zmq::message_t * > *envelope, std::list< zmq::message_t * > *data=nullptr)
Definition: RocServer.cpp:253
virtual void setattr(const std::string &name, const Json::Value &value)
The "to-be-overloaded" setattr handler.
Definition: RocServer.cpp:60
virtual void teardown()
called in handler thread on shutdown
Definition: RocServer.cpp:45
void replyError(std::list< zmq::message_t * > *envelope, int errorNo, const std::string &id="")
Definition: RocServer.cpp:302
virtual Json::Value getattr(const std::string &name)
The "to-be-overloaded" getattr handler.
Definition: RocServer.cpp:55
virtual void startup()
called in handler thread on startup
Definition: RocServer.cpp:41