disconnect_options.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file disconnect_options.h
  3. /// Implementation of the class 'disconnect_options'
  4. /// @date 26-Aug-2016
  5. /////////////////////////////////////////////////////////////////////////////
  6. /****************************************************************************
  7. * Copyright (c) 2016-2017 Frank Pagliughi <fpagliughi@mindspring.com>
  8. *
  9. * All rights reserved. This program and the accompanying materials
  10. * are made available under the terms of the Eclipse Public License v2.0
  11. * and Eclipse Distribution License v1.0 which accompany this distribution.
  12. *
  13. * The Eclipse Public License is available at
  14. * http://www.eclipse.org/legal/epl-v20.html
  15. * and the Eclipse Distribution License is available at
  16. * http://www.eclipse.org/org/documents/edl-v10.php.
  17. *
  18. * Contributors:
  19. * Frank Pagliughi - initial implementation and documentation
  20. ***************************************************************************/
  21. #ifndef __mqtt_disconnect_options_h
  22. #define __mqtt_disconnect_options_h
  23. #include <chrono>
  24. #include "MQTTAsync.h"
  25. #include "mqtt/properties.h"
  26. #include "mqtt/token.h"
  27. #include "mqtt/types.h"
  28. namespace mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * Options for disconnecting from an MQTT broker.
  32. */
  33. class disconnect_options
  34. {
  35. /** The default C struct */
  36. static constexpr MQTTAsync_disconnectOptions DFLT_C_STRUCT
  37. MQTTAsync_disconnectOptions_initializer;
  38. /** The default C struct */
  39. static constexpr MQTTAsync_disconnectOptions DFLT_C_STRUCT5
  40. MQTTAsync_disconnectOptions_initializer5;
  41. /** The underlying C disconnect options */
  42. MQTTAsync_disconnectOptions opts_{DFLT_C_STRUCT};
  43. /** Shared token pointer for context, if any */
  44. token_ptr tok_;
  45. /** Disconnect message properties */
  46. properties props_;
  47. /** The client has special access */
  48. friend class async_client;
  49. /** The options builder has special access */
  50. friend class disconnect_options_builder;
  51. /**
  52. * Updates the underlying C structure to match our cached data.
  53. */
  54. void update_c_struct();
  55. /** Construct options from a C struct */
  56. disconnect_options(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
  57. public:
  58. /**
  59. * Create an empty delivery response object.
  60. */
  61. disconnect_options() {}
  62. /**
  63. * Creates disconnect options tied to the specific token.
  64. * @param timeout The timeout (in milliseconds).
  65. */
  66. disconnect_options(int timeout) : disconnect_options() { set_timeout(timeout); }
  67. /**
  68. * Creates disconnect options tied to the specific token.
  69. * @param to The timeout.
  70. */
  71. template <class Rep, class Period>
  72. disconnect_options(const std::chrono::duration<Rep, Period>& to) : disconnect_options() {
  73. set_timeout(to);
  74. }
  75. /**
  76. * Copy constructor.
  77. * @param opt Another object to copy.
  78. */
  79. disconnect_options(const disconnect_options& opt);
  80. /**
  81. * Move constructor.
  82. * @param opt Another object to move into this new one.
  83. */
  84. disconnect_options(disconnect_options&& opt);
  85. /**
  86. * Creates default options for an MQTT v3.x connection.
  87. * @return Default options for an MQTT v3.x connection.
  88. */
  89. static disconnect_options v3() { return disconnect_options{DFLT_C_STRUCT}; }
  90. /**
  91. * Creates default options for an MQTT v5 connection.
  92. * @return Default options for an MQTT v5 connection.
  93. */
  94. static disconnect_options v5() { return disconnect_options{DFLT_C_STRUCT5}; }
  95. /**
  96. * Copy assignment.
  97. * @param opt Another object to copy.
  98. */
  99. disconnect_options& operator=(const disconnect_options& opt);
  100. /**
  101. * Move assignment.
  102. * @param opt Another object to move into this new one.
  103. */
  104. disconnect_options& operator=(disconnect_options&& opt);
  105. /**
  106. * Expose the underlying C struct for the unit tests.
  107. */
  108. #if defined(UNIT_TESTS)
  109. const MQTTAsync_disconnectOptions& c_struct() const { return opts_; }
  110. #endif
  111. /**
  112. * Gets the timeout used for disconnecting.
  113. * @return The timeout for disconnecting (in milliseconds).
  114. */
  115. std::chrono::milliseconds get_timeout() const {
  116. return std::chrono::milliseconds(opts_.timeout);
  117. }
  118. /**
  119. * Sets the disconnect timeout, in milliseconds.
  120. * This allows for any remaining in-flight messages to be delivered.
  121. * @param timeout The disconnect timeout (in milliseconds).
  122. */
  123. void set_timeout(int timeout) { opts_.timeout = timeout; }
  124. /**
  125. * Sets the disconnect timeout with a duration.
  126. * This allows for any remaining in-flight messages to be delivered.
  127. * @param to The disconnect connect timeout.
  128. */
  129. template <class Rep, class Period>
  130. void set_timeout(const std::chrono::duration<Rep, Period>& to) {
  131. // TODO: check range
  132. set_timeout((int)to_milliseconds_count(to));
  133. }
  134. /**
  135. * Sets the callback context to a delivery token.
  136. * @param tok The delivery token to be used as the callback context.
  137. * @param mqttVersion The version of MQTT we're using for the
  138. * connection.
  139. */
  140. void set_token(const token_ptr& tok, int mqttVersion);
  141. /**
  142. * Gets the callback context to a delivery token.
  143. * @return The delivery token to be used as the callback context.
  144. */
  145. token_ptr get_token() const { return tok_; }
  146. /**
  147. * Gets the disconnect properties.
  148. * @return A const reference to the properties for the disconnect.
  149. */
  150. const properties& get_properties() const { return props_; }
  151. /**
  152. * Gets a mutable reference to the disconnect properties.
  153. * @return A mutable reference to the properties for the disconnect.
  154. */
  155. properties& get_properties() { return props_; }
  156. /**
  157. * Sets the properties for the connect.
  158. * @param props The properties to place into the message.
  159. */
  160. void set_properties(const properties& props) {
  161. props_ = props;
  162. opts_.properties = props_.c_struct();
  163. }
  164. /**
  165. * Moves the properties for the connect.
  166. * @param props The properties to move into the connect object.
  167. */
  168. void set_properties(properties&& props) {
  169. props_ = std::move(props);
  170. opts_.properties = props_.c_struct();
  171. }
  172. /**
  173. * Gets the reason code for the disconnect.
  174. * @return The reason code for the disconnect.
  175. */
  176. ReasonCode get_reason_code() const { return ReasonCode(opts_.reasonCode); }
  177. /**
  178. * Sets the reason code for the disconnect.
  179. * @param code The reason code for the disconnect.
  180. */
  181. void set_reason_code(ReasonCode code) { opts_.reasonCode = MQTTReasonCodes(code); }
  182. };
  183. /////////////////////////////////////////////////////////////////////////////
  184. /**
  185. * Class to build connect options.
  186. */
  187. class disconnect_options_builder
  188. {
  189. /** The underlying options */
  190. disconnect_options opts_;
  191. /** Construct options builder from a C struct */
  192. disconnect_options_builder(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
  193. public:
  194. /** This class */
  195. using self = disconnect_options_builder;
  196. /**
  197. * Default constructor.
  198. */
  199. disconnect_options_builder() {}
  200. /**
  201. * Creates default options builder for an MQTT v3.x connection.
  202. * @return Default options builder for an MQTT v3.x connection.
  203. */
  204. static disconnect_options_builder v3() {
  205. return disconnect_options_builder{disconnect_options::DFLT_C_STRUCT};
  206. }
  207. /**
  208. * Creates default options builder for an MQTT v5 connection.
  209. * @return Default options builder for an MQTT v5 connection.
  210. */
  211. static disconnect_options_builder v5() {
  212. return disconnect_options_builder{disconnect_options::DFLT_C_STRUCT5};
  213. }
  214. /**
  215. * Sets the properties for the disconnect message.
  216. * @param props The properties for the disconnect message.
  217. */
  218. auto properties(mqtt::properties&& props) -> self& {
  219. opts_.set_properties(std::move(props));
  220. return *this;
  221. }
  222. /**
  223. * Sets the properties for the disconnect message.
  224. * @param props The properties for the disconnect message.
  225. */
  226. auto properties(const mqtt::properties& props) -> self& {
  227. opts_.set_properties(props);
  228. return *this;
  229. }
  230. /**
  231. * Sets the disconnect connect timeout.
  232. * This allows for any remaining in-flight messages to be delivered.
  233. * @param to The disconnect timeout.
  234. */
  235. template <class Rep, class Period>
  236. auto timeout(const std::chrono::duration<Rep, Period>& to) -> self& {
  237. opts_.set_timeout(to);
  238. return *this;
  239. }
  240. /**
  241. * Sets the reason code for the disconnect.
  242. * @param code The reason code for the disconnect.
  243. */
  244. auto reason_code(ReasonCode code) -> self& {
  245. opts_.set_reason_code(code);
  246. return *this;
  247. }
  248. /**
  249. * Finish building the options and return them.
  250. * @return The option struct as built.
  251. */
  252. disconnect_options finalize() { return opts_; }
  253. };
  254. /////////////////////////////////////////////////////////////////////////////
  255. } // namespace mqtt
  256. #endif // __mqtt_disconnect_options_h