iasync_client.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 v2.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-v20.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 <vector>
  25. #include "mqtt/callback.h"
  26. #include "mqtt/connect_options.h"
  27. #include "mqtt/delivery_token.h"
  28. #include "mqtt/disconnect_options.h"
  29. #include "mqtt/event.h"
  30. #include "mqtt/exception.h"
  31. #include "mqtt/iaction_listener.h"
  32. #include "mqtt/iclient_persistence.h"
  33. #include "mqtt/message.h"
  34. #include "mqtt/subscribe_options.h"
  35. #include "mqtt/token.h"
  36. #include "mqtt/types.h"
  37. namespace mqtt {
  38. /////////////////////////////////////////////////////////////////////////////
  39. /**
  40. * Enables an application to communicate with an MQTT server using
  41. * non-blocking methods.
  42. *
  43. * It provides applications a simple programming interface to all features
  44. * of the MQTT version 3.1 specification including:
  45. *
  46. * @li connect
  47. * @li publish
  48. * @li subscribe
  49. * @li unsubscribe
  50. * @li disconnect
  51. */
  52. class iasync_client
  53. {
  54. friend class token;
  55. virtual void remove_token(token* tok) = 0;
  56. public:
  57. /** Type for a collection of QOS values */
  58. using qos_collection = std::vector<int>;
  59. /**
  60. * Virtual destructor
  61. */
  62. virtual ~iasync_client() {}
  63. /**
  64. * Connects to an MQTT server using the default options.
  65. * @return token used to track and wait for the connect to complete. The
  66. * token will be passed to any callback that has been set.
  67. * @throw exception for non security related problems
  68. * @throw security_exception for security related problems
  69. */
  70. virtual token_ptr connect() = 0;
  71. /**
  72. * Connects to an MQTT server using the provided connect options.
  73. * @param options a set of connection parameters that override the
  74. * defaults.
  75. * @return token used to track and wait for the connect to complete. The
  76. * token will be passed to any callback that has been set.
  77. * @throw exception for non security related problems
  78. * @throw security_exception for security related problems
  79. */
  80. virtual token_ptr connect(connect_options options) = 0;
  81. /**
  82. * Connects to an MQTT server using the specified options.
  83. *
  84. * @param options a set of connection parameters that override the
  85. * defaults.
  86. * @param userContext optional object used to pass context to the
  87. * callback. Use @em nullptr if not required.
  88. * @param cb callback listener that will be notified when the connect
  89. * completes.
  90. * @return token used to track and wait for the connect to complete. The
  91. * token will be passed to any callback that has been set.
  92. * @throw exception for non security related problems
  93. * @throw security_exception for security related problems
  94. */
  95. virtual token_ptr connect(
  96. connect_options options, void* userContext, iaction_listener& cb
  97. ) = 0;
  98. /**
  99. *
  100. * @param userContext optional object used to pass context to the
  101. * callback. Use @em nullptr if not required.
  102. * @param cb listener that will be notified when the connect completes.
  103. * @return token used to track and wait for the connect to complete. The
  104. * token will be passed to any callback that has been set.
  105. * @throw exception for non security related problems
  106. * @throw security_exception for security related problems
  107. */
  108. virtual token_ptr connect(void* userContext, iaction_listener& cb) = 0;
  109. /**
  110. * Reconnects the client using options from the previous connect.
  111. * The client must have previously called connect() for this to work.
  112. * @return token used to track the progress of the reconnect.
  113. */
  114. virtual token_ptr reconnect() = 0;
  115. /**
  116. * Disconnects from the server.
  117. * @return token used to track and wait for the disconnect to complete.
  118. * The token will be passed to any callback that has been set.
  119. * @throw exception for problems encountered while disconnecting
  120. */
  121. virtual token_ptr disconnect() = 0;
  122. /**
  123. * Disconnects from the server.
  124. * @param opts Options for disconnecting.
  125. * @return token used to track and wait for the disconnect to complete.
  126. * The token will be passed to any callback that has been set.
  127. * @throw exception for problems encountered while disconnecting
  128. */
  129. virtual token_ptr disconnect(disconnect_options opts) = 0;
  130. /**
  131. * Disconnects from the server.
  132. * @param timeout the amount of time in milliseconds to allow for
  133. * existing work to finish before disconnecting. A value
  134. * of zero or less means the client will not quiesce.
  135. * @return token used to track and wait for the disconnect to complete.
  136. * The token will be passed to any callback that has been set.
  137. * @throw exception for problems encountered while disconnecting
  138. */
  139. virtual token_ptr disconnect(int timeout) = 0;
  140. /**
  141. * Disconnects from the server.
  142. * @param timeout the amount of time in milliseconds to allow for
  143. * existing work to finish before disconnecting. A value
  144. * of zero or less means the client will not quiesce.
  145. * @param userContext optional object used to pass context to the
  146. * callback. Use @em nullptr if not required.
  147. * @param cb listener that will be notified when the disconnect
  148. * completes.
  149. * @return token used to track and wait for the disconnect to complete.
  150. * The token will be passed to any callback that has been set.
  151. * @throw exception for problems encountered while disconnecting
  152. */
  153. virtual token_ptr disconnect(int timeout, void* userContext, iaction_listener& cb) = 0;
  154. /**
  155. * Disconnects from the server.
  156. * @param userContext optional object used to pass context to the
  157. * callback. Use @em nullptr if not required.
  158. * @param cb listener that will be notified when the disconnect
  159. * completes.
  160. * @return token used to track and wait for the disconnect to complete.
  161. * The token will be passed to any callback that has been set.
  162. * @throw exception for problems encountered while disconnecting
  163. */
  164. virtual token_ptr disconnect(void* userContext, iaction_listener& cb) = 0;
  165. /**
  166. * Returns the delivery token for the specified message ID.
  167. * @return delivery_token
  168. */
  169. virtual delivery_token_ptr get_pending_delivery_token(int msgID) const = 0;
  170. /**
  171. * Returns the delivery tokens for any outstanding publish operations.
  172. * @return delivery_token[]
  173. */
  174. virtual std::vector<delivery_token_ptr> get_pending_delivery_tokens() const = 0;
  175. /**
  176. * Returns the client ID used by this client.
  177. * @return string
  178. */
  179. virtual string get_client_id() const = 0;
  180. /**
  181. * Returns the address of the server used by this client.
  182. */
  183. virtual string get_server_uri() const = 0;
  184. /**
  185. * Determines if this client is currently connected to the server.
  186. */
  187. virtual bool is_connected() const = 0;
  188. /**
  189. * Publishes a message to a topic on the server
  190. * @param topic The topic to deliver the message to
  191. * @param payload The bytes to use as the message payload
  192. * @param n The number of bytes in the payload
  193. * @param qos The Quality of Service to deliver the message at. Valid
  194. * values are 0, 1 or 2.
  195. * @param retained Whether or not this message should be retained by the
  196. * server.
  197. * @return token used to track and wait for the publish to complete. The
  198. * token will be passed to callback methods if set.
  199. */
  200. virtual delivery_token_ptr publish(
  201. string_ref topic, const void* payload, size_t n, int qos, bool retained,
  202. const properties& props = properties()
  203. ) = 0;
  204. /**
  205. * Publishes a message to a topic on the server
  206. * @param topic The topic to deliver the message to
  207. * @param payload the bytes to use as the message payload
  208. * @param n the number of bytes in the payload
  209. * @return token used to track and wait for the publish to complete. The
  210. * token will be passed to callback methods if set.
  211. */
  212. virtual delivery_token_ptr publish(string_ref topic, const void* payload, size_t n) = 0;
  213. /**
  214. * Publishes a message to a topic on the server
  215. * @param topic The topic to deliver the message to
  216. * @param payload The bytes to use as the message payload
  217. * @param n The number of bytes in the payload
  218. * @param qos The Quality of Service to deliver the message at. Valid
  219. * values are 0, 1 or 2.
  220. * @param retained whether or not this message should be retained by the
  221. * server.
  222. * @param userContext Optional object used to pass context to the
  223. * callback. Use @em nullptr if not required.
  224. * @param cb The listener callback object
  225. * @return token used to track and wait for the publish to complete. The
  226. * token will be passed to callback methods if set.
  227. */
  228. virtual delivery_token_ptr publish(
  229. string_ref topic, const void* payload, size_t n, int qos, bool retained,
  230. void* userContext, iaction_listener& cb
  231. ) = 0;
  232. /**
  233. * Publishes a message to a topic on the server
  234. * @param topic The topic to deliver the message to
  235. * @param payload the bytes to use as the message payload
  236. * @param qos the Quality of Service to deliver the message at. Valid
  237. * values are 0, 1 or 2.
  238. * @param retained whether or not this message should be retained by the
  239. * server.
  240. * @return token used to track and wait for the publish to complete. The
  241. * token will be passed to callback methods if set.
  242. */
  243. virtual delivery_token_ptr publish(
  244. string_ref topic, binary_ref payload, int qos, bool retained,
  245. const properties& props = properties()
  246. ) = 0;
  247. /**
  248. * Publishes a message to a topic on the server.
  249. * @param topic The topic to deliver the message to
  250. * @param payload the bytes to use as the message payload
  251. * @return token used to track and wait for the publish to complete. The
  252. * token will be passed to callback methods if set.
  253. */
  254. virtual delivery_token_ptr publish(string_ref topic, binary_ref payload) = 0;
  255. /**
  256. * Publishes a message to a topic on the server Takes an Message
  257. * message and delivers it to the server at the requested quality of
  258. * service.
  259. * @param msg the message to deliver to the server
  260. * @return token used to track and wait for the publish to complete. The
  261. * token will be passed to callback methods if set.
  262. */
  263. virtual delivery_token_ptr publish(const_message_ptr msg) = 0;
  264. /**
  265. * Publishes a message to a topic on the server.
  266. * @param msg the message to deliver to the server
  267. * @param userContext optional object used to pass context to the
  268. * callback. Use @em nullptr if not required.
  269. * @param cb optional listener that will be notified when message
  270. * delivery has completed to the requested quality of service
  271. * @return token used to track and wait for the publish to complete. The
  272. * token will be passed to callback methods if set.
  273. */
  274. virtual delivery_token_ptr publish(
  275. const_message_ptr msg, void* userContext, iaction_listener& cb
  276. ) = 0;
  277. /**
  278. * Sets a callback listener to use for events that happen
  279. * asynchronously.
  280. * @param cb callback which will be invoked for certain asynchronous
  281. * events
  282. */
  283. virtual void set_callback(callback& cb) = 0;
  284. /**
  285. * Stops the callbacks.
  286. */
  287. virtual void disable_callbacks() = 0;
  288. /**
  289. * Subscribe to a topic, which may include wildcards.
  290. * @param topicFilter the topic to subscribe to, which can include
  291. * wildcards.
  292. * @param qos the maximum quality of service at which to subscribe.
  293. * Messages published at a lower quality of service will be
  294. * received at the published QoS. Messages published at a
  295. * higher quality of service will be received using the QoS
  296. * specified on the subscribe.
  297. * @param opts The options for the subscription.
  298. * @param props The MQTT v5 properties.
  299. * @return token used to track and wait for the subscribe to complete.
  300. * The token will be passed to callback methods if set.
  301. */
  302. virtual token_ptr subscribe(
  303. const string& topicFilter, int qos,
  304. const subscribe_options& opts = subscribe_options(),
  305. const properties& props = properties()
  306. ) = 0;
  307. /**
  308. * Subscribe to a topic, which may include wildcards.
  309. * @param topicFilter the topic to subscribe to, which can include
  310. * wildcards.
  311. * @param qos the maximum quality of service at which to subscribe.
  312. * Messages published at a lower quality of service will be
  313. * received at the published QoS. Messages published at a
  314. * higher quality of service will be received using the QoS
  315. * specified on the subscribe.
  316. * @param userContext optional object used to pass context to the
  317. * callback. Use @em nullptr if not required.
  318. * @param callback listener that will be notified when subscribe has
  319. * completed
  320. * @param opts The options for the subscription.
  321. * @param props The MQTT v5 properties.
  322. * @return token used to track and wait for the subscribe to complete.
  323. * The token will be passed to callback methods if set.
  324. */
  325. virtual token_ptr subscribe(
  326. const string& topicFilter, int qos, void* userContext, iaction_listener& callback,
  327. const subscribe_options& opts = subscribe_options(),
  328. const properties& props = properties()
  329. ) = 0;
  330. /**
  331. * Subscribe to multiple topics, each of which may include wildcards.
  332. * Provides an optimized way to subscribe to multiple topics compared to
  333. * subscribing to each one individually.
  334. * @param topicFilters one or more topics to subscribe to, which can
  335. * include wildcards
  336. * @param qos the maximum quality of service at which to subscribe.
  337. * Messages published at a lower quality of service will be
  338. * received at the published QoS. Messages published at a
  339. * higher quality of service will be received using the QoS
  340. * specified on the subscribe.
  341. * @param opts A collection of subscription options (one for each
  342. * topic)
  343. * @param props The MQTT v5 properties.
  344. * @return token used to track and wait for the subscribe to complete.
  345. * The token will be passed to callback methods if set.
  346. */
  347. virtual token_ptr subscribe(
  348. const_string_collection_ptr topicFilters, const qos_collection& qos,
  349. const std::vector<subscribe_options>& opts = std::vector<subscribe_options>(),
  350. const properties& props = properties()
  351. ) = 0;
  352. /**
  353. * Subscribes to multiple topics, each of which may include wildcards.
  354. * @param topicFilters one or more topics to subscribe to, which can
  355. * include wildcards
  356. * @param qos the maximum quality of service at which to subscribe.
  357. * Messages published at a lower quality of service will be
  358. * received at the published QoS. Messages published at a
  359. * higher quality of service will be received using the QoS
  360. * specified on the subscribe.
  361. * @param userContext optional object used to pass context to the
  362. * callback. Use @em nullptr if not required.
  363. * @param callback listener that will be notified when subscribe has
  364. * completed
  365. * @param opts A collection of subscription options (one for each
  366. * topic)
  367. * @param props The MQTT v5 properties.
  368. * @return token used to track and wait for the subscribe to complete.
  369. * The token will be passed to callback methods if set.
  370. */
  371. virtual token_ptr subscribe(
  372. const_string_collection_ptr topicFilters, const qos_collection& qos,
  373. void* userContext, iaction_listener& callback,
  374. const std::vector<subscribe_options>& opts = std::vector<subscribe_options>(),
  375. const properties& props = properties()
  376. ) = 0;
  377. /**
  378. * Requests the server unsubscribe the client from a topic.
  379. * @param topicFilter the topic to unsubscribe from. It must match a
  380. * topicFilter specified on an earlier subscribe.
  381. * @param props The MQTT v5 properties.
  382. * @return token used to track and wait for the unsubscribe to complete.
  383. * The token will be passed to callback methods if set.
  384. */
  385. virtual token_ptr unsubscribe(
  386. const string& topicFilter, const properties& props = properties()
  387. ) = 0;
  388. /**
  389. * Requests the server unsubscribe the client from one or more topics.
  390. * @param topicFilters one or more topics to unsubscribe from. Each
  391. * topicFilter must match one specified on an
  392. * earlier subscribe.
  393. * @param props The MQTT v5 properties.
  394. * @return token used to track and wait for the unsubscribe to complete.
  395. * The token will be passed to callback methods if set.
  396. */
  397. virtual token_ptr unsubscribe(
  398. const_string_collection_ptr topicFilters, const properties& props = properties()
  399. ) = 0;
  400. /**
  401. * Requests the server unsubscribe the client from one or more topics.
  402. * @param topicFilters one or more topics to unsubscribe from. Each
  403. * topicFilter must match one specified on an
  404. * earlier subscribe.
  405. * @param userContext optional object used to pass context to the
  406. * callback. Use @em nullptr if not required.
  407. * @param cb listener that will be notified when unsubscribe has
  408. * completed
  409. * @param props The MQTT v5 properties.
  410. * @return token used to track and wait for the unsubscribe to complete.
  411. * The token will be passed to callback methods if set.
  412. */
  413. virtual token_ptr unsubscribe(
  414. const_string_collection_ptr topicFilters, void* userContext, iaction_listener& cb,
  415. const properties& props = properties()
  416. ) = 0;
  417. /**
  418. * Requests the server unsubscribe the client from a topics.
  419. * @param topicFilter the topic to unsubscribe from. It must match a
  420. * topicFilter specified on an earlier subscribe.
  421. * @param userContext optional object used to pass context to the
  422. * callback. Use @em nullptr if not required.
  423. * @param cb listener that will be notified when unsubscribe has
  424. * completed.
  425. * @param props The MQTT v5 properties.
  426. * @return Token used to track and wait for the unsubscribe to complete.
  427. * The token will be passed to callback methods if set.
  428. */
  429. virtual token_ptr unsubscribe(
  430. const string& topicFilter, void* userContext, iaction_listener& cb,
  431. const properties& props = properties()
  432. ) = 0;
  433. /**
  434. * Start consuming messages.
  435. * This initializes the client to receive messages through a queue that
  436. * can be read synchronously.
  437. */
  438. virtual void start_consuming() = 0;
  439. /**
  440. * Stop consuming messages.
  441. * This shuts down the internal callback and discards any unread
  442. * messages.
  443. */
  444. virtual void stop_consuming() = 0;
  445. /**
  446. * This clears the consumer queue, discarding any pending event.
  447. */
  448. virtual void clear_consumer() {}
  449. /**
  450. * Determines if the consumer queue has been closed.
  451. * Once closed, any events in the queue can still be read, but no new
  452. * events can be added to it.
  453. * @return @true if the consumer queue has been closed, @false
  454. * otherwise.
  455. */
  456. virtual bool consumer_closed() noexcept { return false; }
  457. /**
  458. * Determines if the consumer queue is "done" (closed and empty).
  459. * Once the queue is done, no more events can be added or removed from
  460. * the queue.
  461. * @return @true if the consumer queue is closed and empty, @false
  462. * otherwise.
  463. */
  464. virtual bool consumer_done() noexcept { return false; }
  465. /**
  466. * Gets the number of events available for immediate consumption.
  467. * Note that this retrieves the number of "raw" events, not messages,
  468. * e.g. may include a connected_event which is not returned by try_consume_message().
  469. * When polling the queue from multiple threads, prefer using try_consume_event(),
  470. * as the event count may change between checking the size and actual retrieval.
  471. * @return the number of events in the queue.
  472. */
  473. virtual std::size_t consumer_queue_size() const { return 0; }
  474. /**
  475. * Read the next message from the queue.
  476. * This blocks until a new message arrives.
  477. * @return The message and topic.
  478. */
  479. virtual const_message_ptr consume_message() = 0;
  480. /**
  481. * Try to read the next message from the queue without blocking.
  482. * @param msg Pointer to the value to receive the message
  483. * @return @em true is a message was read, @em false if no message was
  484. * available.
  485. */
  486. virtual bool try_consume_message(const_message_ptr* msg) = 0;
  487. /**
  488. * Read the next event from the queue.
  489. * This blocks until a new message arrives.
  490. * @return The message and topic.
  491. */
  492. virtual event consume_event() { return event{}; }
  493. /**
  494. * Try to read the next message from the queue without blocking.
  495. * @param evt Pointer to the value to receive the event
  496. * @return @em true is an event was received, @em false if no event was
  497. * available.
  498. */
  499. virtual bool try_consume_event(event* evt) {
  500. (void)evt;
  501. return false;
  502. }
  503. };
  504. /////////////////////////////////////////////////////////////////////////////
  505. } // namespace mqtt
  506. #endif // __mqtt_iasync_client_h