will_options.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file will_options.h
  3. /// Declaration of MQTT will_options class
  4. /// @date Jul 7, 2016
  5. /// @author Guilherme M. Ferreira
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
  9. * Copyright (c) 2016-2019 Frank Pagliughi <fpagliughi@mindspring.com>
  10. *
  11. * All rights reserved. This program and the accompanying materials
  12. * are made available under the terms of the Eclipse Public License v1.0
  13. * and Eclipse Distribution License v1.0 which accompany this distribution.
  14. *
  15. * The Eclipse Public License is available at
  16. * http://www.eclipse.org/legal/epl-v10.html
  17. * and the Eclipse Distribution License is available at
  18. * http://www.eclipse.org/org/documents/edl-v10.php.
  19. *
  20. * Contributors:
  21. * Guilherme M. Ferreira - initial implementation and documentation
  22. * Frank Pagliughi - added copy & move operations
  23. *******************************************************************************/
  24. #ifndef __mqtt_will_options_h
  25. #define __mqtt_will_options_h
  26. #include "MQTTAsync.h"
  27. #include "mqtt/types.h"
  28. #include "mqtt/message.h"
  29. #include "mqtt/topic.h"
  30. namespace mqtt {
  31. class connect_options;
  32. /////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * Holds the set of options that govern the Last Will and Testament feature.
  35. *
  36. * @note
  37. * This wraps struct v1 of the C library's MQTTAsync_willOptions structure.
  38. * It sets the LWT binary payload, via the 'payload' struct field, and
  39. leaves the 'message' field as a nullptr.
  40. */
  41. class will_options
  42. {
  43. /** The default QoS for the LWT, if unspecified */
  44. static constexpr int DFLT_QOS = 0;
  45. /** The defalut retained flag for LWT, if unspecified */
  46. static constexpr bool DFLT_RETAINED = false;
  47. /** A default C struct to support re-initializing variables */
  48. static const MQTTAsync_willOptions DFLT_C_STRUCT;
  49. /** The underlying C LWT options */
  50. MQTTAsync_willOptions opts_;
  51. /** LWT message topic **/
  52. string_ref topic_;
  53. /** LWT message text */
  54. binary_ref payload_;
  55. /**
  56. * The properties for the LWT message.
  57. * Strangely, in the C lib, the will properties are not in the
  58. * willOptions struct, but are rather in the connectOptions.
  59. */
  60. properties props_;
  61. /** The connect options has special access */
  62. friend class connect_options;
  63. friend class connect_options_test;
  64. friend class will_options_test;
  65. /**
  66. * Gets a pointer to the C-language NUL-terminated strings for the
  67. * struct.
  68. * Some structs, such as this one, require valid pointers at all times,
  69. * while others expect NULL pointers for missing parameters.
  70. * So we always return a pointer to a valid C char array, even when
  71. * empty.
  72. * @param str The C++ string object.
  73. * @return Pointer to a NUL terminated string. This is only valid until
  74. * the next time the string is updated. This is never nullptr.
  75. */
  76. const char* c_str(const string_ref& sr) {
  77. return sr ? sr.to_string().c_str() : nullptr;
  78. }
  79. public:
  80. /** Smart/shared pointer to this class. */
  81. using ptr_t = std::shared_ptr<will_options>;
  82. /** Smart/shared pointer to a const object of this class. */
  83. using const_ptr_t = std::shared_ptr<const will_options>;
  84. /**
  85. * Constructs a new object using the default values.
  86. */
  87. will_options();
  88. /**
  89. * Sets the "Last Will and Testament" (LWT) for the connection.
  90. * @param top The LWT message is published to the this topic.
  91. * @param payload The message that is published to the Will Topic.
  92. * @param payload_len The message size in bytes
  93. * @param qos The message Quality of Service.
  94. * @param retained Tell the broker to keep the LWT message after send to
  95. * subscribers.
  96. */
  97. will_options(string_ref top, const void *payload, size_t payload_len,
  98. int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
  99. /**
  100. * Sets the "Last Will and Testament" (LWT) for the connection.
  101. * @param top The LWT message is published to the this topic.
  102. * @param payload The message that is published to the Will Topic.
  103. * @param payload_len The message size in bytes.
  104. * @param qos The message Quality of Service.
  105. * @param retained Tell the broker to keep the LWT message after send to
  106. * subscribers.
  107. */
  108. will_options(const topic& top, const void *payload, size_t payload_len,
  109. int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
  110. /**
  111. * Sets the "Last Will and Testament" (LWT) for the connection.
  112. * @param top The LWT message is published to the this topic.
  113. * @param payload The message payload that is published to the Will
  114. * Topic.
  115. * @param qos The message Quality of Service.
  116. * @param retained Tell the broker to keep the LWT message after send to
  117. * subscribers.
  118. */
  119. will_options(string_ref top, binary_ref payload,
  120. int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
  121. /**
  122. * Sets the "Last Will and Testament" (LWT) for the connection.
  123. * @param top The LWT message is published to the this topic.
  124. * @param payload The message payload that is published to the Will
  125. * Topic, as a string.
  126. * @param qos The message Quality of Service.
  127. * @param retained Tell the broker to keep the LWT message after send to
  128. * subscribers.
  129. */
  130. will_options(string_ref top, const string& payload,
  131. int qos=DFLT_QOS, bool retained=DFLT_QOS);
  132. /**
  133. * Sets the "Last Will and Testament" (LWT) for the connection.
  134. * @param msg The message that is published to the Will Topic.
  135. */
  136. will_options(const message& msg);
  137. /**
  138. * Copy constructor for the LWT options.
  139. * @param opt The other options.
  140. */
  141. will_options(const will_options& opt);
  142. /**
  143. * Move constructor for the LWT options.
  144. * @param opt The other options.
  145. */
  146. will_options(will_options&& opt);
  147. /**
  148. * Copy assignment for the LWT options.
  149. * @param opt The other options.
  150. */
  151. will_options& operator=(const will_options& opt);
  152. /**
  153. * Move assignment for the LWT options.
  154. * @param opt The other options.
  155. */
  156. will_options& operator=(will_options&& opt);
  157. /**
  158. * Gets the LWT message topic name.
  159. * @return The LWT message topic name.
  160. */
  161. string get_topic() const { return topic_ ? topic_.to_string() : string(); }
  162. /**
  163. * Gets the LWT message payload.
  164. * @return The LWT message payload.
  165. */
  166. const binary_ref& get_payload() const { return payload_; }
  167. /**
  168. * Gets the LWT message payload as a string.
  169. * @return The LWT message payload as a string.
  170. */
  171. string get_payload_str() const { return payload_ ? payload_.to_string() : string(); }
  172. /**
  173. * Gets the QoS value for the LWT message.
  174. * @return The QoS value for the LWT message.
  175. */
  176. int get_qos() const { return opts_.qos; }
  177. /**
  178. * Gets the 'retained' flag for the LWT message.
  179. * @return The 'retained' flag for the LWT message.
  180. */
  181. bool is_retained() const { return opts_.retained != 0; }
  182. /**
  183. * Gets the LWT message as a message object.
  184. * @return A pointer to a copy of the LWT message.
  185. */
  186. const_message_ptr get_message() const {
  187. return message::create(topic_, payload_, opts_.qos, to_bool(opts_.retained));
  188. }
  189. /**
  190. * Sets the LWT message topic name.
  191. * @param top The topic where to sent the message
  192. */
  193. void set_topic(string_ref top);
  194. /**
  195. * Sets the LWT message text.
  196. * @param msg The LWT message
  197. */
  198. void set_payload(binary_ref msg);
  199. /**
  200. * Sets the LWT message text.
  201. * @param msg The LWT message
  202. */
  203. void set_payload(string msg) { set_payload(binary_ref(std::move(msg))); }
  204. /**
  205. * Sets the QoS value.
  206. * @param qos The LWT message QoS
  207. */
  208. void set_qos(const int qos) { opts_.qos = qos; }
  209. /**
  210. * Sets the retained flag.
  211. * @param retained Tell the broker to keep the LWT message after send to
  212. * subscribers.
  213. */
  214. void set_retained(bool retained) { opts_.retained = to_int(retained); }
  215. /**
  216. * Gets the connect properties.
  217. * @return A const reference to the properties for the connect.
  218. */
  219. const properties& get_properties() const { return props_; }
  220. /**
  221. * Sets the properties for the connect.
  222. * @param props The properties to place into the message.
  223. */
  224. void set_properties(const properties& props) { props_ = props; }
  225. /**
  226. * Moves the properties for the connect.
  227. * @param props The properties to move into the connect object.
  228. */
  229. void set_properties(properties&& props) { props_ = props; }
  230. };
  231. /** Shared pointer to a will options object. */
  232. using will_options_ptr = will_options::ptr_t;
  233. /** Shared pointer to a const will options object. */
  234. using const_will_options_ptr = will_options::const_ptr_t;
  235. /////////////////////////////////////////////////////////////////////////////
  236. // end namespace mqtt
  237. }
  238. #endif // __mqtt_will_options_h