response_options.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file response_options.h
  3. /// Implementation of the class 'response_options'
  4. /// @date 26-Aug-2016
  5. /////////////////////////////////////////////////////////////////////////////
  6. #ifndef __mqtt_response_options_h
  7. #define __mqtt_response_options_h
  8. #include "MQTTAsync.h"
  9. #include "mqtt/token.h"
  10. #include "mqtt/delivery_token.h"
  11. #include "subscribe_options.h"
  12. namespace mqtt {
  13. class token_test;
  14. /////////////////////////////////////////////////////////////////////////////
  15. // response_options
  16. /////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * The response options for various asynchronous calls.
  19. *
  20. * This is an internal data structure, only used within the library.
  21. * Therefore it is not totally fleshed out, but rather only exposes the
  22. * functionality currently required by the library.
  23. *
  24. * Note, too, in the C lib, this became a place to add MQTT v5 options for
  25. * the outgoing calls without breaking the API, so is also referred to as the
  26. * "call options".
  27. */
  28. class response_options
  29. {
  30. /** The underlying C structure */
  31. MQTTAsync_responseOptions opts_;
  32. /** The token to which we are connected */
  33. token::weak_ptr_t tok_;
  34. /** Packet Properties (Subscribe/Unsubscribe) */
  35. properties props_;
  36. /** A list of subscription options for subscribe-many */
  37. std::vector<MQTTSubscribe_options> subOpts_;
  38. /** The client has special access */
  39. friend class async_client;
  40. /** Update the underlying C struct to match our data */
  41. void update_c_struct();
  42. public:
  43. /**
  44. * Create an empty response object.
  45. * @param mqttVersion The MQTT version for the response.
  46. */
  47. explicit response_options(int mqttVersion=MQTTVERSION_DEFAULT);
  48. /**
  49. * Creates a response object with the specified callbacks.
  50. * @param tok A token to be used as the context.
  51. * @param mqttVersion The MQTT version for the response.
  52. */
  53. response_options(const token_ptr& tok, int mqttVersion=MQTTVERSION_DEFAULT);
  54. /**
  55. * Copy constructor.
  56. * @param other The other options to copy to this one.
  57. */
  58. response_options(const response_options& other);
  59. /**
  60. * Copy operator.
  61. * @param rhs The other options to copy to this one.
  62. */
  63. response_options& operator=(const response_options& rhs);
  64. /**
  65. * Expose the underlying C struct for the unit tests.
  66. */
  67. #if defined(UNIT_TESTS)
  68. const MQTTAsync_responseOptions& c_struct() const { return opts_; }
  69. #endif
  70. /**
  71. * Sets the MQTT protocol version used for the response.
  72. * This sets up proper callbacks for MQTT v5 or versions prior to that.
  73. * @param mqttVersion The MQTT version used by the connection.
  74. */
  75. void set_mqtt_version(int mqttVersion);
  76. /**
  77. * Sets the callback context to a generic token.
  78. * @param tok The token to be used as the callback context.
  79. */
  80. void set_token(const token_ptr& tok);
  81. /**
  82. * Sets the properties for the connect.
  83. * @param props The properties to place into the message.
  84. */
  85. void set_properties(const properties& props) {
  86. props_ = props;
  87. opts_.properties = props_.c_struct();
  88. }
  89. /**
  90. * Moves the properties for the connect.
  91. * @param props The properties to move into the connect object.
  92. */
  93. void set_properties(properties&& props) {
  94. props_ = std::move(props);
  95. opts_.properties = props_.c_struct();
  96. }
  97. /**
  98. * Sets the options for a single topic subscription.
  99. * @param opts The subscribe options.
  100. */
  101. void set_subscribe_options(const subscribe_options& opts);
  102. /**
  103. * Sets the options for a multi-topic subscription.
  104. * @param opts A vector of the subscribe options.
  105. */
  106. void set_subscribe_options(const std::vector<subscribe_options>& opts);
  107. };
  108. /////////////////////////////////////////////////////////////////////////////
  109. /**
  110. * Class to build response options.
  111. */
  112. class response_options_builder
  113. {
  114. /** The underlying options */
  115. response_options opts_;
  116. public:
  117. /** This class */
  118. using self = response_options_builder;
  119. /**
  120. * Default constructor.
  121. */
  122. explicit response_options_builder(int mqttVersion=MQTTVERSION_DEFAULT)
  123. : opts_(mqttVersion) {}
  124. /**
  125. * Sets the MQTT protocol version used for the response.
  126. * This sets up proper callbacks for MQTT v5 or versions prior to that.
  127. * @param mqttVersion The MQTT version used by the connection.
  128. */
  129. auto mqtt_version(int mqttVersion) -> self& {
  130. opts_.set_mqtt_version(mqttVersion);
  131. return *this;
  132. }
  133. /**
  134. * Sets the callback context to a generic token.
  135. * @param tok The token to be used as the callback context.
  136. */
  137. auto token(const token_ptr& tok) -> self& {
  138. opts_.set_token(tok);
  139. return *this;
  140. }
  141. /**
  142. * Sets the properties for the response options.
  143. * @param props The properties for the response options.
  144. */
  145. auto properties(mqtt::properties&& props) -> self& {
  146. opts_.set_properties(std::move(props));
  147. return *this;
  148. }
  149. /**
  150. * Sets the properties for the disconnect message.
  151. * @param props The properties for the disconnect message.
  152. */
  153. auto properties(const mqtt::properties& props) -> self& {
  154. opts_.set_properties(props);
  155. return *this;
  156. }
  157. /**
  158. * Sets the options for a single topic subscription.
  159. * @param opts The subscribe options.
  160. */
  161. auto subscribe_opts(const subscribe_options& opts) -> self& {
  162. opts_.set_subscribe_options(opts);
  163. return *this;
  164. }
  165. /**
  166. * Sets the options for a multi-topic subscription.
  167. * @param opts A vector of the subscribe options.
  168. */
  169. auto subscribe_opts(const std::vector<subscribe_options>& opts) -> self& {
  170. opts_.set_subscribe_options(opts);
  171. return *this;
  172. }
  173. /**
  174. * Finish building the options and return them.
  175. * @return The option struct as built.
  176. */
  177. response_options finalize() { return opts_; }
  178. };
  179. /////////////////////////////////////////////////////////////////////////////
  180. // delivery_response_options
  181. /////////////////////////////////////////////////////////////////////////////
  182. /**
  183. * The response options for asynchronous calls targeted at delivery.
  184. * Each of these objects is tied to a specific delivery_token.
  185. */
  186. class delivery_response_options
  187. {
  188. /** The underlying C structure */
  189. MQTTAsync_responseOptions opts_;
  190. /** The delivery token to which we are connected */
  191. delivery_token::weak_ptr_t dtok_;
  192. /** The client has special access */
  193. friend class async_client;
  194. public:
  195. /**
  196. * Create an empty delivery response object.
  197. */
  198. delivery_response_options(int mqttVersion=MQTTVERSION_DEFAULT);
  199. /**
  200. * Creates a response object tied to the specific delivery token.
  201. * @param dtok A delivery token to be used as the context.
  202. * @param mqttVersion The MQTT version for the response
  203. */
  204. delivery_response_options(const delivery_token_ptr& dtok,
  205. int mqttVersion=MQTTVERSION_DEFAULT);
  206. /**
  207. * Expose the underlying C struct for the unit tests.
  208. */
  209. #if defined(UNIT_TESTS)
  210. const MQTTAsync_responseOptions& c_struct() const { return opts_; }
  211. #endif
  212. /**
  213. * Sets the callback context to a delivery token.
  214. * @param dtok The delivery token to be used as the callback context.
  215. */
  216. void set_token(const delivery_token_ptr& dtok) {
  217. dtok_ = dtok;
  218. opts_.context = dtok.get();
  219. }
  220. };
  221. /////////////////////////////////////////////////////////////////////////////
  222. // end namespace 'mqtt'
  223. }
  224. #endif // __mqtt_response_options_h