SNode.C
Loading...
Searching...
No Matches
Broker.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "iot/mqtt/server/broker/Broker.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "log/Logger.h"
47
48#include <algorithm>
49#include <cstdio>
50#include <fstream>
51#include <nlohmann/json.hpp>
52
53// IWYU pragma: no_include <nlohmann/json_fwd.hpp>
54// IWYU pragma: no_include <nlohmann/detail/iterators/iteration_proxy.hpp>
55
56#endif // DOXYGEN_SHOULD_SKIP_THIS
57
58namespace iot::mqtt::server::broker {
59
60 Broker::Broker(uint8_t maxQoS, const std::string& sessionStoreFileName)
61 : sessionStoreFileName(sessionStoreFileName) // NOLINT
62 , maxQoS(maxQoS)
64 , retainTree(this) {
65 if (!sessionStoreFileName.empty()) {
66 std::ifstream sessionStoreFile(sessionStoreFileName);
67
68 if (sessionStoreFile.is_open()) {
69 try {
70 nlohmann::json sessionStoreJson;
71
72 sessionStoreFile >> sessionStoreJson;
73
74 for (const auto& [clientId, sessionJson] : sessionStoreJson["session_store"].items()) {
75 sessionStore[clientId].fromJson(sessionJson);
76 }
77 retainTree.fromJson(sessionStoreJson["retain_tree"]);
78 subscribtionTree.fromJson(sessionStoreJson["subscribtion_tree"]);
79
80 LOG(INFO) << "MQTT Broker: Persistent session data loaded successful";
81 } catch (const nlohmann::json::exception&) {
82 LOG(INFO) << "MQTT Broker: Starting with empty session: Session store '" << sessionStoreFileName
83 << "' empty or corrupted";
84
85 sessionStore.clear();
88 }
89
90 sessionStoreFile.close();
91 std::remove(sessionStoreFileName.data()); // NOLINT
92
93 LOG(INFO) << "MQTT Broker: Restoring saved session done";
94 } else {
95 PLOG(WARNING) << "MQTT Broker: Could not read session store '" << sessionStoreFileName << "'";
96 }
97 } else {
98 LOG(INFO) << "MQTT Broker: Session not reloaded: Session store filename empty";
99 }
100 }
101
103 if (!sessionStoreFileName.empty()) {
104 nlohmann::json sessionStoreJson;
105
106 for (auto& [clientId, session] : sessionStore) {
107 sessionStoreJson["session_store"][clientId] = session.toJson();
108 }
109 sessionStoreJson["retain_tree"] = retainTree.toJson();
110 sessionStoreJson["subscribtion_tree"] = subscribtionTree.toJson();
111
112 if (sessionStoreJson["session_store"].empty()) {
113 sessionStoreJson.erase("session_store");
114 }
115 if (sessionStoreJson["retain_tree"].empty()) {
116 sessionStoreJson.erase("retain_tree");
117 }
118 if (sessionStoreJson["subscribtion_tree"].empty()) {
119 sessionStoreJson.erase("subscribtion_tree");
120 }
121
122 std::ofstream sessionStoreFile(sessionStoreFileName);
123
124 if (sessionStoreFile.is_open()) {
125 if (!sessionStoreJson.empty()) {
126 sessionStoreFile << sessionStoreJson;
127 }
128
129 sessionStoreFile.close();
130
131 LOG(INFO) << "MQTT Broker: Session store written '" << sessionStoreFileName << "'";
132 } else {
133 PLOG(ERROR) << "MQTT Broker: Could not write session store '" << sessionStoreFileName << "'";
134 }
135 } else {
136 LOG(INFO) << "MQTT Broker: Session not saved: Session store filename empty";
137 }
138 }
139
140 std::shared_ptr<Broker> Broker::instance(uint8_t maxQoS, const std::string& sessionStoreFileName) {
141 static const std::shared_ptr<Broker> broker = std::make_shared<Broker>(maxQoS, sessionStoreFileName);
142
143 return broker;
144 }
145
146 void Broker::appear(const std::string& clientId, const std::string& topic, uint8_t qoS) {
147 retainTree.appear(clientId, topic, qoS);
148 }
149
150 void Broker::unsubscribe(const std::string& clientId) {
152 }
153
154 void
155 Broker::publish(const std::string& originClientId, const std::string& topic, const std::string& message, uint8_t qoS, bool retain) {
156 subscribtionTree.publish(Message(originClientId, topic, message, qoS, retain));
157
158 if (retain) {
159 retainTree.retain(Message(originClientId, topic, message, qoS, retain));
160 }
161 }
162
163 uint8_t Broker::subscribe(const std::string& clientId, const std::string& topic, uint8_t qoS) {
164 qoS = std::min(maxQoS, qoS);
165 uint8_t returnCode = 0;
166
167 if (subscribtionTree.subscribe(topic, clientId, qoS)) {
168 retainTree.appear(clientId, topic, qoS);
169
170 returnCode = SUBSCRIBTION_SUCCESS | qoS;
171 } else {
172 returnCode = SUBSCRIBTION_FAILURE;
173 }
174
175 return returnCode;
176 }
177
178 void Broker::unsubscribe(const std::string& clientId, const std::string& topic) {
180 }
181
182 std::list<std::string> Broker::getSubscriptions(const std::string& clientId) const {
184 }
185
186 std::map<std::string, std::list<std::pair<std::string, uint8_t>>> Broker::getSubscriptionTree() const {
188 }
189
191 return retainTree;
192 }
193
194 bool Broker::hasSession(const std::string& clientId) {
195 return sessionStore.contains(clientId);
196 }
197
198 bool Broker::hasActiveSession(const std::string& clientId) {
199 return hasSession(clientId) && sessionStore[clientId].isActive();
200 }
201
202 bool Broker::hasRetainedSession(const std::string& clientId) {
203 return hasSession(clientId) && !sessionStore[clientId].isActive();
204 }
205
206 bool Broker::isActiveSession(const std::string& clientId, const iot::mqtt::server::Mqtt* mqtt) {
207 return hasSession(clientId) && sessionStore[clientId].isOwnedBy(mqtt);
208 }
209
210 Session* Broker::newSession(const std::string& clientId, iot::mqtt::server::Mqtt* mqtt) {
211 sessionStore[clientId] = iot::mqtt::server::broker::Session(mqtt);
212
213 return &sessionStore[clientId];
214 }
215
216 Session* Broker::renewSession(const std::string& clientId, iot::mqtt::server::Mqtt* mqtt) {
217 return sessionStore[clientId].renew(mqtt);
218 }
219
220 void Broker::restartSession(const std::string& clientId) {
221 LOG(INFO) << "MQTT Broker: Retained: Send PUBLISH: " << clientId;
223
224 LOG(INFO) << "MQTT Broker: Queued: Send PUBLISH: " << clientId;
226 }
227
228 void Broker::retainSession(const std::string& clientId) {
229 sessionStore[clientId].retain();
230 }
231
232 void Broker::deleteSession(const std::string& clientId) {
234 sessionStore.erase(clientId);
235 }
236
237 void Broker::sendPublish(const std::string& clientId, Message& message, uint8_t qoS, bool retain) {
238 LOG(INFO) << "MQTT Broker: Send PUBLISH: " << clientId;
239
240 sessionStore[clientId].sendPublish(message, qoS, retain);
241 }
242
243} // namespace iot::mqtt::server::broker
#define SUBSCRIBTION_FAILURE
Definition Broker.h:69
#define SUBSCRIBTION_SUCCESS
Definition Broker.h:68
void restartSession(const std::string &clientId)
Definition Broker.cpp:220
uint8_t subscribe(const std::string &clientId, const std::string &topic, uint8_t qoS)
Definition Broker.cpp:163
bool hasActiveSession(const std::string &clientId)
Definition Broker.cpp:198
bool isActiveSession(const std::string &clientId, const Mqtt *mqtt)
Definition Broker.cpp:206
iot::mqtt::server::broker::RetainTree retainTree
Definition Broker.h:112
void publish(const std::string &originClientId, const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
Definition Broker.cpp:155
std::map< std::string, iot::mqtt::server::broker::Session > sessionStore
Definition Broker.h:114
std::list< std::string > getSubscriptions(const std::string &clientId) const
Definition Broker.cpp:182
Broker(uint8_t maxQoS, const std::string &sessionStoreFileName)
Definition Broker.cpp:60
bool hasSession(const std::string &clientId)
Definition Broker.cpp:194
void appear(const std::string &clientId, const std::string &topic, uint8_t qoS)
Definition Broker.cpp:146
static std::shared_ptr< Broker > instance(uint8_t maxQoS, const std::string &sessionStoreFileName)
Definition Broker.cpp:140
void unsubscribe(const std::string &clientId, const std::string &topic)
Definition Broker.cpp:178
Session * renewSession(const std::string &clientId, iot::mqtt::server::Mqtt *mqtt)
Definition Broker.cpp:216
void deleteSession(const std::string &clientId)
Definition Broker.cpp:232
iot::mqtt::server::broker::RetainTree & getRetainedTree()
Definition Broker.cpp:190
void unsubscribe(const std::string &clientId)
Definition Broker.cpp:150
std::map< std::string, std::list< std::pair< std::string, uint8_t > > > getSubscriptionTree() const
Definition Broker.cpp:186
bool hasRetainedSession(const std::string &clientId)
Definition Broker.cpp:202
iot::mqtt::server::broker::SubscribtionTree subscribtionTree
Definition Broker.h:111
Session * newSession(const std::string &clientId, iot::mqtt::server::Mqtt *mqtt)
Definition Broker.cpp:210
void sendPublish(const std::string &clientId, Message &message, uint8_t qoS, bool retain)
Definition Broker.cpp:237
void retainSession(const std::string &clientId)
Definition Broker.cpp:228
Message(const std::string &originClientId, const std::string &topic, const std::string &message, uint8_t qoS, bool originRetain)
Definition Message.cpp:53
void appear(const std::string &clientId, const std::string &topic, uint8_t qoS)
void fromJson(const nlohmann::json &json)
RetainTree(iot::mqtt::server::broker::Broker *broker)
void sendPublish(iot::mqtt::server::broker::Message &message, uint8_t qoS, bool retain)
Definition Session.cpp:66
nlohmann::json toJson() const final
Definition Session.cpp:121
Session(iot::mqtt::server::Mqtt *mqtt)
Definition Session.cpp:62
void fromJson(const nlohmann::json &json)
Definition Session.cpp:131
Session * renew(iot::mqtt::server::Mqtt *mqtt)
Definition Session.cpp:103
bool isOwnedBy(const iot::mqtt::server::Mqtt *mqtt) const
Definition Session.cpp:117
std::list< std::string > getSubscriptions(const std::string &clientId) const
void fromJson(const nlohmann::json &json)
std::map< std::string, std::list< std::pair< std::string, uint8_t > > > getSubscriptionTree() const
void unsubscribe(const std::string &clientId)
void appear(const std::string &clientId)
void unsubscribe(const std::string &topic, const std::string &clientId)
SubscribtionTree(iot::mqtt::server::broker::Broker *broker)
bool subscribe(const std::string &topic, const std::string &clientId, uint8_t qoS)