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
44 bool success = false;
45 QString error_message;
46 bool password_reset_required = false;
47};
48
56class ClientManager : public QObject {
57 Q_OBJECT
58
59private:
60 inline static std::string_view logger_name =
61 "ores.qt.client_manager";
62
63 [[nodiscard]] static auto& lg() {
64 using namespace ores::utility::log;
65 static auto instance = make_logger(logger_name);
66 return instance;
67 }
68
69public:
70 explicit ClientManager(std::shared_ptr<eventing::service::event_bus> event_bus,
71 QObject* parent = nullptr);
72 ~ClientManager() override;
73
84 const std::string& host,
85 std::uint16_t port,
86 const std::string& username,
87 const std::string& password);
88
94 void disconnect();
95
102 bool logout();
103
107 bool isConnected() const;
108
114 bool isAdmin() const;
115
121 const std::string& currentUsername() const { return logged_in_username_; }
122
128 const std::string& currentEmail() const { return logged_in_email_; }
129
135 void setCurrentEmail(const std::string& email) { logged_in_email_ = email; }
136
143 std::expected<comms::messaging::frame, comms::messaging::error_code>
145
149 std::shared_ptr<comms::net::client> getClient() const { return client_; }
150
154 boost::asio::any_io_executor getExecutor() {
155 return io_context_->get_executor();
156 }
157
166 void subscribeToEvent(const std::string& eventType);
167
176 void unsubscribeFromEvent(const std::string& eventType);
177
186 void setSupportedCompression(std::uint8_t compression) {
187 supported_compression_ = compression;
188 }
189
190signals:
191 void connected();
192 void disconnected();
193 void reconnecting();
194 void reconnected();
195 void connectionError(const QString& message);
196
203 void notificationReceived(const QString& eventType, const QDateTime& timestamp);
204
205private:
206 void setupIO();
207 void cleanupIO();
208
209private:
210 // Persistent IO infrastructure
211 std::unique_ptr<boost::asio::io_context> io_context_;
212 std::unique_ptr<boost::asio::executor_work_guard<
213 boost::asio::io_context::executor_type>> work_guard_;
214 std::unique_ptr<std::thread> io_thread_;
215
216 // Transient client
217 std::shared_ptr<comms::net::client> client_;
218
219 // Remote event adapter for subscriptions
220 std::unique_ptr<comms::service::remote_event_adapter> event_adapter_;
221
222 // Logged-in account tracking
223 std::optional<boost::uuids::uuid> logged_in_account_id_;
224 std::string logged_in_username_;
225 std::string logged_in_email_;
226
227 // Admin status of logged-in user
228 bool is_admin_{false};
229
230 // Event bus for publishing connection events (passed to client)
231 std::shared_ptr<eventing::service::event_bus> event_bus_;
232
233 // Connection details for event publishing
234 std::string connected_host_;
235 std::uint16_t connected_port_{0};
236
237 // Compression support bitmask (default: all compression types)
238 std::uint8_t supported_compression_{0x07}; // COMPRESSION_SUPPORT_ALL
239};
240
241}
242
243#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
Result of a login attempt.
Definition ClientManager.hpp:43
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:56
bool logout()
Logout the current user without disconnecting.
Definition ClientManager.cpp:268
void unsubscribeFromEvent(const std::string &eventType)
Unsubscribe from server-push notifications for an event type.
Definition ClientManager.cpp:382
std::expected< comms::messaging::frame, comms::messaging::error_code > sendRequest(comms::messaging::frame request)
Send a request if connected.
Definition ClientManager.cpp:352
void notificationReceived(const QString &eventType, const QDateTime &timestamp)
Emitted when a notification is received from the server.
LoginResult 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
void subscribeToEvent(const std::string &eventType)
Subscribe to server-push notifications for an event type.
Definition ClientManager.cpp:359
const std::string & currentUsername() const
Get the current logged-in user's username.
Definition ClientManager.hpp:121
bool isConnected() const
Check if currently connected.
Definition ClientManager.cpp:343
const std::string & currentEmail() const
Get the current logged-in user's email.
Definition ClientManager.hpp:128
std::shared_ptr< comms::net::client > getClient() const
Get the current client (internal use only).
Definition ClientManager.hpp:149
void disconnect()
Logout the current user and disconnect from the server.
Definition ClientManager.cpp:248
void setCurrentEmail(const std::string &email)
Set the current logged-in user's email.
Definition ClientManager.hpp:135
bool isAdmin() const
Check if the logged-in user has admin privileges.
Definition ClientManager.cpp:347
boost::asio::any_io_executor getExecutor()
Get the IO context executor.
Definition ClientManager.hpp:154
void setSupportedCompression(std::uint8_t compression)
Set the supported compression bitmask for client connections.
Definition ClientManager.hpp:186