From 5ca5dfbcef53daaa3f0c3b2ac82054028a628cee Mon Sep 17 00:00:00 2001 From: markuspg Date: Tue, 15 Sep 2020 14:52:57 +0200 Subject: [PATCH] Revise ClientPinger * improve documentation --- src/Lib/client.cpp | 2 +- src/Lib/clientpinger.cpp | 44 ++++++++++++++++++------------- src/Lib/clientpinger.h | 56 +++++++++++++++++----------------------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/Lib/client.cpp b/src/Lib/client.cpp index b25d2ad..51dd7f5 100644 --- a/src/Lib/client.cpp +++ b/src/Lib/client.cpp @@ -42,7 +42,7 @@ lc::Client::Client(const QString &argIP, const QString &argMAC, pinger->moveToThread(&pingerThread); connect(&pingerThread, &QThread::finished, pinger, &QObject::deleteLater); connect(this, &Client::PingWanted, pinger, &ClientPinger::doPing); - connect(pinger, &ClientPinger::PingFinished, this, + connect(pinger, &ClientPinger::ClientStateChanged, this, &Client::GotStatusChanged); pingerThread.start(); diff --git a/src/Lib/clientpinger.cpp b/src/Lib/clientpinger.cpp index ca581b9..2b73391 100644 --- a/src/Lib/clientpinger.cpp +++ b/src/Lib/clientpinger.cpp @@ -19,36 +19,39 @@ #include "clientpinger.h" +/*! + * \brief Construct a new ClientPinger instance + * + * \param[in] argIP The IP address of the client to be pinged + * \param[in] argPingCommand The path to the ping command which shall be used + * \param[in] argParent The instance's parent QObject + */ lc::ClientPinger::ClientPinger(const QString &argIP, const QString &argPingCommand, - QObject *argParent) + QObject *const argParent) : QObject{argParent}, // Arguments: -c 1 (send 1 ECHO_REQUEST packet) -w 1 (timeout after 1 // second) -q (quiet output) - pingArguments{QStringList{} << "-c" - << "1" - << "-w" - << "1" - << "-q" << argIP}, - pingCommand{argPingCommand}, - pingProcess{new QProcess{this}}, state{Client::State::UNINITIALIZED} { + pingArguments{"-c", "1", "-w", "1", "-q", argIP}, + pingCommand{argPingCommand}, pingProcess{new QProcess{this}} { QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); pingProcess->setProcessEnvironment(env); - // emit ping_string(new QString(*ping_command + " " + ping_arguments->join(" - // "))); } -lc::ClientPinger::~ClientPinger() { delete pingProcess; } +//! Destroy the ClientPinger instance +lc::ClientPinger::~ClientPinger() { pingProcess->deleteLater(); } +/*! + * \brief Execute a ping and emit ClientStateChanged if the recognized state + * changes + */ void lc::ClientPinger::doPing() { - // Initialize the new state to be queried - Client::State newState = Client::State::UNINITIALIZED; + // Default-initialize the new state to be queried + Client::State newState = Client::State::ERROR; // Query the current state of the client pingProcess->start(pingCommand, pingArguments); - if (!pingProcess->waitForFinished(2500)) - newState = Client::State::ERROR; - else { + if (pingProcess->waitForFinished(2500)) { if (pingProcess->exitCode() == 0) newState = Client::State::RESPONDING; else @@ -57,10 +60,15 @@ void lc::ClientPinger::doPing() { if (newState != state) { state = newState; - emit PingFinished(newState); + emit ClientStateChanged(newState); } } -void lc::ClientPinger::setStateToZLEAF_RUNNING() { +/*! + * \brief Retrieve the information that a z-Leaf is running + * + * This is needed since otherwise restarting will not result in status updates. + */ +void lc::ClientPinger::setStateToZLEAF_RUNNING() noexcept { state = Client::State::ZLEAF_RUNNING; } diff --git a/src/Lib/clientpinger.h b/src/Lib/clientpinger.h index 14317a2..4973910 100644 --- a/src/Lib/clientpinger.h +++ b/src/Lib/clientpinger.h @@ -20,56 +20,48 @@ #ifndef CLIENTPINGER_H #define CLIENTPINGER_H -#include -#include -#include - #include "client.h" +class QProcess; + namespace lc { -//! The ClientPinger class is used to do repetitive pings of the owning Client -//! instance's client. /*! - This class is just used for executing repetitive pings. -*/ + * \brief Repetitively ping the client represented by the owning Client instance + */ class ClientPinger : public QObject { Q_OBJECT public: - //! ClientPinger's constructor - /*! - @param argIP A reference to the owning Client instance's IP - address - @param argPingCommand The path were the command to be executed for pings - resides - @param argParent The ClientPinger's parent owning this instance of - it - */ explicit ClientPinger(const QString &argIP, const QString &argPingCommand, QObject *argParent = nullptr); - //! ClientPinger's destructor - ~ClientPinger(); + ~ClientPinger() override; public slots: - //! This slot executes a ping when called. void doPing(); - //! Retrieves the information, that zLeaf is running (otherwise restarting - //! will not result in status updates) - void setStateToZLEAF_RUNNING(); + void setStateToZLEAF_RUNNING() noexcept; private: - const QStringList pingArguments; //! The arguments for the 'ping' command - const QString pingCommand; //! The 'ping' command itself - QProcess *pingProcess; //! The 'ping' process which will be executed on every - //! call of 'do_ping()' - Client::State state; //! Stores the current state of the client + //! The arguments passed to the "ping" command + const QStringList pingArguments; + //! The utilized "ping" command itself + const QString pingCommand; + //! The "ping" process which will be executed on every call of "doPing()" + QProcess *const pingProcess = nullptr; + //! Stores the currently assumed state of the client + Client::State state = Client::State::UNINITIALIZED; 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(Client::State state); + /*! + * \brief Signal which is being emitted if a client's state seems to have + * changed + * + * This is emitted if a ping triggered by "doPing()" finished and the detected + * change of the represented client seems to have changed. + * + * \param state The new state the client seems to have + */ + void ClientStateChanged(Client::State state); }; } // namespace lc