ORE Studio 0.0.4
Loading...
Searching...
No Matches
subscription_manager.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_SERVICE_SUBSCRIPTION_MANAGER_HPP
21#define ORES_COMMS_SERVICE_SUBSCRIPTION_MANAGER_HPP
22
23#include <mutex>
24#include <string>
25#include <vector>
26#include <chrono>
27#include <memory>
28#include <functional>
29#include <unordered_map>
30#include <unordered_set>
31#include "ores.logging/make_logger.hpp"
32
33namespace ores::comms::service {
34
42 std::function<bool(const std::string&, std::chrono::system_clock::time_point,
43 const std::vector<std::string>&)>;
44
51using session_id = std::string;
52
85private:
86 [[nodiscard]] static auto& lg() {
87 using namespace ores::logging;
88 static auto instance = make_logger(
89 "ores.comms.service.subscription_manager");
90 return instance;
91 }
92
93 struct session_info {
94 notification_callback callback;
95 std::unordered_set<std::string> subscribed_events;
96 };
97
98public:
99 subscription_manager() = default;
100 ~subscription_manager() = default;
101
103 subscription_manager& operator=(const subscription_manager&) = delete;
105 subscription_manager& operator=(subscription_manager&&) = delete;
106
115 void register_session(const session_id& id, notification_callback callback);
116
125 void unregister_session(const session_id& id);
126
134 bool subscribe(const session_id& id, const std::string& event_type);
135
144 bool unsubscribe(const session_id& id, const std::string& event_type);
145
158 std::size_t notify(const std::string& event_type,
159 std::chrono::system_clock::time_point timestamp,
160 const std::vector<std::string>& entity_ids = {});
161
168 [[nodiscard]] std::size_t subscriber_count(const std::string& event_type) const;
169
175 [[nodiscard]] std::size_t session_count() const;
176
183 [[nodiscard]] std::vector<std::string> get_subscriptions(const session_id& id) const;
184
185private:
186 mutable std::mutex mutex_;
187 std::unordered_map<session_id, session_info> sessions_;
188 std::unordered_map<std::string, std::unordered_set<session_id>> event_subscribers_;
189};
190
191}
192
193#endif
Main server application for ORE Studio.
Definition application.hpp:30
std::string session_id
Unique identifier for a client session.
Definition subscription_manager.hpp:51
std::function< bool(const std::string &, std::chrono::system_clock::time_point, const std::vector< std::string > &)> notification_callback
Callback type for pushing notifications to clients.
Definition subscription_manager.hpp:43
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Manages client subscriptions to event notifications.
Definition subscription_manager.hpp:84
std::size_t session_count() const
Get the total number of registered sessions.
Definition subscription_manager.cpp:221
std::vector< std::string > get_subscriptions(const session_id &id) const
Get the event types a session is subscribed to.
Definition subscription_manager.cpp:226
void unregister_session(const session_id &id)
Unregister a session from the subscription manager.
Definition subscription_manager.cpp:45
bool subscribe(const session_id &id, const std::string &event_type)
Subscribe a session to an event type.
Definition subscription_manager.cpp:78
std::size_t notify(const std::string &event_type, std::chrono::system_clock::time_point timestamp, const std::vector< std::string > &entity_ids={})
Notify all subscribers of an event.
Definition subscription_manager.cpp:148
bool unsubscribe(const session_id &id, const std::string &event_type)
Unsubscribe a session from an event type.
Definition subscription_manager.cpp:109
void register_session(const session_id &id, notification_callback callback)
Register a new session with the subscription manager.
Definition subscription_manager.cpp:26
std::size_t subscriber_count(const std::string &event_type) const
Get the number of subscribers for an event type.
Definition subscription_manager.cpp:210