create_options.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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-2025 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 <variant>
  25. #include "MQTTAsync.h"
  26. #include "mqtt/iclient_persistence.h"
  27. #include "mqtt/types.h"
  28. namespace mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
  30. /** An empty type that can be used as a `persistent_type` variant option. */
  31. struct no_persistence
  32. {
  33. };
  34. /** A constant used to indicate that no persistence is desired */
  35. constexpr no_persistence NO_PERSISTENCE{};
  36. /**
  37. * A variant for the different type of persistence:
  38. * @li @em no_persistence: Any object of this type indicates no persistence
  39. * is desired.
  40. * @li @em string: Indicates file persistence. The string specifies the
  41. * directory for the persistence store.
  42. * @li @em iclient_persistence*: User-defined persistence
  43. */
  44. using persistence_type = std::variant<no_persistence, string, iclient_persistence*>;
  45. /////////////////////////////////////////////////////////////////////////////
  46. /**
  47. * The set of options for constructing a client object.
  48. *
  49. * Note that the numerous, incomplete set of constructors pre-date the
  50. * current, expanded, options structure. For a full set of create options, a
  51. * builder can be used to specify the options, then construct the client
  52. * with those options, like this:
  53. *
  54. * @code
  55. * auto createOpts = mqtt::create_options_builder()
  56. * .server_uri(serverURI)
  57. * .send_while_disconnected()
  58. * .max_buffered_messages(25)
  59. * .delete_oldest_messages()
  60. * .finalize();
  61. *
  62. * mqtt::async_client cli(createOpts);
  63. * @endcode
  64. */
  65. class create_options
  66. {
  67. /** The underlying C options */
  68. MQTTAsync_createOptions opts_ MQTTAsync_createOptions_initializer5;
  69. /** The address of the server to connect to, specified as a URI */
  70. string serverURI_{};
  71. /** A client identifier that is unique on the server */
  72. string clientId_{};
  73. /** The persistence for the client */
  74. persistence_type persistence_{};
  75. /** The client and tests have special access */
  76. friend class async_client;
  77. friend class create_options_builder;
  78. public:
  79. /** Smart/shared pointer to an object of this class. */
  80. using ptr_t = std::shared_ptr<create_options>;
  81. /** Smart/shared pointer to a const object of this class. */
  82. using const_ptr_t = std::shared_ptr<const create_options>;
  83. /**
  84. * Default set of client create options.
  85. */
  86. create_options() {}
  87. /**
  88. * Default create options for the specified version of MQTT.
  89. * @param mqttVersion The MQTT version used to create the client.
  90. */
  91. explicit create_options(int mqttVersion) : create_options() {
  92. opts_.MQTTVersion = mqttVersion;
  93. }
  94. /**
  95. * Default create options, but with off-line buffering enabled.
  96. * @param mqttVersion The MQTT version used to create the client.
  97. * @param maxBufferedMessages the maximum number of messages allowed to
  98. * be buffered while not connected
  99. */
  100. create_options(int mqttVersion, int maxBufferedMessages);
  101. /**
  102. * Create options for the specified server and client ID, with optional
  103. * persistence.
  104. * This allows the caller to specify a user-defined persistence object,
  105. * or use no persistence.
  106. * @param serverURI the address of the server to connect to, specified
  107. * as a URI.
  108. * @param clientId a client identifier that is unique on the server
  109. * being connected to
  110. * @param persistence The desired persistence structure.
  111. * @throw exception if an argument is invalid
  112. */
  113. explicit create_options(
  114. const string& serverURI, const string& clientId = string{},
  115. const persistence_type& persistence = NO_PERSISTENCE
  116. )
  117. : serverURI_{serverURI}, clientId_{clientId}, persistence_{persistence} {}
  118. /**
  119. * Create an async_client that can be used to communicate with an MQTT
  120. * server, which allows for off-line message buffering.
  121. * This uses file-based persistence in the specified directory.
  122. * @param serverURI the address of the server to connect to, specified
  123. * as a URI.
  124. * @param clientId a client identifier that is unique on the server
  125. * being connected to
  126. * @param maxBufferedMessages the maximum number of messages allowed to
  127. * be buffered while not connected
  128. * @param persistence The persistence that the client should use.
  129. * @throw exception if an argument is invalid
  130. */
  131. create_options(
  132. const string& serverURI, const string& clientId, int maxBufferedMessages,
  133. const persistence_type& persistence = NO_PERSISTENCE
  134. )
  135. : serverURI_{serverURI}, clientId_{clientId}, persistence_{persistence} {
  136. opts_.maxBufferedMessages = maxBufferedMessages;
  137. }
  138. /**
  139. * Create an async_client that can be used to communicate with an MQTT
  140. * server, which allows for off-line message buffering.
  141. * This uses file-based persistence in the specified directory.
  142. * @param serverURI the address of the server to connect to, specified
  143. * as a URI.
  144. * @param clientId a client identifier that is unique on the server
  145. * being connected to
  146. * @param opts The create options
  147. * @param persistence The persistence that the client should use.
  148. * @throw exception if an argument is invalid
  149. */
  150. create_options(
  151. const string& serverURI, const string& clientId, const create_options& opts,
  152. const persistence_type& persistence
  153. )
  154. : opts_{opts.opts_},
  155. serverURI_{serverURI},
  156. clientId_{clientId},
  157. persistence_{persistence} {}
  158. /**
  159. * Copy constructor.
  160. * @param opts The other options.
  161. */
  162. create_options(const create_options& opts)
  163. : opts_{opts.opts_},
  164. serverURI_{opts.serverURI_},
  165. clientId_{opts.clientId_},
  166. persistence_{opts.persistence_} {}
  167. /**
  168. * Move constructor.
  169. * @param opts The other options.
  170. */
  171. create_options(create_options&& opts)
  172. : opts_{opts.opts_},
  173. serverURI_{std::move(opts.serverURI_)},
  174. clientId_{std::move(opts.clientId_)},
  175. persistence_{std::move(opts.persistence_)} {}
  176. create_options& operator=(const create_options& rhs);
  177. create_options& operator=(create_options&& rhs);
  178. /**
  179. * Set the address of the server to connect to, specified as a URI
  180. * @param serverURI The URI of the server.
  181. */
  182. void set_server_uri(const string& serverURI) { serverURI_ = serverURI; };
  183. /**
  184. * Get the address of the server to connect to, specified as a URI.
  185. * @return The URI of the server.
  186. */
  187. const string& get_server_uri() const noexcept { return serverURI_; };
  188. /**
  189. * Set the client identifier.
  190. * @param clientId The client identifier.
  191. */
  192. void set_client_id(const string& clientId) { clientId_ = clientId; }
  193. /**
  194. * Get the client identifier.
  195. * @return The client identifier.
  196. */
  197. const string& get_client_id() const noexcept { return clientId_; }
  198. /**
  199. * Set the persistence for the client.
  200. * @param persistence The persistence for the client
  201. */
  202. void set_persistence(const persistence_type& persistence) { persistence_ = persistence; }
  203. /**
  204. * Get the persistence for the client.
  205. * @return The persistence for the client
  206. */
  207. const persistence_type& get_persistence() const noexcept { return persistence_; }
  208. /**
  209. * Gets whether the client will accept message to publish while
  210. * disconnected.
  211. */
  212. bool get_send_while_disconnected() const { return to_bool(opts_.sendWhileDisconnected); }
  213. /**
  214. * Sets whether the client will accept message to publish while
  215. * disconnected.
  216. *
  217. * @param on @em true to allow the application to publish messages while
  218. * disconnected, @em false returns an error on publish if
  219. * disconnected.
  220. * @param anyTime If @em true, allows you to publish messages before the
  221. * first successful connection.
  222. */
  223. void set_send_while_disconnected(bool on, bool anyTime = false) {
  224. opts_.sendWhileDisconnected = to_int(on);
  225. opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
  226. }
  227. /**
  228. * Gets the maximum number of offline buffered messages.
  229. * @return The maximum number of offline buffered messages.
  230. */
  231. int get_max_buffered_messages() const { return opts_.maxBufferedMessages; }
  232. /**
  233. * Sets the maximum number of offline buffered messages.
  234. * @param n The maximum number of offline buffered messages.
  235. */
  236. void set_max_buffered_messages(int n) { opts_.maxBufferedMessages = n; }
  237. /**
  238. * Gets the MQTT version used to create the client.
  239. * @return The MQTT version used to create the client.
  240. */
  241. int mqtt_version() const { return opts_.MQTTVersion; }
  242. /**
  243. * Sets the MQTT version used to create the client.
  244. * @param ver The MQTT version used to create the client.
  245. */
  246. void set_mqtt_version(int ver) { opts_.MQTTVersion = ver; }
  247. /**
  248. * Whether the oldest messages are deleted when the output buffer is
  249. * full.
  250. *
  251. * @return @em true if the oldest messages should be deleted when the
  252. * output buffer is full, @em false if the new messages should
  253. * be dropped when the buffer is full.
  254. */
  255. bool get_delete_oldest_messages() const { return to_bool(opts_.deleteOldestMessages); }
  256. /**
  257. * Determines what to do when the maximum number of buffered messages is
  258. * reached: delete the oldest messages rather than the newest
  259. * @param on @em true When the output queue is full, delete the oldest
  260. * message, @em false drop the newest message being added.
  261. */
  262. void set_delete_oldest_messages(bool on) { opts_.deleteOldestMessages = to_int(on); }
  263. /**
  264. * Whether the messages will be restored from persistence or the store
  265. * will be cleared.
  266. * @return @em true if the messages will be restored from persistence,
  267. * @em false if the persistence store will be cleared.
  268. */
  269. bool get_restore_messages() const { return to_bool(opts_.restoreMessages); }
  270. /**
  271. * Determine whether to restore messages from persistence or clear the
  272. * persistence store.
  273. * @param on @em true to restore messages from persistence, @em false to
  274. * clear the persistence store.
  275. */
  276. void set_restore_messages(bool on) { opts_.restoreMessages = to_int(on); }
  277. /**
  278. * Whether to persist QoS 0 messages.
  279. *
  280. * @return @em true if QoS 0 messages are persisted, @em false if not.
  281. */
  282. bool get_persist_qos0() const { return to_bool(opts_.persistQoS0); }
  283. /**
  284. * Determine whether to persist QoS 0 messages.
  285. *
  286. * @param on @em true if QoS 0 messages are persisted, @em false if not.
  287. */
  288. void set_persist_qos0(bool on) { opts_.persistQoS0 = to_int(on); }
  289. };
  290. /** Smart/shared pointer to a connection options object. */
  291. using create_options_ptr = create_options::ptr_t;
  292. /////////////////////////////////////////////////////////////////////////////
  293. /**
  294. * Builder class to generate the create options.
  295. */
  296. class create_options_builder
  297. {
  298. /** The underlying options */
  299. create_options opts_;
  300. public:
  301. /** This class */
  302. using self = create_options_builder;
  303. /**
  304. * Default constructor.
  305. */
  306. create_options_builder() {}
  307. /**
  308. * Set the server URI.
  309. * @param serverURI The address of the server to connect to, specified
  310. * as a URI
  311. */
  312. auto server_uri(const string& serverURI) -> self& {
  313. opts_.set_server_uri(serverURI);
  314. return *this;
  315. }
  316. /**
  317. * Sets the client ID.
  318. * @param clientId A client identifier that is unique on the server
  319. */
  320. auto client_id(const string& clientId) -> self& {
  321. opts_.set_client_id(clientId);
  322. return *this;
  323. }
  324. /**
  325. * Sets the persistence.
  326. * @param persistence The persistence the client should use.
  327. */
  328. auto persistence(const persistence_type& persistence) -> self& {
  329. opts_.set_persistence(persistence);
  330. return *this;
  331. }
  332. /**
  333. * Sets whether the client will accept message to publish while
  334. * disconnected.
  335. *
  336. * @param on @em true to allow the application to publish messages while
  337. * disconnected, @em false returns an error on publish if
  338. * disconnected.
  339. * @param anyTime If @em true, allows you to publish messages before the
  340. * first successful connection.
  341. * @return A reference to this object.
  342. */
  343. auto send_while_disconnected(bool on = true, bool anyTime = false) -> self& {
  344. opts_.opts_.sendWhileDisconnected = to_int(on);
  345. opts_.opts_.allowDisconnectedSendAtAnyTime = to_int(anyTime);
  346. return *this;
  347. }
  348. /**
  349. * Sets the maximum number of offline buffered messages.
  350. * @param n The maximum number of offline buffered messages.
  351. * @return A reference to this object.
  352. */
  353. auto max_buffered_messages(int n) -> self& {
  354. opts_.opts_.maxBufferedMessages = n;
  355. return *this;
  356. }
  357. /**
  358. * Sets the MQTT version used to create the client.
  359. * @param ver The MQTT version used to create the client.
  360. */
  361. auto mqtt_version(int ver) -> self& {
  362. opts_.opts_.MQTTVersion = ver;
  363. return *this;
  364. }
  365. /**
  366. * Determines what to do when the maximum number of buffered messages is
  367. * reached: delete the oldest messages rather than the newest.
  368. * @param on @em true When the output queue is full, delete the oldest
  369. * message, @em false drop the newest message being added.
  370. * @return A reference to this object.
  371. */
  372. auto delete_oldest_messages(bool on = true) -> self& {
  373. opts_.opts_.deleteOldestMessages = to_int(on);
  374. return *this;
  375. }
  376. /**
  377. * Determines whether to restore persisted messages or clear the
  378. * persistence store. (Defaults true)
  379. *
  380. * @param on @em true to restore persisted messages, @em false to clear
  381. * the persistence store.
  382. * @return A reference to this object.
  383. */
  384. auto restore_messages(bool on = true) -> self& {
  385. opts_.opts_.restoreMessages = to_int(on);
  386. return *this;
  387. }
  388. /**
  389. * Whether to persist QoS 0 messages. (Defaults true)
  390. *
  391. * @param on @em true persist QoS 0 messages, @em false, don't.
  392. * @return A reference to this object
  393. */
  394. auto persist_qos0(bool on = true) -> self& {
  395. opts_.opts_.persistQoS0 = to_int(on);
  396. return *this;
  397. }
  398. /**
  399. * Finish building the options and return them.
  400. * @return The option struct as built.
  401. */
  402. create_options finalize() { return opts_; }
  403. };
  404. /////////////////////////////////////////////////////////////////////////////
  405. } // namespace mqtt
  406. #endif // __mqtt_create_options_h