From f1e86f4283134ff5cbefc339b6ca69ed6f555b98 Mon Sep 17 00:00:00 2001 From: markuspg Date: Tue, 15 Sep 2020 13:21:43 +0200 Subject: [PATCH] Refactor state_t enumeration Rename it to just State and move it into the Client class since that is where it contextually belongs. --- src/Lib/client.cpp | 35 ++++++++++++++++++----------------- src/Lib/client.h | 31 +++++++++++++++++++++++++------ src/Lib/clientpinger.cpp | 12 ++++++------ src/Lib/clientpinger.h | 6 +++--- src/Lib/global.h | 22 ---------------------- src/main.cpp | 4 +++- src/mainwindow.cpp | 19 ++++++++++--------- 7 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/Lib/client.cpp b/src/Lib/client.cpp index 119912e..b25d2ad 100644 --- a/src/Lib/client.cpp +++ b/src/Lib/client.cpp @@ -25,6 +25,7 @@ #include #include "client.h" +#include "clientpinger.h" #include "lablib.h" #include "settings.h" @@ -68,7 +69,7 @@ lc::Client::~Client() { void lc::Client::BeamFile(const QString &argFileToBeam, const QString *const argPublickeyPathUser, const QString *const argUserNameOnClients) { - if (state < state_t::RESPONDING) { + if (state < State::RESPONDING) { return; } @@ -103,16 +104,16 @@ void lc::Client::Boot() { pingTimer->start(3000); protectedCycles = 7; - GotStatusChanged(state_t::BOOTING); + GotStatusChanged(State::BOOTING); } -void lc::Client::GotStatusChanged(state_t argState) { - if ((protectedCycles > 0) && (state == state_t::BOOTING) && - (argState != state_t::RESPONDING)) { +void lc::Client::GotStatusChanged(State argState) { + if ((protectedCycles > 0) && (state == State::BOOTING) && + (argState != State::RESPONDING)) { return; } - if ((protectedCycles > 0) && (state == state_t::SHUTTING_DOWN) && - argState != state_t::NOT_RESPONDING) { + if ((protectedCycles > 0) && (state == State::SHUTTING_DOWN) && + argState != State::NOT_RESPONDING) { return; } state = argState; @@ -142,7 +143,7 @@ void lc::Client::KillZLeaf() { } void lc::Client::OpenFilesystem(const QString *const argUserToBeUsed) { - if (state < state_t::RESPONDING) { + if (state < State::RESPONDING) { return; } QStringList arguments = QStringList{} @@ -158,7 +159,7 @@ void lc::Client::OpenFilesystem(const QString *const argUserToBeUsed) { void lc::Client::OpenTerminal(const QString &argCommand, const bool &argOpenAsRoot) { if (!settings->termEmulCmd.isEmpty()) { - if (state < state_t::RESPONDING) { + if (state < State::RESPONDING) { return; } @@ -198,11 +199,11 @@ void lc::Client::SetStateToZLEAF_RUNNING(QString argClientIP) { if (argClientIP != ip) { return; } - if (state != state_t::ZLEAF_RUNNING) { + if (state != State::ZLEAF_RUNNING) { pingTimer->stop(); // Inform the ClientPinger instance, that zLeaf is now running pinger->setStateToZLEAF_RUNNING(); - this->GotStatusChanged(state_t::ZLEAF_RUNNING); + this->GotStatusChanged(State::ZLEAF_RUNNING); qDebug() << "Client" << name << "got 'ZLEAF_RUNNING' signal."; } } @@ -234,8 +235,8 @@ void lc::Client::ShowDesktopFullControl() { } void lc::Client::Shutdown() { - if (state == state_t::NOT_RESPONDING || state == state_t::BOOTING || - state == state_t::SHUTTING_DOWN) { + if (state == State::NOT_RESPONDING || state == State::BOOTING || + state == State::SHUTTING_DOWN) { return; } QStringList arguments; @@ -257,11 +258,11 @@ void lc::Client::Shutdown() { pingTimer->start(3000); protectedCycles = 3; - GotStatusChanged(state_t::SHUTTING_DOWN); + GotStatusChanged(State::SHUTTING_DOWN); } void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) { - if (state < state_t::RESPONDING || zLeafVersion.isEmpty() || + if (state < State::RESPONDING || zLeafVersion.isEmpty() || GetSessionPort() < 7000) { return; } @@ -269,7 +270,7 @@ void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) { // Create a QMessageBox for user interaction if there is already a zLeaf // running std::unique_ptr messageBoxRunningZLeafFound; - if (state == state_t::ZLEAF_RUNNING) { + if (state == State::ZLEAF_RUNNING) { messageBoxRunningZLeafFound.reset(new QMessageBox{ QMessageBox::Warning, "Running zLeaf found", QString{"There is already a zLeaf running on " + name + "."}, @@ -283,7 +284,7 @@ void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) { if ((messageBoxRunningZLeafFound.get() != nullptr && messageBoxRunningZLeafFound->clickedButton() == messageBoxRunningZLeafFound->button(QMessageBox::Yes)) || - state != state_t::ZLEAF_RUNNING) { + state != State::ZLEAF_RUNNING) { QStringList arguments; if (argFakeName == nullptr) { arguments << "-i" << settings->pkeyPathUser diff --git a/src/Lib/client.h b/src/Lib/client.h index 6a799ba..41a2209 100644 --- a/src/Lib/client.h +++ b/src/Lib/client.h @@ -28,11 +28,10 @@ #include #include -#include "clientpinger.h" -#include "global.h" - namespace lc { +class ClientPinger; + //! Class which represents the clients in the lab /*! This class contains elements and functions needed to represent all functions @@ -46,6 +45,25 @@ public slots: void SetStateToZLEAF_RUNNING(QString argClientIP); public: + //! Opens a terminal for the client + enum class State : unsigned short int { + //! The client is booting but not yet responding + BOOTING, + //! An error occurred determining the client's state + ERROR, + //! The client is not responding to pings + NOT_RESPONDING, + //! The client is shutting down but not yet stopped responding + SHUTTING_DOWN, + //! The client's state is not yet defined (should only occur directly after + //! client creation) + UNINITIALIZED, + //! The client is responding to pings + RESPONDING, + //! The client is running a zLeaf + ZLEAF_RUNNING + }; + const QString ip; const QString mac; const QString name; @@ -89,7 +107,7 @@ public: /*! @return The current state of the client */ - state_t GetClientState() const { return state; } + State GetClientState() const { return state; } int GetSessionPort() const { return sessionPort; } /*! * \brief Kills all processes 'zleaf.exe' on the client @@ -148,14 +166,14 @@ private: unsigned short int protectedCycles; ClientPinger *pinger = nullptr; QThread pingerThread; - state_t state = state_t::UNINITIALIZED; + State state = State::UNINITIALIZED; QTimer *pingTimer = nullptr; //! QTimer used to trigger pings by pinger's //! ClientPinger instance int sessionPort = 0; QString zLeafVersion; private slots: - void GotStatusChanged(state_t argState); + void GotStatusChanged(State argState); void RequestAPing(); signals: @@ -163,5 +181,6 @@ signals: }; } // namespace lc +Q_DECLARE_METATYPE(lc::Client::State) #endif // CLIENT_H diff --git a/src/Lib/clientpinger.cpp b/src/Lib/clientpinger.cpp index 502369c..ca581b9 100644 --- a/src/Lib/clientpinger.cpp +++ b/src/Lib/clientpinger.cpp @@ -31,7 +31,7 @@ lc::ClientPinger::ClientPinger(const QString &argIP, << "1" << "-q" << argIP}, pingCommand{argPingCommand}, - pingProcess{new QProcess{this}}, state{state_t::UNINITIALIZED} { + pingProcess{new QProcess{this}}, state{Client::State::UNINITIALIZED} { QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); pingProcess->setProcessEnvironment(env); // emit ping_string(new QString(*ping_command + " " + ping_arguments->join(" @@ -42,17 +42,17 @@ lc::ClientPinger::~ClientPinger() { delete pingProcess; } void lc::ClientPinger::doPing() { // Initialize the new state to be queried - state_t newState = state_t::UNINITIALIZED; + Client::State newState = Client::State::UNINITIALIZED; // Query the current state of the client pingProcess->start(pingCommand, pingArguments); if (!pingProcess->waitForFinished(2500)) - newState = state_t::ERROR; + newState = Client::State::ERROR; else { if (pingProcess->exitCode() == 0) - newState = state_t::RESPONDING; + newState = Client::State::RESPONDING; else - newState = state_t::NOT_RESPONDING; + newState = Client::State::NOT_RESPONDING; } if (newState != state) { @@ -62,5 +62,5 @@ void lc::ClientPinger::doPing() { } void lc::ClientPinger::setStateToZLEAF_RUNNING() { - state = state_t::ZLEAF_RUNNING; + state = Client::State::ZLEAF_RUNNING; } diff --git a/src/Lib/clientpinger.h b/src/Lib/clientpinger.h index df635b1..14317a2 100644 --- a/src/Lib/clientpinger.h +++ b/src/Lib/clientpinger.h @@ -24,7 +24,7 @@ #include #include -#include "global.h" +#include "client.h" namespace lc { @@ -63,13 +63,13 @@ private: const QString pingCommand; //! The 'ping' command itself QProcess *pingProcess; //! The 'ping' process which will be executed on every //! call of 'do_ping()' - state_t state; //! Stores the current state of the client + Client::State state; //! Stores the current state of the client signals: //! This signal was just implemented for testing purposes //! This signal is emitted if the ping finished and the state of the client //! changed - void PingFinished(state_t state); + void PingFinished(Client::State state); }; } // namespace lc diff --git a/src/Lib/global.h b/src/Lib/global.h index ff2bb7a..4f89413 100644 --- a/src/Lib/global.h +++ b/src/Lib/global.h @@ -20,26 +20,4 @@ #ifndef GLOBAL_H #define GLOBAL_H -#include - -//! Opens a terminal for the client -enum class state_t : unsigned short int { - //! The client is booting but not yet responding - BOOTING, - //! An error occurred determining the client's state - ERROR, - //! The client is not responding to pings - NOT_RESPONDING, - //! The client is shutting down but not yet stopped responding - SHUTTING_DOWN, - //! The client's state is not yet defined (should only occur directly after - //! client creation) - UNINITIALIZED, - //! The client is responding to pings - RESPONDING, - //! The client is running a zLeaf - ZLEAF_RUNNING -}; -Q_DECLARE_METATYPE(state_t) - #endif // GLOBAL_H diff --git a/src/main.cpp b/src/main.cpp index c62db4f..3cfad2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,9 @@ std::unique_ptr settings; int main(int argc, char *argv[]) { QApplication a{argc, argv}; - qRegisterMetaType(); + qRegisterMetaType(); + qRegisterMetaType("Client::State"); + qRegisterMetaType("lc::Client::State"); settings.reset(new lc::Settings{QSettings{"Labcontrol", "Labcontrol"}}); lc::MainWindow w; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6fb2411..8f7bf17 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -492,31 +492,32 @@ void lc::MainWindow::StartReceiptsHandler( void lc::MainWindow::UpdateClientsTableView() { for (auto s : *valid_items) { - state_t state = static_cast(s->data(Qt::UserRole).value()) - ->GetClientState(); + Client::State state = + static_cast(s->data(Qt::UserRole).value()) + ->GetClientState(); switch (state) { - case state_t::RESPONDING: + case Client::State::RESPONDING: s->setBackground(QBrush(QColor(128, 255, 128, 255))); s->setIcon(icons[(int)icons_t::ON]); break; - case state_t::NOT_RESPONDING: + case Client::State::NOT_RESPONDING: s->setBackground(QBrush(QColor(255, 255, 128, 255))); s->setIcon(icons[(int)icons_t::OFF]); break; - case state_t::BOOTING: + case Client::State::BOOTING: s->setBackground(QBrush(QColor(128, 128, 255, 255))); s->setIcon(icons[(int)icons_t::BOOT]); break; - case state_t::SHUTTING_DOWN: + case Client::State::SHUTTING_DOWN: s->setBackground(QBrush(QColor(128, 128, 255, 255))); s->setIcon(icons[(int)icons_t::DOWN]); break; - case state_t::ZLEAF_RUNNING: + case Client::State::ZLEAF_RUNNING: s->setBackground(QBrush(QColor(0, 255, 0, 255))); s->setIcon(icons[(int)icons_t::ZLEAF]); break; - case state_t::UNINITIALIZED: - case state_t::ERROR: + case Client::State::UNINITIALIZED: + case Client::State::ERROR: s->setBackground(QBrush(QColor(255, 128, 128, 255))); break; }