-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_subscriber.cpp
254 lines (206 loc) · 7.97 KB
/
mqtt_subscriber.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
252
253
// mqtt_subscriber.cpp
//
// This is a Paho MQTT C++ client, sample application.
// An adapted version of
// https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/samples/async_subscribe.cpp
//
// This application is an MQTT subscriber using the C++ asynchronous client
// interface, employing callbacks to receive messages and status updates.
//
// The sample demonstrates:
// - Connecting to an MQTT server/broker.
// - Subscribing to a topic
// - Receiving messages through the callback API
// - Receiving network disconnect updates and attempting manual reconnects.
// - Using a "clean session" and manually re-subscribing to topics on
// reconnect.
//
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
#include <thread>
#include <vector>
#include <memory>
#include <iomanip>
#include "mqtt/async_client.h"
const std::string SERVER_ADDRESS("tcp://localhost:1883");
const std::string CLIENT_ID("paho_cpp_async_subcribe");
const std::string TOPIC("data/+/time");
const int QOS = 1;
const int N_RETRY_ATTEMPTS = 5;
/////////////////////////////////////////////////////////////////////////////
// Callbacks for the success or failures of requested actions.
// This could be used to initiate further action, but here we just log the
// results to the console.
class action_listener : public virtual mqtt::iaction_listener
{
std::string name_;
void on_failure(const mqtt::token& tok) override {
std::cout << name_ << " failure";
if (tok.get_message_id() != 0)
std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;
std::cout << std::endl;
}
void on_success(const mqtt::token& tok) override {
std::cout << name_ << " success";
if (tok.get_message_id() != 0)
std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;
auto top = tok.get_topics();
if (top && !top->empty())
std::cout << "\ttoken topic: '" << (*top)[0] << "', ..." << std::endl;
std::cout << std::endl;
}
public:
action_listener(const std::string& name) : name_(name) {}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Local callback & listener class for use with the client connection.
* This is primarily intended to receive messages, but it will also monitor
* the connection to the broker. If the connection is lost, it will attempt
* to restore the connection and re-subscribe to the topic.
*/
class callback : public virtual mqtt::callback,
public virtual mqtt::iaction_listener
{
// Counter for the number of connection retries
int nretry_;
// The MQTT client
mqtt::async_client& cli_;
// Options to use if we need to reconnect
mqtt::connect_options& connOpts_;
// An action listener to display the result of actions.
action_listener subListener_;
// Storage for the clients' message statistics
std::unordered_map<std::string, int> client_storage;
//Table data which will be refreshed
int lines_printed = 0;
// This deomonstrates manually reconnecting to the broker by calling
// connect() again. This is a possibility for an application that keeps
// a copy of it's original connect_options, or if the app wants to
// reconnect with different options.
// Another way this can be done manually, if using the same options, is
// to just call the async_client::reconnect() method.
void reconnect() {
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
try {
cli_.connect(connOpts_, nullptr, *this);
}
catch (const mqtt::exception& exc) {
std::cerr << "Error: " << exc.what() << std::endl;
exit(1);
}
}
// Re-connection failure
void on_failure(const mqtt::token& tok) override {
std::cout << "Connection attempt failed" << std::endl;
if (++nretry_ > N_RETRY_ATTEMPTS)
exit(1);
reconnect();
}
// (Re)connection success
// Either this or connected() can be used for callbacks.
void on_success(const mqtt::token& tok) override {}
// (Re)connection success
void connected(const std::string& cause) override {
std::cout << "\nConnection success" << std::endl;
std::cout << "\nSubscribing to topic '" << TOPIC << "'\n"
<< "\tfor client " << CLIENT_ID
<< " using QoS" << QOS << "\n"
<< "\nPress Q<Enter> to quit\n" << std::endl;
cli_.subscribe(TOPIC, QOS, nullptr, subListener_);
}
// Callback for when the connection is lost.
// This will initiate the attempt to manually reconnect.
void connection_lost(const std::string& cause) override {
std::cout << "\nConnection lost" << std::endl;
if (!cause.empty())
std::cout << "\tcause: " << cause << std::endl;
std::cout << "Reconnecting..." << std::endl;
nretry_ = 0;
reconnect();
}
// Callback for when a message arrives.
void message_arrived(mqtt::const_message_ptr msg) override {
std::string topic = msg->get_topic();
std::string client_name = topic.substr(topic.find("/") + 1, topic.rfind("/") - topic.find("/") - 1);
output_redraw();
if (auto pos = client_storage.find(client_name); pos != client_storage.end()) {
pos->second++;
}
else {
client_storage.insert({client_name, 0});
}
}
void output_redraw() {
const char sep = ' ';
const int nameWidth = 20;
const int numWidth = 22;
for (int i = 0; i < lines_printed; i++) {
std::cout << "\033[1A\033[K";
}
std::cout << "╔" << std::setw(nameWidth + 3) << std::setfill('=') << "╦" <<
std::setw(numWidth + 3) << std::setfill('=') << "╗" << std::endl;
std::cout << "╠" << std::setw(nameWidth + 3) << std::setfill('=') << "╬" <<
std::setw(numWidth + 3) << std::setfill('=') << "╣" << std::endl;
std::cout << "║" << std::setw(nameWidth) << std::setfill(sep) << "Client name ";
std::cout << "║" << std::setw(numWidth) << std::setfill(sep) << "Messages " << "║" << std::endl;
std::cout << "╠" << std::setw(nameWidth + 3) << std::setfill('=') << "╬" <<
std::setw(numWidth + 3) << std::setfill('=') << "╣" << std::endl;
lines_printed = 4;
for (auto & element : client_storage) {
std::cout << "║" << std::setw(nameWidth) << std::setfill(sep) << element.first << "║";
std::cout << std::setw(numWidth) << std::setfill(sep) << element.second << "║" << std::endl;
std::cout << "╠" << std::setw(nameWidth + 3) << std::setfill('=') << "╬" <<
std::setw(numWidth + 3) << std::setfill('=') << "╣" << std::endl;
lines_printed = lines_printed + 2;
}
std::cout << "╚" << std::setw(nameWidth + 3) << std::setfill('=') << "╩" <<
std::setw(numWidth + 3) << std::setfill('=') << "╝" << std::endl;
lines_printed++;
}
void delivery_complete(mqtt::delivery_token_ptr token) override {}
public:
callback(mqtt::async_client& cli, mqtt::connect_options& connOpts)
: nretry_(0), cli_(cli), connOpts_(connOpts), subListener_("Subscription") {}
};
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
// A subscriber often wants the server to remember its messages when its
// disconnected. In that case, it needs a unique ClientID and a
// non-clean session.
mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID);
mqtt::connect_options connOpts;
connOpts.set_clean_session(false);
// Install the callback(s) before connecting.
callback cb(cli, connOpts);
cli.set_callback(cb);
// Start the connection.
// When completed, the callback will subscribe to topic.
try {
std::cout << "Connecting to the MQTT server..." << std::flush;
cli.connect(connOpts, nullptr, cb);
}
catch (const mqtt::exception& exc) {
std::cerr << "\nERROR: Unable to connect to MQTT server: '"
<< SERVER_ADDRESS << "'" << exc << std::endl;
return 1;
}
// Just block till user tells us to quit.
while (std::tolower(std::cin.get()) != 'q')
;
// Disconnect
try {
std::cout << "\nDisconnecting from the MQTT server..." << std::flush;
cli.disconnect()->wait();
std::cout << "OK" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << exc << std::endl;
return 1;
}
return 0;
}