Skip to content

Commit a747824

Browse files
Add API to fetch all subscribed topics along with a list of clients with their qos for each fo the topics as std::map<std::string, std::list<std::pair<std::string, uint8_t>>
std::map<topic, std::list<std::pair<ClientId, QoS>>
1 parent 689e7c7 commit a747824

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/iot/mqtt/server/broker/Broker.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ namespace iot::mqtt::server::broker {
183183
return subscribtionTree.getSubscriptions(clientId);
184184
}
185185

186+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> Broker::getSubscriptionTree() const {
187+
return subscribtionTree.getSubscriptionTree();
188+
}
189+
186190
bool Broker::hasSession(const std::string& clientId) {
187191
return sessionStore.contains(clientId);
188192
}

src/iot/mqtt/server/broker/Broker.h

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ namespace iot::mqtt::server {
5757
#include <map>
5858
#include <memory>
5959
#include <string>
60+
#include <utility>
61+
62+
// IWYU pragma: no_include <iterator>
6063

6164
#endif // DOXYGEN_SHOULD_SKIP_THIS
6265

@@ -83,6 +86,7 @@ namespace iot::mqtt::server::broker {
8386
void unsubscribe(const std::string& clientId, const std::string& topic);
8487

8588
std::list<std::string> getSubscriptions(const std::string& clientId) const;
89+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> getSubscriptionTree() const;
8690

8791
bool hasSession(const std::string& clientId);
8892
bool hasActiveSession(const std::string& clientId);

src/iot/mqtt/server/broker/SubscribtionTree.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include <algorithm>
5252
#include <nlohmann/json.hpp>
5353
#include <string>
54-
#include <utility>
5554

5655
// IWYU pragma: no_include <nlohmann/detail/iterators/iteration_proxy.hpp>
5756

@@ -110,6 +109,10 @@ namespace iot::mqtt::server::broker {
110109
return subscriptions;
111110
}
112111

112+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> SubscribtionTree::getSubscriptionTree() const {
113+
return head.getSubscriptionTree();
114+
}
115+
113116
void SubscribtionTree::fromJson(const nlohmann::json& json) {
114117
if (!json.empty()) {
115118
head.fromJson(json);
@@ -281,6 +284,10 @@ namespace iot::mqtt::server::broker {
281284
return getSubscriptions("", clientId);
282285
}
283286

287+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> SubscribtionTree::TopicLevel::getSubscriptionTree() const {
288+
return getSubscriptionTree("");
289+
}
290+
284291
std::list<std::string> SubscribtionTree::TopicLevel::getSubscriptions(const std::string& absoluteTopicLevel,
285292
const std::string& clientId) const {
286293
std::list<std::string> topicLevelList;
@@ -298,6 +305,26 @@ namespace iot::mqtt::server::broker {
298305
return topicLevelList;
299306
}
300307

308+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>>
309+
SubscribtionTree::TopicLevel::getSubscriptionTree(const std::string& absoluteTopicLevel) const {
310+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> topicLevelTree;
311+
312+
for (const auto& [topicLevelName, nextTopicLevel] : topicLevels) {
313+
const std::string composedAbsoluteTopicLevelName = absoluteTopicLevel + topicLevelName;
314+
315+
for (const auto& clientId : nextTopicLevel.clientIds) {
316+
topicLevelTree[composedAbsoluteTopicLevelName].emplace_back(clientId);
317+
}
318+
319+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> subSubscriptionTree =
320+
nextTopicLevel.getSubscriptionTree(composedAbsoluteTopicLevelName + "/");
321+
322+
topicLevelTree.insert(subSubscriptionTree.begin(), subSubscriptionTree.end());
323+
}
324+
325+
return topicLevelTree;
326+
}
327+
301328
SubscribtionTree::TopicLevel& SubscribtionTree::TopicLevel::fromJson(const nlohmann::json& json) {
302329
clientIds.clear();
303330
topicLevels.clear();

src/iot/mqtt/server/broker/SubscribtionTree.h

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ namespace iot::mqtt::server::broker {
5454
#include <map>
5555
#include <nlohmann/json_fwd.hpp>
5656
#include <string>
57+
#include <utility>
58+
59+
// IWYU pragma: no_include <iterator>
5760

5861
#endif // DOXYGEN_SHOULD_SKIP_THIS
5962

@@ -76,6 +79,7 @@ namespace iot::mqtt::server::broker {
7679
void fromJson(const nlohmann::json& json);
7780

7881
std::list<std::string> getSubscriptions(const std::string& clientId) const;
82+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> getSubscriptionTree() const;
7983

8084
void clear();
8185

@@ -95,13 +99,17 @@ namespace iot::mqtt::server::broker {
9599

96100
std::list<std::string> getSubscriptions(const std::string& clientId) const;
97101

102+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>> getSubscriptionTree() const;
103+
98104
TopicLevel& fromJson(const nlohmann::json& json);
99105
nlohmann::json toJson() const;
100106

101107
void clear();
102108

103109
private:
104110
std::list<std::string> getSubscriptions(const std::string& absoluteTopicLevel, const std::string& clientId) const;
111+
std::map<std::string, std::list<std::pair<std::string, uint8_t>>>
112+
getSubscriptionTree(const std::string& absoluteTopicLevel) const;
105113

106114
iot::mqtt::server::broker::Broker* broker;
107115

0 commit comments

Comments
 (0)