SNode.C
Loading...
Searching...
No Matches
Session.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/Session.h"
43
44#include "iot/mqtt/server/Mqtt.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "log/Logger.h"
49
50#include <algorithm>
51#include <iterator>
52#include <map>
53#include <nlohmann/json.hpp>
54#include <string>
55
56// IWYU pragma: no_include <nlohmann/detail/iterators/iter_impl.hpp>
57
58#endif // DOXYGEN_SHOULD_SKIP_THIS
59
60namespace iot::mqtt::server::broker {
61
62 Session::Session(iot::mqtt::server::Mqtt* mqtt)
63 : mqtt(mqtt) {
64 }
65
66 void Session::sendPublish(Message& message, uint8_t qoS, bool retain) {
67 LOG(INFO) << "MQTT Broker: TopicName: " << message.getTopic();
68 LOG(INFO) << "MQTT Broker: Message:\n" << iot::mqtt::Mqtt::toHexString(message.getMessage());
69 LOG(DEBUG) << "MQTT Broker: QoS: " << static_cast<uint16_t>(std::min(qoS, message.getQoS()));
70
71 if (isActive()) {
72 LOG(DEBUG) << "MQTT Broker: ClientId: " << mqtt->getClientId();
73 LOG(DEBUG) << "MQTT Broker: OriginClientId: " << message.getOriginClientId();
74
75 if ((mqtt->getReflect() || mqtt->getClientId() != message.getOriginClientId())) {
77 message.getMessage(),
78 std::min(message.getQoS(), qoS),
79 !mqtt->getReflect() ? message.getOriginRetain() || retain : retain);
80 } else {
81 LOG(INFO) << "MQTT Broker: Suppress reflection to origin to avoid message looping";
82 }
83 } else {
84 // Offline session behavior:
85 // - QoS 0 publications MUST NOT be queued (best-effort only).
86 // - QoS 1/2 publications may be queued for later delivery (persisted sessions).
87 const uint8_t effectiveQoS = static_cast<uint8_t>(std::min(message.getQoS(), qoS));
88 if (effectiveQoS > 0) {
89 message.setQoS(effectiveQoS);
90 messageQueue.emplace_back(message);
91 } else {
92 LOG(INFO) << "MQTT Broker: Drop QoS0 message for inactive session";
93 }
94 }
95 }
96
98 LOG(INFO) << "MQTT Broker: send queued messages ...";
99 for (iot::mqtt::server::broker::Message& message : messageQueue) {
100 sendPublish(message, message.getQoS(), false);
101 }
102 LOG(INFO) << "MQTT Broker: ... done";
103
104 messageQueue.clear();
105 }
106
107 Session* Session::renew(iot::mqtt::server::Mqtt* mqtt) {
108 this->mqtt = mqtt;
109
110 return this;
111 }
112
113 void Session::retain() {
114 this->mqtt = nullptr;
115 }
116
117 bool Session::isActive() const {
118 return mqtt != nullptr;
119 }
120
121 bool Session::isOwnedBy(const iot::mqtt::server::Mqtt* mqtt) const {
122 return this->mqtt == mqtt;
123 }
124
125 nlohmann::json Session::toJson() const {
126 nlohmann::json json = iot::mqtt::Session::toJson();
127
128 std::transform(messageQueue.begin(), messageQueue.end(), std::back_inserter(json["message_queue"]), [](const Message& message) {
129 return message.toJson();
130 });
131
132 return json;
133 }
134
135 void Session::fromJson(const nlohmann::json& json) {
136 std::transform(json["message_queue"].begin(),
137 json["message_queue"].end(),
138 std::back_inserter(messageQueue),
139 [](const nlohmann::json& jsonMessage) {
140 return Message().fromJson(jsonMessage);
141 });
142
143 iot::mqtt::Session::fromJson(json);
144 }
145
146} // namespace iot::mqtt::server::broker
static std::string toHexString(const std::string &data)
Definition Mqtt.cpp:427
void sendPublish(const std::string &topic, const std::string &message, uint8_t qoS, bool retain) const
Definition Mqtt.cpp:217
virtual nlohmann::json toJson() const
Definition Session.cpp:59
void fromJson(const nlohmann::json &json)
Definition Session.cpp:108
bool getReflect() const
Definition Mqtt.cpp:444
std::string getClientId() const
Definition Mqtt.cpp:400
const std::string & getOriginClientId() const
Definition Message.cpp:62
nlohmann::json toJson() const
Definition Message.cpp:94
const std::string & getTopic() const
Definition Message.cpp:66
const std::string & getMessage() const
Definition Message.cpp:74
Message & fromJson(const nlohmann::json &json)
Definition Message.cpp:105
void sendPublish(iot::mqtt::server::broker::Message &message, uint8_t qoS, bool retain)
Definition Session.cpp:66
std::deque< Message > messageQueue
Definition Session.h:84
nlohmann::json toJson() const final
Definition Session.cpp:125
Session(iot::mqtt::server::Mqtt *mqtt)
Definition Session.cpp:62
void fromJson(const nlohmann::json &json)
Definition Session.cpp:135
Session * renew(iot::mqtt::server::Mqtt *mqtt)
Definition Session.cpp:107
bool isOwnedBy(const iot::mqtt::server::Mqtt *mqtt) const
Definition Session.cpp:121