delivery_token.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file delivery_token.h
  3. /// Declaration of MQTT delivery_token class
  4. /// @date May 1, 2013
  5. /// @author Frank Pagliughi
  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_delivery_token_h
  23. #define __mqtt_delivery_token_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/token.h"
  26. #include "mqtt/message.h"
  27. #include <memory>
  28. namespace mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * Provides a mechanism to track the delivery progress of a message.
  32. * Used to track the the delivery progress of a message when a publish is
  33. * executed in a non-blocking manner (run in the background) action.
  34. */
  35. class delivery_token : public token
  36. {
  37. /** The message being tracked. */
  38. const_message_ptr msg_;
  39. /** Client has special access. */
  40. friend class async_client;
  41. /**
  42. * Sets the message to which this token corresponds.
  43. * @param msg
  44. */
  45. void set_message(const_message_ptr msg) { msg_ = msg; }
  46. public:
  47. /** Smart/shared pointer to an object of this class */
  48. using ptr_t = std::shared_ptr<delivery_token>;
  49. /** Smart/shared pointer to a const object of this class */
  50. using const_ptr_t = std::shared_ptr<delivery_token>;
  51. /** Weak pointer to an object of this class */
  52. using weak_ptr_t = std::weak_ptr<delivery_token>;
  53. /**
  54. * Creates an empty delivery token connected to a particular client.
  55. * @param cli The asynchronous client object.
  56. */
  57. delivery_token(iasync_client& cli) : token(token::Type::PUBLISH, cli) {}
  58. /**
  59. * Creates a delivery token connected to a particular client.
  60. * @param cli The asynchronous client object.
  61. * @param msg The message being tracked.
  62. */
  63. delivery_token(iasync_client& cli, const_message_ptr msg)
  64. : token(token::Type::PUBLISH, cli, msg->get_topic()), msg_(std::move(msg)) {}
  65. /**
  66. * Creates a delivery token connected to a particular client.
  67. * @param cli The asynchronous client object.
  68. * @param msg The message data.
  69. * @param userContext optional object used to pass context to the
  70. * callback. Use @em nullptr if not required.
  71. * @param cb callback optional listener that will be notified when message
  72. * delivery has completed to the requested quality of
  73. * service
  74. */
  75. delivery_token(iasync_client& cli, const_message_ptr msg,
  76. void* userContext, iaction_listener& cb)
  77. : token(token::Type::PUBLISH, cli, msg->get_topic(), userContext, cb), msg_(std::move(msg)) {}
  78. /**
  79. * Creates an empty delivery token connected to a particular client.
  80. * @param cli The asynchronous client object.
  81. */
  82. static ptr_t create(iasync_client& cli) {
  83. return std::make_shared<delivery_token>(cli);
  84. }
  85. /**
  86. * Creates a delivery token connected to a particular client.
  87. * @param cli The asynchronous client object.
  88. * @param msg The message data.
  89. */
  90. static ptr_t create(iasync_client& cli, const_message_ptr msg) {
  91. return std::make_shared<delivery_token>(cli, msg);
  92. }
  93. /**
  94. * Creates a delivery token connected to a particular client.
  95. * @param cli The asynchronous client object.
  96. * @param msg The message data.
  97. * @param userContext optional object used to pass context to the
  98. * callback. Use @em nullptr if not required.
  99. * @param cb callback optional listener that will be notified when message
  100. * delivery has completed to the requested quality of
  101. * service
  102. */
  103. static ptr_t create(iasync_client& cli, const_message_ptr msg,
  104. void* userContext, iaction_listener& cb) {
  105. return std::make_shared<delivery_token>(cli, msg, userContext, cb);
  106. }
  107. /**
  108. * Gets the message associated with this token.
  109. * @return The message associated with this token.
  110. */
  111. virtual const_message_ptr get_message() const { return msg_; }
  112. };
  113. /** Smart/shared pointer to a delivery_token */
  114. using delivery_token_ptr = delivery_token::ptr_t;
  115. /** Smart/shared pointer to a const delivery_token */
  116. using const_delivery_token_ptr = delivery_token::const_ptr_t;
  117. /////////////////////////////////////////////////////////////////////////////
  118. // end namespace mqtt
  119. }
  120. #endif // __mqtt_delivery_token_h