abstract qt components frmo logos_host; refactor

This commit is contained in:
Iuri Matias
2026-03-27 17:04:37 -04:00
parent 3af618e529
commit f736c7fcc8
12 changed files with 130 additions and 94 deletions
+3
View File
@@ -11,6 +11,9 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core RemoteObjects)
# Find nlohmann_json
find_package(nlohmann_json REQUIRED)
# Find CLI11
find_package(CLI11 REQUIRED)
# Set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
+1
View File
@@ -20,6 +20,7 @@
pkgs.zstd
pkgs.gtest
pkgs.nlohmann_json
pkgs.cli11
logosModule
processStats
logosPackageManager
+5
View File
@@ -113,6 +113,10 @@ set(LOGOS_HOST_SOURCES
logos_host/command_line_parser.h
logos_host/plugin_initializer.cpp
logos_host/plugin_initializer.h
logos_host/qt/qt_app.cpp
logos_host/qt/qt_app.h
logos_host/qt/qt_token_receiver.cpp
logos_host/qt/qt_token_receiver.h
)
# Create the logos core library
@@ -222,6 +226,7 @@ target_link_libraries(logos_host PRIVATE
Qt${QT_VERSION_MAJOR}::RemoteObjects
logos_sdk
logos_module
CLI11::CLI11
)
# Include directories for the logos host application
+17 -36
View File
@@ -1,45 +1,26 @@
#include "command_line_parser.h"
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QStringList>
#include <QDebug>
#include <CLI/CLI.hpp>
PluginArgs parseCommandLineArgs(QCoreApplication& app)
PluginArgs parseCommandLineArgs(int argc, char *argv[])
{
PluginArgs args;
args.valid = false;
PluginArgs result;
result.valid = false;
// Setup command line parser
QCommandLineParser parser;
parser.setApplicationDescription("Logos host for loading plugins in separate processes");
parser.addHelpOption();
parser.addVersionOption();
CLI::App app{"Logos host for loading plugins in separate processes"};
app.set_version_flag("-v,--version", "1.0");
// Add plugin name option
QCommandLineOption pluginNameOption(QStringList() << "n" << "name",
"Name of the plugin to load",
"plugin_name");
parser.addOption(pluginNameOption);
app.add_option("-n,--name", result.name, "Name of the plugin to load")
->required();
app.add_option("-p,--path", result.path, "Path to the plugin file")
->required();
// Add plugin path option
QCommandLineOption pluginPathOption(QStringList() << "p" << "path",
"Path to the plugin file",
"plugin_path");
parser.addOption(pluginPathOption);
// Process the command line arguments
parser.process(app);
// Get plugin name and path
args.name = parser.value(pluginNameOption);
args.path = parser.value(pluginPathOption);
if (args.name.isEmpty() || args.path.isEmpty()) {
qCritical() << "Both plugin name and path must be specified";
qCritical() << "Usage:" << app.arguments()[0] << "--name <plugin_name> --path <plugin_path>";
return args;
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
app.exit(e);
return result;
}
args.valid = true;
return args;
result.valid = true;
return result;
}
+4 -6
View File
@@ -1,16 +1,14 @@
#ifndef COMMAND_LINE_PARSER_H
#define COMMAND_LINE_PARSER_H
#include <QString>
class QCoreApplication;
#include <string>
struct PluginArgs {
QString name;
QString path;
std::string name;
std::string path;
bool valid;
};
PluginArgs parseCommandLineArgs(QCoreApplication& app);
PluginArgs parseCommandLineArgs(int argc, char *argv[]);
#endif // COMMAND_LINE_PARSER_H
+10 -9
View File
@@ -1,26 +1,27 @@
#include <QCoreApplication>
#include <QString>
#include "command_line_parser.h"
#include "plugin_initializer.h"
#include "qt/qt_app.h"
#include "logos_api.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
app.setApplicationName("logos_host");
app.setApplicationVersion("1.0");
PluginArgs args = parseCommandLineArgs(app);
PluginArgs args = parseCommandLineArgs(argc, argv);
if (!args.valid) {
return 1;
}
LogosAPI* logos_api = setupPlugin(args.name, args.path);
QtApp::init(argc, argv);
LogosAPI* logos_api = setupPlugin(QString::fromStdString(args.name),
QString::fromStdString(args.path));
if (!logos_api) {
return 1;
}
int result = app.exec();
int result = QtApp::exec();
delete logos_api;
QtApp::cleanup();
return result;
}
}
+2 -41
View File
@@ -1,6 +1,5 @@
#include "plugin_initializer.h"
#include <QLocalServer>
#include <QLocalSocket>
#include "qt/qt_token_receiver.h"
#include <QObject>
#include <QDebug>
#include <QFileInfo>
@@ -12,44 +11,6 @@
using namespace ModuleLib;
QString receiveAuthToken(const QString& pluginName)
{
// Set up IPC server to receive auth token securely
QString socketName = QString("logos_token_%1").arg(pluginName);
QLocalServer* tokenServer = new QLocalServer();
// Remove any existing socket file
QLocalServer::removeServer(socketName);
if (!tokenServer->listen(socketName)) {
qCritical() << "Failed to start token server:" << tokenServer->errorString();
return QString();
}
QString authToken;
if (tokenServer->waitForNewConnection(10000)) {
QLocalSocket* clientSocket = tokenServer->nextPendingConnection();
if (clientSocket->waitForReadyRead(5000)) {
QByteArray tokenData = clientSocket->readAll();
authToken = QString::fromUtf8(tokenData);
}
clientSocket->deleteLater();
} else {
qCritical() << "Timeout waiting for auth token";
tokenServer->deleteLater();
return QString();
}
tokenServer->deleteLater();
if (authToken.isEmpty()) {
qCritical() << "No auth token received";
return QString();
}
return authToken;
}
LogosModule loadPlugin(const QString& pluginPath, const QString& expectedName)
{
// Load the plugin using module_lib for abstraction
@@ -98,7 +59,7 @@ LogosAPI* initializeLogosAPI(const QString& pluginName, QObject* plugin,
LogosAPI* setupPlugin(const QString& pluginName, const QString& pluginPath)
{
// 1. Receive auth token securely
QString authToken = receiveAuthToken(pluginName);
QString authToken = QtTokenReceiver::receiveAuthToken(pluginName);
if (authToken.isEmpty()) {
return nullptr;
}
-2
View File
@@ -8,8 +8,6 @@ class PluginInterface;
class LogosAPI;
class QObject;
QString receiveAuthToken(const QString& pluginName);
ModuleLib::LogosModule loadPlugin(const QString& pluginPath, const QString& expectedName);
LogosAPI* initializeLogosAPI(const QString& pluginName, QObject* plugin,
+24
View File
@@ -0,0 +1,24 @@
#include "qt_app.h"
#include <QCoreApplication>
namespace {
QCoreApplication* s_app = nullptr;
}
namespace QtApp {
void init(int argc, char* argv[]) {
s_app = new QCoreApplication(argc, argv);
}
int exec() {
if (!s_app) return -1;
return s_app->exec();
}
void cleanup() {
delete s_app;
s_app = nullptr;
}
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef QT_APP_H
#define QT_APP_H
namespace QtApp {
void init(int argc, char* argv[]);
int exec();
void cleanup();
}
#endif // QT_APP_H
+44
View File
@@ -0,0 +1,44 @@
#include "qt_token_receiver.h"
#include <QLocalServer>
#include <QLocalSocket>
#include <QDebug>
namespace QtTokenReceiver {
QString receiveAuthToken(const QString& pluginName)
{
QString socketName = QString("logos_token_%1").arg(pluginName);
QLocalServer* tokenServer = new QLocalServer();
QLocalServer::removeServer(socketName);
if (!tokenServer->listen(socketName)) {
qCritical() << "Failed to start token server:" << tokenServer->errorString();
return QString();
}
QString authToken;
if (tokenServer->waitForNewConnection(10000)) {
QLocalSocket* clientSocket = tokenServer->nextPendingConnection();
if (clientSocket->waitForReadyRead(5000)) {
QByteArray tokenData = clientSocket->readAll();
authToken = QString::fromUtf8(tokenData);
}
clientSocket->deleteLater();
} else {
qCritical() << "Timeout waiting for auth token";
tokenServer->deleteLater();
return QString();
}
tokenServer->deleteLater();
if (authToken.isEmpty()) {
qCritical() << "No auth token received";
return QString();
}
return authToken;
}
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef QT_TOKEN_RECEIVER_H
#define QT_TOKEN_RECEIVER_H
#include <QString>
namespace QtTokenReceiver {
QString receiveAuthToken(const QString& pluginName);
}
#endif // QT_TOKEN_RECEIVER_H