connect_options.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file connect_options.h
  3. /// Declaration of MQTT connect_options class
  4. /// @date May 1, 2013
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2013-2016 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 v1.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-v10.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_connect_options_h
  23. #define __mqtt_connect_options_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/types.h"
  26. #include "mqtt/message.h"
  27. #include "mqtt/topic.h"
  28. #include "mqtt/token.h"
  29. #include "mqtt/string_collection.h"
  30. #include "mqtt/will_options.h"
  31. #include "mqtt/ssl_options.h"
  32. #include <vector>
  33. #include <chrono>
  34. namespace mqtt {
  35. /////////////////////////////////////////////////////////////////////////////
  36. /**
  37. * Holds the set of options that control how the client connects to a
  38. * server.
  39. */
  40. class connect_options
  41. {
  42. /** The default C struct */
  43. static const MQTTAsync_connectOptions DFLT_C_STRUCT ;
  44. /** The underlying C connection options */
  45. MQTTAsync_connectOptions opts_;
  46. /** The LWT options */
  47. will_options will_;
  48. /** The SSL options */
  49. ssl_options ssl_;
  50. /** The user name to use for the connection. */
  51. string_ref userName_;
  52. /** The password to use for the connection. */
  53. binary_ref password_;
  54. /** Shared token pointer for context, if any */
  55. token_ptr tok_;
  56. /** Collection of server URIs, if any */
  57. const_string_collection_ptr serverURIs_;
  58. /** The connect properties */
  59. properties props_;
  60. /** The client has special access */
  61. friend class async_client;
  62. friend class connect_options_test;
  63. friend class token_test;
  64. /**
  65. * Gets a pointer to the C-language NUL-terminated strings for the
  66. * struct.
  67. * @note In the connect options, by default, the Paho C treats
  68. * nullptr char arrays as unset values, so we keep that semantic and
  69. * only set those char arrays if the string is non-empty.
  70. * @param str The C++ string object.
  71. * @return Pointer to a NUL terminated string. This is only valid until
  72. * the next time the string is updated.
  73. */
  74. const char* c_str(const string_ref& sr) {
  75. return sr.empty() ? nullptr : sr.c_str();
  76. }
  77. public:
  78. /** Smart/shared pointer to an object of this class. */
  79. using ptr_t = std::shared_ptr<connect_options>;
  80. /** Smart/shared pointer to a const object of this class. */
  81. using const_ptr_t = std::shared_ptr<const connect_options>;
  82. /**
  83. * Constructs a new object using the default values.
  84. */
  85. connect_options();
  86. /**
  87. * Constructs a new object using the specified user name and password.
  88. * @param userName The name of the user for connecting to the server
  89. * @param password The password for connecting to the server
  90. */
  91. connect_options(string_ref userName, binary_ref password);
  92. /**
  93. * Copy constructor.
  94. * @param opt Another object to copy.
  95. */
  96. connect_options(const connect_options& opt);
  97. /**
  98. * Move constructor.
  99. * @param opt Another object to move into this new one.
  100. */
  101. connect_options(connect_options&& opt);
  102. /**
  103. * Copy assignment.
  104. * @param opt Another object to copy.
  105. */
  106. connect_options& operator=(const connect_options& opt);
  107. /**
  108. * Move assignment.
  109. * @param opt Another object to move into this new one.
  110. */
  111. connect_options& operator=(connect_options&& opt);
  112. /**
  113. * Gets the "keep alive" interval.
  114. * @return The keep alive interval in seconds.
  115. */
  116. std::chrono::seconds get_keep_alive_interval() const {
  117. return std::chrono::seconds(opts_.keepAliveInterval);
  118. }
  119. /**
  120. * Gets the connection timeout.
  121. * This is the amount of time the underlying library will wait for a
  122. * timeout before failing.
  123. * @return The connect timeout in seconds.
  124. */
  125. std::chrono::seconds get_connect_timeout() const {
  126. return std::chrono::seconds(opts_.connectTimeout);
  127. }
  128. /**
  129. * Gets the user name to use for the connection.
  130. * @return The user name to use for the connection.
  131. */
  132. string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
  133. /**
  134. * Gets the password to use for the connection.
  135. * @return The password to use for the connection.
  136. */
  137. binary_ref get_password() const { return password_; }
  138. /**
  139. * Gets the password to use for the connection.
  140. * @return The password to use for the connection.
  141. */
  142. string get_password_str() const {
  143. return password_ ? password_.to_string() : string();
  144. }
  145. /**
  146. * Gets the maximum number of messages that can be in-flight
  147. * simultaneously.
  148. * @return The maximum number of inflight messages.
  149. */
  150. int get_max_inflight() const { return opts_.maxInflight; }
  151. /**
  152. * Gets the topic to be used for last will and testament (LWT).
  153. * @return The topic to be used for last will and testament (LWT).
  154. */
  155. string get_will_topic() const {
  156. return will_.get_topic();
  157. }
  158. /**
  159. * Gets the message to be sent as last will and testament (LWT).
  160. * @return The message to be sent as last will and testament (LWT).
  161. */
  162. const_message_ptr get_will_message() const {
  163. return will_.get_message();
  164. }
  165. /**
  166. * Get the LWT options to use for the connection.
  167. * @return The LWT options to use for the connection.
  168. */
  169. const will_options& get_will_options() const { return will_; }
  170. /**
  171. * Get the SSL options to use for the connection.
  172. * @return The SSL options to use for the connection.
  173. */
  174. const ssl_options& get_ssl_options() const { return ssl_; }
  175. /**
  176. * Sets the SSL for the connection.
  177. * These will only have an effect if compiled against the SSL version of
  178. * the Paho C library.
  179. * @param ssl The SSL options.
  180. */
  181. void set_ssl(const ssl_options& ssl);
  182. void set_ssl(ssl_options&& ssl);
  183. /**
  184. * Returns whether the server should remember state for the client
  185. * across reconnects.
  186. * @return @em true if requesting a clean session, @em false if not.
  187. */
  188. bool is_clean_session() const { return to_bool(opts_.cleansession); }
  189. /**
  190. * Gets the token used as the callback context.
  191. * @return The delivery token used as the callback context.
  192. */
  193. token_ptr get_token() const { return tok_; }
  194. /**
  195. * Gets the list of servers to which the client will connect.
  196. * @return A collection of server URI's. Each entry should be of the
  197. * form @em protocol://host:port where @em protocol must be tcp
  198. * or @em ssl. For @em host, you can specify either an IP
  199. * address or a domain name.
  200. */
  201. const_string_collection_ptr get_servers() const { return serverURIs_; }
  202. /**
  203. * Gets the version of MQTT to be used on the connect.
  204. * @return
  205. * @li MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that
  206. * fails, fall back to 3.1
  207. * @li MQTTVERSION_3_1 (3) = only try version 3.1
  208. * @li MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  209. */
  210. int get_mqtt_version() const { return opts_.MQTTVersion; }
  211. /**
  212. * Determines if the options have been configured for automatic
  213. * reconnect.
  214. * @return @em true if configured for automatic reconnect, @em false if
  215. * not.
  216. */
  217. bool get_automatic_reconnect() const { return to_bool(opts_.automaticReconnect); }
  218. /**
  219. * Gets the minimum retry interval for automatic reconnect.
  220. * @return The minimum retry interval for automatic reconnect, in
  221. * seconds.
  222. */
  223. std::chrono::seconds get_min_retry_interval() const {
  224. return std::chrono::seconds(opts_.minRetryInterval);
  225. }
  226. /**
  227. * Gets the maximum retry interval for automatic reconnect.
  228. * @return The maximum retry interval for automatic reconnect, in
  229. * seconds.
  230. */
  231. std::chrono::seconds get_max_retry_interval() const {
  232. return std::chrono::seconds(opts_.maxRetryInterval);
  233. }
  234. /**
  235. * Sets whether the server should remember state for the client across
  236. * reconnects.
  237. * @param cleanSession
  238. */
  239. void set_clean_session(bool cleanSession) {
  240. opts_.cleansession = to_int(cleanSession);
  241. }
  242. /**
  243. * Sets the "keep alive" interval.
  244. * This is the maximum time that should pass without communications
  245. * between client and server. If no massages pass in this time, the
  246. * client will ping the broker.
  247. * @param keepAliveInterval The keep alive interval in seconds.
  248. */
  249. void set_keep_alive_interval(int keepAliveInterval) {
  250. opts_.keepAliveInterval = keepAliveInterval;
  251. }
  252. /**
  253. * Sets the "keep alive" interval with a chrono duration.
  254. * This is the maximum time that should pass without communications
  255. * between client and server. If no massages pass in this time, the
  256. * client will ping the broker.
  257. * @param interval The keep alive interval.
  258. */
  259. template <class Rep, class Period>
  260. void set_keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) {
  261. // TODO: Check range
  262. set_keep_alive_interval((int) to_seconds_count(interval));
  263. }
  264. /**
  265. * Sets the connect timeout in seconds.
  266. * This is the maximum time that the underlying library will wait for a
  267. * connection before failing.
  268. * @param timeout The connect timeout in seconds.
  269. */
  270. void set_connect_timeout(int timeout) {
  271. opts_.connectTimeout = timeout;
  272. }
  273. /**
  274. * Sets the connect timeout with a chrono duration.
  275. * This is the maximum time that the underlying library will wait for a
  276. * connection before failing.
  277. * @param timeout The connect timeout in seconds.
  278. */
  279. template <class Rep, class Period>
  280. void set_connect_timeout(const std::chrono::duration<Rep, Period>& timeout) {
  281. // TODO: check range
  282. set_connect_timeout((int) to_seconds_count(timeout));
  283. }
  284. /**
  285. * Sets the user name to use for the connection.
  286. * @param userName
  287. */
  288. void set_user_name(string_ref userName);
  289. /**
  290. * Sets the password to use for the connection.
  291. */
  292. void set_password(binary_ref password);
  293. /**
  294. * Sets the maximum number of messages that can be in-flight
  295. * simultaneously.
  296. * @param n The maximum number of inflight messages.
  297. */
  298. void set_max_inflight(int n) { opts_.maxInflight = n; }
  299. /**
  300. * Sets the "Last Will and Testament" (LWT) for the connection.
  301. * @param will The LWT options.
  302. */
  303. void set_will(const will_options& will);
  304. void set_will(will_options&& will);
  305. /**
  306. * Sets the "Last Will and Testament" (LWT) as a message
  307. * @param msg The LWT message
  308. */
  309. void set_will_message(const message& msg) {
  310. set_will(will_options(msg));
  311. }
  312. /**
  313. * Sets the "Last Will and Testament" (LWT) as a message
  314. * @param msg Pointer to a LWT message
  315. */
  316. void set_will_message(const_message_ptr msg) {
  317. if (msg) set_will(will_options(*msg));
  318. }
  319. /**
  320. * Sets the callback context to a delivery token.
  321. * @param tok The delivery token to be used as the callback context.
  322. */
  323. void set_token(const token_ptr& tok);
  324. /**
  325. * Sets the list of servers to which the client will connect.
  326. * @param serverURIs A pointer to a collection of server URI's. Each
  327. * entry should be of the form @em
  328. * protocol://host:port where @em protocol must be
  329. * @em tcp or @em ssl. For @em host, you can specify
  330. * either an IP address or a domain name.
  331. */
  332. void set_servers(const_string_collection_ptr serverURIs);
  333. /**
  334. * Sets the version of MQTT to be used on the connect.
  335. *
  336. * This will also set other connect options to legal values dependent on
  337. * the selected version.
  338. *
  339. * @param mqttVersion The MQTT version to use for the connection:
  340. * @li MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if
  341. * that fails, fall back to 3.1
  342. * @li MQTTVERSION_3_1 (3) = only try version 3.1
  343. * @li MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  344. * @li MQTTVERSION_5 (5) = only try version 5
  345. */
  346. void set_mqtt_version(int mqttVersion);
  347. /**
  348. * Enable or disable automatic reconnects.
  349. * The retry intervals are not affected.
  350. * @param on Whether to turn reconnects on or off
  351. */
  352. void set_automatic_reconnect(bool on) {
  353. opts_.automaticReconnect = to_int(on);
  354. }
  355. /**
  356. * Enable or disable automatic reconnects.
  357. * @param minRetryInterval Minimum retry interval in seconds. Doubled
  358. * on each failed retry.
  359. * @param maxRetryInterval Maximum retry interval in seconds. The
  360. * doubling stops here on failed retries.
  361. */
  362. void set_automatic_reconnect(int minRetryInterval, int maxRetryInterval);
  363. /**
  364. * Enable or disable automatic reconnects.
  365. * @param minRetryInterval Minimum retry interval. Doubled on each
  366. * failed retry.
  367. * @param maxRetryInterval Maximum retry interval. The doubling stops
  368. * here on failed retries.
  369. */
  370. template <class Rep1, class Period1, class Rep2, class Period2>
  371. void set_automatic_reconnect(const std::chrono::duration<Rep1, Period1>& minRetryInterval,
  372. const std::chrono::duration<Rep2, Period2>& maxRetryInterval) {
  373. set_automatic_reconnect((int) to_seconds_count(minRetryInterval),
  374. (int) to_seconds_count(maxRetryInterval));
  375. }
  376. /**
  377. * Determines if the 'clean start' flag is set for the connect.
  378. * @return @em true if the 'clean start' flag is set for the connect, @em
  379. * false if not.
  380. */
  381. bool is_clean_start() const {
  382. return to_bool(opts_.cleanstart);
  383. }
  384. /**
  385. * Sets the 'clean start' flag for the connection.
  386. * @param cleanStart Whether to set the 'clean start' flag for the connect.
  387. */
  388. void set_clean_start(bool cleanStart) {
  389. opts_.cleanstart = to_int(cleanStart);
  390. }
  391. /**
  392. * Gets the connect properties.
  393. * @return A const reference to the properties for the connect.
  394. */
  395. const properties& get_properties() const {
  396. return props_;
  397. }
  398. /**
  399. * Sets the properties for the connect.
  400. * @param props The properties to place into the message.
  401. */
  402. void set_properties(const properties& props) {
  403. props_ = props;
  404. opts_.connectProperties = const_cast<MQTTProperties*>(&props_.c_struct());
  405. }
  406. /**
  407. * Moves the properties for the connect.
  408. * @param props The properties to move into the connect object.
  409. */
  410. void set_properties(properties&& props) {
  411. props_ = props;
  412. opts_.connectProperties = const_cast<MQTTProperties*>(&props_.c_struct());
  413. }
  414. /**
  415. * Gets a string representation of the object.
  416. * @return A string representation of the object.
  417. */
  418. string to_string() const;
  419. };
  420. /** Smart/shared pointer to a connection options object. */
  421. using connect_options_ptr = connect_options::ptr_t;
  422. /////////////////////////////////////////////////////////////////////////////
  423. // end namespace mqtt
  424. }
  425. #endif // __mqtt_connect_options_h