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.logging/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::logging;
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
92 bool subscribe_sync(const std::string& event_type);
93
102 boost::asio::awaitable<bool> unsubscribe(const std::string& event_type);
103
110 bool unsubscribe_sync(const std::string& event_type);
111
118 bool is_subscribed(const std::string& event_type) const;
119
125 std::set<std::string> get_subscriptions() const;
126
134 boost::asio::awaitable<std::size_t> resubscribe_all();
135
142
143private:
147 void on_notification(const std::string& event_type,
148 std::chrono::system_clock::time_point timestamp,
149 const std::vector<std::string>& entity_ids);
150
151 std::shared_ptr<net::client> client_;
152 mutable std::mutex mutex_;
153 std::set<std::string> subscriptions_;
154 net::notification_callback_t user_callback_;
155};
156
157}
158
159#endif
Main server application for ORE Studio.
Definition application.hpp:30
std::function< void(const std::string &event_type, std::chrono::system_clock::time_point timestamp, const std::vector< std::string > &entity_ids)> notification_callback_t
Callback invoked when client receives a notification from server.
Definition client.hpp:109
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
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
bool unsubscribe_sync(const std::string &event_type)
Unsubscribe from notifications for an event type (blocking version).
Definition remote_event_adapter.cpp:139
boost::asio::awaitable< std::size_t > resubscribe_all()
Re-subscribe to all previously subscribed event types.
Definition remote_event_adapter.cpp:176
boost::asio::awaitable< bool > unsubscribe(const std::string &event_type)
Unsubscribe from notifications for an event type.
Definition remote_event_adapter.cpp:109
std::set< std::string > get_subscriptions() const
Get the set of currently subscribed event types.
Definition remote_event_adapter.cpp:171
bool is_subscribed(const std::string &event_type) const
Check if currently subscribed to an event type.
Definition remote_event_adapter.cpp:166
void set_notification_callback(net::notification_callback_t callback)
Set callback to be invoked when notifications are received.
Definition remote_event_adapter.cpp:215
~remote_event_adapter()
Destructor.
Definition remote_event_adapter.cpp:42
bool subscribe_sync(const std::string &event_type)
Subscribe to notifications for an event type (blocking version).
Definition remote_event_adapter.cpp:81