disconnect_options.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "MQTTAsync.h"
  24. #include "mqtt/types.h"
  25. #include "mqtt/token.h"
  26. #include "mqtt/properties.h"
  27. #include <chrono>
  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 const MQTTAsync_disconnectOptions DFLT_C_STRUCT;
  37. /** The default C struct */
  38. static const MQTTAsync_disconnectOptions DFLT_C_STRUCT5;
  39. /** The underlying C disconnect options */
  40. MQTTAsync_disconnectOptions opts_;
  41. /** Shared token pointer for context, if any */
  42. token_ptr tok_;
  43. /** Disconnect message properties */
  44. properties props_;
  45. /** The client has special access */
  46. friend class async_client;
  47. /** The options builder has special access */
  48. friend class disconnect_options_builder;
  49. /**
  50. * Updates the underlying C structure to match our cached data.
  51. */
  52. void update_c_struct();
  53. /** Construct options from a C struct */
  54. disconnect_options(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
  55. public:
  56. /**
  57. * Create an empty delivery response object.
  58. */
  59. disconnect_options();
  60. /**
  61. * Creates disconnect options tied to the specific token.
  62. * @param timeout The timeout (in milliseconds).
  63. */
  64. disconnect_options(int timeout) : disconnect_options() {
  65. set_timeout(timeout);
  66. }
  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)
  73. : disconnect_options() {
  74. set_timeout(to);
  75. }
  76. /**
  77. * Copy constructor.
  78. * @param opt Another object to copy.
  79. */
  80. disconnect_options(const disconnect_options& opt);
  81. /**
  82. * Move constructor.
  83. * @param opt Another object to move into this new one.
  84. */
  85. disconnect_options(disconnect_options&& opt);
  86. /**
  87. * Creates default options for an MQTT v3.x connection.
  88. * @return Default options for an MQTT v3.x connection.
  89. */
  90. static disconnect_options v3();
  91. /**
  92. * Creates default options for an MQTT v5 connection.
  93. * @return Default options for an MQTT v5 connection.
  94. */
  95. static disconnect_options v5();
  96. /**
  97. * Copy assignment.
  98. * @param opt Another object to copy.
  99. */
  100. disconnect_options& operator=(const disconnect_options& opt);
  101. /**
  102. * Move assignment.
  103. * @param opt Another object to move into this new one.
  104. */
  105. disconnect_options& operator=(disconnect_options&& opt);
  106. /**
  107. * Expose the underlying C struct for the unit tests.
  108. */
  109. #if defined(UNIT_TESTS)
  110. const MQTTAsync_disconnectOptions& c_struct() const { return opts_; }
  111. #endif
  112. /**
  113. * Gets the timeout used for disconnecting.
  114. * @return The timeout for disconnecting (in milliseconds).
  115. */
  116. std::chrono::milliseconds get_timeout() const {
  117. return std::chrono::milliseconds(opts_.timeout);
  118. }
  119. /**
  120. * Sets the disconnect timeout, in milliseconds.
  121. * This allows for any remaining in-flight messages to be delivered.
  122. * @param timeout The disconnect timeout (in milliseconds).
  123. */
  124. void set_timeout(int timeout) { opts_.timeout = timeout; }
  125. /**
  126. * Sets the disconnect timeout with a duration.
  127. * This allows for any remaining in-flight messages to be delivered.
  128. * @param to The disconnect connect timeout.
  129. */
  130. template <class Rep, class Period>
  131. void set_timeout(const std::chrono::duration<Rep, Period>& to) {
  132. // TODO: check range
  133. set_timeout((int) to_milliseconds_count(to));
  134. }
  135. /**
  136. * Sets the callback context to a delivery token.
  137. * @param tok The delivery token to be used as the callback context.
  138. * @param mqttVersion The version of MQTT we're using for the
  139. * connection.
  140. */
  141. void set_token(const token_ptr& tok, int mqttVersion);
  142. /**
  143. * Gets the callback context to a delivery token.
  144. * @return The delivery token to be used as the callback context.
  145. */
  146. token_ptr get_token() const { return tok_; }
  147. /**
  148. * Gets the disconnect properties.
  149. * @return A const reference to the properties for the disconnect.
  150. */
  151. const properties& get_properties() const { return props_; }
  152. /**
  153. * Gets a mutable reference to the disconnect properties.
  154. * @return A mutable reference to the properties for the disconnect.
  155. */
  156. properties& get_properties() { return props_; }
  157. /**
  158. * Sets the properties for the connect.
  159. * @param props The properties to place into the message.
  160. */
  161. void set_properties(const properties& props) {
  162. props_ = props;
  163. opts_.properties = props_.c_struct();
  164. }
  165. /**
  166. * Moves the properties for the connect.
  167. * @param props The properties to move into the connect object.
  168. */
  169. void set_properties(properties&& props) {
  170. props_ = std::move(props);
  171. opts_.properties = props_.c_struct();
  172. }
  173. /**
  174. * Gets the reason code for the disconnect.
  175. * @return The reason code for the disconnect.
  176. */
  177. ReasonCode get_reason_code() const {
  178. return ReasonCode(opts_.reasonCode);
  179. }
  180. /**
  181. * Sets the reason code for the disconnect.
  182. * @param code The reason code for the disconnect.
  183. */
  184. void set_reason_code(ReasonCode code) {
  185. opts_.reasonCode = MQTTReasonCodes(code);
  186. }
  187. };
  188. /////////////////////////////////////////////////////////////////////////////
  189. /**
  190. * Class to build connect options.
  191. */
  192. class disconnect_options_builder
  193. {
  194. /** The underlying options */
  195. disconnect_options opts_;
  196. /** Construct options builder from a C struct */
  197. disconnect_options_builder(const MQTTAsync_disconnectOptions& copts) : opts_{copts} {}
  198. public:
  199. /** This class */
  200. using self = disconnect_options_builder;
  201. /**
  202. * Default constructor.
  203. */
  204. disconnect_options_builder() {}
  205. /**
  206. * Creates default options builder for an MQTT v3.x connection.
  207. * @return Default options builder for an MQTT v3.x connection.
  208. */
  209. static disconnect_options_builder v3();
  210. /**
  211. * Creates default options builder for an MQTT v5 connection.
  212. * @return Default options builder for an MQTT v5 connection.
  213. */
  214. static disconnect_options_builder v5();
  215. /**
  216. * Sets the properties for the disconnect message.
  217. * @param props The properties for the disconnect message.
  218. */
  219. auto properties(mqtt::properties&& props) -> self& {
  220. opts_.set_properties(std::move(props));
  221. return *this;
  222. }
  223. /**
  224. * Sets the properties for the disconnect message.
  225. * @param props The properties for the disconnect message.
  226. */
  227. auto properties(const mqtt::properties& props) -> self& {
  228. opts_.set_properties(props);
  229. return *this;
  230. }
  231. /**
  232. * Sets the disconnect connect timeout.
  233. * This allows for any remaining in-flight messages to be delivered.
  234. * @param to The disconnect timeout.
  235. */
  236. template <class Rep, class Period>
  237. auto timeout(const std::chrono::duration<Rep, Period>& to) -> self&{
  238. opts_.set_timeout(to);
  239. return *this;
  240. }
  241. /**
  242. * Sets the reason code for the disconnect.
  243. * @param code The reason code for the disconnect.
  244. */
  245. auto reason_code(ReasonCode code) -> self& {
  246. opts_.set_reason_code(code);
  247. return *this;
  248. }
  249. /**
  250. * Finish building the options and return them.
  251. * @return The option struct as built.
  252. */
  253. disconnect_options finalize() { return opts_; }
  254. };
  255. /////////////////////////////////////////////////////////////////////////////
  256. // end namespace 'mqtt'
  257. }
  258. #endif // __mqtt_disconnect_options_h