Reformat ClientPinger

remotes/origin/HEAD
markuspg 6 years ago
parent c2d4f6024c
commit 385a42dedf

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 Markus Prasser * Copyright 2014-2018 Markus Prasser, Tobias Weiss
* *
* This file is part of Labcontrol. * This file is part of Labcontrol.
* *
@ -19,45 +19,44 @@
#include "clientpinger.h" #include "clientpinger.h"
lc::ClientPinger::ClientPinger( const QString &argIP, lc::ClientPinger::ClientPinger(const QString &argIP,
const QString &argPingCommand, QObject *argParent ) : const QString &argPingCommand,
QObject{ argParent }, QObject *argParent) :
QObject{argParent},
// Arguments: -c 1 (send 1 ECHO_REQUEST packet) -w 1 (timeout after 1 second) -q (quiet output) // 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 }, pingArguments{"-c", "1", "-w", "1", "-q", argIP},
pingCommand{ argPingCommand }, pingCommand{argPingCommand},
pingProcess{ new QProcess{ this } }, pingProcess{std::make_unique<QProcess>()}
state{ state_t::UNINITIALIZED }
{ {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); pingProcess->setProcessEnvironment(QProcessEnvironment::systemEnvironment());
pingProcess->setProcessEnvironment(env);
// emit ping_string(new QString(*ping_command + " " + ping_arguments->join(" "))); // emit ping_string(new QString(*ping_command + " " + ping_arguments->join(" ")));
} }
lc::ClientPinger::~ClientPinger() { void lc::ClientPinger::doPing()
delete pingProcess; {
}
void lc::ClientPinger::doPing() {
// Initialize the new state to be queried // Initialize the new state to be queried
state_t newState = state_t::UNINITIALIZED; state_t newState = state_t::UNINITIALIZED;
// 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)
|| pingProcess->exitStatus() != QProcess::NormalExit)
newState = state_t::ERROR; newState = state_t::ERROR;
else { else {
if ( pingProcess->exitCode() == 0 ) if (pingProcess->exitCode() == 0) {
newState = state_t::RESPONDING; newState = state_t::RESPONDING;
else } else {
newState = state_t::NOT_RESPONDING; newState = state_t::NOT_RESPONDING;
} }
}
if ( newState != state ) { if (newState != state) {
state = newState; state = newState;
emit PingFinished( newState ); emit PingFinished(state);
} }
} }
void lc::ClientPinger::setStateToZLEAF_RUNNING() { void lc::ClientPinger::setStateToZLEAF_RUNNING()
{
state = state_t::ZLEAF_RUNNING; state = state_t::ZLEAF_RUNNING;
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 Markus Prasser * Copyright 2014-2018 Markus Prasser, Tobias Weiss
* *
* This file is part of Labcontrol. * This file is part of Labcontrol.
* *
@ -20,11 +20,14 @@
#ifndef CLIENTPINGER_H #ifndef CLIENTPINGER_H
#define CLIENTPINGER_H #define CLIENTPINGER_H
#include "global.h"
#include <QObject> #include <QObject>
#include <QProcess> #include <QProcess>
#include <QThread>
#include "global.h" #include <memory>
class QProcess;
namespace lc { namespace lc {
@ -32,7 +35,8 @@ namespace lc {
/*! /*!
This class is just used for executing repetitive pings. This class is just used for executing repetitive pings.
*/ */
class ClientPinger : public QObject { class ClientPinger : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
@ -42,10 +46,8 @@ public:
@param argPingCommand The path were the command to be executed for pings resides @param argPingCommand The path were the command to be executed for pings resides
@param argParent The ClientPinger's parent owning this instance of it @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();
public slots: public slots:
//! This slot executes a ping when called. //! This slot executes a ping when called.
@ -54,17 +56,21 @@ public slots:
void setStateToZLEAF_RUNNING(); void setStateToZLEAF_RUNNING();
private: private:
const QStringList pingArguments; //! The arguments for the 'ping' command //! The arguments for the 'ping' command
const QString pingCommand; //! The 'ping' command itself const QStringList pingArguments;
QProcess * pingProcess; //! The 'ping' process which will be executed on every call of 'do_ping()' //! The 'ping' command itself
state_t state; //! Stores the current state of the client const QString pingCommand;
//! The 'ping' process which will be executed on every call of 'do_ping()'
std::unique_ptr<QProcess> pingProcess;
//! Stores the current state of the client
state_t state = state_t::UNINITIALIZED;
signals: signals:
//! This signal was just implemented for testing purposes //! This signal was just implemented for testing purposes
//! This signal is emitted if the ping finished and the state of the client changed //! This signal is emitted if the ping finished and the state of the client changed
void PingFinished( state_t state ); void PingFinished(state_t state);
}; };
} } // namespace lc
#endif // CLIENTPINGER_H #endif // CLIENTPINGER_H

Loading…
Cancel
Save