/* 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 . */ #ifndef SUBSCRIPTIONSTORE_H #define SUBSCRIPTIONSTORE_H #include #include #include #include #include #include #include #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; // 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(); }; struct ReceivingSubscriber { const std::shared_ptr session; const char qos; public: ReceivingSubscriber(const std::shared_ptr &ses, char qos); }; class SubscriptionNode { std::string subtopic; std::unordered_map subscribers; public: SubscriptionNode(const std::string &subtopic); SubscriptionNode(const SubscriptionNode &node) = delete; SubscriptionNode(SubscriptionNode &&node) = delete; std::unordered_map &getSubscribers(); const std::string &getSubtopic() const; void addSubscriber(const std::shared_ptr &subscriber, char qos); void removeSubscriber(const std::shared_ptr &subscriber); std::unordered_map> children; std::unique_ptr childrenPlus; std::unique_ptr childrenPound; SubscriptionNode *getChildren(const std::string &subtopic) const; int cleanSubscriptions(); }; class RetainedMessageNode { friend class SubscriptionStore; std::unordered_map> children; std::unordered_set retainedMessages; void addPayload(const Publish &publish, int64_t &totalCount); RetainedMessageNode *getChildren(const std::string &subtopic) const; }; class QueuedWill { std::weak_ptr will; std::weak_ptr session; public: QueuedWill(const std::shared_ptr &will, const std::shared_ptr &session); const std::weak_ptr &getWill() const; std::shared_ptr getSession(); }; class SubscriptionStore { #ifdef TESTING friend class MainTests; #endif SubscriptionNode root; SubscriptionNode rootDollar; pthread_rwlock_t subscriptionsRwlock = PTHREAD_RWLOCK_INITIALIZER; std::unordered_map> sessionsById; const std::unordered_map> &sessionsByIdConst; std::mutex queuedSessionRemovalsMutex; std::map>> queuedSessionRemovals; pthread_rwlock_t retainedMessagesRwlock = PTHREAD_RWLOCK_INITIALIZER; RetainedMessageNode retainedMessagesRoot; RetainedMessageNode retainedMessagesRootDollar; int64_t retainedMessageCount = 0; std::mutex pendingWillsMutex; std::map> pendingWillMessages; std::chrono::time_point lastTreeCleanup; Logger *logger = Logger::getInstance(); static void publishNonRecursively(const std::unordered_map &subscribers, std::forward_list &targetSessions); static void publishRecursively(std::vector::const_iterator cur_subtopic_it, std::vector::const_iterator end, SubscriptionNode *this_node, std::forward_list &targetSessions); void giveClientRetainedMessagesRecursively(std::vector::const_iterator cur_subtopic_it, std::vector::const_iterator end, RetainedMessageNode *this_node, bool poundMode, std::forward_list &packetList) const; void getRetainedMessages(RetainedMessageNode *this_node, std::vector &outputList) const; void getSubscriptions(SubscriptionNode *this_node, const std::string &composedTopic, bool root, std::unordered_map> &outputList) const; void countSubscriptions(SubscriptionNode *this_node, int64_t &count) const; SubscriptionNode *getDeepestNode(const std::string &topic, const std::vector &subtopics); public: SubscriptionStore(); void addSubscription(std::shared_ptr &client, const std::string &topic, const std::vector &subtopics, char qos); void removeSubscription(std::shared_ptr &client, const std::string &topic); void registerClientAndKickExistingOne(std::shared_ptr &client); void registerClientAndKickExistingOne(std::shared_ptr &client, bool clean_start, uint16_t clientReceiveMax, uint32_t sessionExpiryInterval); std::shared_ptr lockSession(const std::string &clientid); void sendQueuedWillMessages(); void queueWillMessage(const std::shared_ptr &willMessage, const std::shared_ptr &session, bool forceNow = false); void queuePacketAtSubscribers(PublishCopyFactory ©Factory, bool dollar = false); void giveClientRetainedMessages(const std::shared_ptr &ses, const std::vector &subscribeSubtopics, char max_qos); void setRetainedMessage(const Publish &publish, const std::vector &subtopics); void removeSession(const std::shared_ptr &session); void removeExpiredSessionsClients(); int64_t getRetainedMessageCount() const; uint64_t getSessionCount() const; int64_t getSubscriptionCount(); void saveRetainedMessages(const std::string &filePath); void loadRetainedMessages(const std::string &filePath); void saveSessionsAndSubscriptions(const std::string &filePath); void loadSessionsAndSubscriptions(const std::string &filePath); void queueSessionRemoval(const std::shared_ptr &session); }; #endif // SUBSCRIPTIONSTORE_H