topic.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file topic.h
  3. /// Declaration of MQTT topic 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_topic_h
  23. #define __mqtt_topic_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/delivery_token.h"
  26. #include "mqtt/subscribe_options.h"
  27. #include "mqtt/message.h"
  28. #include "mqtt/types.h"
  29. #include <vector>
  30. namespace mqtt {
  31. class iasync_client;
  32. /////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * Represents a topic destination, used for publish/subscribe messaging.
  35. */
  36. class topic
  37. {
  38. /** The client to which this topic is connected */
  39. iasync_client& cli_;
  40. /** The topic name */
  41. string name_;
  42. /** The default QoS */
  43. int qos_;
  44. /** The default retined flag */
  45. bool retained_;
  46. public:
  47. /** A smart/shared pointer to this class. */
  48. using ptr_t = std::shared_ptr<topic>;
  49. /** A smart/shared pointer to this class. */
  50. using const_ptr_t = std::shared_ptr<const topic>;
  51. /**
  52. * Construct an MQTT topic destination for messages.
  53. * @param cli Client to which the topic is attached
  54. * @param name The topic string
  55. * @param qos The default QoS for publishing.
  56. * @param retained The default retained flag for the topic.
  57. */
  58. topic(iasync_client& cli, const string& name,
  59. int qos=message::DFLT_QOS, bool retained=message::DFLT_RETAINED)
  60. : cli_(cli), name_(name), qos_(qos), retained_(retained) {}
  61. /**
  62. * Creates a new topic
  63. * @param cli Client to which the topic is attached
  64. * @param name The topic string
  65. * @param qos The default QoS for publishing.
  66. * @param retained The default retained flag for the topic.
  67. * @return A shared pointer to the topic.
  68. */
  69. static ptr_t create(iasync_client& cli, const string& name,
  70. int qos=message::DFLT_QOS,
  71. bool retained=message::DFLT_RETAINED) {
  72. return std::make_shared<topic>(cli, name, qos, retained);
  73. }
  74. /**
  75. * Gets a reference to the MQTT client used by this topic
  76. * @return The MQTT client used by this topic
  77. */
  78. iasync_client& get_client() { return cli_; }
  79. /**
  80. * Gets the name of the topic.
  81. * @return The name of the topic.
  82. */
  83. const string& get_name() const { return name_; }
  84. /**
  85. * Gets the default quality of service for this topic.
  86. * @return The default quality of service for this topic.
  87. */
  88. int get_qos() const { return qos_; }
  89. /**
  90. * Gets the default retained flag used for this topic.
  91. * @return The default retained flag used for this topic.
  92. */
  93. bool get_retained() const { return retained_; }
  94. /**
  95. * Sets the default quality of service for this topic.
  96. * @param qos The default quality of service for this topic.
  97. */
  98. void set_qos(int qos) {
  99. message::validate_qos(qos);
  100. qos_ = qos;
  101. }
  102. /**
  103. * Sets the default retained flag used for this topic.
  104. * @param retained The default retained flag used for this topic.
  105. */
  106. void set_retained(bool retained) { retained_ = retained; }
  107. /**
  108. * Publishes a message on the topic using the default QoS and retained
  109. * flag.
  110. * @param payload the bytes to use as the message payload
  111. * @param n the number of bytes in the payload
  112. * @return The delivery token used to track and wait for the publish to
  113. * complete.
  114. */
  115. delivery_token_ptr publish(const void* payload, size_t n);
  116. /**
  117. * Publishes a message on the topic.
  118. * @param payload the bytes to use as the message payload
  119. * @param n the number of bytes in the payload
  120. * @param qos the Quality of Service to deliver the message at. Valid
  121. * values are 0, 1 or 2.
  122. * @param retained whether or not this message should be retained by the
  123. * server.
  124. * @return The delivery token used to track and wait for the publish to
  125. * complete.
  126. */
  127. delivery_token_ptr publish(const void* payload, size_t n,
  128. int qos, bool retained);
  129. /**
  130. * Publishes a message on the topic using the default QoS and retained
  131. * flag.
  132. * @param payload the bytes to use as the message payload
  133. * @return The delivery token used to track and wait for the publish to
  134. * complete.
  135. */
  136. delivery_token_ptr publish(binary_ref payload);
  137. /**
  138. * Publishes a message on the topic.
  139. * @param payload the bytes to use as the message payload
  140. * @param qos the Quality of Service to deliver the message at. Valid
  141. * values are 0, 1 or 2.
  142. * @param retained whether or not this message should be retained by the
  143. * server.
  144. * @return The delivery token used to track and wait for the publish to
  145. * complete.
  146. */
  147. delivery_token_ptr publish(binary_ref payload, int qos, bool retained);
  148. /**
  149. * Subscribe to the topic.
  150. * @return A token used to track the progress of the operation.
  151. */
  152. token_ptr subscribe(const subscribe_options& opts=subscribe_options());
  153. /**
  154. * Returns a string representation of this topic.
  155. * @return The name of the topic
  156. */
  157. string to_string() const { return name_; }
  158. };
  159. /** A smart/shared pointer to a topic object. */
  160. using topic_ptr = topic::ptr_t ;
  161. /** A smart/shared pointer to a const topic object. */
  162. using const_topic_ptr = topic::const_ptr_t ;
  163. /////////////////////////////////////////////////////////////////////////////
  164. // end namespace mqtt
  165. }
  166. #endif // __mqtt_topic_h