2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "iot/mqtt/server/broker/Broker.h"
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
46#include "log/Logger.h"
51#include <nlohmann/json.hpp>
60 Broker::
Broker(uint8_t maxQoS,
const std::string& sessionStoreFileName)
65 if (!sessionStoreFileName.empty()) {
66 std::ifstream sessionStoreFile(sessionStoreFileName);
68 if (sessionStoreFile.is_open()) {
70 nlohmann::json sessionStoreJson;
72 sessionStoreFile >> sessionStoreJson;
74 for (
const auto& [clientId, sessionJson] : sessionStoreJson[
"session_store"].items()) {
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";
90 sessionStoreFile.close();
91 std::remove(sessionStoreFileName.data());
93 LOG(INFO) <<
"MQTT Broker: Restoring saved session done";
95 PLOG(WARNING) <<
"MQTT Broker: Could not read session store '" << sessionStoreFileName <<
"'";
98 LOG(INFO) <<
"MQTT Broker: Session not reloaded: Session store filename empty";
104 nlohmann::json sessionStoreJson;
107 sessionStoreJson[
"session_store"][clientId] = session
.toJson();
112 if (sessionStoreJson[
"session_store"].empty()) {
113 sessionStoreJson.erase(
"session_store");
115 if (sessionStoreJson[
"retain_tree"].empty()) {
116 sessionStoreJson.erase(
"retain_tree");
118 if (sessionStoreJson[
"subscribtion_tree"].empty()) {
119 sessionStoreJson.erase(
"subscribtion_tree");
124 if (sessionStoreFile.is_open()) {
125 if (!sessionStoreJson.empty()) {
126 sessionStoreFile << sessionStoreJson;
129 sessionStoreFile.close();
136 LOG(INFO) <<
"MQTT Broker: Session not saved: Session store filename empty";
141 static const std::shared_ptr<
Broker> broker = std::make_shared<
Broker>(maxQoS, sessionStoreFileName);
146 void Broker::
appear(
const std::string& clientId,
const std::string& topic, uint8_t qoS) {
155 Broker::
publish(
const std::string& originClientId,
const std::string& topic,
const std::string& message, uint8_t qoS,
bool retain) {
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;
221 LOG(INFO) <<
"MQTT Broker: Retained: Send PUBLISH: " << clientId;
224 LOG(INFO) <<
"MQTT Broker: Queued: Send PUBLISH: " << clientId;
238 LOG(INFO) <<
"MQTT Broker: Send PUBLISH: " << clientId;
#define SUBSCRIBTION_FAILURE
#define SUBSCRIBTION_SUCCESS
void restartSession(const std::string &clientId)
std::string sessionStoreFileName
uint8_t subscribe(const std::string &clientId, const std::string &topic, uint8_t qoS)
bool hasActiveSession(const std::string &clientId)
bool isActiveSession(const std::string &clientId, const Mqtt *mqtt)
iot::mqtt::server::broker::RetainTree retainTree
void publish(const std::string &originClientId, const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
std::map< std::string, iot::mqtt::server::broker::Session > sessionStore
std::list< std::string > getSubscriptions(const std::string &clientId) const
Broker(uint8_t maxQoS, const std::string &sessionStoreFileName)
bool hasSession(const std::string &clientId)
void appear(const std::string &clientId, const std::string &topic, uint8_t qoS)
static std::shared_ptr< Broker > instance(uint8_t maxQoS, const std::string &sessionStoreFileName)
void unsubscribe(const std::string &clientId, const std::string &topic)
Session * renewSession(const std::string &clientId, iot::mqtt::server::Mqtt *mqtt)
void deleteSession(const std::string &clientId)
iot::mqtt::server::broker::RetainTree & getRetainedTree()
void unsubscribe(const std::string &clientId)
std::map< std::string, std::list< std::pair< std::string, uint8_t > > > getSubscriptionTree() const
bool hasRetainedSession(const std::string &clientId)
iot::mqtt::server::broker::SubscribtionTree subscribtionTree
Session * newSession(const std::string &clientId, iot::mqtt::server::Mqtt *mqtt)
void sendPublish(const std::string &clientId, Message &message, uint8_t qoS, bool retain)
void retainSession(const std::string &clientId)
Message(const std::string &originClientId, const std::string &topic, const std::string &message, uint8_t qoS, bool originRetain)
void appear(const std::string &clientId, const std::string &topic, uint8_t qoS)
void retain(Message &&message)
void fromJson(const nlohmann::json &json)
RetainTree(iot::mqtt::server::broker::Broker *broker)
nlohmann::json toJson() const
void sendPublish(iot::mqtt::server::broker::Message &message, uint8_t qoS, bool retain)
nlohmann::json toJson() const final
Session(iot::mqtt::server::Mqtt *mqtt)
void fromJson(const nlohmann::json &json)
Session * renew(iot::mqtt::server::Mqtt *mqtt)
bool isOwnedBy(const iot::mqtt::server::Mqtt *mqtt) const
nlohmann::json toJson() const
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 publish(Message &&message)
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)