topic.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 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_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 retained 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. * Splits a topic string into individual fields.
  86. *
  87. * @param topic A slash-delimited MQTT topic string.
  88. * @return A vector containing the fields of the topic.
  89. */
  90. static std::vector<std::string> split(const std::string& topic);
  91. /**
  92. * Gets the default quality of service for this topic.
  93. * @return The default quality of service for this topic.
  94. */
  95. int get_qos() const { return qos_; }
  96. /**
  97. * Gets the default retained flag used for this topic.
  98. * @return The default retained flag used for this topic.
  99. */
  100. bool get_retained() const { return retained_; }
  101. /**
  102. * Sets the default quality of service for this topic.
  103. * @param qos The default quality of service for this topic.
  104. */
  105. void set_qos(int qos) {
  106. message::validate_qos(qos);
  107. qos_ = qos;
  108. }
  109. /**
  110. * Sets the default retained flag used for this topic.
  111. * @param retained The default retained flag used for this topic.
  112. */
  113. void set_retained(bool retained) { retained_ = retained; }
  114. /**
  115. * Publishes a message on the topic using the default QoS and retained
  116. * flag.
  117. * @param payload the bytes to use as the message payload
  118. * @param n the number of bytes in the payload
  119. * @return The delivery token used to track and wait for the publish to
  120. * complete.
  121. */
  122. delivery_token_ptr publish(const void* payload, size_t n);
  123. /**
  124. * Publishes a message on the topic.
  125. * @param payload the bytes to use as the message payload
  126. * @param n the number of bytes in the payload
  127. * @param qos the Quality of Service to deliver the message at. Valid
  128. * values are 0, 1 or 2.
  129. * @param retained whether or not this message should be retained by the
  130. * server.
  131. * @return The delivery token used to track and wait for the publish to
  132. * complete.
  133. */
  134. delivery_token_ptr publish(const void* payload, size_t n,
  135. int qos, bool retained);
  136. /**
  137. * Publishes a message on the topic using the default QoS and retained
  138. * flag.
  139. * @param payload the bytes to use as the message payload
  140. * @return The delivery token used to track and wait for the publish to
  141. * complete.
  142. */
  143. delivery_token_ptr publish(binary_ref payload);
  144. /**
  145. * Publishes a message on the topic.
  146. * @param payload the bytes to use as the message payload
  147. * @param qos the Quality of Service to deliver the message at. Valid
  148. * values are 0, 1 or 2.
  149. * @param retained whether or not this message should be retained by the
  150. * server.
  151. * @return The delivery token used to track and wait for the publish to
  152. * complete.
  153. */
  154. delivery_token_ptr publish(binary_ref payload, int qos, bool retained);
  155. /**
  156. * Subscribe to the topic.
  157. * @return A token used to track the progress of the operation.
  158. */
  159. token_ptr subscribe(const subscribe_options& opts=subscribe_options());
  160. /**
  161. * Returns a string representation of this topic.
  162. * @return The name of the topic
  163. */
  164. string to_string() const { return name_; }
  165. };
  166. /** A smart/shared pointer to a topic object. */
  167. using topic_ptr = topic::ptr_t ;
  168. /** A smart/shared pointer to a const topic object. */
  169. using const_topic_ptr = topic::const_ptr_t ;
  170. /////////////////////////////////////////////////////////////////////////////
  171. // Topic Filter
  172. /////////////////////////////////////////////////////////////////////////////
  173. /**
  174. * An MQTT topic filter.
  175. *
  176. * This is a multi-field string, delimited by forward slashes, '/', in which
  177. * fields can contain the wildcards:
  178. *
  179. * '+' - Matches a single field
  180. * '#' - Matches all subsequent fields (must be last field in filter)
  181. *
  182. * It can be used to match against specific topics.
  183. */
  184. class topic_filter
  185. {
  186. /** We store the filter as a vector of the individual fields. */
  187. std::vector<string> fields_;
  188. public:
  189. /**
  190. * Creates a new topic filter.
  191. *
  192. * @param filter A string MQTT topic filter. This is a slash ('/')
  193. * delimited topic string that can contain wildcards
  194. * '+' and '#'.
  195. */
  196. explicit topic_filter(const string& filter);
  197. /**
  198. * Determines if the specified topic filter contains any wildcards.
  199. *
  200. * @param filter The topic filter string to check for wildcards.
  201. * @return @em true if any of the fields contain a wildcard, @em false
  202. * if not.
  203. */
  204. static bool has_wildcards(const string& topic);
  205. /**
  206. * Determines if this topic filter contains any wildcards.
  207. *
  208. * @return @em true if any of the fields contain a wildcard, @em false
  209. * if not.
  210. */
  211. bool has_wildcards() const;
  212. /**
  213. * Determine if the topic matches this filter.
  214. *
  215. * @param topic An MQTT topic. It should not contain wildcards.
  216. * @return @em true of the topic matches this filter, @em false
  217. * otherwise.
  218. */
  219. bool matches(const string& topic) const;
  220. };
  221. /////////////////////////////////////////////////////////////////////////////
  222. // end namespace mqtt
  223. }
  224. #endif // __mqtt_topic_h