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 "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] : publishMap) {
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 if (!publishPacketIdentifierSet.empty()) {
83 json["publish_packetidentifier_set"] = publishPacketIdentifierSet;
84 }
85
86 return json;
87 }
88
89 void Session::fromJson(const nlohmann::json& json) {
90 if (json.contains("pubrel_packetidentifier_set")) {
91 pubrelPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["pubrel_packetidentifier_set"]);
92 }
93 if (json.contains("publish_packetidentifier_set")) {
94 publishPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["publish_packetidentifier_set"]);
95 }
96 if (json.contains("publish_map")) {
97 for (const auto& publishJson : json["publish_map"]) {
98 if (publishJson.contains("packet_identifier")) {
99 publishMap.emplace(publishJson["packet_identifier"],
100 iot::mqtt::packets::Publish(publishJson["packet_identifier"],
101 publishJson["topic"],
102 publishJson["message"],
103 publishJson["qos"],
104 publishJson["dup"],
105 publishJson["retain"]));
106 }
107 }
108 }
109 }
110
111 void Session::clear() {
112 publishMap.clear();
115 }
116
117} // namespace iot::mqtt
std::set< uint16_t > publishPacketIdentifierSet
Definition Session.h:85
virtual nlohmann::json toJson() const
Definition Session.cpp:59
std::map< uint16_t, iot::mqtt::packets::Publish > publishMap
Definition Session.h:81
Session(const nlohmann::json &json)
Definition Session.cpp:55
void fromJson(const nlohmann::json &json)
Definition Session.cpp:89
std::set< uint16_t > pubrelPacketIdentifierSet
Definition Session.h:82
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