From d37369ad4f989102a08f5986cf6115313497ad35 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 25 Apr 2024 23:11:46 +0200 Subject: [PATCH] add AutoStart option, Last Start and Last Stop --- CMakeLists.txt | 2 +- SMSubProcess.cpp | 8 ++++---- SMSubProcess.h | 4 ++-- SMSubServer.cpp | 13 +++++++++---- SMSubServer.h | 6 ++++-- main.cpp | 31 ++++++++++++++++++++++++++++--- 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d25973c..bf8df83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(smsub LANGUAGES CXX VERSION 0.7) +project(smsub LANGUAGES CXX VERSION 0.8) set(CMAKE_INCLUDE_CURRENT_DIR ON) diff --git a/SMSubProcess.cpp b/SMSubProcess.cpp index 14ef091..e9016c6 100644 --- a/SMSubProcess.cpp +++ b/SMSubProcess.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * smsub Server Manager Subprocess -* Copyright (C) 2020-2023 Syping +* Copyright (C) 2020-2024 Syping * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -18,9 +18,9 @@ #include #include +#include #include "SMSubProcess.h" #include "smsub.h" -#include SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout, const bool &keepAlive) : termTimeout(termTimeout), keepAlive(keepAlive) @@ -41,12 +41,12 @@ SMSubProcess::SMSubProcess(const QString &executable, const QStringList &argumen QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError); QObject::connect(&process, &QProcess::started, this, [=]() { QTextStream(stderr) << "Subprocess started!" << smsub_endl; - emit statusUpdated(true); + emit statusUpdated(true, QDateTime::currentDateTimeUtc().toSecsSinceEpoch()); }); QObject::connect(&process, QOverload::of(&QProcess::finished), this, [=](int exitCode) { QTextStream(stderr) << "Subprocess exited!" << smsub_endl; // Exit with the same exit code as the process - emit statusUpdated(false); + emit statusUpdated(false, QDateTime::currentDateTimeUtc().toSecsSinceEpoch()); if (!keepAlive) QCoreApplication::exit(exitCode); }); diff --git a/SMSubProcess.h b/SMSubProcess.h index cd2dddc..568cf9a 100644 --- a/SMSubProcess.h +++ b/SMSubProcess.h @@ -1,6 +1,6 @@ /***************************************************************************** * smsub Server Manager Subprocess -* Copyright (C) 2020-2023 Syping +* Copyright (C) 2020-2024 Syping * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -46,7 +46,7 @@ private slots: signals: void outputWritten(const QByteArray &output); - void statusUpdated(const bool status); + void statusUpdated(const bool status, const qint64 time); }; #endif // SMSUBPROCESS_H diff --git a/SMSubServer.cpp b/SMSubServer.cpp index c753b79..3e8402a 100644 --- a/SMSubServer.cpp +++ b/SMSubServer.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * smsub Server Manager Subprocess -* Copyright (C) 2020-2023 Syping +* Copyright (C) 2020-2024 Syping * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -33,6 +33,8 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &soc type = ServerType::Local; server = localServer; status = false; + startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch(); + stopTime = startTime; } SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port) : serverSettings(serverSettings) @@ -45,6 +47,8 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &ser type = ServerType::WebSocket; server = webSocketServer; status = false; + startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch(); + stopTime = startTime; } bool SMSubServer::isListening() @@ -126,10 +130,10 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message) } else if (message.startsWith("status")) { if (status) { - sendMessage(socket, "Status: on\n"); + sendMessage(socket, QString("Status: on\nLast Start: %1\nLast Stop: %2\n").arg(QString::number(startTime), QString::number(stopTime)).toUtf8()); } else { - sendMessage(socket, "Status: off\n"); + sendMessage(socket, QString("Status: off\nLast Start: %1\nLast Stop: %2\n").arg(QString::number(startTime), QString::number(stopTime)).toUtf8()); } } else if (message.startsWith("start")) { @@ -273,7 +277,8 @@ void SMSubServer::registerToken(const QString &token) }); } -void SMSubServer::statusUpdated(const bool status_) +void SMSubServer::statusUpdated(const bool status_, const qint64 time) { status = status_; + status ? (startTime = time) : (stopTime = time); } diff --git a/SMSubServer.h b/SMSubServer.h index daabb22..dc1b743 100644 --- a/SMSubServer.h +++ b/SMSubServer.h @@ -1,6 +1,6 @@ /***************************************************************************** * smsub Server Manager Subprocess -* Copyright (C) 2020-2023 Syping +* Copyright (C) 2020-2024 Syping * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -45,7 +45,7 @@ public: public slots: void writeOutput(const QByteArray &output); void registerToken(const QString &token); - void statusUpdated(const bool status); + void statusUpdated(const bool status, const qint64 time); private slots: void wsMessageReceived(const QByteArray &message); @@ -60,6 +60,8 @@ private: SMSubServerSettings *serverSettings; QVector sockets; QVector tokens; + qint64 startTime; + qint64 stopTime; ServerType type; QObject *server; bool status; diff --git a/main.cpp b/main.cpp index aebda4d..adf5f7b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * smsub Server Manager Subprocess -* Copyright (C) 2020-2023 Syping +* Copyright (C) 2020-2024 Syping * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -134,13 +134,14 @@ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); a.setApplicationName("Server Manager Subprocess"); - a.setApplicationVersion("0.7"); + a.setApplicationVersion("0.8"); #ifdef Q_OS_UNIX catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM}); #endif bool rportSet = false; + bool autoStart = true; bool keepAlive = false; bool timeoutSet = false; int termTimeout = 60000; @@ -152,6 +153,7 @@ int main(int argc, char *argv[]) QStringList argumentList; const QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE"); + const QByteArray envAutoStart = qgetenv("SMSUB_AUTOSTART"); const QByteArray envKeepAlive = qgetenv("SMSUB_KEEPALIVE"); const QByteArray envManifest = qgetenv("SMSUB_JSON"); const QByteArray envExecutable = qgetenv("SMSUB_EXEC"); @@ -204,6 +206,12 @@ int main(int argc, char *argv[]) } } + if (!envAutoStart.isEmpty()) { + if (envAutoStart == "0" || envAutoStart.toLower() == "false" || envAutoStart.toLower() == "no") { + autoStart = false; + } + } + if (!envKeepAlive.isEmpty()) { if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") { keepAlive = true; @@ -342,6 +350,12 @@ int main(int argc, char *argv[]) executable = QString::fromUtf8(envExecutable); } + if (!envAutoStart.isEmpty()) { + if (envAutoStart == "0" || envAutoStart.toLower() == "false" || envAutoStart.toLower() == "no") { + autoStart = false; + } + } + if (!envKeepAlive.isEmpty()) { if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") { keepAlive = true; @@ -385,6 +399,16 @@ int main(int argc, char *argv[]) return 1; } + if (jsonObject.contains("AutoStart")) { + const QJsonValue jsonAutoStart = jsonObject.value("AutoStart"); + if (!jsonAutoStart.isBool()) { + QTextStream(stderr) << "AutoStart is not a bool in manifest, aborting!" << smsub_endl; + manifestFile.close(); + return 1; + } + autoStart = jsonAutoStart.toBool(); + } + if (jsonObject.contains("KeepAlive")) { const QJsonValue jsonKeepAlive = jsonObject.value("KeepAlive"); if (!jsonKeepAlive.isBool()) { @@ -562,7 +586,8 @@ int main(int argc, char *argv[]) QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess); QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit); - subProcess.startProcess(); + if (autoStart) + subProcess.startProcess(); QTextStream(stderr) << QString("SMSub Version %1 initialized!").arg(QCoreApplication::applicationVersion()) << smsub_endl;