server_response.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 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 "MQTTAsync.h"
  25. #include "mqtt/types.h"
  26. #include "mqtt/properties.h"
  27. #include <iostream>
  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)
  46. : props_(props) {}
  47. /**
  48. * Creates a server response with the specified properties.
  49. * @param props The properties in the response.
  50. */
  51. server_response(properties&& props)
  52. : props_(std::move(props)) {}
  53. /**
  54. * Virtual destructor.
  55. */
  56. virtual ~server_response() {}
  57. /**
  58. * Gets the properties from the response.
  59. * @return The properties from the response.
  60. */
  61. const properties& get_properties() const { return props_; }
  62. };
  63. /**
  64. * Response for a connect request
  65. */
  66. class connect_response : public server_response
  67. {
  68. /** The connection string of the server */
  69. string serverURI_;
  70. /** The version of MQTT being used */
  71. int mqttVersion_;
  72. /** The session present flag returned from the server */
  73. bool sessionPresent_;
  74. friend class token;
  75. connect_response(const MQTTAsync_successData5* rsp) :
  76. server_response(properties(rsp->properties)),
  77. serverURI_(string(rsp->alt.connect.serverURI)),
  78. mqttVersion_(rsp->alt.connect.MQTTVersion),
  79. sessionPresent_(to_bool(rsp->alt.connect.sessionPresent)) {
  80. }
  81. connect_response(const MQTTAsync_successData* rsp) :
  82. serverURI_(string(rsp->alt.connect.serverURI)),
  83. mqttVersion_(rsp->alt.connect.MQTTVersion),
  84. sessionPresent_(to_bool(rsp->alt.connect.sessionPresent)) {
  85. }
  86. public:
  87. /**
  88. * Gets the URI of the broker to which we connected.
  89. * @return The URI of the broker.
  90. */
  91. string get_server_uri() const { return serverURI_; }
  92. /**
  93. * Gets the MQTT version for the connection.
  94. * @return The MQTT version for the connection.
  95. */
  96. int get_mqtt_version() const { return mqttVersion_; }
  97. /**
  98. * Determines whether a session already existed for this client on the
  99. * server.
  100. * This tells whether the server has a persistent session stored for the
  101. * client, given the ClientID specified in the connect message.
  102. * @return Whether a session already existed for this client on the server.
  103. */
  104. bool is_session_present() const { return sessionPresent_; }
  105. };
  106. /**
  107. * Response for subscribe messages
  108. */
  109. struct subscribe_response : public server_response
  110. {
  111. /** The reason/result code for each topic request. */
  112. std::vector<ReasonCode> reasonCodes_;
  113. friend class token;
  114. /**
  115. * Create v5 subscribe response.
  116. * @param rsp The v5 response struct from the C lib
  117. */
  118. subscribe_response(MQTTAsync_successData5* rsp)
  119. : server_response(properties(rsp->properties)) {
  120. if (rsp->alt.sub.reasonCodeCount < 2)
  121. reasonCodes_.push_back(ReasonCode(rsp->reasonCode));
  122. else if (rsp->alt.sub.reasonCodes) {
  123. for (int i=0; i<rsp->alt.sub.reasonCodeCount; ++i)
  124. reasonCodes_.push_back(ReasonCode(rsp->alt.sub.reasonCodes[i]));
  125. }
  126. }
  127. /**
  128. * Create v3 subscribe response.
  129. * @param n The number of subscription topics
  130. * @param rsp The v3 response struct from the C lib
  131. */
  132. subscribe_response(size_t n, MQTTAsync_successData* rsp) {
  133. if (n < 2)
  134. reasonCodes_.push_back(ReasonCode(rsp->alt.qos));
  135. else if (rsp->alt.qosList) {
  136. for (size_t i=0; i<n; ++i)
  137. reasonCodes_.push_back(ReasonCode(rsp->alt.qosList[i]));
  138. }
  139. }
  140. public:
  141. /**
  142. * Gets the reason codes from the server response.
  143. * On a subscribe ack there is a reason code for each topic that
  144. * was sent in the subscribe packet. Each tells the granted QoS
  145. * for the corresponding topic.
  146. * @return A collection of return codes corresponding to
  147. * subscribing each topic. On success, this is the
  148. * granted QoS for each topic. On failure it is the
  149. * reason for the failure.
  150. */
  151. const std::vector<ReasonCode>& get_reason_codes() const {
  152. return reasonCodes_;
  153. }
  154. };
  155. /**
  156. * Response for unsubscribe messages.
  157. */
  158. class unsubscribe_response : public server_response
  159. {
  160. /** The reason/result code for each topic request. */
  161. std::vector<ReasonCode> reasonCodes_;
  162. friend class token;
  163. unsubscribe_response(MQTTAsync_successData5* rsp)
  164. : server_response(properties(rsp->properties)) {
  165. if (rsp->alt.unsub.reasonCodeCount < 2)
  166. reasonCodes_.push_back(ReasonCode(rsp->reasonCode));
  167. else if (rsp->alt.unsub.reasonCodes) {
  168. for (int i=0; i<rsp->alt.unsub.reasonCodeCount; ++i)
  169. reasonCodes_.push_back(ReasonCode(rsp->alt.unsub.reasonCodes[i]));
  170. }
  171. }
  172. unsubscribe_response(MQTTAsync_successData* /*rsp*/) {}
  173. public:
  174. /**
  175. * Gets the reason codes from the server response.
  176. * On an unsubscribe ack there is a reason code for each topic
  177. * that was sent in the unsubscribe packet. Each tells the
  178. * result of unsubscribing to the corresponding topic.
  179. * @return A collection of return codes corresponding to
  180. * unsubscribing each topic.
  181. */
  182. const std::vector<ReasonCode>& get_reason_codes() const {
  183. return reasonCodes_;
  184. }
  185. };
  186. /////////////////////////////////////////////////////////////////////////////
  187. // end namespace mqtt
  188. }
  189. #endif // __mqtt_server_response_h