-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqttsubscriber.cpp
More file actions
153 lines (125 loc) · 3.72 KB
/
Copy pathmqttsubscriber.cpp
File metadata and controls
153 lines (125 loc) · 3.72 KB
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
#include "mqttsubscriber.h"
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
MQTTSubscriber::MQTTSubscriber(const std::string &address, const std::string &topic, int qos, int timeout, int numRetries) :
_address(address), _topic(topic), _qos(qos), _timeOut(timeout), _numRetries(numRetries)
{
_logger = DNRLogger::instance();
_getDatacb = nullptr;
_callee = nullptr;
_client = nullptr;
char buffer[32];
srand(time(nullptr));
unsigned int clid = rand();
sprintf(buffer,"%d",clid);
_clientID = buffer;
_connOpts = new mqtt::connect_options;
}
MQTTSubscriber::~MQTTSubscriber()
{
stop();
}
void MQTTSubscriber::setDebugLogging(bool value)
{
_logger->setDebugOut(value);
}
bool MQTTSubscriber::areCallBacksEnabled()
{
return (NULL != _cb);
}
void MQTTSubscriber::connectCallback(MQGetDataCallbackFunctionPtr cb, void *p)
{
_getDatacb = cb; //CallBack
_callee = p; //clienpointer
}
void MQTTSubscriber::setPersistance(const std::string& clientID, short qos)
{
if(_client == NULL)
{
_clientID = clientID;
_qos = qos;
}
else
{
_logger->logNotice("MQTTSubscriber::setPersistance Needs to be called before Start");
}
}
bool MQTTSubscriber::start()
{
try
{
if(_client == NULL)
{
_subListener = new action_listenerSub(this, "MQTTSubscriber");
_client = new mqtt::async_client(_address, _clientID);
if(_client)
{
_connOpts->set_keep_alive_interval(20);
_connOpts->set_clean_session((_qos == 0));
_cb = new callbackSub(this, *_client, *_connOpts, *_subListener);
_client->set_callback(*_cb);
_logger->logInfo("MQTTSubscriber::Start Connecting to the MQTT server");
_client->connect(*_connOpts, nullptr, *_cb);
}
else
{
_logger->logInfo("MQTTSubscriber::Start No MQQ Client Halting");
return false;
}
}
}
catch (const mqtt::exception&)
{
std::stringstream info;
info << "MQTTSubscriber::Start Unable to connect to MQTT server: '"
<< _address;
_logger->logCritical(info.str());
return false;
}
return true;
}
bool MQTTSubscriber::stop()
{
std::stringstream info;
try
{
if(_client)
{
info << "MQTTSubscriber::Stop Disconnecting from the MQTT server";
_logger->logInfo(info.str());
//One oddity, you can not unsubscribe if you have durribale consumers.
//You need to NOT unsubscribe. So... If QOS is anything BUT ZERO
//I will not Unsubscribe!
if(_qos == 0)
{
info.str("");
info << "MQTTSubscriber::Stop QOS is 0 Unsubscribing To: " << _topic;
_logger->logNotice(info.str());
_client->unsubscribe(_topic);
}
else
{
info.str("");
info << "MQTTSubscriber::Stop QOS is Not 0 Staying Subscribed Topic: " << _topic;
_logger->logNotice(info.str());
}
_client->disconnect()->wait();
_logger->logInfo("MQTTSubscriber::Stop Disconnected from the MQTT server");
delete _subListener;
delete _client;
delete _cb;
delete _connOpts;
_client = NULL;
return true;
}
}
catch (const mqtt::exception& exc)
{
info.str("");
info << "MQTTSubscriber::Stop Exception: " << exc.what();
_logger->logCritical(info.str());
return false;
}
return true;
}