Blame view

include/mqttsuccess.h 5.92 KB
b5d9e433   Peter M. Groen   Fixed License Hea...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /* ****************************************************************************
   * Copyright 2019 Open Systems Development BV                                 *
   *                                                                            *
   * Permission is hereby granted, free of charge, to any person obtaining a    *
   * copy of this software and associated documentation files (the "Software"), *
   * to deal in the Software without restriction, including without limitation  *
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
   * and/or sell copies of the Software, and to permit persons to whom the      *
   * Software is furnished to do so, subject to the following conditions:       *
   *                                                                            *
   * The above copyright notice and this permission notice shall be included in *
   * all copies or substantial portions of the Software.                        *
   *                                                                            *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
   * DEALINGS IN THE SOFTWARE.                                                  *
   * ***************************************************************************/
51becbde   Peter M. Groen   Committed the ent...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  #ifndef OSDEV_COMPONENTS_MQTT_MQTTSUCCESS_H
  #define OSDEV_COMPONENTS_MQTT_MQTTSUCCESS_H
  
  // std
  #include <string>
  #include <vector>
  
  // boost
  #include <boost/variant.hpp>
  
  // paho
  #include <MQTTAsync.h>
  
  // osdev::components::mqtt
  #include "mqttmessage.h"
  
  namespace osdev {
  namespace components {
  namespace mqtt {
  
  /**
   * @brief Class that holds paho connection data which is returned in the connect response.
   */
  class ConnectionData
  {
  public:
      /*!
       *  \brief  Construct an empty ConnectData instance.
       */
      ConnectionData();
  
      /*!
       *  \brief  Construct ConnectData based on incoming values from paho.
       *  \param  serverUri       - The serverUri to which the connection is made (needs to be copied).
       *  \param  mqttVersion     - The mqtt version used by the broker.
       *  \param  sessionPresent  - Flag that indicates if a session was present for the given clientId.
       */
      ConnectionData(char* serverUri, int mqttVersion, int sessionPresent);
  
      /*!
       *  \return The server uri.
       */
      const std::string& serverUri() const { return m_serverUri; }
  
      /*!
       *  \return The mqtt version.
       */
      int mqttVersion() const { return m_mqttVersion; }
  
      /*!
       *  \return if a session was present for the given clientId.
       */
      bool sessionPresent() const { return m_sessionPresent; }
  
  private:
      std::string m_serverUri; ///< The broker server uri.
      int m_mqttVersion;       ///< The mqtt version used by the broker.
      bool m_sessionPresent;   ///< Flag that indicates whether a session was present for the client id used in the connect.
  };
  
  struct Unspecified
  {
  };
  
  /*!
   *  \brief  Class for paho mqtt success response data.
   *          The paho success response data uses a union and can have different information depending on the command.
   */
  class MqttSuccess
  {
  public:
      /*!
       *  \brief Response data for commands without specific data.
       *  \param token The token that identifies to which command this response belongs.
       */
      explicit MqttSuccess(MQTTAsync_token token);
  
      /*!
       *  \brief Response data for a subscribe command.
       *  \param token The token that identifies to which command this response belongs.
       *  \param qos Actual quality of service of the subscription.
       */
      MqttSuccess(MQTTAsync_token token, int qos);
  
      /*!
       *  \brief Response data for a subscribe many command.
       *  \param token The token that identifies to which command this response belongs.
       *  \param qosMany Actual quality of service of the subscription for each topic filter.
       */
      MqttSuccess(MQTTAsync_token token, const std::vector<int>& qosMany);
  
      /*!
       *  \brief Response data for a publish command.
       *  \param token The token that identifies to which command this response belongs.
       *  \param pubMsg The message that was published.
       */
      MqttSuccess(MQTTAsync_token token, const MqttMessage& pubMsg);
  
      /*!
       *  \brief Response data for a connect command.
       *  \param token The token that identifies to which command this response belongs.
       *  \param connData The connection data.
       */
      MqttSuccess(MQTTAsync_token token, const ConnectionData& connData);
  
      /*!
       *  \return the command token.
       */
      MQTTAsync_token token() const { return m_token; }
  
      /*!
       *  \return the qos
       *  \throw exception when command is not a subscribe command.
       */
      int qos() const;
  
      /*!
       *  \return a vector of qos values (matching the topics in the subscribe many command).
       *  \throw exception when command is not a subscribe many command.
       */
      std::vector<int> qosMany() const;
  
      /*!
       *  \return Message that has been published.
       *  \throw exception when command is not a publish command.
       */
      MqttMessage publishData() const;
  
      /*!
       *  return Connection data.
       *  throw exception when command is not a connect command.
       */
      ConnectionData connectionData() const;
  
  private:
      MQTTAsync_token m_token;                                                                ///< Command token.
      boost::variant<int, std::vector<int>, MqttMessage, ConnectionData, Unspecified> m_data; ///< Data for the various commands.
  };
  
  }       // End namespace mqtt
  }       // End namespace components
  }       // End namespace osdev
  
  #endif  // OSDEV_COMPONENTS_MQTT_MQTTSUCCESS_H