MQTTSuite
Loading...
Searching...
No Matches
mqtt::mqttintegrator::lib::Mqtt Class Reference

#include <Mqtt.h>

Inheritance diagram for mqtt::mqttintegrator::lib::Mqtt:
Collaboration diagram for mqtt::mqttintegrator::lib::Mqtt:

Classes

class  DelayedQueue
struct  ScheduledPublish

Public Member Functions

 Mqtt (const std::string &connectionName, std::shared_ptr< mqtt::lib::MqttMapper > mqttMapper, const std::string &sessionStoreFileName)
 ~Mqtt () override

Static Public Member Functions

static mqtt::lib::admin::ReloadResult updateSubscriptions (bool mustReconnect)

Private Types

using Super = iot::mqtt::client::Mqtt

Private Member Functions

void onConnected () final
bool onSignal (int signum) final
void onConnack (const iot::mqtt::packets::Connack &connack) final
void onPublish (const iot::mqtt::packets::Publish &publish) final
std::pair< std::size_t, std::size_t > resubscribe ()

Private Attributes

std::shared_ptr< mqtt::lib::MqttMappermqttMapper
std::list< iot::mqtt::Topic > currentSubscriptions
class mqtt::mqttintegrator::lib::Mqtt::DelayedQueue delayedQueue

Static Private Attributes

static std::set< Mqtt * > mqttInstances

Detailed Description

Definition at line 78 of file Mqtt.h.

Member Typedef Documentation

◆ Super

using mqtt::mqttintegrator::lib::Mqtt::Super = iot::mqtt::client::Mqtt
private

Definition at line 88 of file Mqtt.h.

Constructor & Destructor Documentation

◆ Mqtt()

mqtt::mqttintegrator::lib::Mqtt::Mqtt ( const std::string & connectionName,
std::shared_ptr< mqtt::lib::MqttMapper > mqttMapper,
const std::string & sessionStoreFileName )
explicit

Definition at line 71 of file Mqtt.cpp.

74 : iot::mqtt::client::Mqtt(connectionName, //
75 mqttMapper->getClientId(),
76 mqttMapper->getKeepAlive(),
77 sessionStoreFileName)
79 , currentSubscriptions(mqttMapper->extractSubscriptions())
80 , delayedQueue(this) {
81 mqttInstances.insert(this);
82 }
static std::set< Mqtt * > mqttInstances
Definition Mqtt.h:129
class mqtt::mqttintegrator::lib::Mqtt::DelayedQueue delayedQueue
std::shared_ptr< mqtt::lib::MqttMapper > mqttMapper
Definition Mqtt.h:100
std::list< iot::mqtt::Topic > currentSubscriptions
Definition Mqtt.h:101

References currentSubscriptions, mqtt::mqttintegrator::lib::Mqtt::DelayedQueue::DelayedQueue(), mqtt::lib::MqttMapper::extractSubscriptions(), mqtt::lib::MqttMapper::getClientId(), mqtt::lib::MqttMapper::getKeepAlive(), mqttInstances, and mqttMapper.

Here is the call graph for this function:

◆ ~Mqtt()

mqtt::mqttintegrator::lib::Mqtt::~Mqtt ( )
override

Definition at line 84 of file Mqtt.cpp.

84 {
85 mqttInstances.erase(this);
86 }

References mqttInstances.

Member Function Documentation

◆ onConnack()

void mqtt::mqttintegrator::lib::Mqtt::onConnack ( const iot::mqtt::packets::Connack & connack)
finalprivate

Definition at line 129 of file Mqtt.cpp.

129 {
130 if (connack.getReturnCode() == 0 && !connack.getSessionPresent()) {
131 sendSubscribe(currentSubscriptions);
132 }
133 }

References currentSubscriptions.

◆ onConnected()

void mqtt::mqttintegrator::lib::Mqtt::onConnected ( )
finalprivate

Definition at line 112 of file Mqtt.cpp.

112 {
113 const auto& [cleanSession, //
114 willTopic,
115 willMessage,
116 WillQoS,
117 willRetain,
118 username,
119 password] = mqttMapper->getConnectPayload();
120
121 sendConnect(cleanSession, willTopic, willMessage, WillQoS, willRetain, username, password);
122 }

References mqtt::lib::MqttMapper::getConnectPayload(), and mqttMapper.

Here is the call graph for this function:

◆ onPublish()

void mqtt::mqttintegrator::lib::Mqtt::onPublish ( const iot::mqtt::packets::Publish & publish)
finalprivate

Definition at line 135 of file Mqtt.cpp.

135 {
136 const auto& [immediatePublishes, scheduledPublishes] = mqttMapper->getMappings(publish);
137
138 for (const mqtt::lib::MqttMapper::ScheduledPublish& delayedPublish : scheduledPublishes) {
139 delayedQueue.delayPublish(delayedPublish.delay, delayedPublish.publish);
140 }
141
142 for (const iot::mqtt::packets::Publish& immediatePublish : immediatePublishes) {
143 sendPublish(
144 immediatePublish.getTopic(), immediatePublish.getMessage(), immediatePublish.getQoS(), immediatePublish.getRetain());
145 }
146 }

References mqtt::lib::MqttMapper::ScheduledPublish::delay, mqtt::mqttintegrator::lib::Mqtt::DelayedQueue::delayPublish(), mqtt::lib::MqttMapper::getMappings(), mqttMapper, and mqtt::lib::MqttMapper::ScheduledPublish::publish.

Here is the call graph for this function:

◆ onSignal()

bool mqtt::mqttintegrator::lib::Mqtt::onSignal ( int signum)
nodiscardfinalprivate

Definition at line 124 of file Mqtt.cpp.

124 {
125 sendDisconnect();
126 return Super::onSignal(signum);
127 }

◆ resubscribe()

std::pair< std::size_t, std::size_t > mqtt::mqttintegrator::lib::Mqtt::resubscribe ( )
private

Definition at line 148 of file Mqtt.cpp.

148 {
149 std::list<iot::mqtt::Topic> newSubscriptions = mqttMapper->extractSubscriptions();
150
151 std::list<std::string> topicsToUnsubscribe;
152 for (const auto& currentTopic : currentSubscriptions) {
153 const bool existsInNew = std::any_of(newSubscriptions.begin(), newSubscriptions.end(), [&](const auto& newTopic) {
154 return currentTopic.getName() == newTopic.getName() && currentTopic.getQoS() == newTopic.getQoS();
155 });
156
157 if (!existsInNew) {
158 topicsToUnsubscribe.push_back(currentTopic.getName());
159 }
160 }
161
162 if (!topicsToUnsubscribe.empty()) {
163 sendUnsubscribe(topicsToUnsubscribe);
164 }
165
166 std::list<iot::mqtt::Topic> topicsToSubscribe;
167 for (const auto& newTopic : newSubscriptions) {
168 const bool existsInOld = std::any_of(currentSubscriptions.begin(), currentSubscriptions.end(), [&](const auto& currentTopic) {
169 return currentTopic.getName() == newTopic.getName() && currentTopic.getQoS() == newTopic.getQoS();
170 });
171
172 if (!existsInOld) {
173 topicsToSubscribe.push_back(newTopic);
174 }
175 }
176
177 if (!topicsToSubscribe.empty()) {
178 sendSubscribe(topicsToSubscribe);
179 }
180
181 currentSubscriptions = newSubscriptions;
182
183 return {topicsToSubscribe.size(), topicsToUnsubscribe.size()};
184 }

References currentSubscriptions, mqtt::lib::MqttMapper::extractSubscriptions(), and mqttMapper.

Referenced by updateSubscriptions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateSubscriptions()

mqtt::lib::admin::ReloadResult mqtt::mqttintegrator::lib::Mqtt::updateSubscriptions ( bool mustReconnect)
static

Definition at line 88 of file Mqtt.cpp.

88 {
89 mqtt::lib::admin::ReloadResult reloadResult;
90
91 reloadResult.instances = mqttInstances.size();
92 if (mustReconnect) {
93 reloadResult.mode = "reconnect";
94 } else {
95 reloadResult.mode = "hot";
96 }
97
98 for (Mqtt* mqtt : mqttInstances) {
99 if (mustReconnect) {
100 mqtt->sendDisconnect();
101 } else {
102 auto [subscribeCount, unsubscribeCount] = mqtt->resubscribe();
103
104 reloadResult.subscribed += subscribeCount;
105 reloadResult.unsubscribed += unsubscribeCount;
106 }
107 }
108
109 return reloadResult;
110 }
Mqtt(const std::string &connectionName, std::shared_ptr< mqtt::lib::MqttMapper > mqttMapper, const std::string &sessionStoreFileName)
Definition Mqtt.cpp:71

References mqtt::lib::admin::ReloadResult::instances, mqtt::lib::admin::ReloadResult::mode, mqttInstances, resubscribe(), mqtt::lib::admin::ReloadResult::subscribed, and mqtt::lib::admin::ReloadResult::unsubscribed.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ currentSubscriptions

std::list<iot::mqtt::Topic> mqtt::mqttintegrator::lib::Mqtt::currentSubscriptions
private

Definition at line 101 of file Mqtt.h.

Referenced by Mqtt(), onConnack(), and resubscribe().

◆ delayedQueue

class mqtt::mqttintegrator::lib::Mqtt::DelayedQueue mqtt::mqttintegrator::lib::Mqtt::delayedQueue
private

◆ mqttInstances

std::set< Mqtt * > mqtt::mqttintegrator::lib::Mqtt::mqttInstances
staticprivate

Definition at line 129 of file Mqtt.h.

Referenced by Mqtt(), updateSubscriptions(), and ~Mqtt().

◆ mqttMapper

std::shared_ptr<mqtt::lib::MqttMapper> mqtt::mqttintegrator::lib::Mqtt::mqttMapper
private

Definition at line 100 of file Mqtt.h.

Referenced by Mqtt(), onConnected(), onPublish(), and resubscribe().


The documentation for this class was generated from the following files: