Dripline-Cpp  v2.4.2
Dripline Implementation in C++
message.hh
Go to the documentation of this file.
1 /*
2  * mt_message.hh
3  *
4  * Created on: Jul 9, 2015
5  * Author: N.S. Oblath
6  */
7 
8 #ifndef DRIPLINE_MESSAGE_HH_
9 #define DRIPLINE_MESSAGE_HH_
10 
11 #include "member_variables.hh"
12 #include "param.hh"
13 
14 #include "amqp.hh"
15 #include "dripline_api.hh"
16 #include "dripline_constants.hh"
17 #include "dripline_fwd.hh"
18 #include "return_codes.hh"
19 #include "specifier.hh"
20 #include "uuid.hh"
21 
22 #include <memory>
23 #include <tuple>
24 #include <string>
25 
26 namespace scarab
27 {
28  class version_semantic;
29 }
30 
31 namespace dripline
32 {
33  class dripline_error;
34  struct return_code;
35 
36  //***********
37  // Message
38  //***********
39 
54  {
55  public:
56  enum class encoding
57  {
58  json
59  };
60 
61  public:
62  message();
63  virtual ~message();
64 
65  virtual bool is_request() const = 0;
66  virtual bool is_reply() const = 0;
67  virtual bool is_alert() const = 0;
68 
69  public:
71  static std::tuple< std::string, unsigned, unsigned > parse_message_id( const std::string& a_message_id );
72 
74  static message_ptr_t process_message( amqp_split_message_ptrs a_message_ptrs, const std::string& a_routing_key );
75 
77  amqp_split_message_ptrs create_amqp_messages( unsigned a_max_size = DL_MAX_PAYLOAD_SIZE );
78 
80  void encode_message_body( std::vector< std::string >& a_body_vec, unsigned a_max_size, const scarab::param_node& a_options = scarab::param_node() ) const;
81 
83  std::string encode_full_message( unsigned a_max_size, const scarab::param_node& a_options = scarab::param_node() ) const;
84 
85  protected:
86  virtual void derived_modify_amqp_message( amqp_message_ptr a_amqp_msg, AmqpClient::Table& a_properties ) const = 0;
87 
88  virtual void derived_modify_message_param( scarab::param_node& a_message_node ) const = 0;
89 
90  std::string interpret_encoding() const;
91 
92  public:
94  mv_accessible( bool, is_valid );
95 
96  mv_referrable( std::string, routing_key );
97  mv_referrable( std::string, correlation_id );
98  mv_referrable( std::string, message_id );
99  mv_referrable( std::string, reply_to );
100  mv_accessible( encoding, encoding );
101  mv_referrable( std::string, timestamp );
102 
103  mv_referrable( std::string, sender_exe );
104  mv_referrable( std::string, sender_hostname );
105  mv_referrable( std::string, sender_username );
106  mv_referrable( std::string, sender_service_name );
107 
109  {
110  std::string f_version;
111  std::string f_commit;
112  std::string f_package;
114  sender_package_version( const scarab::version_semantic& a_version );
115  sender_package_version( const std::string& a_version, const std::string& a_commit, const std::string& a_package );
116  bool operator==( const sender_package_version& a_rhs ) const;
117  };
118  typedef std::map< std::string, sender_package_version > sender_version_map_t;
119  mv_referrable( sender_version_map_t, sender_versions );
120 
121  protected:
123 
124  public:
125  specifier& parsed_specifier();
126  const specifier& parsed_specifier() const;
127 
128  virtual msg_t message_type() const = 0;
129 
131  scarab::param_node get_sender_info() const;
133  void set_sender_info( const scarab::param_node& a_sender_info );
134 
136  scarab::param_node get_message_param( bool a_include_payload = true ) const;
137 
138  public:
139  scarab::param& payload();
140  const scarab::param& payload() const;
141 
142  void set_payload( scarab::param_ptr_t a_payload );
143  const scarab::param_ptr_t& get_payload_ptr() const;
144 
145  private:
146  scarab::param_ptr_t f_payload;
147 
148  public:
149  static const char s_message_id_separator = '/';
150 
151  };
152 
153  DRIPLINE_API bool operator==( const message& a_lhs, const message& a_rhs );
154 
155  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, message::encoding a_enc );
156 
157  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const message& a_message );
158 
159  //***********
160  // Request
161  //***********
162 
175  {
176  public:
177  msg_request();
178  virtual ~msg_request();
179 
181  static request_ptr_t create( scarab::param_ptr_t a_payload, op_t a_msg_op, const std::string& a_routing_key, const std::string& a_specifier = "", const std::string& a_reply_to = "", message::encoding a_encoding = encoding::json );
182 
183  bool is_request() const;
184  bool is_reply() const;
185  bool is_alert() const;
186 
188  reply_ptr_t reply( const return_code& a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload = scarab::param_ptr_t( new scarab::param() ) ) const;
190  reply_ptr_t reply( const unsigned a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload = scarab::param_ptr_t( new scarab::param() ) ) const;
191 
192  private:
193  void derived_modify_amqp_message( amqp_message_ptr a_amqp_msg, AmqpClient::Table& a_properties ) const;
194  virtual void derived_modify_message_param( scarab::param_node& a_message_node ) const;
195 
196  public:
197  virtual msg_t message_type() const;
198  mv_accessible_static_noset( msg_t, message_type );
199 
200  mv_referrable( uuid_t, lockout_key );
201  mv_accessible( bool, lockout_key_valid );
202  mv_accessible( op_t, message_operation );
203 
204  };
205 
206  DRIPLINE_API bool operator==( const msg_request& a_lhs, const msg_request& a_rhs );
207 
208  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const msg_request& a_message );
209 
210 
211  //*********
212  // Reply
213  //*********
214 
230  {
231  public:
232  msg_reply();
233  virtual ~msg_reply();
234 
236  static reply_ptr_t create( const return_code& a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const std::string& a_routing_key, const std::string& a_specifier = "", message::encoding a_encoding = encoding::json );
238  static reply_ptr_t create( unsigned a_return_code_value, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const std::string& a_routing_key, const std::string& a_specifier = "", message::encoding a_encoding = encoding::json );
240  static reply_ptr_t create( const return_code& a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const msg_request& a_request );
242  static reply_ptr_t create( unsigned a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const msg_request& a_request );
243 
244  bool is_request() const;
245  bool is_reply() const;
246  bool is_alert() const;
247 
248  private:
249  void derived_modify_amqp_message( amqp_message_ptr a_amqp_msg, AmqpClient::Table& a_properties ) const;
250  virtual void derived_modify_message_param( scarab::param_node& a_message_node ) const;
251 
252  public:
253  virtual msg_t message_type() const;
254  mv_accessible_static_noset( msg_t, message_type );
255 
256  mv_accessible( unsigned, return_code );
257  mv_referrable( std::string, return_message );
258 
259  private:
260  mutable std::string f_return_buffer;
261 
262  };
263 
264  DRIPLINE_API bool operator==( const msg_reply& a_lhs, const msg_reply& a_rhs );
265 
266  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const msg_reply& a_message );
267 
268 
269  //*********
270  // Alert
271  //*********
272 
285  {
286  public:
287  msg_alert();
288  virtual ~msg_alert();
289 
291  static alert_ptr_t create( scarab::param_ptr_t a_payload, const std::string& a_routing_key, const std::string& a_specifier = "", message::encoding a_encoding = encoding::json );
292 
293  bool is_request() const;
294  bool is_reply() const;
295  bool is_alert() const;
296 
297  private:
298  void derived_modify_amqp_message( amqp_message_ptr a_amqp_msg, AmqpClient::Table& a_properties ) const;
299  virtual void derived_modify_message_param( scarab::param_node& a_message_node ) const;
300 
301  public:
302  virtual msg_t message_type() const;
303  mv_accessible_static_noset( msg_t, message_type );
304 
305  };
306 
307  DRIPLINE_API bool operator==( const msg_alert& a_lhs, const msg_alert& a_rhs );
308 
309  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const msg_alert& a_message );
310 
311 
312  //***********
313  // Message
314  //***********
315 
316  inline specifier& message::parsed_specifier()
317  {
318  return f_specifier;
319  }
320 
321  inline const specifier& message::parsed_specifier() const
322  {
323  return f_specifier;
324  }
325 
326  inline scarab::param& message::payload()
327  {
328  return *f_payload;
329  }
330 
331  inline const scarab::param& message::payload() const
332  {
333  return *f_payload;
334  }
335 
336  inline void message::set_payload( scarab::param_ptr_t a_payload )
337  {
338  f_payload = std::move(a_payload);
339  }
340 
341  inline const scarab::param_ptr_t& message::get_payload_ptr() const
342  {
343  return f_payload;
344  }
345 
346 
347  //***********
348  // Request
349  //***********
350 
351  inline bool msg_request::is_request() const
352  {
353  return true;
354  }
355  inline bool msg_request::is_reply() const
356  {
357  return false;
358  }
359  inline bool msg_request::is_alert() const
360  {
361  return false;
362  }
363 
364  inline void msg_request::derived_modify_amqp_message( amqp_message_ptr /*a_amqp_msg*/, AmqpClient::Table& a_properties ) const
365  {
366  a_properties.insert( AmqpClient::TableEntry( "message_operation", AmqpClient::TableValue(to_uint(f_message_operation)) ) );
367  a_properties.insert( AmqpClient::TableEntry( "lockout_key", AmqpClient::TableValue(string_from_uuid(lockout_key())) ) );
368  return;
369  }
370 
371  inline void msg_request::derived_modify_message_param( scarab::param_node& a_message_node ) const
372  {
373  a_message_node.add( "message_operation", to_uint(f_message_operation) );
374  a_message_node.add( "lockout_key", string_from_uuid(lockout_key()) );
375  return;
376  }
377 
378  inline reply_ptr_t msg_request::reply( const return_code& a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload ) const
379  {
380  return msg_reply::create( a_return_code, a_ret_msg, std::move(a_payload), *this );
381  }
382 
383  inline reply_ptr_t msg_request::reply( unsigned a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload ) const
384  {
385  return msg_reply::create( a_return_code, a_ret_msg, std::move(a_payload), *this );
386  }
387 
388 
389  //*********
390  // Reply
391  //*********
392 
393  inline reply_ptr_t msg_reply::create( const return_code& a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const msg_request& a_request )
394  {
395  return msg_reply::create( a_return_code.rc_value(), a_ret_msg, std::move(a_payload), a_request );
396  }
397 
398  inline reply_ptr_t msg_reply::create( unsigned a_return_code, const std::string& a_ret_msg, scarab::param_ptr_t a_payload, const msg_request& a_request )
399  {
400  reply_ptr_t t_reply = msg_reply::create( a_return_code, a_ret_msg, std::move(a_payload), a_request.reply_to(), "", a_request.get_encoding() );
401  t_reply->correlation_id() = a_request.correlation_id();
402  return t_reply;
403  }
404 
405  inline bool msg_reply::is_request() const
406  {
407  return false;
408  }
409  inline bool msg_reply::is_reply() const
410  {
411  return true;
412  }
413  inline bool msg_reply::is_alert() const
414  {
415  return false;
416  }
417 
418  inline void msg_reply::derived_modify_amqp_message( amqp_message_ptr, AmqpClient::Table& a_properties ) const
419  {
420  a_properties.insert( AmqpClient::TableEntry( "return_code", AmqpClient::TableValue(f_return_code) ) );
421  a_properties.insert( AmqpClient::TableEntry( "return_message", AmqpClient::TableValue(f_return_message) ) );
422  return;
423  }
424 
425  inline void msg_reply::derived_modify_message_param( scarab::param_node& a_message_node ) const
426  {
427  a_message_node.add( "return_code", f_return_code );
428  a_message_node.add( "return_message", f_return_message );
429  return;
430  }
431 
432 
433  //*********
434  // Alert
435  //*********
436 
437  inline bool msg_alert::is_request() const
438  {
439  return false;
440  }
441  inline bool msg_alert::is_reply() const
442  {
443  return false;
444  }
445  inline bool msg_alert::is_alert() const
446  {
447  return true;
448  }
449 
450  inline void msg_alert::derived_modify_amqp_message( amqp_message_ptr, AmqpClient::Table& ) const
451  {
452  return;
453  }
454 
455  inline void msg_alert::derived_modify_message_param( scarab::param_node& ) const
456  {
457  return;
458  }
459 
460 } /* namespace dripline */
461 
462 #endif /* DRIPLINE_MESSAGE_HH_ */
Base class for return codes.
Definition: return_codes.hh:39
std::map< std::string, sender_package_version > sender_version_map_t
Definition: message.hh:118
AmqpClient::BasicMessage::ptr_t amqp_message_ptr
Definition: amqp.hh:26
Parses routing keys and stores the tokenized information.
Definition: specifier.hh:27
Request message class.
Definition: message.hh:174
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
std::shared_ptr< msg_alert > alert_ptr_t
Definition: dripline_fwd.hh:25
virtual unsigned rc_value() const =0
std::ostream & operator<<(std::ostream &a_os, op_t an_op)
Pass the integer-equivalent of a message-operation enum to an ostream.
Contains all of the information common to all types of Dripline messages.
Definition: message.hh:53
Definition: core.hh:17
std::string f_return_buffer
Definition: message.hh:260
#define DRIPLINE_API
Definition: dripline_api.hh:34
std::vector< amqp_message_ptr > amqp_split_message_ptrs
Definition: amqp.hh:31
std::string string_from_uuid(const uuid_t &a_id)
Generates a string representation of the provided UUID.
Definition: uuid.cc:92
boost::uuids::uuid uuid_t
Universally-unique-identifier type containing 16 hexadecimal characters.
Definition: uuid.hh:26
Alert message class.
Definition: message.hh:284
scarab::param_ptr_t f_payload
Definition: message.hh:146
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
uint32_t to_uint(op_t an_op)
Convert a message-operation enum to an integer.
Parses specifiers and stores the tokenized information.
Definition: specifier.hh:55
bool operator==(const message &a_lhs, const message &a_rhs)
Definition: message.cc:586
std::shared_ptr< message > message_ptr_t
Definition: dripline_fwd.hh:20
Reply message class.
Definition: message.hh:229
#define DL_MAX_PAYLOAD_SIZE
specifier f_specifier
Definition: message.hh:122