ORE Studio 0.0.4
Loading...
Searching...
No Matches
remote_event_adapter.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_REMOTE_EVENT_ADAPTER_HPP
21#define ORES_COMMS_SERVICE_REMOTE_EVENT_ADAPTER_HPP
22
23#include <set>
24#include <mutex>
25#include <string>
26#include <memory>
27#include <functional>
28#include <string_view>
29#include <boost/asio/awaitable.hpp>
30#include "ores.utility/log/make_logger.hpp"
31#include "ores.comms/net/client.hpp"
32
33namespace ores::comms::service {
34
48private:
49 inline static std::string_view logger_name =
50 "ores.comms.service.remote_event_adapter";
51
52 static auto& lg() {
53 using namespace ores::utility::log;
54 static auto instance = make_logger(logger_name);
55 return instance;
56 }
57
58public:
66 explicit remote_event_adapter(std::shared_ptr<net::client> client);
67
74
84 boost::asio::awaitable<bool> subscribe(const std::string& event_type);
85
94 boost::asio::awaitable<bool> unsubscribe(const std::string& event_type);
95
102 bool is_subscribed(const std::string& event_type) const;
103
109 std::set<std::string> get_subscriptions() const;
110
118 boost::asio::awaitable<std::size_t> resubscribe_all();
119
126
127private:
131 void on_notification(const std::string& event_type,
132 std::chrono::system_clock::time_point timestamp);
133
134 std::shared_ptr<net::client> client_;
135 mutable std::mutex mutex_;
136 std::set<std::string> subscriptions_;
137 net::notification_callback_t user_callback_;
138};
139
140}
141
142#endif
std::function< void(const std::string &event_type, std::chrono::system_clock::time_point timestamp)> notification_callback_t
Callback invoked when client receives a notification from server.
Definition client.hpp:101
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Adapts remote server subscriptions to local notification callbacks.
Definition remote_event_adapter.hpp:47
boost::asio::awaitable< bool > subscribe(const std::string &event_type)
Subscribe to notifications for an event type.
Definition remote_event_adapter.cpp:51
boost::asio::awaitable< std::size_t > resubscribe_all()
Re-subscribe to all previously subscribed event types.
Definition remote_event_adapter.cpp:146
boost::asio::awaitable< bool > unsubscribe(const std::string &event_type)
Unsubscribe from notifications for an event type.
Definition remote_event_adapter.cpp:94
std::set< std::string > get_subscriptions() const
Get the set of currently subscribed event types.
Definition remote_event_adapter.cpp:141
bool is_subscribed(const std::string &event_type) const
Check if currently subscribed to an event type.
Definition remote_event_adapter.cpp:136
void set_notification_callback(net::notification_callback_t callback)
Set callback to be invoked when notifications are received.
Definition remote_event_adapter.cpp:185
~remote_event_adapter()
Destructor.
Definition remote_event_adapter.cpp:42