subscriptionstore.h
4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SUBSCRIPTIONSTORE_H
#define SUBSCRIPTIONSTORE_H
#include <unordered_map>
#include <forward_list>
#include <list>
#include <mutex>
#include <pthread.h>
#include "forward_declarations.h"
#include "client.h"
#include "session.h"
#include "utils.h"
#include "retainedmessage.h"
#include "logger.h"
struct Subscription
{
std::weak_ptr<Session> session; // Weak pointer expires when session has been cleaned by 'clean session' connect or when it was remove because it expired
char qos;
bool operator==(const Subscription &rhs) const;
void reset();
bool sessionGone() const;
};
class SubscriptionNode
{
std::string subtopic;
std::vector<Subscription> subscribers;
public:
SubscriptionNode(const std::string &subtopic);
SubscriptionNode(const SubscriptionNode &node) = delete;
SubscriptionNode(SubscriptionNode &&node) = delete;
std::vector<Subscription> &getSubscribers();
void addSubscriber(const std::shared_ptr<Session> &subscriber, char qos);
void removeSubscriber(const std::shared_ptr<Session> &subscriber);
std::unordered_map<std::string, std::unique_ptr<SubscriptionNode>> children;
std::unique_ptr<SubscriptionNode> childrenPlus;
std::unique_ptr<SubscriptionNode> childrenPound;
SubscriptionNode *getChildren(const std::string &subtopic) const;
int cleanSubscriptions();
};
class RetainedMessageNode
{
friend class SubscriptionStore;
std::unordered_map<std::string, std::unique_ptr<RetainedMessageNode>> children;
std::unordered_set<RetainedMessage> retainedMessages;
void addPayload(const std::string &topic, const std::string &payload, char qos, int64_t &totalCount);
RetainedMessageNode *getChildren(const std::string &subtopic) const;
};
class SubscriptionStore
{
SubscriptionNode root;
SubscriptionNode rootDollar;
pthread_rwlock_t subscriptionsRwlock = PTHREAD_RWLOCK_INITIALIZER;
std::unordered_map<std::string, std::shared_ptr<Session>> sessionsById;
const std::unordered_map<std::string, std::shared_ptr<Session>> &sessionsByIdConst;
pthread_rwlock_t retainedMessagesRwlock = PTHREAD_RWLOCK_INITIALIZER;
RetainedMessageNode retainedMessagesRoot;
RetainedMessageNode retainedMessagesRootDollar;
int64_t retainedMessageCount = 0;
Logger *logger = Logger::getInstance();
void publishNonRecursively(const MqttPacket &packet, const std::vector<Subscription> &subscribers, uint64_t &count) const;
void publishRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end,
SubscriptionNode *this_node, const MqttPacket &packet, uint64_t &count) const;
public:
SubscriptionStore();
void addSubscription(std::shared_ptr<Client> &client, const std::string &topic, const std::vector<std::string> &subtopics, char qos);
void removeSubscription(std::shared_ptr<Client> &client, const std::string &topic);
void registerClientAndKickExistingOne(std::shared_ptr<Client> &client);
bool sessionPresent(const std::string &clientid);
void queuePacketAtSubscribers(const std::vector<std::string> &subtopics, const MqttPacket &packet, bool dollar = false);
uint64_t giveClientRetainedMessages(const std::shared_ptr<Session> &ses, const std::string &subscribe_topic, char max_qos);
void giveClientRetainedMessagesRecursively(std::vector<std::string>::const_iterator cur_subtopic_it, std::vector<std::string>::const_iterator end,
RetainedMessageNode *this_node, char max_qos, const std::shared_ptr<Session> &ses,
bool poundMode, uint64_t &count) const;
uint64_t giveClientRetainedMessages(const std::shared_ptr<Session> &ses, const std::vector<std::string> &subscribeSubtopics, char max_qos);
void setRetainedMessage(const std::string &topic, const std::vector<std::string> &subtopics, const std::string &payload, char qos);
void removeExpiredSessionsClients(int expireSessionsAfterSeconds);
int64_t getRetainedMessageCount() const;
};
#endif // SUBSCRIPTIONSTORE_H