Refactor state_t enumeration

Rename it to just State and move it into the Client class since that is
where it contextually belongs.
remotes/origin/HEAD
markuspg 4 years ago
parent 2bc825bf54
commit f1e86f4283

@ -25,6 +25,7 @@
#include <QRegularExpression>
#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<QMessageBox> 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

@ -28,11 +28,10 @@
#include <QThread>
#include <QTimer>
#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

@ -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;
}

@ -24,7 +24,7 @@
#include <QProcess>
#include <QThread>
#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

@ -20,26 +20,4 @@
#ifndef GLOBAL_H
#define GLOBAL_H
#include <QMetaType>
//! 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

@ -29,7 +29,9 @@ std::unique_ptr<lc::Settings> settings;
int main(int argc, char *argv[]) {
QApplication a{argc, argv};
qRegisterMetaType<state_t>();
qRegisterMetaType<lc::Client::State>();
qRegisterMetaType<lc::Client::State>("Client::State");
qRegisterMetaType<lc::Client::State>("lc::Client::State");
settings.reset(new lc::Settings{QSettings{"Labcontrol", "Labcontrol"}});
lc::MainWindow w;

@ -492,31 +492,32 @@ void lc::MainWindow::StartReceiptsHandler(
void lc::MainWindow::UpdateClientsTableView() {
for (auto s : *valid_items) {
state_t state = static_cast<Client *>(s->data(Qt::UserRole).value<void *>())
->GetClientState();
Client::State state =
static_cast<Client *>(s->data(Qt::UserRole).value<void *>())
->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;
}

Loading…
Cancel
Save