From 385a42dedfa44d7825c23364fbe82c12ff146381 Mon Sep 17 00:00:00 2001 From: markuspg Date: Mon, 12 Mar 2018 21:57:25 +0100 Subject: [PATCH] Reformat ClientPinger --- src/Lib/clientpinger.cpp | 43 ++++++++++++++++++++-------------------- src/Lib/clientpinger.h | 34 ++++++++++++++++++------------- 2 files changed, 41 insertions(+), 36 deletions(-) mode change 100755 => 100644 src/Lib/clientpinger.cpp mode change 100755 => 100644 src/Lib/clientpinger.h diff --git a/src/Lib/clientpinger.cpp b/src/Lib/clientpinger.cpp old mode 100755 new mode 100644 index 65d6f0e..8f4a9d6 --- a/src/Lib/clientpinger.cpp +++ b/src/Lib/clientpinger.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 Markus Prasser + * Copyright 2014-2018 Markus Prasser, Tobias Weiss * * This file is part of Labcontrol. * @@ -19,45 +19,44 @@ #include "clientpinger.h" -lc::ClientPinger::ClientPinger( const QString &argIP, - const QString &argPingCommand, QObject *argParent ) : - QObject{ argParent }, +lc::ClientPinger::ClientPinger(const QString &argIP, + const QString &argPingCommand, + QObject *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{ state_t::UNINITIALIZED } + pingArguments{"-c", "1", "-w", "1", "-q", argIP}, + pingCommand{argPingCommand}, + pingProcess{std::make_unique()} { - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - pingProcess->setProcessEnvironment(env); + pingProcess->setProcessEnvironment(QProcessEnvironment::systemEnvironment()); // emit ping_string(new QString(*ping_command + " " + ping_arguments->join(" "))); } -lc::ClientPinger::~ClientPinger() { - delete pingProcess; -} - -void lc::ClientPinger::doPing() { +void lc::ClientPinger::doPing() +{ // Initialize the new state to be queried state_t newState = state_t::UNINITIALIZED; // Query the current state of the client - pingProcess->start( pingCommand, pingArguments ); - if ( !pingProcess->waitForFinished( 2500 ) ) + pingProcess->start(pingCommand, pingArguments); + if (!pingProcess->waitForFinished(2500) + || pingProcess->exitStatus() != QProcess::NormalExit) newState = state_t::ERROR; else { - if ( pingProcess->exitCode() == 0 ) + if (pingProcess->exitCode() == 0) { newState = state_t::RESPONDING; - else + } else { newState = state_t::NOT_RESPONDING; + } } - if ( newState != state ) { + if (newState != state) { state = newState; - emit PingFinished( newState ); + emit PingFinished(state); } } -void lc::ClientPinger::setStateToZLEAF_RUNNING() { +void lc::ClientPinger::setStateToZLEAF_RUNNING() +{ state = state_t::ZLEAF_RUNNING; } diff --git a/src/Lib/clientpinger.h b/src/Lib/clientpinger.h old mode 100755 new mode 100644 index 4b10490..30b14eb --- a/src/Lib/clientpinger.h +++ b/src/Lib/clientpinger.h @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 Markus Prasser + * Copyright 2014-2018 Markus Prasser, Tobias Weiss * * This file is part of Labcontrol. * @@ -20,11 +20,14 @@ #ifndef CLIENTPINGER_H #define CLIENTPINGER_H +#include "global.h" + #include #include -#include -#include "global.h" +#include + +class QProcess; namespace lc { @@ -32,7 +35,8 @@ namespace lc { /*! This class is just used for executing repetitive pings. */ -class ClientPinger : public QObject { +class ClientPinger : public QObject +{ Q_OBJECT public: @@ -42,10 +46,8 @@ public: @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(); + explicit ClientPinger(const QString &argIP, const QString &argPingCommand, + QObject *argParent = nullptr); public slots: //! This slot executes a ping when called. @@ -54,17 +56,21 @@ public slots: void setStateToZLEAF_RUNNING(); 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()' - state_t state; //! Stores the current state of the client + //! The arguments for the 'ping' command + const QStringList pingArguments; + //! The 'ping' command itself + const QString pingCommand; + //! The 'ping' process which will be executed on every call of 'do_ping()' + std::unique_ptr pingProcess; + //! Stores the current state of the client + state_t state = state_t::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( state_t state ); + void PingFinished(state_t state); }; -} +} // namespace lc #endif // CLIENTPINGER_H