ORE Studio 0.0.4
Loading...
Searching...
No Matches
ClientManager.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_QT_CLIENT_MANAGER_HPP
21#define ORES_QT_CLIENT_MANAGER_HPP
22
23#include <chrono>
24#include <memory>
25#include <optional>
26#include <string>
27#include <thread>
28#include <boost/asio/io_context.hpp>
29#include <boost/asio/executor_work_guard.hpp>
30#include <boost/uuid/uuid.hpp>
31#include <QObject>
32#include <QDateTime>
33#include "ores.comms/net/client.hpp"
34#include "ores.comms/service/remote_event_adapter.hpp"
35#include "ores.eventing/service/event_bus.hpp"
36#include "ores.utility/log/make_logger.hpp"
37
38namespace ores::qt {
39
47class ClientManager : public QObject {
48 Q_OBJECT
49
50private:
51 inline static std::string_view logger_name =
52 "ores.qt.client_manager";
53
54 [[nodiscard]] static auto& lg() {
55 using namespace ores::utility::log;
56 static auto instance = make_logger(logger_name);
57 return instance;
58 }
59
60public:
61 explicit ClientManager(std::shared_ptr<eventing::service::event_bus> event_bus,
62 QObject* parent = nullptr);
63 ~ClientManager() override;
64
74 std::pair<bool, QString> connectAndLogin(
75 const std::string& host,
76 std::uint16_t port,
77 const std::string& username,
78 const std::string& password);
79
85 void disconnect();
86
93 bool logout();
94
98 bool isConnected() const;
99
106 std::expected<comms::messaging::frame, comms::messaging::error_code>
108
112 std::shared_ptr<comms::net::client> getClient() const { return client_; }
113
117 boost::asio::any_io_executor getExecutor() {
118 return io_context_->get_executor();
119 }
120
129 void subscribeToEvent(const std::string& eventType);
130
139 void unsubscribeFromEvent(const std::string& eventType);
140
149 void setSupportedCompression(std::uint8_t compression) {
150 supported_compression_ = compression;
151 }
152
153signals:
154 void connected();
155 void disconnected();
156 void reconnecting();
157 void reconnected();
158 void connectionError(const QString& message);
159
166 void notificationReceived(const QString& eventType, const QDateTime& timestamp);
167
168private:
169 void setupIO();
170 void cleanupIO();
171
172private:
173 // Persistent IO infrastructure
174 std::unique_ptr<boost::asio::io_context> io_context_;
175 std::unique_ptr<boost::asio::executor_work_guard<
176 boost::asio::io_context::executor_type>> work_guard_;
177 std::unique_ptr<std::thread> io_thread_;
178
179 // Transient client
180 std::shared_ptr<comms::net::client> client_;
181
182 // Remote event adapter for subscriptions
183 std::unique_ptr<comms::service::remote_event_adapter> event_adapter_;
184
185 // Logged-in account tracking
186 std::optional<boost::uuids::uuid> logged_in_account_id_;
187
188 // Event bus for publishing connection events (passed to client)
189 std::shared_ptr<eventing::service::event_bus> event_bus_;
190
191 // Connection details for event publishing
192 std::string connected_host_;
193 std::uint16_t connected_port_{0};
194
195 // Compression support bitmask (default: all compression types)
196 std::uint8_t supported_compression_{0x07}; // COMPRESSION_SUPPORT_ALL
197};
198
199}
200
201#endif
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Complete frame with header and payload.
Definition frame.hpp:77
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:47
bool logout()
Logout the current user without disconnecting.
Definition ClientManager.cpp:263
void unsubscribeFromEvent(const std::string &eventType)
Unsubscribe from server-push notifications for an event type.
Definition ClientManager.cpp:363
std::expected< comms::messaging::frame, comms::messaging::error_code > sendRequest(comms::messaging::frame request)
Send a request if connected.
Definition ClientManager.cpp:333
void notificationReceived(const QString &eventType, const QDateTime &timestamp)
Emitted when a notification is received from the server.
void subscribeToEvent(const std::string &eventType)
Subscribe to server-push notifications for an event type.
Definition ClientManager.cpp:340
bool isConnected() const
Check if currently connected.
Definition ClientManager.cpp:328
std::pair< bool, QString > connectAndLogin(const std::string &host, std::uint16_t port, const std::string &username, const std::string &password)
Connect to the server and perform login.
Definition ClientManager.cpp:80
std::shared_ptr< comms::net::client > getClient() const
Get the current client (internal use only).
Definition ClientManager.hpp:112
void disconnect()
Logout the current user and disconnect from the server.
Definition ClientManager.cpp:243
boost::asio::any_io_executor getExecutor()
Get the IO context executor.
Definition ClientManager.hpp:117
void setSupportedCompression(std::uint8_t compression)
Set the supported compression bitmask for client connections.
Definition ClientManager.hpp:149