SNode.C
Loading...
Searching...
No Matches
iot::mqtt::server::broker::RetainTree::TopicLevel Class Reference
Collaboration diagram for iot::mqtt::server::broker::RetainTree::TopicLevel:

Public Member Functions

 TopicLevel (iot::mqtt::server::broker::Broker *broker)
 
void retain (const Message &message, std::string topic)
 
bool release (std::string topic)
 
void appear (const std::string &clientId, std::string topic, uint8_t qoS)
 
TopicLevelfromJson (const nlohmann::json &json)
 
nlohmann::json toJson () const
 
void clear ()
 

Private Member Functions

void appear (const std::string &clientId, uint8_t clientQoS)
 

Private Attributes

Message message
 
std::map< std::string, TopicLevelsubTopicLevels
 
iot::mqtt::server::broker::Brokerbroker = nullptr
 

Detailed Description

Definition at line 53 of file RetainTree.h.

Constructor & Destructor Documentation

◆ TopicLevel()

iot::mqtt::server::broker::RetainTree::TopicLevel::TopicLevel ( iot::mqtt::server::broker::Broker * broker)
explicit

Definition at line 71 of file RetainTree.cpp.

72 : broker(broker) {
73 }
iot::mqtt::server::broker::Broker * broker
Definition RetainTree.h:74

Referenced by clear().

Here is the caller graph for this function:

Member Function Documentation

◆ appear() [1/2]

void iot::mqtt::server::broker::RetainTree::TopicLevel::appear ( const std::string & clientId,
std::string topic,
uint8_t qoS )

Definition at line 129 of file RetainTree.cpp.

129 {
130 if (topic.empty()) {
131 if (!message.getTopic().empty()) {
132 LOG(INFO) << "MQTT Broker: Retained Topic found:";
133 LOG(INFO) << "MQTT Broker: Topic: " << message.getTopic();
134 LOG(INFO) << "MQTT Broker: Message:\n" << iot::mqtt::Mqtt::toHexString(message.getMessage());
135 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(message.getQoS());
136 LOG(DEBUG) << "MQTT Broker: Client:";
137 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(qoS);
138
139 LOG(INFO) << "MQTT Broker: Distributing message ...";
140 broker->sendPublish(clientId, message, std::min(message.getQoS(), qoS), true);
141 LOG(INFO) << "MQTT Broker: ... distributing message completed";
142 }
143 } else {
144 const std::string topicLevel = topic.substr(0, topic.find('/'));
145
146 topic.erase(0, topicLevel.size() + 1);
147
148 auto foundNode = subTopicLevels.find(topicLevel);
149 if (foundNode != subTopicLevels.end()) {
150 foundNode->second.appear(clientId, topic, qoS);
151 } else if (topicLevel == "+") {
152 for (auto& [notUsed, topicTree] : subTopicLevels) {
153 topicTree.appear(clientId, topic, qoS);
154 }
155 } else if (topicLevel == "#") {
156 appear(clientId, qoS);
157 }
158 }
159 }
static std::string toHexString(const std::vector< char > &data)
Definition Mqtt.cpp:367
void sendPublish(const std::string &clientId, Message &message, uint8_t qoS, bool retain)
Definition Broker.cpp:204
const std::string & getTopic() const
Definition Message.cpp:44
const std::string & getMessage() const
Definition Message.cpp:52
void appear(const std::string &clientId, std::string topic, uint8_t qoS)
std::map< std::string, TopicLevel > subTopicLevels
Definition RetainTree.h:72

◆ appear() [2/2]

void iot::mqtt::server::broker::RetainTree::TopicLevel::appear ( const std::string & clientId,
uint8_t clientQoS )
private

Definition at line 161 of file RetainTree.cpp.

161 {
162 if (!message.getTopic().empty()) {
163 LOG(INFO) << "MQTT Broker: Retained Topic found:";
164 LOG(INFO) << "MQTT Broker: Topic: " << message.getTopic();
165 LOG(INFO) << "MQTT Broker: Message:\n" << iot::mqtt::Mqtt::toHexString(message.getMessage());
166 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(message.getQoS());
167 LOG(DEBUG) << "MQTT Broker: Client:";
168 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(clientQoS);
169
170 LOG(INFO) << "MQTT Broker: Distributing message ...";
171 broker->sendPublish(clientId, message, std::min(message.getQoS(), clientQoS), true);
172 LOG(INFO) << "MQTT Broker: ... distributing message completed";
173 }
174
175 for (auto& [topicLevel, topicTree] : subTopicLevels) {
176 topicTree.appear(clientId, clientQoS);
177 }
178 }

◆ clear()

void iot::mqtt::server::broker::RetainTree::TopicLevel::clear ( )

Definition at line 194 of file RetainTree.cpp.

194 {
195 *this = TopicLevel(broker);
196 }
TopicLevel(iot::mqtt::server::broker::Broker *broker)

References TopicLevel().

Here is the call graph for this function:

◆ fromJson()

RetainTree::TopicLevel & iot::mqtt::server::broker::RetainTree::TopicLevel::fromJson ( const nlohmann::json & json)

Definition at line 75 of file RetainTree.cpp.

75 {
76 subTopicLevels.clear();
77
78 message.fromJson(json.value("message", nlohmann::json()));
79
80 if (json.contains("topic_level")) {
81 for (const auto& topicLevelItem : json["topic_level"].items()) {
82 subTopicLevels.emplace(topicLevelItem.key(), TopicLevel(broker).fromJson(topicLevelItem.value()));
83 }
84 }
85
86 return *this;
87 }
Message & fromJson(const nlohmann::json &json)
Definition Message.cpp:83
TopicLevel & fromJson(const nlohmann::json &json)

◆ release()

bool iot::mqtt::server::broker::RetainTree::TopicLevel::release ( std::string topic)

Definition at line 105 of file RetainTree.cpp.

105 {
106 if (topic.empty()) {
107 LOG(DEBUG) << "MQTT Broker: Release retained:";
108 LOG(DEBUG) << "MQTT Broker: Topic: " << message.getTopic();
109
110 message = Message();
111 } else {
112 const std::string topicLevel = topic.substr(0, topic.find('/'));
113
114 auto&& it = subTopicLevels.find(topicLevel);
115 if (it != subTopicLevels.end()) {
116 topic.erase(0, topicLevel.size() + 1);
117
118 if (it->second.release(topic)) {
119 LOG(DEBUG) << " Erase: " << topicLevel;
120
121 subTopicLevels.erase(it);
122 }
123 }
124 }
125
126 return subTopicLevels.empty() && message.getMessage().empty();
127 }

◆ retain()

void iot::mqtt::server::broker::RetainTree::TopicLevel::retain ( const Message & message,
std::string topic )

Definition at line 88 of file RetainTree.cpp.

88 {
89 if (topic.empty()) {
90 LOG(DEBUG) << "MQTT Broker: Retain:";
91 LOG(DEBUG) << "MQTT Broker: Topic: " << message.getTopic();
92 LOG(DEBUG) << "MQTT Broker: Message:\n" << iot::mqtt::Mqtt::toHexString(message.getMessage());
93 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(message.getQoS());
94
95 this->message = message;
96 } else {
97 const std::string topicLevel = topic.substr(0, topic.find('/'));
98
99 topic.erase(0, topicLevel.size() + 1);
100
101 subTopicLevels.insert({topicLevel, RetainTree::TopicLevel(broker)}).first->second.retain(message, topic);
102 }
103 }

References iot::mqtt::server::broker::Message::getQoS().

Here is the call graph for this function:

◆ toJson()

nlohmann::json iot::mqtt::server::broker::RetainTree::TopicLevel::toJson ( ) const

Definition at line 180 of file RetainTree.cpp.

180 {
181 nlohmann::json json;
182
183 if (!message.getMessage().empty()) {
184 json["message"] = message.toJson();
185 }
186
187 for (const auto& [topicLevel, topicLevelValue] : subTopicLevels) {
188 json["topic_level"][topicLevel] = topicLevelValue.toJson();
189 }
190
191 return json;
192 }
nlohmann::json toJson() const
Definition Message.cpp:72

Member Data Documentation

◆ broker

iot::mqtt::server::broker::Broker* iot::mqtt::server::broker::RetainTree::TopicLevel::broker = nullptr
private

Definition at line 74 of file RetainTree.h.

◆ message

Message iot::mqtt::server::broker::RetainTree::TopicLevel::message
private

Definition at line 70 of file RetainTree.h.

◆ subTopicLevels

std::map<std::string, TopicLevel> iot::mqtt::server::broker::RetainTree::TopicLevel::subTopicLevels
private

Definition at line 72 of file RetainTree.h.


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