will_options.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-2023 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 v2.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-v20.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. #include "mqtt/platform.h"
  31. namespace mqtt {
  32. class connect_options;
  33. /////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * Holds the set of options that govern the Last Will and Testament feature.
  36. *
  37. * @note
  38. * This wraps struct v1 of the C library's MQTTAsync_willOptions structure.
  39. * It sets the LWT binary payload, via the 'payload' struct field, and
  40. leaves the 'message' field as a nullptr.
  41. */
  42. class will_options
  43. {
  44. public:
  45. /** The default QoS for the LWT, if unspecified */
  46. PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0;
  47. /** The default retained flag for LWT, if unspecified */
  48. PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false;
  49. private:
  50. /** A default C struct to support re-initializing variables */
  51. PAHO_MQTTPP_EXPORT static const MQTTAsync_willOptions DFLT_C_STRUCT;
  52. /** The underlying C LWT options */
  53. MQTTAsync_willOptions opts_;
  54. /** LWT message topic **/
  55. string_ref topic_;
  56. /** LWT message text */
  57. binary_ref payload_;
  58. /**
  59. * The properties for the LWT message.
  60. * Strangely, in the C lib, the will properties are not in the
  61. * willOptions struct, but are rather in the connectOptions.
  62. * So we keep the cached properties here, but need to transfer them to
  63. * the connect_options when we're added to that struct.
  64. */
  65. properties props_;
  66. /** The connect options has special access */
  67. friend class connect_options;
  68. /**
  69. * Gets a pointer to the C-language NUL-terminated strings for the
  70. * struct.
  71. * Some structs, such as this one, require valid pointers at all times,
  72. * while others expect NULL pointers for missing parameters.
  73. * So we always return a pointer to a valid C char array, even when
  74. * empty.
  75. * @param str The C++ string object.
  76. * @return Pointer to a NUL terminated string. This is only valid until
  77. * the next time the string is updated. This is never nullptr.
  78. */
  79. const char* c_str(const string_ref& sr) {
  80. return sr ? sr.to_string().c_str() : nullptr;
  81. }
  82. public:
  83. /** Smart/shared pointer to an object of this class. */
  84. using ptr_t = std::shared_ptr<will_options>;
  85. /** Smart/shared pointer to a const object of this class. */
  86. using const_ptr_t = std::shared_ptr<const will_options>;
  87. /** Smart/shared pointer to an object of this class. */
  88. using unique_ptr_t = std::unique_ptr<will_options>;
  89. /**
  90. * Constructs a new object using the default values.
  91. */
  92. will_options();
  93. /**
  94. * Sets the "Last Will and Testament" (LWT) for the connection.
  95. * @param top The LWT message is published to the this topic.
  96. * @param payload The message that is published to the Will Topic.
  97. * @param payload_len The message size in bytes
  98. * @param qos The message Quality of Service.
  99. * @param retained Tell the broker to keep the LWT message after send to
  100. * subscribers.
  101. * @param props MQTT v5 properties for the will message.
  102. */
  103. will_options(string_ref top, const void *payload, size_t payload_len,
  104. int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
  105. const properties& props=properties());
  106. /**
  107. * Sets the "Last Will and Testament" (LWT) for the connection.
  108. * @param top The LWT message is published to the this topic.
  109. * @param payload The message that is published to the Will Topic.
  110. * @param payload_len The message size in bytes.
  111. * @param qos The message Quality of Service.
  112. * @param retained Tell the broker to keep the LWT message after send to
  113. * subscribers.
  114. * @param props MQTT v5 properties for the will message.
  115. */
  116. will_options(const topic& top, const void *payload, size_t payload_len,
  117. int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
  118. const properties& props=properties());
  119. /**
  120. * Sets the "Last Will and Testament" (LWT) for the connection.
  121. * @param top The LWT message is published to the this topic.
  122. * @param payload The message payload that is published to the Will
  123. * Topic.
  124. * @param qos The message Quality of Service.
  125. * @param retained Tell the broker to keep the LWT message after send to
  126. * subscribers.
  127. * @param props MQTT v5 properties for the will message.
  128. */
  129. will_options(string_ref top, binary_ref payload,
  130. int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
  131. const properties& props=properties());
  132. /**
  133. * Sets the "Last Will and Testament" (LWT) for the connection.
  134. * @param top The LWT message is published to the this topic.
  135. * @param payload The message payload that is published to the Will
  136. * Topic, as a string.
  137. * @param qos The message Quality of Service.
  138. * @param retained Tell the broker to keep the LWT message after send to
  139. * subscribers.
  140. * @param props MQTT v5 properties for the will message.
  141. */
  142. will_options(string_ref top, const string& payload,
  143. int qos=DFLT_QOS, bool retained=DFLT_QOS,
  144. const properties& props=properties());
  145. /**
  146. * Sets the "Last Will and Testament" (LWT) for the connection.
  147. * @param msg The message that is published to the Will Topic.
  148. */
  149. will_options(const message& msg);
  150. /**
  151. * Copy constructor for the LWT options.
  152. * @param opt The other options.
  153. */
  154. will_options(const will_options& opt);
  155. /**
  156. * Move constructor for the LWT options.
  157. * @param opt The other options.
  158. */
  159. will_options(will_options&& opt);
  160. /**
  161. * Copy assignment for the LWT options.
  162. * @param opt The other options.
  163. */
  164. will_options& operator=(const will_options& opt);
  165. /**
  166. * Move assignment for the LWT options.
  167. * @param opt The other options.
  168. */
  169. will_options& operator=(will_options&& opt);
  170. /**
  171. * Expose the underlying C struct for the unit tests.
  172. */
  173. #if defined(UNIT_TESTS)
  174. const MQTTAsync_willOptions& c_struct() const { return opts_; }
  175. #endif
  176. /**
  177. * Gets the LWT message topic name.
  178. * @return The LWT message topic name.
  179. */
  180. string get_topic() const { return topic_ ? topic_.to_string() : string(); }
  181. /**
  182. * Gets the LWT message payload.
  183. * @return The LWT message payload.
  184. */
  185. const binary_ref& get_payload() const { return payload_; }
  186. /**
  187. * Gets the LWT message payload as a string.
  188. * @return The LWT message payload as a string.
  189. */
  190. string get_payload_str() const { return payload_ ? payload_.to_string() : string(); }
  191. /**
  192. * Gets the QoS value for the LWT message.
  193. * @return The QoS value for the LWT message.
  194. */
  195. int get_qos() const { return opts_.qos; }
  196. /**
  197. * Gets the 'retained' flag for the LWT message.
  198. * @return The 'retained' flag for the LWT message.
  199. */
  200. bool is_retained() const { return opts_.retained != 0; }
  201. /**
  202. * Gets the LWT message as a message object.
  203. * @return A pointer to a copy of the LWT message.
  204. */
  205. const_message_ptr get_message() const {
  206. return message::create(topic_, payload_, opts_.qos, to_bool(opts_.retained));
  207. }
  208. /**
  209. * Sets the LWT message topic name.
  210. * @param top The topic where to sent the message
  211. */
  212. void set_topic(string_ref top);
  213. /**
  214. * Sets the LWT message text.
  215. * @param msg The LWT message
  216. */
  217. void set_payload(binary_ref msg);
  218. /**
  219. * Sets the LWT message text.
  220. * @param msg The LWT message
  221. */
  222. void set_payload(string msg) { set_payload(binary_ref(std::move(msg))); }
  223. /**
  224. * Sets the QoS value.
  225. * @param qos The LWT message QoS
  226. */
  227. void set_qos(const int qos) { opts_.qos = qos; }
  228. /**
  229. * Sets the retained flag.
  230. * @param retained Tell the broker to keep the LWT message after send to
  231. * subscribers.
  232. */
  233. void set_retained(bool retained) { opts_.retained = to_int(retained); }
  234. /**
  235. * Gets the connect properties.
  236. * @return A const reference to the properties for the connect.
  237. */
  238. const properties& get_properties() const { return props_; }
  239. /**
  240. * Sets the properties for the connect.
  241. * @param props The properties to place into the message.
  242. */
  243. void set_properties(const properties& props) { props_ = props; }
  244. /**
  245. * Moves the properties for the connect.
  246. * @param props The properties to move into the connect object.
  247. */
  248. void set_properties(properties&& props) { props_ = std::move(props); }
  249. };
  250. /** Shared pointer to a will options object. */
  251. using will_options_ptr = will_options::ptr_t;
  252. /** Shared pointer to a const will options object. */
  253. using const_will_options_ptr = will_options::const_ptr_t;
  254. /** Unique pointer to a will options object. */
  255. using will_options_unique_ptr = will_options::unique_ptr_t;
  256. /////////////////////////////////////////////////////////////////////////////
  257. // end namespace mqtt
  258. }
  259. #endif // __mqtt_will_options_h