create_options.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file create_options.h
  3. /// Declaration of MQTT create_options class
  4. /// @date Oct 17, 2020
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2020-2023 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v2.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v20.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_create_options_h
  23. #define __mqtt_create_options_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/types.h"
  26. namespace mqtt {
  27. /////////////////////////////////////////////////////////////////////////////
  28. /**
  29. * Options for creating a client object.
  30. */
  31. class create_options
  32. {
  33. /** The default C struct */
  34. static const MQTTAsync_createOptions DFLT_C_STRUCT;
  35. /** The underlying C options */
  36. MQTTAsync_createOptions opts_;
  37. /** The client and tests have special access */
  38. friend class async_client;
  39. friend class create_options_builder;
  40. public:
  41. /** Smart/shared pointer to an object of this class. */
  42. using ptr_t = std::shared_ptr<create_options>;
  43. /** Smart/shared pointer to a const object of this class. */
  44. using const_ptr_t = std::shared_ptr<const create_options>;
  45. /**
  46. * Default set of client create options.
  47. */
  48. create_options();
  49. /**
  50. * Default create options for the specified version of MQTT.
  51. * @param mqttVersion The MQTT version used to create the client.
  52. */
  53. explicit create_options(int mqttVersion);
  54. /**
  55. * Default create options, but with off-line buffering enabled.
  56. * @param mqttVersion The MQTT version used to create the client.
  57. * @param maxBufferedMessages the maximum number of messages allowed to
  58. * be buffered while not connected
  59. */
  60. create_options(int mqttVersion, int maxBufferedMessages);
  61. /**
  62. * Gets whether the client will accept message to publish while
  63. * disconnected.
  64. */
  65. bool get_send_while_disconnected() const {
  66. return to_bool(opts_.sendWhileDisconnected);
  67. }
  68. /**
  69. * Sets whether the client will accept message to publish while
  70. * disconnected.
  71. *
  72. * @param on @em true to allow the application to publish messages while
  73. * disconnected, @em false returns an error on publish if
  74. * disconnected.
  75. * @param anyTime If @em true, allows you to publish messages before the
  76. * first successful connection.
  77. */
  78. void set_send_while_disconnected(bool on, bool anyTime=false) {
  79. opts_.sendWhileDisconnected = to_int(on);
  80. opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
  81. }
  82. /**
  83. * Gets the maximum number of offline buffered messages.
  84. * @return The maximum number of offline buffered messages.
  85. */
  86. int get_max_buffered_messages() const {
  87. return opts_.maxBufferedMessages;
  88. }
  89. /**
  90. * Sets the maximum number of offline buffered messages.
  91. * @param n The maximum number of offline buffered messages.
  92. */
  93. void set_max_buffered_messages(int n) {
  94. opts_.maxBufferedMessages = n;
  95. }
  96. /**
  97. * Gets the MQTT version used to create the client.
  98. * @return The MQTT version used to create the client.
  99. */
  100. int mqtt_version() const { return opts_.MQTTVersion; }
  101. /**
  102. * Sets the MQTT version used to create the client.
  103. * @param ver The MQTT version used to create the client.
  104. */
  105. void set_mqtt_version(int ver) { opts_.MQTTVersion = ver; }
  106. /**
  107. * Whether the oldest messages are deleted when the output buffer is
  108. * full.
  109. *
  110. * @return @em true if the oldest messages should be deleted when the
  111. * output buffer is full, @em false if the new messages should
  112. * be dropped when the buffer is full.
  113. */
  114. bool get_delete_oldest_messages() const {
  115. return to_bool(opts_.deleteOldestMessages);
  116. }
  117. /**
  118. * Determines what to do when the maximum number of buffered messages is
  119. * reached: delete the oldest messages rather than the newest
  120. * @param on @em true When the output queue is full, delete the oldest
  121. * message, @em false drop the newest message being added.
  122. */
  123. void set_delete_oldest_messages(bool on) {
  124. opts_.deleteOldestMessages = to_int(on);
  125. }
  126. /**
  127. * Whether the messages will be restored from persistence or the store
  128. * will be cleared.
  129. * @return @em true if the messages will be restored from persistence,
  130. * @em false if the persistence store will be cleared.
  131. */
  132. bool get_restore_messages() const {
  133. return to_bool(opts_.restoreMessages);
  134. }
  135. /**
  136. * Determine whether to restore messages from persistence or clear the
  137. * persistence store.
  138. * @param on @em true to restore messages from persistence, @em false to
  139. * clear the persistence store.
  140. */
  141. void set_restore_messages(bool on) {
  142. opts_.restoreMessages = to_int(on);
  143. }
  144. /**
  145. * Whether to persist QoS 0 messages.
  146. *
  147. * @return @em true if QoS 0 messages are persisted, @em false if not.
  148. */
  149. bool get_persist_qos0() const {
  150. return to_bool(opts_.persistQoS0);
  151. }
  152. /**
  153. * Determine whether to persist QoS 0 messages.
  154. *
  155. * @param on @em true if QoS 0 messages are persisted, @em false if not.
  156. */
  157. void set_persist_qos0(bool on) {
  158. opts_.persistQoS0 = to_int(on);
  159. }
  160. };
  161. /** Smart/shared pointer to a connection options object. */
  162. using create_options_ptr = create_options::ptr_t;
  163. /////////////////////////////////////////////////////////////////////////////
  164. /**
  165. * Builder class to generate the create options.
  166. */
  167. class create_options_builder
  168. {
  169. /** The underlying options */
  170. create_options opts_;
  171. public:
  172. /** This class */
  173. using self = create_options_builder;
  174. /**
  175. * Default constructor.
  176. */
  177. create_options_builder() {}
  178. /**
  179. *
  180. * Sets whether the client will accept message to publish while
  181. * disconnected.
  182. *
  183. * @param on @em true to allow the application to publish messages while
  184. * disconnected, @em false returns an error on publish if
  185. * disconnected.
  186. * @param anyTime If @em true, allows you to publish messages before the
  187. * first successful connection.
  188. * @return A reference to this object.
  189. */
  190. auto send_while_disconnected(bool on=true, bool anyTime=false) -> self& {
  191. opts_.opts_.sendWhileDisconnected = to_int(on);
  192. opts_.opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
  193. return *this;
  194. }
  195. /**
  196. * Sets the maximum number of offline buffered messages.
  197. * @param n The maximum number of offline buffered messages.
  198. * @return A reference to this object.
  199. */
  200. auto max_buffered_messages(int n) -> self& {
  201. opts_.opts_.maxBufferedMessages = n;
  202. return *this;
  203. }
  204. /**
  205. * Sets the MQTT version used to create the client.
  206. * @param ver The MQTT version used to create the client.
  207. */
  208. auto mqtt_version(int ver) -> self& {
  209. opts_.opts_.MQTTVersion = ver;
  210. return *this;
  211. }
  212. /**
  213. * Determines what to do when the maximum number of buffered messages is
  214. * reached: delete the oldest messages rather than the newest.
  215. * @param on @em true When the output queue is full, delete the oldest
  216. * message, @em false drop the newest message being added.
  217. * @return A reference to this object.
  218. */
  219. auto delete_oldest_messages(bool on=true) -> self& {
  220. opts_.opts_.deleteOldestMessages = to_int(on);
  221. return *this;
  222. }
  223. /**
  224. * Determines whether to restore persisted messages or clear the
  225. * persistence store. (Defaults true)
  226. *
  227. * @param on @em true to restore persisted messages, @em false to clear
  228. * the persistence store.
  229. * @return A reference to this object.
  230. */
  231. auto restore_messages(bool on=true) -> self& {
  232. opts_.opts_.restoreMessages = to_int(on);
  233. return *this;
  234. }
  235. /**
  236. * Whether to persist QoS 0 messages. (Defaults true)
  237. *
  238. * @param on @em true persist QoS 0 messages, @em false, don't.
  239. * @return A reference to this object
  240. */
  241. auto persist_qos0(bool on=true) -> self& {
  242. opts_.opts_.persistQoS0 = to_int(on);
  243. return *this;
  244. }
  245. /**
  246. * Finish building the options and return them.
  247. * @return The option struct as built.
  248. */
  249. create_options finalize() { return opts_; }
  250. };
  251. /////////////////////////////////////////////////////////////////////////////
  252. // end namespace mqtt
  253. }
  254. #endif // __mqtt_create_options_h