Dripline-Cpp  v2.10.11
Dripline Implementation in C++
core.hh
Go to the documentation of this file.
1 /*
2  * core.hh
3  *
4  * Created on: Jul 13, 2015
5  * Author: N.S. Oblath
6  */
7 
8 #ifndef DRIPLINE_CORE_HH_
9 #define DRIPLINE_CORE_HH_
10 
11 #include "dripline_config.hh"
12 #include "message.hh"
13 
14 #include <map>
15 #include <mutex>
16 #include <thread>
17 
18 namespace scarab
19 {
20  class authentication;
21  class param_node;
22 }
23 
24 namespace dripline
25 {
40  {
41  std::mutex f_mutex;
43  std::string f_consumer_tag;
45  std::string f_send_error_message;
46  ~sent_msg_pkg();
47  };
48 
75  {
76  public:
77  static bool s_offline;
78 
79  enum class post_listen_status
80  {
81  unknown,
82  message_received,
83  timeout,
84  soft_error,
85  hard_error
86  };
87 
88  public:
89  /*
90  \brief Extracts necessary configuration and authentication information and prepares the DL object to interact with the RabbitMQ broker. Does not initiate connection to the broker.
91  @param a_config Dripline configuration object. Contents can be:
92  - `broker` (string; default: localhost) -- Address of the RabbitMQ broker
93  - `broker_port` (int; default: 5672) -- Port used by the RabbitMQ broker
94  - `requests_exchange` (string; default: requests) -- Name of the exchange used for DL requests
95  - `alerts_exchange` (string; default: alerts) -- Name of the exchange used for DL alerts
96  - `heartbeat_routing_key` (string; default: heartbeat) -- Routing key used for sending heartbeats
97  - `make_connection` (bool; default: true) -- Flag for performing a dry run -- no connection to a broker is made; this parameter overrides the parameter in the constructor and is the preferred flag to use.
98  - `max_payload_size` (int; default: DL_MAX_PAYLOAD_SIZE) -- Maximum size of payloads, in bytes
99  - `max_connection_attempts` (int; default: 10) -- Maximum number of attempts that will be made to connect to the broker
100  - `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>} <, ...>]`.
101  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
102  @param a_auth Authentication object (type scarab::authentication); authentication specification should be processed, and the authentication data should include:
103  @param a_make_connection Flag for whether or not to contact a broker; if true, this object operates in "dry-run" mode
104  */
105  core( const scarab::param_node& a_config = dripline_config(), const scarab::authentication& a_auth = scarab::authentication(), const bool a_make_connection = true );
106  core( const core& a_orig ) = default;
107  core( core&& a_orig ) = default;
108  virtual ~core() = default;
109 
110  core& operator=( const core& a_orig ) = default;
111  core& operator=( core&& a_orig ) = default;
112 
113  public:
117  virtual sent_msg_pkg_ptr send( request_ptr_t a_request, amqp_channel_ptr a_channel = amqp_channel_ptr() ) const;
118 
122  virtual sent_msg_pkg_ptr send( reply_ptr_t a_reply, amqp_channel_ptr a_channel = amqp_channel_ptr() ) const;
123 
127  virtual sent_msg_pkg_ptr send( alert_ptr_t a_alert, amqp_channel_ptr a_channel = amqp_channel_ptr() ) const;
128 
129  mv_referrable( std::string, address );
130  mv_accessible( unsigned, port );
131  mv_referrable( std::string, username );
132  mv_referrable( std::string, password );
133 
134  mv_referrable( std::string, requests_exchange );
135  mv_referrable( std::string, alerts_exchange );
136 
137  mv_referrable( std::string, heartbeat_routing_key );
138 
139  mv_accessible( unsigned, max_payload_size );
140 
141  mv_accessible( bool, make_connection );
142  mv_accessible( unsigned, max_connection_attempts );
143 
144  protected:
145  friend class receiver;
146 
147  sent_msg_pkg_ptr do_send( message_ptr_t a_message, const std::string& a_exchange, bool a_expect_reply, amqp_channel_ptr a_channel = amqp_channel_ptr() ) const;
148 
149  amqp_channel_ptr send_withreply( message_ptr_t a_message, std::string& a_reply_consumer_tag, const std::string& a_exchange ) const;
150 
151  bool send_noreply( message_ptr_t a_message, const std::string& a_exchange ) const;
152 
153  amqp_channel_ptr open_channel() const;
154 
155  static bool setup_exchange( amqp_channel_ptr a_channel, const std::string& a_exchange );
156 
157  static bool setup_queue( amqp_channel_ptr a_channel, const std::string& a_queue_name );
158 
159  static bool bind_key( amqp_channel_ptr a_channel, const std::string& a_exchange, const std::string& a_queue_name, const std::string& a_routing_key );
160 
161  static std::string start_consuming( amqp_channel_ptr a_channel, const std::string& a_queue_name );
162 
163  static bool stop_consuming( amqp_channel_ptr a_channel, std::string& a_consumer_tag );
164 
165  static bool remove_queue( amqp_channel_ptr a_channel, const std::string& a_queue_name );
166 
167  public:
169  static void listen_for_message( amqp_envelope_ptr& a_envelope, post_listen_status& a_status, amqp_channel_ptr a_channel, const std::string& a_consumer_tag, int a_timeout_ms = 0, bool a_do_ack = true );
170  };
171 
172 } /* namespace dripline */
173 
174 #endif /* DRIPLINE_CORE_HH_ */
Basic AMQP interactions, including sending messages and interacting with AMQP channels.
Definition: core.hh:75
core(const core &a_orig)=default
static bool s_offline
Definition: core.hh:77
core(core &&a_orig)=default
post_listen_status
Definition: core.hh:80
virtual ~core()=default
core & operator=(const core &a_orig)=default
bool send_noreply(message_ptr_t a_message, const std::string &a_exchange) const
amqp_channel_ptr send_withreply(message_ptr_t a_message, std::string &a_reply_consumer_tag, const std::string &a_exchange) const
core & operator=(core &&a_orig)=default
Sets the default configuration used by core. These parameters pertain to the dripline mesh that will ...
A receiver is able to collect Dripline message chunks and reassemble them into a complete Dripline me...
Definition: receiver.hh:78
#define DRIPLINE_API
Definition: dripline_api.hh:34
AmqpClient::Channel::ptr_t amqp_channel_ptr
Definition: amqp.hh:24
std::shared_ptr< msg_alert > alert_ptr_t
Definition: dripline_fwd.hh:25
std::shared_ptr< message > message_ptr_t
Definition: dripline_fwd.hh:20
std::shared_ptr< sent_msg_pkg > sent_msg_pkg_ptr
Definition: dripline_fwd.hh:27
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
AmqpClient::Envelope::ptr_t amqp_envelope_ptr
Definition: amqp.hh:25
Definition: agent.hh:18
Contains information about sent messages.
Definition: core.hh:40
std::mutex f_mutex
Definition: core.hh:41
bool f_successful_send
Definition: core.hh:44
amqp_channel_ptr f_channel
Definition: core.hh:42
std::string f_consumer_tag
Definition: core.hh:43
std::string f_send_error_message
Definition: core.hh:45