Dripline-Cpp  v2.10.11
Dripline Implementation in C++
hub.hh
Go to the documentation of this file.
1 /*
2  * hub.hh
3  *
4  * Created on: Jan 7, 2016
5  * Author: N.S. Oblath
6  */
7 
8 #ifndef DRIPLINE_HUB_HH_
9 #define DRIPLINE_HUB_HH_
10 
11 #include "service.hh"
12 
13 #include "dripline_exceptions.hh"
14 
15 #include <unordered_map>
16 
17 namespace dripline
18 {
19 
70  class DRIPLINE_API hub : public service
71  {
72  private:
73  typedef std::function< reply_ptr_t( const dripline::request_ptr_t ) > handler_func_t;
74 
75  public:
76  /*
77  \brief Extracts necessary configuration and authentication information and prepares the hub to interact with the RabbitMQ broker. Does not initiate connection to the broker.
78  @param a_config Dripline configuration object. The `name` must be unique for each hub. The `dripline.broker` (and `dripline.broker_port` if needed) should be made appropriate for the mesh.
79  The other parameters can be left as their defaults, or should be made uniform across the mesh.
80  - *Service parameters*
81  - `name` (string; default: dlcpp_service) -- Name of the service and of the queue used by the service
82  - `enable-scheduling` (bool; default: false) -- Flag for enabling the scheduler
83  - `broadcast-key` (string; default: broadcast) -- Routing key used for broadcasts
84  - `loop-timeout-ms` (int; default: 1000) -- Maximum time used for listening timeouts (e.g. waiting for replies) in ms
85  - `message-wait-ms` (int; default: 1000) -- Maximum time used to wait for another AMQP message before declaring a DL message complete, in ms
86  - `heartbeat-interval-s` (int; default: 60) -- Interval between sending heartbeat messages in s
87  - *Dripline core parameters -- within the `dripline` config object*
88  - `dripline.broker` (string; default: localhost) -- Address of the RabbitMQ broker
89  - `dripline.broker_port` (int; default: 5672) -- Port used by the RabbitMQ broker
90  - `dripline.requests_exchange` (string; default: requests) -- Name of the exchange used for DL requests
91  - `dripline.alerts_exchange` (string; default: alerts) -- Name of the exchange used for DL alerts
92  - `dripline.heartbeat_routing_key` (string; default: heartbeat) -- Routing key used for sending heartbeats
93  - `dripline.max_payload_size` (int; default: DL_MAX_PAYLOAD_SIZE) -- Maximum size of payloads, in bytes
94  - `dripline.max_connection_attempts` (int; default: 10) -- Maximum number of attempts that will be made to connect to the broker
95  - `dripline.return_codes` (string or array of nodes; default: not present) -- Optional specification of additional return codes in the form of an array of nodes: `[{name: "<name>", value: <ret code>} <, ...>]`.
96  If this is a string, it's treated as a file can be interpreted by the param system (e.g. YAML or JSON) using the previously-mentioned format
97  @param a_auth Authentication object (type scarab::authentication); authentication specification should be processed, and the authentication data should include:
98  @param a_make_connection Flag for whether or not to contact a broker; if true, this object operates in "dry-run" mode
99  */
100  hub( const scarab::param_node& a_config, const scarab::authentication& a_auth, const bool a_make_connection = true );
101  hub( const hub& ) = delete;
102  hub( hub&& ) = default;
103  virtual ~hub() = default;
104 
105  hub& operator=( const hub& ) = delete;
106  hub& operator=( hub&& );
107 
109  void set_run_handler( const handler_func_t& a_func );
111  void register_get_handler( const std::string& a_key, const handler_func_t& a_func );
113  void register_set_handler( const std::string& a_key, const handler_func_t& a_func );
115  void register_cmd_handler( const std::string& a_key, const handler_func_t& a_func );
116 
118  void remove_get_handler( const std::string& a_key );
120  void remove_set_handler( const std::string& a_key );
122  void remove_cmd_handler( const std::string& a_key );
123 
124  private:
125  //*************************
126  // Hub request distributors
127  //*************************
128 
129  virtual reply_ptr_t do_run_request( const request_ptr_t a_request );
130  virtual reply_ptr_t do_get_request( const request_ptr_t a_request );
131  virtual reply_ptr_t do_set_request( const request_ptr_t a_request );
132  virtual reply_ptr_t do_cmd_request( const request_ptr_t a_request );
133 
135 
136  typedef std::unordered_map< std::string, handler_func_t > handler_funcs_t;
140 
141  };
142 
143 } /* namespace dripline */
144 
145 #endif /* DRIPLINE_HUB_HH_ */
Service class aimed at adding a Dripline API to an existing codebase.
Definition: hub.hh:71
virtual ~hub()=default
hub(hub &&)=default
handler_funcs_t f_set_handlers
Definition: hub.hh:138
std::function< reply_ptr_t(const dripline::request_ptr_t) > handler_func_t
Definition: hub.hh:73
std::unordered_map< std::string, handler_func_t > handler_funcs_t
Definition: hub.hh:136
handler_funcs_t f_cmd_handlers
Definition: hub.hh:139
handler_funcs_t f_get_handlers
Definition: hub.hh:137
hub(const hub &)=delete
handler_func_t f_run_handler
Definition: hub.hh:134
hub & operator=(const hub &)=delete
Primary unit of software that connects to a broker and typically provides an interface with an instru...
Definition: service.hh:85
#define DRIPLINE_API
Definition: dripline_api.hh:34
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23