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 if (message.getQoS() == 0) {
85 messageQueue.clear();
86 }
87
88 message.setQoS(std::min(message.getQoS(), qoS));
89 messageQueue.emplace_back(message);
90 }
91 }
92
94 LOG(INFO) << "MQTT Broker: send queued messages ...";
95 for (iot::mqtt::server::broker::Message& message : messageQueue) {
96 sendPublish(message, message.getQoS(), false);
97 }
98 LOG(INFO) << "MQTT Broker: ... done";
99
100 messageQueue.clear();
101 }
102
103 Session* Session::renew(iot::mqtt::server::Mqtt* mqtt) {
104 this->mqtt = mqtt;
105
106 return this;
107 }
108
109 void Session::retain() {
110 this->mqtt = nullptr;
111 }
112
113 bool Session::isActive() const {
114 return mqtt != nullptr;
115 }
116
117 bool Session::isOwnedBy(const iot::mqtt::server::Mqtt* mqtt) const {
118 return this->mqtt == mqtt;
119 }
120
121 nlohmann::json Session::toJson() const {
122 nlohmann::json json = iot::mqtt::Session::toJson();
123
124 std::transform(messageQueue.begin(), messageQueue.end(), std::back_inserter(json["message_queue"]), [](const Message& message) {
125 return message.toJson();
126 });
127
128 return json;
129 }
130
131 void Session::fromJson(const nlohmann::json& json) {
132 std::transform(json["message_queue"].begin(),
133 json["message_queue"].end(),
134 std::back_inserter(messageQueue),
135 [](const nlohmann::json& jsonMessage) {
136 return Message().fromJson(jsonMessage);
137 });
138
139 iot::mqtt::Session::fromJson(json);
140 }
141
142} // namespace iot::mqtt::server::broker
static std::string toHexString(const std::string &data)
Definition Mqtt.cpp:394
void sendPublish(const std::string &topic, const std::string &message, uint8_t qoS, bool retain)
Definition Mqtt.cpp:217
virtual nlohmann::json toJson() const
Definition Session.cpp:59
void fromJson(const nlohmann::json &json)
Definition Session.cpp:89
bool getReflect() const
Definition Mqtt.cpp:436
std::string getClientId() const
Definition Mqtt.cpp:392
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: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