ORE Studio 0.0.4
Loading...
Searching...
No Matches
server_session.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_NET_SERVER_SESSION_HPP
21#define ORES_COMMS_NET_SERVER_SESSION_HPP
22
23#include <mutex>
24#include <queue>
25#include <atomic>
26#include <memory>
27#include <string>
28#include <chrono>
29#include <boost/asio/steady_timer.hpp>
30#include <boost/asio/strand.hpp>
31#include "ores.comms/net/connection.hpp"
32#include "ores.utility/log/make_logger.hpp"
33#include "ores.comms/messaging/message_dispatcher.hpp"
34
35namespace ores::comms::service { class subscription_manager; }
36
37namespace ores::comms::net {
38
45class server_session final {
46private:
47 inline static std::string_view logger_name = "ores.comms.net.server_session";
48
49 static auto& lg() {
50 using namespace ores::utility::log;
51 static auto instance = make_logger(logger_name);
52 return instance;
53 }
54
55public:
60 std::string event_type;
61 std::chrono::system_clock::time_point timestamp;
62 };
63
68 bool available;
69 std::string error_message;
70 std::chrono::system_clock::time_point timestamp;
71 };
72
82 explicit server_session(std::unique_ptr<connection> conn, std::string server_id,
83 std::shared_ptr<messaging::message_dispatcher> dispatcher,
84 boost::asio::any_io_executor io_executor,
85 std::shared_ptr<service::subscription_manager> subscription_mgr = nullptr);
86
92 boost::asio::awaitable<void> run();
93
99 void stop();
100
111 bool queue_notification(const std::string& event_type,
112 std::chrono::system_clock::time_point timestamp);
113
124 bool queue_database_status(bool available, const std::string& error_message,
125 std::chrono::system_clock::time_point timestamp);
126
127private:
133 boost::asio::awaitable<bool> perform_handshake();
134
138 boost::asio::awaitable<void> process_messages();
139
146 boost::asio::awaitable<void> run_notification_writer();
147
154 boost::asio::awaitable<void> send_pending_notifications();
155
161 boost::asio::awaitable<void> send_pending_database_status();
162
166 void register_with_subscription_manager();
167
171 void unregister_from_subscription_manager();
172
173 std::unique_ptr<connection> conn_;
174 std::string server_id_;
175 std::shared_ptr<messaging::message_dispatcher> dispatcher_;
176 std::shared_ptr<service::subscription_manager> subscription_mgr_;
177 std::uint32_t sequence_number_;
178 bool handshake_complete_;
179 std::atomic<bool> active_{false};
180
181 // Strand for serializing write operations
182 boost::asio::strand<boost::asio::any_io_executor> write_strand_;
183
184 // Timer used as async signal for notification availability
185 boost::asio::steady_timer notification_signal_;
186
187 // Thread-safe notification queue
188 mutable std::mutex notification_mutex_;
189 std::queue<pending_notification> pending_notifications_;
190 std::queue<pending_database_status> pending_database_status_;
191
192 // Session compression type negotiated during handshake
193 messaging::compression_type session_compression_{messaging::compression_type::none};
194};
195
196}
197
198#endif
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:96
Contains the networking elements of the comms library.
Definition client.hpp:42
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Represents a client session on the server side.
Definition server_session.hpp:45
bool queue_notification(const std::string &event_type, std::chrono::system_clock::time_point timestamp)
Queue a notification to be sent to this session's client.
Definition server_session.cpp:65
void stop()
Stop the session by closing its connection.
Definition server_session.cpp:52
boost::asio::awaitable< void > run()
Run the session.
Definition server_session.cpp:108
bool queue_database_status(bool available, const std::string &error_message, std::chrono::system_clock::time_point timestamp)
Queue a database status notification to be sent to this client.
Definition server_session.cpp:87
Pending notification to be sent to client.
Definition server_session.hpp:59
Pending database status notification to be sent to client.
Definition server_session.hpp:67