server_response.h 5.9 KB

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