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

#include <Mqtt.h>

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

Public Member Functions

 Mqtt (const std::string &connectionName, const std::string &clientId, std::uint8_t qoSDefault, std::uint16_t keepAlive, bool cleanSession, const std::string &willTopic, const std::string &willMessage, std::uint8_t willQoS, bool willRetain, const std::string &username, const std::string &password, const std::list< std::string > &subTopics, MariaDbStorage::ConnectionConfig connectionConfig, const std::string &rawTable, bool autoCreateRawTable, StoragePlan storagePlan, const std::string &sessionStoreFileName="")

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
void onSuback (const iot::mqtt::packets::Suback &suback) final

Static Private Member Functions

static std::uint8_t getQos (const std::string &qoSString)

Private Attributes

MariaDbStorage storage
const std::uint8_t qoSDefault
const bool cleanSession
const std::string willTopic
const std::string willMessage
const std::uint8_t willQoS
const bool willRetain
const std::string username
const std::string password
const std::list< std::string > subTopics

Detailed Description

Definition at line 33 of file Mqtt.h.

Member Typedef Documentation

◆ Super

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

Definition at line 54 of file Mqtt.h.

Constructor & Destructor Documentation

◆ Mqtt()

mqtt::mqttstore::lib::Mqtt::Mqtt ( const std::string & connectionName,
const std::string & clientId,
std::uint8_t qoSDefault,
std::uint16_t keepAlive,
bool cleanSession,
const std::string & willTopic,
const std::string & willMessage,
std::uint8_t willQoS,
bool willRetain,
const std::string & username,
const std::string & password,
const std::list< std::string > & subTopics,
MariaDbStorage::ConnectionConfig connectionConfig,
const std::string & rawTable,
bool autoCreateRawTable,
StoragePlan storagePlan,
const std::string & sessionStoreFileName = "" )
explicit

Definition at line 37 of file Mqtt.cpp.

54 : iot::mqtt::client::Mqtt(connectionName, clientId, keepAlive, sessionStoreFileName)
55 , storage(connectionName, connectionConfig, rawTable, autoCreateRawTable, std::move(storagePlan))
65 VLOG(1) << "MQTTStore client id: " << clientId;
66 VLOG(1) << " Keep Alive: " << keepAlive;
67 VLOG(1) << " Clean Session: " << cleanSession;
68 VLOG(1) << " Will Topic: " << willTopic;
69 VLOG(1) << " Will QoS: " << static_cast<std::uint16_t>(willQoS);
70 VLOG(1) << " Will Retain: " << willRetain;
71 VLOG(1) << " Username configured: " << (!username.empty());
72 }
const std::string willTopic
Definition Mqtt.h:68
const std::uint8_t qoSDefault
Definition Mqtt.h:66
MariaDbStorage storage
Definition Mqtt.h:65
const bool cleanSession
Definition Mqtt.h:67
const bool willRetain
Definition Mqtt.h:71
const std::string willMessage
Definition Mqtt.h:69
const std::uint8_t willQoS
Definition Mqtt.h:70
const std::list< std::string > subTopics
Definition Mqtt.h:74
const std::string username
Definition Mqtt.h:72
const std::string password
Definition Mqtt.h:73

References cleanSession, Mqtt(), password, qoSDefault, username, willMessage, willQoS, willRetain, and willTopic.

Referenced by Mqtt().

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

Member Function Documentation

◆ getQos()

std::uint8_t mqtt::mqttstore::lib::Mqtt::getQos ( const std::string & qoSString)
staticnodiscardprivate

Definition at line 87 of file Mqtt.cpp.

87 {
88 const unsigned long qoS = std::stoul(qoSString);
89
90 if (qoS > 2) {
91 throw std::out_of_range("qos " + qoSString + " not in range [0..2]");
92 }
93
94 return static_cast<std::uint8_t>(qoS);
95 }

Referenced by onConnack().

Here is the caller graph for this function:

◆ onConnack()

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

Definition at line 97 of file Mqtt.cpp.

97 {
98 if (connack.getReturnCode() != 0) {
99 VLOG(0) << connectionName << " MQTTStore: broker rejected connection with return code "
100 << static_cast<int>(connack.getReturnCode());
101 sendDisconnect();
102 return;
103 }
104
105 if (subTopics.empty()) {
106 VLOG(0) << connectionName << " MQTTStore: no subscriptions configured";
107 sendDisconnect();
108 return;
109 }
110
111 try {
112 std::list<iot::mqtt::Topic> topicList;
113 std::transform(subTopics.begin(),
114 subTopics.end(),
115 std::back_inserter(topicList),
116 [qoSDefault = this->qoSDefault](const std::string& compositeTopic) {
117 const std::size_t pos = compositeTopic.rfind("##");
118 const std::string topic = compositeTopic.substr(0, pos);
119 std::uint8_t qoS = qoSDefault;
120
121 if (pos != std::string::npos) {
122 qoS = getQos(compositeTopic.substr(pos + 2));
123 }
124
125 VLOG(0) << "MQTTStore subscribe: QoS " << static_cast<int>(qoS) << " | " << topic;
126 return iot::mqtt::Topic(topic, qoS);
127 });
128
129 sendSubscribe(topicList);
130 } catch (const std::logic_error& error) {
131 VLOG(0) << connectionName << " MQTTStore subscription failed: " << error.what();
132 sendDisconnect();
133 }
134 }

References getQos(), and qoSDefault.

Here is the call graph for this function:

◆ onConnected()

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

Definition at line 74 of file Mqtt.cpp.

74 {
75 VLOG(1) << "MQTTStore: initiating MQTT session";
77 }

References cleanSession, password, username, willMessage, willQoS, willRetain, and willTopic.

◆ onPublish()

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

Definition at line 136 of file Mqtt.cpp.

136 {
137 VLOG(1) << connectionName << " MQTTStore received publish: topic='" << publish.getTopic()
138 << "' qos=" << static_cast<std::uint16_t>(publish.getQoS()) << " retain=" << (publish.getRetain() != 0)
139 << " dup=" << (publish.getDup() != 0);
140
141 storage.store({.connectionName = connectionName,
142 .topic = publish.getTopic(),
143 .payload = publish.getMessage(),
144 .qoS = publish.getQoS(),
145 .retain = publish.getRetain() != 0,
146 .dup = publish.getDup() != 0,
147 .packetIdentifier = publish.getPacketIdentifier()});
148 }

◆ onSignal()

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

Definition at line 79 of file Mqtt.cpp.

79 {
80 VLOG(1) << "MQTTStore: exit due to '" << strsignal(signum) << "' (SIG" << utils::system::sigabbrev_np(signum) << " = " << signum
81 << ")";
82 sendDisconnect();
83
84 return Super::onSignal(signum);
85 }

◆ onSuback()

void mqtt::mqttstore::lib::Mqtt::onSuback ( const iot::mqtt::packets::Suback & suback)
finalprivate

Definition at line 150 of file Mqtt.cpp.

150 {
151 VLOG(1) << "MQTTStore Suback";
152
153 for (auto returnCode : suback.getReturnCodes()) {
154 VLOG(0) << " r: " << static_cast<int>(returnCode);
155 }
156 }

Member Data Documentation

◆ cleanSession

const bool mqtt::mqttstore::lib::Mqtt::cleanSession
private

Definition at line 67 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ password

const std::string mqtt::mqttstore::lib::Mqtt::password
private

Definition at line 73 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ qoSDefault

const std::uint8_t mqtt::mqttstore::lib::Mqtt::qoSDefault
private

Definition at line 66 of file Mqtt.h.

Referenced by Mqtt(), and onConnack().

◆ storage

MariaDbStorage mqtt::mqttstore::lib::Mqtt::storage
private

Definition at line 65 of file Mqtt.h.

◆ subTopics

const std::list<std::string> mqtt::mqttstore::lib::Mqtt::subTopics
private

Definition at line 74 of file Mqtt.h.

◆ username

const std::string mqtt::mqttstore::lib::Mqtt::username
private

Definition at line 72 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ willMessage

const std::string mqtt::mqttstore::lib::Mqtt::willMessage
private

Definition at line 69 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ willQoS

const std::uint8_t mqtt::mqttstore::lib::Mqtt::willQoS
private

Definition at line 70 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ willRetain

const bool mqtt::mqttstore::lib::Mqtt::willRetain
private

Definition at line 71 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().

◆ willTopic

const std::string mqtt::mqttstore::lib::Mqtt::willTopic
private

Definition at line 68 of file Mqtt.h.

Referenced by Mqtt(), and onConnected().


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