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, 2026
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 "Session.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include <nlohmann/json.hpp>
47#include <vector>
48
49// IWYU pragma: no_include <nlohmann/detail/iterators/iter_impl.hpp>
50
51#endif // DOXYGEN_SHOULD_SKIP_THIS
52
53namespace iot::mqtt {
54
55 Session::Session(const nlohmann::json& json) {
56 fromJson(json);
57 }
58
59 nlohmann::json Session::toJson() const {
60 nlohmann::json json;
61
62 std::vector<nlohmann::json> publishJsonVector;
63 for (const auto& [packetIdentifier, publish] : outgoingPublishMap) {
64 nlohmann::json publishJson;
65
66 publishJson["packet_identifier"] = packetIdentifier;
67 publishJson["topic"] = publish.getTopic();
68 publishJson["message"] = publish.getMessage();
69 publishJson["dup"] = publish.getDup();
70 publishJson["qos"] = publish.getQoS();
71 publishJson["retain"] = publish.getRetain();
72
73 publishJsonVector.emplace_back(publishJson);
74 }
75
76 if (!publishJsonVector.empty()) {
77 json["publish_map"] = publishJsonVector;
78 }
79 if (!pubrelPacketIdentifierSet.empty()) {
80 json["pubrel_packetidentifier_set"] = pubrelPacketIdentifierSet;
81 }
82
83 // QoS 2 inbound state (receiver side)
84 std::vector<nlohmann::json> incomingPublishJsonVector;
85 for (const auto& [packetIdentifier, publish] : incomingPublishMap) {
86 nlohmann::json publishJson;
87
88 publishJson["packet_identifier"] = packetIdentifier;
89 publishJson["topic"] = publish.getTopic();
90 publishJson["message"] = publish.getMessage();
91 publishJson["dup"] = publish.getDup();
92 publishJson["qos"] = publish.getQoS();
93 publishJson["retain"] = publish.getRetain();
94
95 incomingPublishJsonVector.emplace_back(publishJson);
96 }
97
98 if (!incomingPublishJsonVector.empty()) {
99 json["incoming_publish_map"] = incomingPublishJsonVector;
100 }
101 if (!pubcompPacketIdentifierSet.empty()) {
102 json["pubcomp_packetidentifier_set"] = pubcompPacketIdentifierSet;
103 }
104
105 return json;
106 }
107
108 void Session::fromJson(const nlohmann::json& json) {
109 if (json.contains("pubrel_packetidentifier_set")) {
110 pubrelPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["pubrel_packetidentifier_set"]);
111 }
112 if (json.contains("publish_map")) {
113 for (const auto& publishJson : json["publish_map"]) {
114 if (publishJson.contains("packet_identifier")) {
115 outgoingPublishMap.emplace(publishJson["packet_identifier"],
116 iot::mqtt::packets::Publish(publishJson["packet_identifier"],
117 publishJson["topic"],
118 publishJson["message"],
119 publishJson["qos"],
120 publishJson["dup"],
121 publishJson["retain"]));
122 }
123 }
124 }
125
126 // QoS 2 inbound state (receiver side)
127 if (json.contains("pubcomp_packetidentifier_set")) {
128 pubcompPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["pubcomp_packetidentifier_set"]);
129 }
130
131 if (json.contains("incoming_publish_map")) {
132 for (const auto& publishJson : json["incoming_publish_map"]) {
133 if (publishJson.contains("packet_identifier")) {
134 incomingPublishMap.emplace(publishJson["packet_identifier"],
135 iot::mqtt::packets::Publish(publishJson["packet_identifier"],
136 publishJson["topic"],
137 publishJson["message"],
138 publishJson["qos"],
139 publishJson["dup"],
140 publishJson["retain"]));
141 }
142 }
143 }
144 }
145
146 void Session::clear() {
147 outgoingPublishMap.clear();
149 incomingPublishMap.clear();
151 }
152
153} // namespace iot::mqtt
virtual nlohmann::json toJson() const
Definition Session.cpp:59
std::map< uint16_t, iot::mqtt::packets::Publish > outgoingPublishMap
Definition Session.h:79
std::map< uint16_t, iot::mqtt::packets::Publish > incomingPublishMap
Definition Session.h:83
Session(const nlohmann::json &json)
Definition Session.cpp:55
std::set< uint16_t > pubcompPacketIdentifierSet
Definition Session.h:84
void fromJson(const nlohmann::json &json)
Definition Session.cpp:108
std::set< uint16_t > pubrelPacketIdentifierSet
Definition Session.h:80
uint8_t getQoS() const
Definition Publish.cpp:85
std::string getTopic() const
Definition Publish.cpp:93
std::string getMessage() const
Definition Publish.cpp:97
Publish(uint16_t packetIdentifier, const std::string &topic, const std::string &message, uint8_t qoS, bool dup, bool retain)
Definition Publish.cpp:56