iasync_client.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file iasync_client.h
  3. /// Implementation of the interface for the asynchronous clients,
  4. /// 'iasync_client'
  5. /// @date 25-Aug-2016
  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_iasync_client_h
  23. #define __mqtt_iasync_client_h
  24. #include "mqtt/types.h"
  25. #include "mqtt/token.h"
  26. #include "mqtt/delivery_token.h"
  27. #include "mqtt/iclient_persistence.h"
  28. #include "mqtt/iaction_listener.h"
  29. #include "mqtt/connect_options.h"
  30. #include "mqtt/disconnect_options.h"
  31. #include "mqtt/subscribe_options.h"
  32. #include "mqtt/exception.h"
  33. #include "mqtt/message.h"
  34. #include "mqtt/callback.h"
  35. #include <vector>
  36. namespace mqtt {
  37. /////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * Enables an application to communicate with an MQTT server using
  40. * non-blocking methods.
  41. *
  42. * It provides applications a simple programming interface to all features
  43. * of the MQTT version 3.1 specification including:
  44. *
  45. * @li connect
  46. * @li publish
  47. * @li subscribe
  48. * @li unsubscribe
  49. * @li disconnect
  50. */
  51. class iasync_client
  52. {
  53. friend class token;
  54. virtual void remove_token(token* tok) =0;
  55. public:
  56. /** Type for a collection of QOS values */
  57. using qos_collection = std::vector<int>;
  58. /**
  59. * Virtual destructor
  60. */
  61. virtual ~iasync_client() {}
  62. /**
  63. * Connects to an MQTT server using the default options.
  64. * @return token used to track and wait for the connect to complete. The
  65. * token will be passed to any callback that has been set.
  66. * @throw exception for non security related problems
  67. * @throw security_exception for security related problems
  68. */
  69. virtual token_ptr connect() =0;
  70. /**
  71. * Connects to an MQTT server using the provided connect options.
  72. * @param options a set of connection parameters that override the
  73. * defaults.
  74. * @return token used to track and wait for the connect to complete. The
  75. * token will be passed to any callback that has been set.
  76. * @throw exception for non security related problems
  77. * @throw security_exception for security related problems
  78. */
  79. virtual token_ptr connect(connect_options options) =0;
  80. /**
  81. * Connects to an MQTT server using the specified options.
  82. *
  83. * @param options a set of connection parameters that override the
  84. * defaults.
  85. * @param userContext optional object used to pass context to the
  86. * callback. Use @em nullptr if not required.
  87. * @param cb callback listener that will be notified when the connect
  88. * completes.
  89. * @return token used to track and wait for the connect to complete. The
  90. * token will be passed to any callback that has been set.
  91. * @throw exception for non security related problems
  92. * @throw security_exception for security related problems
  93. */
  94. virtual token_ptr connect(connect_options options, void* userContext,
  95. iaction_listener& cb) =0;
  96. /**
  97. *
  98. * @param userContext optional object used to pass context to the
  99. * callback. Use @em nullptr if not required.
  100. * @param cb listener that will be notified when the connect completes.
  101. * @return token used to track and wait for the connect to complete. The
  102. * token will be passed to any callback that has been set.
  103. * @throw exception for non security related problems
  104. * @throw security_exception for security related problems
  105. */
  106. virtual token_ptr connect(void* userContext, iaction_listener& cb) =0;
  107. /**
  108. * Reconnects the client using options from the previous connect.
  109. * The client must have previously called connect() for this to work.
  110. * @return token used to track the progress of the reconnect.
  111. */
  112. virtual token_ptr reconnect() =0;
  113. /**
  114. * Disconnects from the server.
  115. * @return token used to track and wait for the disconnect to complete.
  116. * The token will be passed to any callback that has been set.
  117. * @throw exception for problems encountered while disconnecting
  118. */
  119. virtual token_ptr disconnect() =0;
  120. /**
  121. * Disconnects from the server.
  122. * @param opts Options for disconnecting.
  123. * @return token used to track and wait for the disconnect to complete.
  124. * The token will be passed to any callback that has been set.
  125. * @throw exception for problems encountered while disconnecting
  126. */
  127. virtual token_ptr disconnect(disconnect_options opts) =0;
  128. /**
  129. * Disconnects from the server.
  130. * @param timeout the amount of time in milliseconds to allow for
  131. * existing work to finish before disconnecting. A value
  132. * of zero or less means the client will not quiesce.
  133. * @return token used to track and wait for the disconnect to complete.
  134. * The token will be passed to any callback that has been set.
  135. * @throw exception for problems encountered while disconnecting
  136. */
  137. virtual token_ptr disconnect(int timeout) =0;
  138. /**
  139. * Disconnects from the server.
  140. * @param timeout the amount of time in milliseconds to allow for
  141. * existing work to finish before disconnecting. A value
  142. * of zero or less means the client will not quiesce.
  143. * @param userContext optional object used to pass context to the
  144. * callback. Use @em nullptr if not required.
  145. * @param cb listener that will be notified when the disconnect
  146. * completes.
  147. * @return token used to track and wait for the disconnect to complete.
  148. * The token will be passed to any callback that has been set.
  149. * @throw exception for problems encountered while disconnecting
  150. */
  151. virtual token_ptr disconnect(int timeout, void* userContext, iaction_listener& cb) =0;
  152. /**
  153. * Disconnects from the server.
  154. * @param userContext optional object used to pass context to the
  155. * callback. Use @em nullptr if not required.
  156. * @param cb listener that will be notified when the disconnect
  157. * completes.
  158. * @return token used to track and wait for the disconnect to complete.
  159. * The token will be passed to any callback that has been set.
  160. * @throw exception for problems encountered while disconnecting
  161. */
  162. virtual token_ptr disconnect(void* userContext, iaction_listener& cb) =0;
  163. /**
  164. * Returns the delivery token for the specified message ID.
  165. * @return delivery_token
  166. */
  167. virtual delivery_token_ptr get_pending_delivery_token(int msgID) const =0;
  168. /**
  169. * Returns the delivery tokens for any outstanding publish operations.
  170. * @return delivery_token[]
  171. */
  172. virtual std::vector<delivery_token_ptr> get_pending_delivery_tokens() const =0;
  173. /**
  174. * Returns the client ID used by this client.
  175. * @return string
  176. */
  177. virtual string get_client_id() const =0;
  178. /**
  179. * Returns the address of the server used by this client.
  180. */
  181. virtual string get_server_uri() const =0;
  182. /**
  183. * Determines if this client is currently connected to the server.
  184. */
  185. virtual bool is_connected() const =0;
  186. /**
  187. * Publishes a message to a topic on the server
  188. * @param topic The topic to deliver the message to
  189. * @param payload the bytes to use as the message payload
  190. * @param n the number of bytes in the payload
  191. * @param qos the Quality of Service to deliver the message at. Valid
  192. * values are 0, 1 or 2.
  193. * @param retained whether or not this message should be retained by the
  194. * server.
  195. * @return token used to track and wait for the publish to complete. The
  196. * token will be passed to callback methods if set.
  197. */
  198. virtual delivery_token_ptr publish(string_ref topic,
  199. const void* payload, size_t n,
  200. int qos, bool retained) =0;
  201. /**
  202. * Publishes a message to a topic on the server
  203. * @param topic The topic to deliver the message to
  204. * @param payload the bytes to use as the message payload
  205. * @param n the number of bytes in the payload
  206. * @return token used to track and wait for the publish to complete. The
  207. * token will be passed to callback methods if set.
  208. */
  209. virtual delivery_token_ptr publish(string_ref topic,
  210. const void* payload, size_t n) =0;
  211. /**
  212. * Publishes a message to a topic on the server
  213. * @param topic The topic to deliver the message to
  214. * @param payload the bytes to use as the message payload
  215. * @param n the number of bytes in the payload
  216. * @param qos the Quality of Service to deliver the message at. Valid
  217. * values are 0, 1 or 2.
  218. * @param retained whether or not this message should be retained by the
  219. * server.
  220. * @param userContext optional object used to pass context to the
  221. * callback. Use @em nullptr if not required.
  222. * @param cb
  223. * @return token used to track and wait for the publish to complete. The
  224. * token will be passed to callback methods if set.
  225. */
  226. virtual delivery_token_ptr publish(string_ref topic,
  227. const void* payload, size_t n,
  228. int qos, bool retained,
  229. void* userContext, iaction_listener& cb) =0;
  230. /**
  231. * Publishes a message to a topic on the server
  232. * @param topic The topic to deliver the message to
  233. * @param payload the bytes to use as the message payload
  234. * @param qos the Quality of Service to deliver the message at. Valid
  235. * values are 0, 1 or 2.
  236. * @param retained whether or not this message should be retained by the
  237. * server.
  238. * @return token used to track and wait for the publish to complete. The
  239. * token will be passed to callback methods if set.
  240. */
  241. virtual delivery_token_ptr publish(string_ref topic, binary_ref payload,
  242. int qos, bool retained) =0;
  243. /**
  244. * Publishes a message to a topic on the server.
  245. * @param topic The topic to deliver the message to
  246. * @param payload the bytes to use as the message payload
  247. * @return token used to track and wait for the publish to complete. The
  248. * token will be passed to callback methods if set.
  249. */
  250. virtual delivery_token_ptr publish(string_ref topic, binary_ref payload) =0;
  251. /**
  252. * Publishes a message to a topic on the server Takes an Message
  253. * message and delivers it to the server at the requested quality of
  254. * service.
  255. * @param msg the message to deliver to the server
  256. * @return token used to track and wait for the publish to complete. The
  257. * token will be passed to callback methods if set.
  258. */
  259. virtual delivery_token_ptr publish(const_message_ptr msg) =0;
  260. /**
  261. * Publishes a message to a topic on the server.
  262. * @param msg the message to deliver to the server
  263. * @param userContext optional object used to pass context to the
  264. * callback. Use @em nullptr if not required.
  265. * @param cb optional listener that will be notified when message
  266. * delivery has completed to the requested quality of service
  267. * @return token used to track and wait for the publish to complete. The
  268. * token will be passed to callback methods if set.
  269. */
  270. virtual delivery_token_ptr publish(const_message_ptr msg,
  271. void* userContext, iaction_listener& cb) =0;
  272. /**
  273. * Sets a callback listener to use for events that happen
  274. * asynchronously.
  275. * @param cb callback which will be invoked for certain asynchronous
  276. * events
  277. */
  278. virtual void set_callback(callback& cb) =0;
  279. /**
  280. * Stops the callbacks.
  281. */
  282. virtual void disable_callbacks() =0;
  283. /**
  284. * Subscribe to a topic, which may include wildcards.
  285. * @param topicFilter the topic to subscribe to, which can include
  286. * wildcards.
  287. * @param qos the maximum quality of service at which to subscribe.
  288. * Messages published at a lower quality of service will be
  289. * received at the published QoS. Messages published at a
  290. * higher quality of service will be received using the QoS
  291. * specified on the subscribe.
  292. * @param opts The options for the subscription.
  293. * @return token used to track and wait for the subscribe to complete.
  294. * The token will be passed to callback methods if set.
  295. */
  296. virtual token_ptr subscribe(const string& topicFilter, int qos,
  297. const subscribe_options& opts=subscribe_options()) =0;
  298. /**
  299. * Subscribe to a topic, which may include wildcards.
  300. * @param topicFilter the topic to subscribe to, which can include
  301. * wildcards.
  302. * @param qos the maximum quality of service at which to subscribe.
  303. * Messages published at a lower quality of service will be
  304. * received at the published QoS. Messages published at a
  305. * higher quality of service will be received using the QoS
  306. * specified on the subscribe.
  307. * @param userContext optional object used to pass context to the
  308. * callback. Use @em nullptr if not required.
  309. * @param callback listener that will be notified when subscribe has
  310. * completed
  311. * @param opts The options for the subscription.
  312. * @return token used to track and wait for the subscribe to complete.
  313. * The token will be passed to callback methods if set.
  314. */
  315. virtual token_ptr subscribe(const string& topicFilter, int qos,
  316. void* userContext, iaction_listener& callback,
  317. const subscribe_options& opts=subscribe_options()) =0;
  318. /**
  319. * Subscribe to multiple topics, each of which may include wildcards.
  320. * Provides an optimized way to subscribe to multiple topics compared to
  321. * subscribing to each one individually.
  322. * @param topicFilters one or more topics to subscribe to, which can
  323. * include wildcards
  324. * @param qos the maximum quality of service at which to subscribe.
  325. * Messages published at a lower quality of service will be
  326. * received at the published QoS. Messages published at a
  327. * higher quality of service will be received using the QoS
  328. * specified on the subscribe.
  329. * @param opts A collection of subscription optsions (one for each
  330. * topic)
  331. * @return token used to track and wait for the subscribe to complete.
  332. * The token will be passed to callback methods if set.
  333. */
  334. virtual token_ptr subscribe(const_string_collection_ptr topicFilters,
  335. const qos_collection& qos,
  336. const std::vector<subscribe_options>& opts=std::vector<subscribe_options>()) =0;
  337. /**
  338. * Subscribes to multiple topics, each of which may include wildcards.
  339. * @param topicFilters one or more topics to subscribe to, which can
  340. * include wildcards
  341. * @param qos the maximum quality of service at which to subscribe.
  342. * Messages published at a lower quality of service will be
  343. * received at the published QoS. Messages published at a
  344. * higher quality of service will be received using the QoS
  345. * specified on the subscribe.
  346. * @param userContext optional object used to pass context to the
  347. * callback. Use @em nullptr if not required.
  348. * @param callback listener that will be notified when subscribe has
  349. * completed
  350. * @param opts A collection of subscription optsions (one for each
  351. * topic)
  352. * @return token used to track and wait for the subscribe to complete.
  353. * The token will be passed to callback methods if set.
  354. */
  355. virtual token_ptr subscribe(const_string_collection_ptr topicFilters,
  356. const qos_collection& qos,
  357. void* userContext, iaction_listener& callback,
  358. const std::vector<subscribe_options>& opts=std::vector<subscribe_options>()) =0;
  359. /**
  360. * Requests the server unsubscribe the client from a topic.
  361. * @param topicFilter the topic to unsubscribe from. It must match a
  362. * topicFilter specified on an earlier subscribe.
  363. * @return token used to track and wait for the unsubscribe to complete.
  364. * The token will be passed to callback methods if set.
  365. */
  366. virtual token_ptr unsubscribe(const string& topicFilter) =0;
  367. /**
  368. * Requests the server unsubscribe the client from one or more topics.
  369. * @param topicFilters one or more topics to unsubscribe from. Each
  370. * topicFilter must match one specified on an
  371. * earlier subscribe.
  372. * @return token used to track and wait for the unsubscribe to complete.
  373. * The token will be passed to callback methods if set.
  374. */
  375. virtual token_ptr unsubscribe(const_string_collection_ptr topicFilters) =0;
  376. /**
  377. * Requests the server unsubscribe the client from one or more topics.
  378. * @param topicFilters one or more topics to unsubscribe from. Each
  379. * topicFilter must match one specified on an
  380. * earlier subscribe.
  381. * @param userContext optional object used to pass context to the
  382. * callback. Use @em nullptr if not required.
  383. * @param cb listener that will be notified when unsubscribe has
  384. * completed
  385. * @return token used to track and wait for the unsubscribe to complete.
  386. * The token will be passed to callback methods if set.
  387. */
  388. virtual token_ptr unsubscribe(const_string_collection_ptr topicFilters,
  389. void* userContext, iaction_listener& cb) =0;
  390. /**
  391. * Requests the server unsubscribe the client from a topics.
  392. * @param topicFilter the topic to unsubscribe from. It must match a
  393. * topicFilter specified on an earlier subscribe.
  394. * @param userContext optional object used to pass context to the
  395. * callback. Use @em nullptr if not required.
  396. * @param cb listener that will be notified when unsubscribe has
  397. * completed
  398. * @return token used to track and wait for the unsubscribe to complete.
  399. * The token will be passed to callback methods if set.
  400. */
  401. virtual token_ptr unsubscribe(const string& topicFilter,
  402. void* userContext, iaction_listener& cb) =0;
  403. };
  404. /////////////////////////////////////////////////////////////////////////////
  405. // end namespace 'mqtt'
  406. }
  407. #endif // __mqtt_iasync_client_h