-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathmain.cpp
251 lines (221 loc) · 10.1 KB
/
main.cpp
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
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Api.h>
#include <aws/crt/UUID.h>
#include <aws/crt/mqtt/Mqtt5Packets.h>
#include <aws/iot/Mqtt5Client.h>
#include <thread>
#include "../../utils/CommandLineUtils.h"
using namespace Aws::Crt;
int main(int argc, char *argv[])
{
/************************ Setup ****************************/
// Do the global initialization for the API.
ApiHandle apiHandle;
// Variables needed for the sample
std::mutex receiveMutex;
std::condition_variable receiveSignal;
uint32_t receivedCount = 0;
/**
* cmdData is the arguments/input from the command line placed into a single struct for
* use in this sample. This handles all of the command line parsing, validating, etc.
* See the Utils/CommandLineUtils for more information.
*/
Utils::cmdData cmdData = Utils::parseSampleInputPubSub(argc, argv, &apiHandle, "mqtt5-pubsub");
// Create the MQTT5 builder and populate it with data from cmdData.
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));
// Check if the builder setup correctly.
if (builder == nullptr)
{
printf(
"Failed to setup mqtt5 client builder with error code %d: %s", LastError(), ErrorDebugString(LastError()));
return -1;
}
// Setup connection options
std::shared_ptr<Mqtt5::ConnectPacket> connectOptions = std::make_shared<Mqtt5::ConnectPacket>();
connectOptions->WithClientId(cmdData.input_clientId);
builder->WithConnectOptions(connectOptions);
if (cmdData.input_port != 0)
{
builder->WithPort(static_cast<uint32_t>(cmdData.input_port));
}
std::promise<bool> connectionPromise;
std::promise<void> stoppedPromise;
std::promise<void> disconnectPromise;
std::promise<bool> subscribeSuccess;
// Setup lifecycle callbacks
builder->WithClientConnectionSuccessCallback(
[&connectionPromise](const Mqtt5::OnConnectionSuccessEventData &eventData) {
fprintf(
stdout,
"Mqtt5 Client connection succeed, clientid: %s.\n",
eventData.negotiatedSettings->getClientId().c_str());
connectionPromise.set_value(true);
});
builder->WithClientConnectionFailureCallback([&connectionPromise](
const Mqtt5::OnConnectionFailureEventData &eventData) {
fprintf(stdout, "Mqtt5 Client connection failed with error: %s.\n", aws_error_debug_str(eventData.errorCode));
connectionPromise.set_value(false);
});
builder->WithClientStoppedCallback([&stoppedPromise](const Mqtt5::OnStoppedEventData &) {
fprintf(stdout, "Mqtt5 Client stopped.\n");
stoppedPromise.set_value();
});
builder->WithClientAttemptingConnectCallback([](const Mqtt5::OnAttemptingConnectEventData &) {
fprintf(stdout, "Mqtt5 Client attempting connection...\n");
});
builder->WithClientDisconnectionCallback([&disconnectPromise](const Mqtt5::OnDisconnectionEventData &eventData) {
fprintf(stdout, "Mqtt5 Client disconnection with reason: %s.\n", aws_error_debug_str(eventData.errorCode));
disconnectPromise.set_value();
});
// This is invoked upon the receipt of a Publish on a subscribed topic.
builder->WithPublishReceivedCallback(
[&receiveMutex, &receivedCount, &receiveSignal](const Mqtt5::PublishReceivedEventData &eventData) {
if (eventData.publishPacket == nullptr)
return;
std::lock_guard<std::mutex> lock(receiveMutex);
++receivedCount;
fprintf(stdout, "Publish received on topic %s:", eventData.publishPacket->getTopic().c_str());
fwrite(eventData.publishPacket->getPayload().ptr, 1, eventData.publishPacket->getPayload().len, stdout);
fprintf(stdout, "\n");
for (Mqtt5::UserProperty prop : eventData.publishPacket->getUserProperties())
{
fprintf(stdout, "\twith UserProperty:(%s,%s)\n", prop.getName().c_str(), prop.getValue().c_str());
}
receiveSignal.notify_all();
});
// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
if (client == nullptr)
{
fprintf(
stdout, "Failed to Init Mqtt5Client with error code %d: %s", LastError(), ErrorDebugString(LastError()));
return -1;
}
/************************ Run the sample ****************************/
// Start mqtt5 connection session
if (client->Start())
{
if (connectionPromise.get_future().get() == false)
{
return -1;
}
auto onSubAck = [&subscribeSuccess](int error_code, std::shared_ptr<Mqtt5::SubAckPacket> suback) {
if (error_code != 0)
{
fprintf(
stdout,
"MQTT5 Client Subscription failed with error code: (%d)%s\n",
error_code,
aws_error_debug_str(error_code));
subscribeSuccess.set_value(false);
}
if (suback != nullptr)
{
for (Mqtt5::SubAckReasonCode reasonCode : suback->getReasonCodes())
{
if (reasonCode > Mqtt5::SubAckReasonCode::AWS_MQTT5_SARC_UNSPECIFIED_ERROR)
{
fprintf(stdout, "MQTT5 Client Subscription failed with server error code: %d\n", reasonCode);
if (suback->getReasonString().has_value())
{
fprintf(stdout, "\tError reason string: %s\n", suback->getReasonString()->c_str());
}
subscribeSuccess.set_value(false);
return;
}
}
}
subscribeSuccess.set_value(true);
};
Mqtt5::Subscription sub1(cmdData.input_topic, Mqtt5::QOS::AWS_MQTT5_QOS_AT_LEAST_ONCE);
sub1.WithNoLocal(false);
std::shared_ptr<Mqtt5::SubscribePacket> subPacket = std::make_shared<Mqtt5::SubscribePacket>();
subPacket->WithSubscription(std::move(sub1));
if (client->Subscribe(subPacket, onSubAck))
{
// Waiting for subscription completed.
if (subscribeSuccess.get_future().get() == true)
{
fprintf(stdout, "Subscription Success.\n");
/**
* Setup publish completion callback. The callback will get triggered when the publish completes (when
* the client received the PubAck from the server).
*/
auto onPublishComplete = [](int, std::shared_ptr<Aws::Crt::Mqtt5::PublishResult> result) {
if (!result->wasSuccessful())
{
fprintf(stdout, "Publish failed with error_code: %d", result->getErrorCode());
}
else if (result != nullptr)
{
std::shared_ptr<Mqtt5::PubAckPacket> puback =
std::dynamic_pointer_cast<Mqtt5::PubAckPacket>(result->getAck());
if (puback->getReasonCode() == 0)
{
fprintf(stdout, "Publish Succeed.\n");
}
else
{
fprintf(stdout, "PubACK reason code: %d\n", puback->getReasonCode());
if (puback->getReasonString().has_value())
{
fprintf(stdout, "\nError reason string: %s\n", puback->getReasonString()->c_str());
}
}
}
};
uint32_t publishedCount = 0;
while (publishedCount < cmdData.input_count)
{
// Add \" to 'JSON-ify' the message
String message = "\"" + cmdData.input_message + std::to_string(publishedCount + 1).c_str() + "\"";
ByteCursor payload = ByteCursorFromString(message);
std::shared_ptr<Mqtt5::PublishPacket> publish = std::make_shared<Mqtt5::PublishPacket>(
cmdData.input_topic, payload, Mqtt5::QOS::AWS_MQTT5_QOS_AT_LEAST_ONCE);
if (client->Publish(publish, onPublishComplete))
{
++publishedCount;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
{
std::unique_lock<std::mutex> receivedLock(receiveMutex);
receiveSignal.wait(receivedLock, [&] { return receivedCount >= cmdData.input_count; });
}
// Unsubscribe from the topic.
std::promise<void> unsubscribeFinishedPromise;
std::shared_ptr<Mqtt5::UnsubscribePacket> unsub = std::make_shared<Mqtt5::UnsubscribePacket>();
unsub->WithTopicFilter(cmdData.input_topic);
if (!client->Unsubscribe(unsub, [&](int, std::shared_ptr<Mqtt5::UnSubAckPacket>) {
unsubscribeFinishedPromise.set_value();
}))
{
fprintf(stdout, "Unsubscription failed.\n");
exit(-1);
}
unsubscribeFinishedPromise.get_future().wait();
}
else
{
fprintf(stdout, "Subscription failed.\n");
}
}
else
{
fprintf(stdout, "Subscribe operation failed on client.\n");
}
// Disconnect
if (!client->Stop())
{
fprintf(stdout, "Failed to disconnect from the mqtt connection. Exiting..\n");
return -1;
}
stoppedPromise.get_future().wait();
}
return 0;
}