server_response.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file server_response.h
  3. /// Declaration of MQTT server response classes.
  4. /// @date July 26, 2019
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2019-2024 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_server_response_h
  23. #define __mqtt_server_response_h
  24. #include <iostream>
  25. #include "MQTTAsync.h"
  26. #include "mqtt/properties.h"
  27. #include "mqtt/types.h"
  28. namespace mqtt {
  29. /**
  30. * Base class for responses from the server.
  31. */
  32. class server_response
  33. {
  34. /** The properties from the acknowledge */
  35. properties props_;
  36. public:
  37. /**
  38. * Creates a response with empty property list.
  39. */
  40. server_response() {}
  41. /**
  42. * Creates a server response with the specified properties.
  43. * @param props The properties in the response.
  44. */
  45. server_response(const properties& props) : props_{props} {}
  46. /**
  47. * Creates a server response with the specified properties.
  48. * @param props The properties in the response.
  49. */
  50. server_response(properties&& props) : props_{std::move(props)} {}
  51. /**
  52. * Virtual destructor.
  53. */
  54. virtual ~server_response() {}
  55. /**
  56. * Gets the properties from the response.
  57. * @return The properties from the response.
  58. */
  59. const properties& get_properties() const noexcept { return props_; }
  60. };
  61. /**
  62. * Response for a connect request.
  63. */
  64. class connect_response : public server_response
  65. {
  66. /** The connection string of the server */
  67. string serverURI_;
  68. /** The version of MQTT being used */
  69. int mqttVersion_;
  70. /** The session present flag returned from the server */
  71. bool sessionPresent_;
  72. friend class token;
  73. /**
  74. * Create v5 connect response.
  75. * @param rsp The v5 response struct from the C lib
  76. */
  77. connect_response(const MQTTAsync_successData5* rsp);
  78. /**
  79. * Create v3 connect response.
  80. * @param rsp The v3 response struct from the C lib
  81. */
  82. connect_response(const MQTTAsync_successData* rsp);
  83. public:
  84. /**
  85. * Gets the URI of the broker to which we connected.
  86. * @return The URI of the broker.
  87. */
  88. string get_server_uri() const { return serverURI_; }
  89. /**
  90. * Gets the MQTT version for the connection.
  91. * @return The MQTT version for the connection.
  92. */
  93. int get_mqtt_version() const { return mqttVersion_; }
  94. /**
  95. * Determines whether a session already existed for this client on the
  96. * server.
  97. * This tells whether the server has a persistent session stored for the
  98. * client, given the ClientID specified in the connect message.
  99. * @return Whether a session already existed for this client on the server.
  100. */
  101. bool is_session_present() const { return sessionPresent_; }
  102. };
  103. /**
  104. * Response for a subscribe request.
  105. *
  106. * This contains the information returned from the broker in the SUBACK
  107. * packet. It gives information about the granted Qos for each topc in the
  108. * subscribe request.
  109. *
  110. * @li MQTT v3: These are return "codes" with the value 0-2 for each of the
  111. * topic filters sent in the subscribe message.
  112. * @li MQTT v5 These are reason codes, with one for each of the topics sent
  113. * in the subscribe message. On success, the values are the same as for
  114. * MQTT v3: the granted QoS 0-2. For errors, each could be an error code
  115. * with a value >= 0x80, as described in the MQTT v5 spec: (not
  116. * authorized, quota exceeded, etc).
  117. */
  118. struct subscribe_response : public server_response
  119. {
  120. /** The reason/result code for each topic request. */
  121. std::vector<ReasonCode> reasonCodes_;
  122. friend class token;
  123. /**
  124. * Create v5 subscribe response.
  125. * @param rsp The v5 response struct from the C lib
  126. */
  127. subscribe_response(MQTTAsync_successData5* rsp);
  128. /**
  129. * Create v3 subscribe response.
  130. * @param n The number of subscription topics
  131. * @param rsp The v3 response struct from the C lib
  132. */
  133. subscribe_response(size_t n, MQTTAsync_successData* rsp);
  134. public:
  135. /**
  136. * Gets the reason codes from the server response.
  137. *
  138. * On a subscribe ack there is a return/reason code for each topic that
  139. * was sent in the subscribe packet. Each tells the granted QoS
  140. * for the corresponding topic.
  141. *
  142. * For MQTT v5 values over 0x80 are error codes as described in the MQTT
  143. * v5 spec.
  144. *
  145. * @return A collection of return/reason codes corresponding to
  146. * subscribing each topic. On success, this is the
  147. * granted QoS for each topic. On failure it is the
  148. * reason for the failure.
  149. */
  150. const std::vector<ReasonCode>& get_reason_codes() const { return reasonCodes_; }
  151. };
  152. /**
  153. * Response for unsubscribe messages.
  154. */
  155. class unsubscribe_response : public server_response
  156. {
  157. /** The reason/result code for each topic request. */
  158. std::vector<ReasonCode> reasonCodes_;
  159. friend class token;
  160. /**
  161. * Create v5 unsubscribe response.
  162. * @param rsp The v5 response struct from the C lib
  163. */
  164. unsubscribe_response(MQTTAsync_successData5* rsp);
  165. /**
  166. * Create v3 subscribe response.
  167. * The broker doesn't return any useful information for an unsubscribe
  168. * in MQTT v3.
  169. */
  170. unsubscribe_response(MQTTAsync_successData*) {}
  171. public:
  172. /**
  173. * Gets the reason codes from the server response.
  174. * On an unsubscribe ack there is a reason code for each topic
  175. * that was sent in the unsubscribe packet. Each tells the
  176. * result of unsubscribing to the corresponding topic.
  177. * @return A collection of return codes corresponding to
  178. * unsubscribing each topic.
  179. */
  180. const std::vector<ReasonCode>& get_reason_codes() const { return reasonCodes_; }
  181. };
  182. /////////////////////////////////////////////////////////////////////////////
  183. } // namespace mqtt
  184. #endif // __mqtt_server_response_h