forked from aws/aws-iot-device-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.hpp
414 lines (377 loc) · 21.2 KB
/
Client.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file Client.hpp
* @brief Contains the MQTT Client class
*
* Defines MQTT client wrapper using a Client Core instance. This is provided
* for ease of use. Instead of separately having to define Core Client and adding
* Actions to the client, applications can use this class directly.
*/
#pragma once
#include "util/Utf8String.hpp"
#include "ClientCore.hpp"
#include "mqtt/Connect.hpp"
#include "mqtt/Publish.hpp"
#include "mqtt/Subscribe.hpp"
#include "mqtt/ClientState.hpp"
namespace awsiotsdk {
/**
* @brief MQTT Client Class
*
* Defining a class for the MQTT Client.
* This class is a wrapper on the Core Client and creates a Client Core instance with MQTT Actions
* It also provides APIs to perform MQTT operations directly on the Core Client instance
*
*/
AWS_API_EXPORT class MqttClient {
protected:
std::unique_ptr<ClientCore> p_client_core_; ///< Unique pointer to the Client Core instance
std::shared_ptr<mqtt::ClientState> p_client_state_; ///< MQTT Client state
/**
* @brief Constructor
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
* @param disconnect_callback_ptr - pointer of the disconnect callback handler
* @param p_disconnect_app_handler_data - context data for the disconnect handler
* @param reconnect_callback_ptr - pointer of the reconnect callback handler
* @param p_reconnect_app_handler_data - context data for the reconnect handler
* @param resubscribe_callback_ptr - pointer of the resubscribe callback handler
* @param p_resubscribe_app_handler_data - context data for the resubscribe handler
*/
MqttClient(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout,
ClientCoreState::ApplicationDisconnectCallbackPtr disconnect_callback_ptr,
std::shared_ptr<DisconnectCallbackContextData> p_disconnect_app_handler_data,
ClientCoreState::ApplicationReconnectCallbackPtr reconnect_callback_ptr,
std::shared_ptr<ReconnectCallbackContextData> p_reconnect_app_handler_data,
ClientCoreState::ApplicationResubscribeCallbackPtr resubscribe_callback_ptr,
std::shared_ptr<ResubscribeCallbackContextData> p_resubscribe_app_handler_data);
/**
* @brief Constructor
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
* @param disconnect_callback_ptr - pointer of the disconnect callback handler
* @param p_disconnect_app_handler_data - context data for the disconnect handler
*/
MqttClient(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout,
ClientCoreState::ApplicationDisconnectCallbackPtr disconnect_callback_ptr,
std::shared_ptr<DisconnectCallbackContextData> p_disconnect_app_handler_data);
/**
* @brief Constructor
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
*/
MqttClient(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout);
public:
// Disabling default and copy constructors. Defining a virtual destructor
// Client instances should not be copied to avoid possible Connection issues with two clients
// using same connection data
MqttClient() = delete; // Delete Default constructor
MqttClient(const MqttClient &) = delete; // Delete Copy constructor
MqttClient(MqttClient &&) = default; // Default Move constructor
MqttClient &operator=(const MqttClient &) & = delete; // Delete Copy assignment operator
MqttClient &operator=(MqttClient &&) & = default; // Default Move assignment operator
virtual ~MqttClient();
/**
* @brief Create factory method. Returns a unique instance of MqttClient
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
*
* @return std::unique_ptr<MqttClient> pointing to a unique MQTT client instance
*/
static std::unique_ptr<MqttClient> Create(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout);
/**
* @brief Create factory method, with additional parameters for disconnect callback.
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
* @param disconnect_callback_ptr_ptr - pointer of the disconnect callback handler
* @param p_app_handler_data - context data for the disconnect handler
* @return std::unique_ptr<MqttClient> pointing to a unique MQTT client instance
*/
static std::unique_ptr<MqttClient> Create(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout,
ClientCoreState::ApplicationDisconnectCallbackPtr disconnect_callback_ptr,
std::shared_ptr<DisconnectCallbackContextData> p_disconnect_app_handler_data);
/**
* @brief Create factory method, with additional parameters for disconnect, reconnect and resubscribe callbacks.
*
* @param p_network_connection - Network connection to use with this MQTT Client instance
* @param mqtt_command_timeout - Command timeout in milliseconds for internal blocking operations (Reconnect and Resubscribe)
* @param disconnect_callback_ptr - pointer of the disconnect callback handler
* @param p_app_handler_data - context data for the disconnect handler
* @param reconnect_callback_ptr - pointer of the reconnect callback handler
* @param p_reconnect_app_handler_data - context data for the reconnect handler
* @param resubscribe_callback_ptr - pointer of the resubscribe callback handler
* @param p_resubscribe_app_handler_data - context data for the resubscribe handler
* @return std::unique_ptr<MqttClient> pointing to a unique MQTT client instance
*/
static std::unique_ptr<MqttClient> Create(std::shared_ptr<NetworkConnection> p_network_connection,
std::chrono::milliseconds mqtt_command_timeout,
ClientCoreState::ApplicationDisconnectCallbackPtr disconnect_callback_ptr,
std::shared_ptr<DisconnectCallbackContextData> p_disconnect_app_handler_data,
ClientCoreState::ApplicationReconnectCallbackPtr reconnect_callback_ptr,
std::shared_ptr<ReconnectCallbackContextData> p_reconnect_app_handler_data,
ClientCoreState::ApplicationResubscribeCallbackPtr p_resubscribec_callback,
std::shared_ptr<ResubscribeCallbackContextData> p_resubscribe_app_handler_data);
// Sync API
/**
* @brief Perform Sync Connect
*
* Performs a Network and MQTT Connect operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent. SDK metrics enabled by default
*
* @param action_response_timeout Timeout in milliseconds within which response should be obtained after request is sent
* @param is_clean_session
* @param mqtt_version
* @param keep_alive_timeout
* @param p_client_id
* @param p_username
* @param p_password
* @param p_will_msg Last Will and Testament message
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Connect(std::chrono::milliseconds action_response_timeout, bool is_clean_session,
mqtt::Version mqtt_version, std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id, std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg);
/**
* @brief Perform Sync Connect
*
* Performs a Network and MQTT Connect operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent.
*
* @param action_response_timeout Timeout in milliseconds within which response should be obtained after request is sent
* @param is_clean_session
* @param mqtt_version
* @param keep_alive_timeout
* @param p_client_id
* @param p_username
* @param p_password
* @param p_will_msg Last Will and Testament message
* @param is_metrics_enabled
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Connect(std::chrono::milliseconds action_response_timeout, bool is_clean_session,
mqtt::Version mqtt_version, std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id, std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg,
bool is_metrics_enabled);
/**
* @brief Perform Sync Disconnect
*
* Performs a Network and MQTT Disconnect operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent.
*
* @param action_response_timeout - Timeout in milliseconds within which response should be obtained after request is sent
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Disconnect(std::chrono::milliseconds action_response_timeout);
/**
* @brief Perform Sync Publish
*
* Performs a MQTT Publish operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent.
*
* @param p_topic_name topic name on which the publish is performed
* @param is_retained last message is retained
* @param is_duplicate is a duplicate message
* @param qos quality of service
* @param payload MQTT message payload
* @param action_response_timeout Timeout in milliseconds within which response should be obtained after request is sent
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Publish(std::unique_ptr<Utf8String> p_topic_name, bool is_retained, bool is_duplicate,
mqtt::QoS qos, const util::String &payload,
std::chrono::milliseconds action_response_timeout);
/**
* @brief Perform Sync Subscribe
*
* Performs a MQTT Subscribe operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent.
*
* @param subscription_list - A list of subscriptions to use for the operation
* @param action_response_timeout - Timeout in milliseconds within which response should be obtained after request is sent
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Subscribe(util::Vector<std::shared_ptr<mqtt::Subscription>> subscription_list,
std::chrono::milliseconds action_response_timeout);
/**
* @brief Perform Sync Unsubscribe
*
* Performs a MQTT Unsubscribe operation in blocking mode. Action timeout here is the time for which
* the client waits for a response AFTER the request is sent.
*
* @param p_unsubscribe_packet - Unsubscribe packet to use for the operation
* @param action_response_timeout - Timeout in milliseconds within which response should be obtained after request is sent
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode Unsubscribe(util::Vector<std::unique_ptr<Utf8String>> topic_list,
std::chrono::milliseconds action_response_timeout);
// Async API
/**
* @brief Perform Async Publish
*
* Performs a MQTT Publish operation in Async mode. In the case of QoS1 requests, packet ID obtained from this
* function can be used to match Ack to specific requests if needed. QoS0 requests do not have a corrosponding
* Ack message and we do not support QoS2 at this time. The request is queued up and in the case of QoS1,
* the Ack Handler is called if a PUBACK is received. If not, the handler is called with a ResponseCode
* indicating timeout
*
* @param p_topic_name on which the publish is performed
* @param is_retained last message is retained
* @param is_duplicate is a duplicate message
* @param qos quality of service
* @param payload MQTT message payload
* @param p_async_ack_handler the ack handling function
* @param packet_id_out packet ID of the message being sent
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode PublishAsync(std::unique_ptr<Utf8String> p_topic_name, bool is_retained, bool is_duplicate,
mqtt::QoS qos, const util::String &payload,
ActionData::AsyncAckNotificationHandlerPtr p_async_ack_handler,
uint16_t &packet_id_out);
/**
* @brief Perform Async Subscribe
*
* Performs a MQTT Subscribe operation in Async mode. Packet ID obtained from this function can be
* used to match Ack to specific requests if needed. The Subscribe request is queued up and Client automatically
* activates Subscription if successful SUBACK is received. If not, the assigned Ack handler will be called
* with the corrosponding ResponseCode
*
* @param subscription_list - A list of subscriptions to use for the operation
* @param p_async_ack_handler - AsyncAck notification handler to be called when response for this request is processed
* @param packet_id_out - Packet ID assigned to outgoing packet
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode SubscribeAsync(util::Vector<std::shared_ptr<mqtt::Subscription>> subscription_list,
ActionData::AsyncAckNotificationHandlerPtr p_async_ack_handler,
uint16_t &packet_id_out);
/**
* @brief Perform Async Unsubscribe
*
* Performs a MQTT Unsubscribe operation in Async mode. Packet ID obtained from this function can be used to
* match Ack to specific requests if needed. The Unsubscribe request is queued up and Client automatically
* deactivates the subscription if successful UNSUBACK is received. If not, the assigned Ack handler will be
* called with the corrosponding ResponseCode
*
* @param p_unsubscribe_packet - Unsubscribe packet to use for the operation
* @param p_async_ack_handler - AsyncAck notification handler to be called when response for this request is processed
* @param packet_id_out - Packet ID assigned to outgoing packet
*
* @return ResponseCode indicating status of request
*/
virtual ResponseCode UnsubscribeAsync(util::Vector<std::unique_ptr<Utf8String>> topic_list,
ActionData::AsyncAckNotificationHandlerPtr p_async_ack_handler,
uint16_t &packet_id_out);
/**
* @brief Check if Client is in Connected state
*
* @return boolean indicating connection status
*/
virtual bool IsConnected();
/**
* @brief Sets the auto-reconnect flag for the client.
*
* @param value for setting the flag
*/
virtual void SetAutoReconnectEnabled(bool value) {
p_client_state_->SetAutoReconnectEnabled(value);
}
/**
* @brief returns the current state of the auto-reconnect flag
*
* @return boolean indicating state of the flag
*/
virtual bool IsAutoReconnectEnabled() {
return p_client_state_->IsAutoReconnectEnabled();
}
/**
* @brief returns the minimum back-off time value
*
* Returns the minimum back-off time that is set, which is the minimum time a client waits for
* before attempting a reconnect.
*
* @return seconds
*/
virtual std::chrono::seconds GetMinReconnectBackoffTimeout();
/**
* @brief sets the minimum back-off time value
*
* @param min_reconnect_backoff_timeout
*/
virtual void SetMinReconnectBackoffTimeout(std::chrono::seconds min_reconnect_backoff_timeout);
/**
* @brief returns the maximum back-off time value
*
* Returns the maximum back-off time that is set, which is the maximum time a client waits for
* before attempting a reconnect.
*
* @return seconds
*/
virtual std::chrono::seconds GetMaxReconnectBackoffTimeout();
/**
* @brief sets the maximum back-off time value
*
* @param max_reconnect_backoff_timeout
*/
virtual void SetMaxReconnectBackoffTimeout(std::chrono::seconds max_reconnect_backoff_timeout);
/**
* @brief Set the callback function for disconnects
*
* @param p_callback_ptr
* @param p_app_handler_data
* @return ResponseCode indicating whether the callback was set successfully
*/
virtual ResponseCode SetDisconnectCallbackPtr(ClientCoreState::ApplicationDisconnectCallbackPtr p_callback_ptr,
std::shared_ptr<DisconnectCallbackContextData> p_app_handler_data);
/**
* @brief Set the callback function for reconnects
*
* @param p_callback_ptr
* @param p_app_handler_data
* @return ResponseCode indicating whether the callback was set successfully
*/
virtual ResponseCode SetReconnectCallbackPtr(ClientCoreState::ApplicationReconnectCallbackPtr p_callback_ptr,
std::shared_ptr<ReconnectCallbackContextData> p_app_handler_data);
/**
* @brief Set the callback function for resubscribes
*
* @param p_callback_ptr
* @param p_app_handler_data
* @return ResponseCode indicating whether the callback was set successfully
*/
virtual ResponseCode SetResubscribeCallbackPtr(ClientCoreState::ApplicationResubscribeCallbackPtr p_callback_ptr,
std::shared_ptr<ResubscribeCallbackContextData> p_app_handler_data);
};
}