Revise ClientPinger

* improve documentation
remotes/origin/HEAD
markuspg 6 years ago
parent df2a4e9074
commit 5ca5dfbcef

@ -42,7 +42,7 @@ lc::Client::Client(const QString &argIP, const QString &argMAC,
pinger->moveToThread(&pingerThread); pinger->moveToThread(&pingerThread);
connect(&pingerThread, &QThread::finished, pinger, &QObject::deleteLater); connect(&pingerThread, &QThread::finished, pinger, &QObject::deleteLater);
connect(this, &Client::PingWanted, pinger, &ClientPinger::doPing); connect(this, &Client::PingWanted, pinger, &ClientPinger::doPing);
connect(pinger, &ClientPinger::PingFinished, this, connect(pinger, &ClientPinger::ClientStateChanged, this,
&Client::GotStatusChanged); &Client::GotStatusChanged);
pingerThread.start(); pingerThread.start();

@ -19,36 +19,39 @@
#include "clientpinger.h" #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, lc::ClientPinger::ClientPinger(const QString &argIP,
const QString &argPingCommand, const QString &argPingCommand,
QObject *argParent) QObject *const argParent)
: QObject{argParent}, : QObject{argParent},
// Arguments: -c 1 (send 1 ECHO_REQUEST packet) -w 1 (timeout after 1 // Arguments: -c 1 (send 1 ECHO_REQUEST packet) -w 1 (timeout after 1
// second) -q (quiet output) // second) -q (quiet output)
pingArguments{QStringList{} << "-c" pingArguments{"-c", "1", "-w", "1", "-q", argIP},
<< "1" pingCommand{argPingCommand}, pingProcess{new QProcess{this}} {
<< "-w"
<< "1"
<< "-q" << argIP},
pingCommand{argPingCommand},
pingProcess{new QProcess{this}}, state{Client::State::UNINITIALIZED} {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
pingProcess->setProcessEnvironment(env); 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() { void lc::ClientPinger::doPing() {
// Initialize the new state to be queried // Default-initialize the new state to be queried
Client::State newState = Client::State::UNINITIALIZED; Client::State newState = Client::State::ERROR;
// Query the current state of the client // Query the current state of the client
pingProcess->start(pingCommand, pingArguments); pingProcess->start(pingCommand, pingArguments);
if (!pingProcess->waitForFinished(2500)) if (pingProcess->waitForFinished(2500)) {
newState = Client::State::ERROR;
else {
if (pingProcess->exitCode() == 0) if (pingProcess->exitCode() == 0)
newState = Client::State::RESPONDING; newState = Client::State::RESPONDING;
else else
@ -57,10 +60,15 @@ void lc::ClientPinger::doPing() {
if (newState != state) { if (newState != state) {
state = newState; 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; state = Client::State::ZLEAF_RUNNING;
} }

@ -20,56 +20,48 @@
#ifndef CLIENTPINGER_H #ifndef CLIENTPINGER_H
#define CLIENTPINGER_H #define CLIENTPINGER_H
#include <QObject>
#include <QProcess>
#include <QThread>
#include "client.h" #include "client.h"
class QProcess;
namespace lc { 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 { class ClientPinger : public QObject {
Q_OBJECT Q_OBJECT
public: 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, explicit ClientPinger(const QString &argIP, const QString &argPingCommand,
QObject *argParent = nullptr); QObject *argParent = nullptr);
//! ClientPinger's destructor ~ClientPinger() override;
~ClientPinger();
public slots: public slots:
//! This slot executes a ping when called.
void doPing(); void doPing();
//! Retrieves the information, that zLeaf is running (otherwise restarting void setStateToZLEAF_RUNNING() noexcept;
//! will not result in status updates)
void setStateToZLEAF_RUNNING();
private: private:
const QStringList pingArguments; //! The arguments for the 'ping' command //! The arguments passed to the "ping" command
const QString pingCommand; //! The 'ping' command itself const QStringList pingArguments;
QProcess *pingProcess; //! The 'ping' process which will be executed on every //! The utilized "ping" command itself
//! call of 'do_ping()' const QString pingCommand;
Client::State state; //! Stores the current state of the client //! 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: signals:
//! This signal was just implemented for testing purposes /*!
//! This signal is emitted if the ping finished and the state of the client * \brief Signal which is being emitted if a client's state seems to have
//! changed * changed
void PingFinished(Client::State state); *
* 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 } // namespace lc

Loading…
Cancel
Save