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#include "Session.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <nlohmann/json.hpp>
25#include <vector>
26
27// IWYU pragma: no_include <nlohmann/detail/iterators/iter_impl.hpp>
28
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace iot::mqtt {
32
33 Session::Session(const nlohmann::json& json) {
34 fromJson(json);
35 }
36
38 nlohmann::json json;
39
40 std::vector<nlohmann::json> publishJsonVector;
41 for (const auto& [packetIdentifier, publish] : publishMap) {
42 nlohmann::json publishJson;
43
44 publishJson["packet_identifier"] = packetIdentifier;
45 publishJson["topic"] = publish.getTopic();
46 publishJson["message"] = publish.getMessage();
47 publishJson["dup"] = publish.getDup();
48 publishJson["qos"] = publish.getQoS();
49 publishJson["retain"] = publish.getRetain();
50
51 publishJsonVector.emplace_back(publishJson);
52 }
53
54 if (!publishJsonVector.empty()) {
55 json["publish_map"] = publishJsonVector;
56 }
57 if (!pubrelPacketIdentifierSet.empty()) {
58 json["pubrel_packetidentifier_set"] = pubrelPacketIdentifierSet;
59 }
60 if (!publishPacketIdentifierSet.empty()) {
61 json["publish_packetidentifier_set"] = publishPacketIdentifierSet;
62 }
63
64 return json;
65 }
66
67 void Session::fromJson(const nlohmann::json& json) {
68 if (json.contains("pubrel_packetidentifier_set")) {
69 pubrelPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["pubrel_packetidentifier_set"]);
70 }
71 if (json.contains("publish_packetidentifier_set")) {
72 publishPacketIdentifierSet = static_cast<std::set<uint16_t>>(json["publish_packetidentifier_set"]);
73 }
74 if (json.contains("publish_map")) {
75 for (const auto& publishJson : json["publish_map"]) {
76 if (publishJson.contains("packet_identifier")) {
77 publishMap.emplace(publishJson["packet_identifier"],
78 iot::mqtt::packets::Publish(publishJson["packet_identifier"],
79 publishJson["topic"],
80 publishJson["message"],
81 publishJson["qos"],
82 publishJson["dup"],
83 publishJson["retain"]));
84 }
85 }
86 }
87 }
88
89 void Session::clear() {
90 publishMap.clear();
91 pubrelPacketIdentifierSet.clear();
92 publishPacketIdentifierSet.clear();
93 }
94
95} // namespace iot::mqtt
virtual nlohmann::json toJson() const
Definition Session.cpp:37
Session(const nlohmann::json &json)
Definition Session.cpp:33
void fromJson(const nlohmann::json &json)
Definition Session.cpp:67