From 8febebf264765912b35dda552940a66736c12303 Mon Sep 17 00:00:00 2001 From: Steven de Ridder Date: Mon, 24 Jan 2022 16:22:10 +0100 Subject: [PATCH] Initial commit. dependencies not resolved yet. --- .gitignore | 2 ++ CMakeLists.txt | 25 +++++++++++++++++++++++++ README.md | 0 src/CMakeLists.txt | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/autodiscover.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/autodiscover.h | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/compatqt514.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hostbuffer.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hostbuffer.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/httpclient.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/httpclient.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/httpclientmanager.cpp | 30 ++++++++++++++++++++++++++++++ src/httpclientmanager.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/httpclientthread.cpp | 29 +++++++++++++++++++++++++++++ src/httpclientthread.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/namesolver.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/namesolver.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/pingdata.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingdata.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingdataqueue.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingdataqueue.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingmanager.cpp | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingmanager.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingrunner.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingrunner.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingthread.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pingthread.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ 28 files changed, 1991 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 src/CMakeLists.txt create mode 100644 src/autodiscover.cpp create mode 100644 src/autodiscover.h create mode 100644 src/compatqt514.h create mode 100644 src/hostbuffer.cpp create mode 100644 src/hostbuffer.h create mode 100644 src/httpclient.cpp create mode 100644 src/httpclient.h create mode 100644 src/httpclientmanager.cpp create mode 100644 src/httpclientmanager.h create mode 100644 src/httpclientthread.cpp create mode 100644 src/httpclientthread.h create mode 100644 src/namesolver.cpp create mode 100644 src/namesolver.h create mode 100644 src/pingdata.cpp create mode 100644 src/pingdata.h create mode 100644 src/pingdataqueue.cpp create mode 100644 src/pingdataqueue.h create mode 100644 src/pingmanager.cpp create mode 100644 src/pingmanager.h create mode 100644 src/pingrunner.cpp create mode 100644 src/pingrunner.h create mode 100644 src/pingthread.cpp create mode 100644 src/pingthread.h create mode 100644 tests/CMakeLists.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ff047c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e1e8f9a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.0) + +# Check to see where cmake is located. +if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/cmake ) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +elseif( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../cmake ) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) +else() + return() +endif() + +# Check to see if there is versioning information available +if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake) + include(osdevversion) +endif() + +include(projectheader) +project_header(osdev_iputils) + +add_subdirectory(src) +add_subdirectory(tests) + +# include(packaging) +# package_component() diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..42c4a7d --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,56 @@ +cmake_minimum_required(VERSION 3.0) +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake) +include(projectheader) +project_header(iputils) + +find_package( Qt5Core REQUIRED ) +find_package( Qt5Network REQUIRED ) + +include_directories( SYSTEM + ${Qt5Core_INCLUDE_DIRS} + ${Qt5Network_INCLUDE_DIRS} +) + +include(compiler) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../logutils +) + +set(SRC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/namesolver.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/autodiscover.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/hostbuffer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pingrunner.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pingdata.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pingdataqueue.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pingmanager.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pingthread.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/httpclient.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/compatqt514.h +) + +include(qtmoc) +create_mocs( SRC_LIST MOC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/autodiscover.h + ${CMAKE_CURRENT_SOURCE_DIR}/hostbuffer.h + ${CMAKE_CURRENT_SOURCE_DIR}/pingrunner.h + ${CMAKE_CURRENT_SOURCE_DIR}/pingdataqueue.h + ${CMAKE_CURRENT_SOURCE_DIR}/pingmanager.h + ${CMAKE_CURRENT_SOURCE_DIR}/pingthread.h + ${CMAKE_CURRENT_SOURCE_DIR}/httpclient.h +) + +link_directories( + ${CMAKE_BINARY_DIR}/lib +) + +include(library) +add_libraries( + ${Qt5Core_LIBRARIES} + ${Qt5Network_LIBRARIES} + logutils +) + +include(installation) +install_component() diff --git a/src/autodiscover.cpp b/src/autodiscover.cpp new file mode 100644 index 0000000..f88c47d --- /dev/null +++ b/src/autodiscover.cpp @@ -0,0 +1,130 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +// Local +#include "autodiscover.h" +#include "log.h" +#include "httpclient.h" + +// Qt +#include +#include +#include + +#include + +using namespace osdev::components; + +AutoDiscover::AutoDiscover( QObject *parent ) + : QObject( parent ) + , m_hosts_found( QStringList() ) + , m_hosts_to_check( QStringList() ) + , m_pingManager( 10 ) +{ + // Connect the PingManager to this object + connect( &m_pingManager, &PingManager::signalAllThreadsFinished, this, &AutoDiscover::slotPingThreadsReady ); + + // Start the network discovery + this->startDiscovery(); +} + +void AutoDiscover::getNetworks() +{ + // Make sure we start clean and fresh. + m_hosts_to_check.clear(); + LogInfo( "[AutoDiscover::getNetworks]", QString( "Starting with %1 hosts in the buffer" ).arg( m_hosts_to_check.count() ) ); + foreach( const QHostAddress &address, QNetworkInterface::allAddresses() ) + { + if( address.protocol() == QAbstractSocket::IPv4Protocol + && address != QHostAddress( QHostAddress::LocalHost ) ) + { + LogInfo( "[AutoDiscover::getNetworks]", QString( "Found network address : %1" ).arg( address.toString() ) ); + QStringList octets = address.toString().split( "." ); + if( octets.size() == 4 ) + { + for( int host = 1; host < 255; host++ ) + { + m_hosts_to_check.append( QString( "%1.%2.%3.%4" ) + .arg( octets.at( 0 ) ) + .arg( octets.at( 1 ) ) + .arg( octets.at( 2 ) ) + .arg( host ) ); + } + } + } + } +} + +void AutoDiscover::slotPingThreadsReady() +{ + LogInfo( "[AutoDiscover::slotPingThreadsReady]", QString( "Wrapping up the pings. Getting all Active IP-addresses" ) ); + // All Threads are done, which means the pingDataQueue is ready for reading. + QStringList ip_lijst = m_pingManager.getActiveIPs(); + + for( const auto &ip : ip_lijst ) + { + LogInfo( "[AutoDiscover::slotPingThreadsReady]", + QString( "Host : %1 is active." ) .arg( ip ) ); + + HttpClient *pClient = new HttpClient( this ); + connect( pClient, &HttpClient::signalDataReceived, this, &AutoDiscover::slotDataReceived ); + pClient->sendRequest( ip, "/id" ); + } + +} + +void AutoDiscover::startDiscovery() +{ + // Produce all nodes within this network + getNetworks(); + // Check if the found hosts are actually sensors. + checkHost(); +} + +void AutoDiscover::checkHost() +{ + LogInfo( "[AutoDiscover::checkHost]", QString( "Adding %1 hosts to the pingmanager" ).arg( m_hosts_to_check.count() ) ); + m_pingManager.addHosts( m_hosts_to_check ); + m_pingManager.startPingTests(); +} + +void AutoDiscover::slotDataReceived( const QString &ip_address, const QString &data, QObject *client ) +{ + if( data.contains( "CO2MOS" ) ) + { + LogInfo( "[AutoDiscover::slotDataReceived]", QString( "Host : %1 seems to be a CO2MOS Smart Sensor. Data Received : %2" ) + .arg( ip_address ) + .arg( data ) ); + + // Send this ip-address with String, Id and IP-address, separated by colons. + emit signalSensorFound( data ); + } + else + { + LogInfo( "[AutoDiscover::slotDataReceived]", QString( "Host : %1 doesn't seem to be a CO2 Sensor." ).arg( ip_address ) ); + } + + if( client ) + { + LogInfo( "[AutoDiscover::slotDataReceived]", QString( "Deleting the ClientObject will be done if the parent object is removed." ) ); + // delete client; + } +} diff --git a/src/autodiscover.h b/src/autodiscover.h new file mode 100644 index 0000000..e43c9ac --- /dev/null +++ b/src/autodiscover.h @@ -0,0 +1,115 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_AUTODISCOVER_H +#define OSDEV_COMPONENTS_AUTODISCOVER_H + +// Qt +#include +#include +#include +#include + +// Local +#include "pingmanager.h" +/* + _______________________________________ +/ Dealing with failure is easy: \ +| | +| Work hard to improve. Success is also | +| easy to handle: | +| | +| You've solved the wrong problem. | +| | +\ Work hard to improve. / + --------------------------------------- + .--. + |o_o | + |:_/ | + // \ \ + (| | ) + /'\_ _/`\ + \___)=(___/ +*/ + +namespace osdev { +namespace components { + +/*! + * \brief Autodiscover will check the NIC for its networkaddress and scans + * the network for connected and active sensors. + */ +class AutoDiscover : public QObject +{ + Q_OBJECT + +public: + /*! + * \brief AutoDiscover - The default Constructor + * \param parent - The calling QObject + */ + explicit AutoDiscover( QObject *parent = nullptr ); + +public slots: + /*! + * \brief startDiscovery - Start scanning the network for active sensors. + */ + void startDiscovery(); + +private slots: + /*! + * \brief slotPingThreadsReady - Will be called when all threads are ready + */ + void slotPingThreadsReady(); + + /*! + * \brief slotDataReceived - Called by the thread that received data. + * \param ip_address - The ip-address of the sensor + * \param data - The data received + * \param client - The pointer to the calling object. Can be NULL in most cases. + */ + void slotDataReceived( const QString &ip_address, const QString &data, QObject *client = nullptr ); + +signals: + /*! + * \brief signalSensorFound - Signal will fire when data was received from one of the threads. + * \param data - The received data. + */ + void signalSensorFound( const QString &data ); + +private: + /// Determine the network subnet(s) we're living in. + void getNetworks(); + /// CHeck all found hosts if they respond to the ID query + void checkHost(); + +private: // Members ( Giggity! ) + QStringList m_hosts_found; + QStringList m_hosts_to_check; + + PingManager m_pingManager; + +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_AUTODISCOVER_H */ diff --git a/src/compatqt514.h b/src/compatqt514.h new file mode 100644 index 0000000..ab9b444 --- /dev/null +++ b/src/compatqt514.h @@ -0,0 +1,61 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_COMPATQT514_H +#define OSDEV_COMPONENTS_COMPATQT514_H + +#include + +/* + _________________________________________ +/ This conjunction of an immense military \ +| establishment and a large arms industry | +| is now in the American experience... We | +| must not fail to comprehend its grave | +| implications... We must guard against | +| the acquisition of unwarranted | +| influence...by the military-industrial | +| complex. The potential for the | +| disastrous rise of misplaced power | +| exists and will persist. | +| | +| -- Dwight D. Eisenhower, from his | +\ farewell address in 1961 / + ----------------------------------------- + \ + \ + .--. + |o_o | + |:_/ | + // \ \ + (| | ) + /'\_ _/`\ + \___)=(___/ +*/ + +// A little something to satisfy the namespace change in Qt. +#if(QT_VERSION >= QT_VERSION_CHECK( 5, 14, 0) ) +#define SKIP_EMPTY_PARTS Qt::SkipEmptyParts +#else +#define SKIP_EMPTY_PARTS QString::SkipEmptyParts +#endif + +#endif /* OSDEV_COMPONENTS_COMPATQT514_H */ diff --git a/src/hostbuffer.cpp b/src/hostbuffer.cpp new file mode 100644 index 0000000..8ae4c26 --- /dev/null +++ b/src/hostbuffer.cpp @@ -0,0 +1,74 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "hostbuffer.h" + +using namespace osdev::components; + +HostBuffer::HostBuffer( QObject *parent ) + : QObject( parent ) + , m_data( QStringList() ) + , m_mutList() +{ + +} + +HostBuffer::HostBuffer( const QStringList &host_list, QObject *parent ) + : QObject( parent ) + , m_data( host_list ) + , m_mutList() +{ + +} + +int HostBuffer::addHost( const QString &host ) +{ + QMutexLocker lock( &m_mutList ); + m_data.append( host ); + + return m_data.size(); +} + +int HostBuffer::addHosts( const QStringList &host_list ) +{ + for( QString host : host_list ) + { + this->addHost( host ); + } + return m_data.size(); +} + +QString HostBuffer::getHost() +{ + QMutexLocker lock( &m_mutList ); + if( m_data.size() > 0 ) + { + return m_data.takeFirst(); + } + + return QString(); +} + +int HostBuffer::count() +{ + QMutexLocker lock( &m_mutList ); + return m_data.size(); +} diff --git a/src/hostbuffer.h b/src/hostbuffer.h new file mode 100644 index 0000000..895b521 --- /dev/null +++ b/src/hostbuffer.h @@ -0,0 +1,64 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_HOSTBUFFER_H +#define OSDEV_COMPONENTS_HOSTBUFFER_H + +#include +#include +#include +#include + +namespace osdev { +namespace components { + +class HostBuffer : public QObject +{ + Q_OBJECT + +public: + /// Default CTor + HostBuffer( QObject *parent = nullptr ); + + /// Overloaded constuctor taken a host list during initialisation + HostBuffer( const QStringList &host_list, QObject *parent = nullptr ); + + /// Adds a single host to the buffer + int addHost( const QString &host ); + + /// Adds a list of hosts. It will not overwrite hosts already present but adds them. + int addHosts( const QStringList &host_list ); + + /// Retrieve -a- host from the buffer. It will be removed! + QString getHost(); + + /// Get the number of hosts in the buffer + int count(); + +private: + QStringList m_data; + QMutex m_mutList; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_HOSTBUFFER_H */ diff --git a/src/httpclient.cpp b/src/httpclient.cpp new file mode 100644 index 0000000..1179833 --- /dev/null +++ b/src/httpclient.cpp @@ -0,0 +1,105 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "httpclient.h" +#include "log.h" + +using namespace osdev::components; + +HttpClient::HttpClient( QObject *parent ) + : QObject( parent ) + , m_pNetworkManager( new QNetworkAccessManager( this ) ) +{ + this->connectSignalsToSlots(); +} + +void HttpClient::connectSignalsToSlots() +{ + connect( m_pNetworkManager.data(), &QNetworkAccessManager::finished, this, &HttpClient::slotHttpRequestFinished ); +} + +void HttpClient::sendRequest( const QString &ip_address, const QString &argument ) +{ + QUrl l_url = QString( "http://" + ip_address + argument ); + m_pNetworkManager->get( QNetworkRequest( l_url ) ); +} + +void HttpClient::slotHttpRequestFinished( QNetworkReply *data ) +{ + if( data ) + { + switch( data->error() ) + { + case QNetworkReply::NoError: + { + QString l_data = data->readAll(); + emit signalDataReceived( data->url().toString(), l_data, this ); + break; + } + case QNetworkReply::TimeoutError: + { + // Set all data to '0' to indicate the sensor offline + break; + } + case QNetworkReply::ConnectionRefusedError: + case QNetworkReply::RemoteHostClosedError: + case QNetworkReply::HostNotFoundError: + case QNetworkReply::OperationCanceledError: + case QNetworkReply::SslHandshakeFailedError: + case QNetworkReply::TemporaryNetworkFailureError: + case QNetworkReply::NetworkSessionFailedError: + case QNetworkReply::BackgroundRequestNotAllowedError: +#if(QT_VERSION > QT_VERSION_CHECK( 5, 6, 0) ) + case QNetworkReply::TooManyRedirectsError: + case QNetworkReply::InsecureRedirectError: +#endif + case QNetworkReply::ProxyConnectionRefusedError: + case QNetworkReply::ProxyConnectionClosedError: + case QNetworkReply::ProxyNotFoundError: + case QNetworkReply::ProxyTimeoutError: + case QNetworkReply::ProxyAuthenticationRequiredError: + case QNetworkReply::ContentAccessDenied: + case QNetworkReply::ContentOperationNotPermittedError: + case QNetworkReply::ContentNotFoundError: + case QNetworkReply::AuthenticationRequiredError: + case QNetworkReply::ContentReSendError: + case QNetworkReply::ContentConflictError: + case QNetworkReply::ContentGoneError: + case QNetworkReply::InternalServerError: + case QNetworkReply::OperationNotImplementedError: + case QNetworkReply::ServiceUnavailableError: + case QNetworkReply::ProtocolUnknownError: + case QNetworkReply::ProtocolInvalidOperationError: + case QNetworkReply::UnknownNetworkError: + case QNetworkReply::UnknownProxyError: + case QNetworkReply::UnknownContentError: + case QNetworkReply::ProtocolFailure: + case QNetworkReply::UnknownServerError: + { + LogWarning( "[HttpClient::slotHttpRequestFinished]", + QString( "The following error occured : %1. Host : %2" ) + .arg( data->errorString() ) + .arg( data->url().host() ) ); + break; + } + } + } +} diff --git a/src/httpclient.h b/src/httpclient.h new file mode 100644 index 0000000..472f38e --- /dev/null +++ b/src/httpclient.h @@ -0,0 +1,110 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_HTTPCLIENT_H +#define OSDEV_COMPONENTS_HTTPCLIENT_H + +#include +#include +#include +#include +#include + +/*_________________________________________ +/ "I got into an elevator at work and \ +| this man followed in after me... I | +| pushed '1' and he just stood there... I | +| said 'Hi, where you going?' He said, | +| 'Phoenix.' So I pushed Phoenix. A few | +| seconds later the doors opened, two | +| tumbleweeds blew in... we were in | +| downtown Phoenix. I looked at him and | +| said 'You know, you're the kind of guy | +| I want to hang around with.' We got | +| into his car and drove out to his shack | +| in the desert. Then the phone rang. He | +| said 'You get it.' I picked it up and | +| said 'Hello?'... the other side said | +| 'Is this Steven Wright?'... I said | +| 'Yes...' The guy said 'Hi, I'm Mr. | +| Jones, the student loan director from | +| your bank... It seems you have missed | +| your last 17 payments, and the | +| university you attended said that they | +| received none of the $17,000 we loaned | +| you... we would just like to know what | +| happened to the money?' I said, 'Mr. | +| Jones, I'll give it to you straight. I | +| gave all of the money to my friend | +| Slick, and with it he built a nuclear | +| weapon... and I would appreciate it if | +| you never called me again." | +| | +\ -- Steven Wright / + ----------------------------------------- + \ + \ + .--. + |o_o | + |:_/ | + // \ \ + (| | ) + /'\_ _/`\ + \___)=(___/ +*/ + +namespace osdev { +namespace components { + +class HttpClient : public QObject +{ + Q_OBJECT + +public: + /// Default CTor + explicit HttpClient( QObject *parent = nullptr ); + +signals: + /// Triggered when net data comes in + void signalDataReceived( const QString &ip_address, const QString &data, QObject *client = nullptr ) ; + +public slots: + /*! + * \brief sendRequest - Sends the request to the ip_address with the argument. ( Full URL ). + * \param ip_address - The ip_address we send the request to... + * \param argument - The argument that will complete the URL. + */ + void sendRequest( const QString &ip_address, const QString &argument = "/" ); + +private slots: + void slotHttpRequestFinished( QNetworkReply *data ); + +private: // Methods + void connectSignalsToSlots(); + +private: // Members ( Giggity! ) + QPointer m_pNetworkManager; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_HTTPCLIENT_H */ diff --git a/src/httpclientmanager.cpp b/src/httpclientmanager.cpp new file mode 100644 index 0000000..85c5a5f --- /dev/null +++ b/src/httpclientmanager.cpp @@ -0,0 +1,30 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "httpclientmanager.h" + + + +HttpClientManager::HttpClientManager( QObject *parent ) + : QObject( parent ) +{ + +} diff --git a/src/httpclientmanager.h b/src/httpclientmanager.h new file mode 100644 index 0000000..2af4c69 --- /dev/null +++ b/src/httpclientmanager.h @@ -0,0 +1,57 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_HTTPCLIENTMANAGER_H +#define OSDEV_COMPONENTS_HTTPCLIENTMANAGER_H + +#include +#include +#include + +#include "httpclientthread.h" + +namespace osdev { +namespace components { + +class HttpClientManager : public QObject +{ + Q_OBJECT + +public: + explicit HttpClientManager( QObject *parent = nullptr ); + + HttpClientManager( const HttpClientManager& ) = delete; + HttpClientManager( const HttpClientManager&& ) = delete; + HttpClientManager& operator=( const HttpClientManager& ) = delete; + HttpClientManager& operator=( const HttpClientManager&& ) = delete; + + virtual ~HttpClientManager(); + + + +signals: + +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_HTTPCLIENTMANAGER_H */ diff --git a/src/httpclientthread.cpp b/src/httpclientthread.cpp new file mode 100644 index 0000000..b04ad18 --- /dev/null +++ b/src/httpclientthread.cpp @@ -0,0 +1,29 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "httpclientthread.h" + + + +HttpClientThread::HttpClientThread() +{ + +} diff --git a/src/httpclientthread.h b/src/httpclientthread.h new file mode 100644 index 0000000..d1cb78f --- /dev/null +++ b/src/httpclientthread.h @@ -0,0 +1,61 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_HTTPCLIENTTHREAD_H +#define OSDEV_COMPONENTS_HTTPCLIENTTHREAD_H + +#include +#include + +namespace osdev { +namespace components { + +class HttpClientThread : public QThread +{ + Q_OBJECT + +public: + HttpClientThread( const QString &ip_address, const QString &url, QObject *parent = nullptr ); + + HttpClientThread( const HttpClientThread& ) = delete; + HttpClientThread( const HttpClientThread&& ) = delete; + HttpClientThread& operator=( const HttpClientThread& ) = delete; + HttpClientThread& operator=( const HttpClientThread&& ) = delete; + + virtual ~HttpClientThread() {} + + void setPriority( QThread::Priority priority = QThread::NormalPriority ); + void run() override; + +public: + void slotStartThread(); + + void slotThreadFinished(); + +signals: + void signalThreadFinished( QThread *thread ); + +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_HTTPCLIENTTHREAD_H */ diff --git a/src/namesolver.cpp b/src/namesolver.cpp new file mode 100644 index 0000000..45fe2ad --- /dev/null +++ b/src/namesolver.cpp @@ -0,0 +1,62 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +// Local +#include "namesolver.h" +#include "log.h" + +// System +#include +#include + +using namespace osdev::components; + +QStringList NameSolver::hostname_to_ip( const QString &host_name ) +{ + QStringList sResult; + + char ip_arr[100]; + + struct addrinfo hints, *servinfo, *p; + struct sockaddr_in *h; + int rv; + + memset( &hints, 0, sizeof( hints ) ); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if( ( rv = getaddrinfo( host_name.toLatin1().data(), nullptr, &hints, &servinfo ) ) != 0 ) + { + LogInfo( "[NameSolver::hostname_to_ip]", QString( "getaddrinfo : %1" ).arg( gai_strerror( rv ) ) ); + return sResult; + } + + // Loop through all the results and connect to the first we can + for( p = servinfo; p != nullptr; p = p->ai_next ) + { + h = reinterpret_cast( p->ai_addr ); + strcpy( ip_arr, inet_ntoa( h->sin_addr ) ); + sResult.append( QString( reinterpret_cast( ip_arr ) ) ); + } + + freeaddrinfo( servinfo ); + return sResult; +} diff --git a/src/namesolver.h b/src/namesolver.h new file mode 100644 index 0000000..2e95ccc --- /dev/null +++ b/src/namesolver.h @@ -0,0 +1,47 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_NAMESOLVER_H +#define OSDEV_COMPONENTS_NAMESOLVER_H + +// Qt +#include + +// System +#include +#include +#include + +namespace osdev { +namespace components { + +class NameSolver +{ +public: + explicit NameSolver(); + + static QStringList hostname_to_ip( const QString &host_name ); +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_NAMESOLVER_H */ diff --git a/src/pingdata.cpp b/src/pingdata.cpp new file mode 100644 index 0000000..0df6035 --- /dev/null +++ b/src/pingdata.cpp @@ -0,0 +1,147 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +// Local +#include "pingdata.h" +#include "compatqt514.h" + +// Qt +#include + +using namespace osdev::components; + +PingData::PingData() + : m_time_stamp() + , m_host_name() + , m_ip_address() + , m_packet_loss( 0 ) + , m_rtt_min( 0 ) + , m_rtt_avg( 0 ) + , m_rtt_max( 0 ) + , m_rtt_mdev( 0 ) + , m_available( false ) +{ + +} + +PingData::PingData( const QString &host_name, const QStringList &output_data ) + : m_time_stamp() + , m_host_name( host_name ) + , m_ip_address() + , m_packet_loss( 0 ) + , m_rtt_min( 0 ) + , m_rtt_avg( 0 ) + , m_rtt_max( 0 ) + , m_rtt_mdev( 0 ) + , m_available( false ) +{ + parseOutputData( output_data ); +} + +PingData::~PingData() +{ + +} + +void PingData::parseOutputData( const QStringList &output_data ) +{ + m_time_stamp = QDateTime::currentDateTime(); + if( output_data.size() == 5 ) + { + // ------------------------------------------------------------------------- + // Get the IP-Address from the structure + // Format of the line : PING osdev.nl (x.x.x.x) 56(84) bytes of data. + QString ipAddr = output_data.at(1).split( " ", SKIP_EMPTY_PARTS ).at( 2 ); + m_ip_address = ipAddr.replace( "(", "" ).replace( ")", "" ); + + // ------------------------------------------------------------------------- + // Get the packet losses + if( output_data.at( 3 ).split( " ", SKIP_EMPTY_PARTS ).size() < 12 ) + { + QString s_losses = output_data.at( 3 ).split( " ", SKIP_EMPTY_PARTS ).at( 5 ); + m_packet_loss = s_losses.replace( "%", "" ).toInt(); + } + else if( output_data.at( 3 ).split( " ", SKIP_EMPTY_PARTS ).size() > 2 ) + { + QString s_losses = output_data.at( 3 ).split( " ", SKIP_EMPTY_PARTS ).at( 7 ); + m_packet_loss = s_losses.replace( "%", "" ).toInt(); + } + + // ------------------------------------------------------------------------- + // Get the min/max statistics. + if( output_data.at( 4 ).split( " ", SKIP_EMPTY_PARTS ).size() > 2 ) + { + QString s_statline = output_data.at( 4 ).split( " ", SKIP_EMPTY_PARTS ).at( 3 ); + QStringList s_lstData = s_statline.split( "/", SKIP_EMPTY_PARTS ); + + m_rtt_min = s_lstData.at( 0 ).toFloat(); + m_rtt_avg = s_lstData.at( 1 ).toFloat(); + m_rtt_max = s_lstData.at( 2 ).toFloat(); + m_rtt_mdev = s_lstData.at( 3 ).toFloat(); + } + + if( output_data.at( 3 ).contains( "100% packet loss" ) ) + { + m_available = false; + } + else + { + m_available = true; + } + } + else if( output_data.size() == 4 ) + { + // Get the IP-Address + QString ipAddr = output_data.at( 1 ).split( " ", SKIP_EMPTY_PARTS ).at( 2 ); + m_ip_address = ipAddr.replace( "(", "" ).replace( ")", "" ); + + // Get the packet losses + QString s_losses = output_data.at( 3 ).split( " ", SKIP_EMPTY_PARTS ).at( 5 ); + m_packet_loss = s_losses.replace( "%", "" ).toInt(); + + m_available = false; + } +} + + +QString PingData::asString() +{ + QString sResult; + + sResult += QString( "Hostname : %1\n" ).arg( m_host_name ); + sResult += QString( "\t|-> IpAddress : %1\n" ).arg( m_ip_address ); + sResult += QString( "\t|-> Packet loss : %1\n" ).arg( m_packet_loss ); + sResult += QString( "\t|-> RTT min : %1\n" ).arg( m_rtt_min ); + sResult += QString( "\t|-> RTT max : %1\n" ).arg( m_rtt_max ); + sResult += QString( "\t|-> RTT average : %1\n" ).arg( m_rtt_avg ); + sResult += QString( "\t|-> RTT mdev : %1\n" ).arg( m_rtt_mdev ); + sResult += QString( "\t|-> Available : " ); + if( m_available ) + { + sResult += QString( "yes\n" ); + } + else + { + sResult += QString( "no\n" ); + } + + return sResult; +} diff --git a/src/pingdata.h b/src/pingdata.h new file mode 100644 index 0000000..53f5fef --- /dev/null +++ b/src/pingdata.h @@ -0,0 +1,87 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_PINGDATA_H +#define OSDEV_COMPONENTS_PINGDATA_H + +#include +#include +#include + +namespace osdev { +namespace components { + +class PingData +{ +public: + PingData(); + PingData( const QString &host_name, const QStringList &output_data ); + + virtual ~PingData(); + + // All getters and setters. + void setTimeStamp( const QDateTime &time_stamp ) { m_time_stamp = time_stamp; } + const QDateTime& timeStamp() { return m_time_stamp; } + + void setHostName( const QString &host_name ) { m_host_name = host_name; } + const QString& hostName() { return m_host_name; } + + void setIpAddress( const QString &ip_address ) { m_ip_address = ip_address; } + const QString& ipAddress() { return m_ip_address; } + + void setPacketLoss( const int packet_loss ) { m_packet_loss = packet_loss; } + int packetLoss() { return m_packet_loss; } + + void setRttMin( const float rtt_min ) { m_rtt_min = rtt_min; } + float rttMin() { return m_rtt_min; } + + void setRttAvg( const float rtt_avg ) { m_rtt_avg = rtt_avg; } + float rttAvg() { return m_rtt_avg; } + + void setRttMax( const float rtt_max ) { m_rtt_max = rtt_max; } + float rttMax() { return m_rtt_max; } + + void setRttMdev( const float rtt_mdev ) { m_rtt_mdev = rtt_mdev; } + float rttMdev() { return m_rtt_mdev; } + + bool available() { return m_available; } + + QString asString(); + +private: + void parseOutputData( const QStringList &output_data ); + +private: + QDateTime m_time_stamp; + QString m_host_name; + QString m_ip_address; + int m_packet_loss; + float m_rtt_min; + float m_rtt_avg; + float m_rtt_max; + float m_rtt_mdev; + bool m_available; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_PINGDATA_H */ diff --git a/src/pingdataqueue.cpp b/src/pingdataqueue.cpp new file mode 100644 index 0000000..c4ba7bf --- /dev/null +++ b/src/pingdataqueue.cpp @@ -0,0 +1,81 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "pingdataqueue.h" +#include "log.h" + +#include + +using namespace osdev::components; + +PingDataQueue::PingDataQueue( QObject *parent ) + : QObject( parent ) + , m_qhData() + , m_mutex() +{ + +} + +PingDataQueue::~PingDataQueue() +{ + +} + +void PingDataQueue::setData( PingData *data ) +{ + QMutexLocker lock( &m_mutex ); + if( nullptr != data ) + { + QUuid newID = QUuid::createUuid(); + m_qhData.insert( newID, data ); + emit signalNewDataReceived( newID ); + QCoreApplication::processEvents(); + } + else + { + LogWarning( "[PingDataQueue::setData]", QString( "There was an error storing data : %1" ).arg( data->hostName() ) ); + } +} + +void PingDataQueue::setData( const QString &host_name, const QStringList &data ) +{ + PingData *l_data = new PingData( host_name, data ); + this->setData( l_data ); +} + +int PingDataQueue::num_items() +{ + return m_qhData.count(); +} + +QList PingDataQueue::getIds() +{ + return m_qhData.keys(); +} + +PingData* PingDataQueue::getData( const QUuid &id, bool remove ) +{ + QMutexLocker lock( &m_mutex ); + if( remove ) + return m_qhData.take( id ); + else + return m_qhData.value( id ); +} diff --git a/src/pingdataqueue.h b/src/pingdataqueue.h new file mode 100644 index 0000000..03575eb --- /dev/null +++ b/src/pingdataqueue.h @@ -0,0 +1,70 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_PINGDATAQUEUE_H +#define OSDEV_COMPONENTS_PINGDATAQUEUE_H + +// Local +#include "pingdata.h" + +// Qt +#include +#include +#include +#include +#include +#include + +namespace osdev { +namespace components { + +class PingDataQueue : public QObject +{ + Q_OBJECT + +public: + PingDataQueue( QObject *parent = nullptr ); + + PingDataQueue( const PingDataQueue& ) = delete; + PingDataQueue( const PingDataQueue&& ) = delete; + PingDataQueue& operator=( const PingDataQueue& ) = delete; + PingDataQueue& operator=( const PingDataQueue&& ) = delete; + + virtual ~PingDataQueue(); + + void setData( PingData *data ); + void setData( const QString &host_name, const QStringList &data ); + QList getIds(); + PingData* getData( const QUuid &id, bool remove = false ); + int num_items(); + +signals: + void signalNewDataReceived( const QUuid &id ); + +private: + QHash m_qhData; + QMutex m_mutex; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_PINGDATAQUEUE_H */ diff --git a/src/pingmanager.cpp b/src/pingmanager.cpp new file mode 100644 index 0000000..2323fdb --- /dev/null +++ b/src/pingmanager.cpp @@ -0,0 +1,164 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "pingmanager.h" +#include "log.h" +#include "pingdata.h" + +#include + +using namespace osdev::components; + +PingManager::PingManager( int max_threads, QObject *parent ) + : QObject( parent ) + , m_qhThreads() + , m_pSharedBuffer() + , m_pHostBuffer() + , m_max_threads( max_threads ) +{ + m_pSharedBuffer = new PingDataQueue(); + if( nullptr != m_pSharedBuffer ) + { + connect( m_pSharedBuffer, &PingDataQueue::signalNewDataReceived, this, &PingManager::slotNewData ); + } + + m_pHostBuffer = new HostBuffer(); + + createThreads(); +} + +PingManager::~PingManager() +{ + // Cleanup the SharedBuffer + if( nullptr != m_pSharedBuffer ) + { + delete m_pSharedBuffer; + m_pSharedBuffer = nullptr; + } + + // Cleanup the HostBuffer + if( nullptr != m_pHostBuffer ) + { + delete m_pHostBuffer; + m_pHostBuffer = nullptr; + } +} + +void PingManager::createThreads() +{ + // Create all threads + for( int threadCounter = 0; threadCounter < m_max_threads; threadCounter++ ) + { + PingThread *pThread = new PingThread( m_pHostBuffer, m_pSharedBuffer ); + if( nullptr != pThread ) + { + connect( pThread, &PingThread::signalThreadFinished, this, &PingManager::slotThreadFinished ); + connect( this, &PingManager::signalStartThreads, pThread, &PingThread::slotStartThread ); + + m_qhThreads.append( pThread ); + } + else + { + LogError( "[PingManager::startPingTests]", QString( "Unable to start threadnumber : %1" ).arg( threadCounter ) ); + } + } + LogInfo( "[PingManager::createThreads]", QString( "%1 threads created and ready to run." ).arg( m_qhThreads.count() ) ); +} + +bool PingManager::startPingTests() +{ + bool bResult = false; + + if( m_pHostBuffer->count() > 0 ) + { + LogInfo( "[PingManager::startPingTests]", QString( "Signalling all threads to start checking hosts" ) ); + emit signalStartThreads(); + QCoreApplication::processEvents(); + + bResult &= true; + } + else + { + LogInfo( "[PingManager::startPingTests]", + QString( "No hosts to check." ) ); + } + + return bResult; +} + +int PingManager::addHosts( const QStringList &host_list ) +{ + if( nullptr == m_pHostBuffer ) + { + m_pHostBuffer = new HostBuffer(); + } + + m_pHostBuffer->addHosts( host_list ); + + return m_pHostBuffer->count(); +} + +void PingManager::slotNewData( const QUuid &id ) +{ + PingData* pData = m_pSharedBuffer->getData( id ); + if( nullptr != pData ) + { + QCoreApplication::processEvents(); + } +} + +void PingManager::slotThreadFinished( QThread *thread ) +{ + LogInfo( "[PingManager::slotThreadFinished]", QString( "Thread signalled finished : %1" ).arg( m_qhThreads.indexOf( thread ) ) ); + + bool running_threads = false; + + // Here we check if there are still running threads. If not, we signal the object + foreach( const auto cur_thread, m_qhThreads ) + { + running_threads &= cur_thread->isRunning(); + } + + if( !running_threads ) + { + LogInfo( "[PingManager::slotThreadFinished]", QString( "Signalling all Threads finished -> signalAllThreadsFinished()" ) ); + emit signalAllThreadsFinished(); + } + else + { + LogInfo( "[PingManager::slotThreadFinished]", QString( "Still running threads...()" ) ); + } +} + +QStringList PingManager::getActiveIPs() +{ + QStringList l_result; + + for( const auto id : m_pSharedBuffer->getIds() ) + { + if( m_pSharedBuffer->getData( id )->available() ) + { + l_result.append( m_pSharedBuffer->getData( id )->ipAddress() ); + } + } + + return l_result; +} diff --git a/src/pingmanager.h b/src/pingmanager.h new file mode 100644 index 0000000..faf7e54 --- /dev/null +++ b/src/pingmanager.h @@ -0,0 +1,81 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_PINGMANAGER_H +#define OSDEV_COMPONENTS_PINGMANAGER_H + +#include +#include +#include + +#include "pingdataqueue.h" +#include "pingthread.h" +#include "hostbuffer.h" + +namespace osdev { +namespace components { + +class PingManager : public QObject +{ + Q_OBJECT + +public: + PingManager( int max_threads = 10, QObject *parent = nullptr ); + + PingManager( const PingManager& ) = delete; + PingManager( const PingManager&& ) = delete; + PingManager& operator=( const PingManager& ) = delete; + PingManager& operator=( const PingManager&& ) = delete; + + virtual ~PingManager(); + + bool startPingTests(); + bool startPingTests( const QString &host ); + + int addHosts( const QStringList &host_list ); + + QStringList getActiveIPs(); + + + +public slots: + void slotNewData( const QUuid &id ); + void slotThreadFinished( QThread *thread ); + +signals: + void signalStartThreads(); + void signalAllThreadsFinished(); + +private: + void createThreads(); + +private: // Members ( Giggity! ) + QList m_qhThreads; + PingDataQueue *m_pSharedBuffer; + HostBuffer *m_pHostBuffer; + + int m_max_threads; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_PINGMANAGER_H */ diff --git a/src/pingrunner.cpp b/src/pingrunner.cpp new file mode 100644 index 0000000..65cb955 --- /dev/null +++ b/src/pingrunner.cpp @@ -0,0 +1,92 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +// local +#include "pingrunner.h" +#include "log.h" +#include "compatqt514.h" + +// Qt +#include +#include + +// std +#include + +using namespace osdev::components; + +PingRunner::PingRunner( QObject *parent ) + : QObject( parent ) + , m_shrBuf( nullptr ) + , m_host_to_ping( QString() ) +{ + this->connectProcess(); +} + +PingRunner::PingRunner( PingDataQueue *l_shrBuffer, QObject *parent ) + : QObject( parent ) + , m_shrBuf( l_shrBuffer ) + , m_host_to_ping( QString() ) +{ + this->connectProcess(); +} + +void PingRunner::connectProcess() +{ + // Use old style connect construction, as overloading in Signal / Slots + // doesn't work without lambda functions. +} + +PingRunner::~PingRunner() +{ +} + +void PingRunner::setSharedBuffer( PingDataQueue *l_shrBuffer ) +{ + if( nullptr != l_shrBuffer ) + { + m_shrBuf = l_shrBuffer; + } +} + +void PingRunner::startPing( const QString &host_to_ping ) +{ + QProcess m_process; + + m_host_to_ping = host_to_ping; + QStringList arguments( { "-n", "-q", "-c", "10", "-i", "0.2", "-W", "2", host_to_ping } ); + + m_process.start( QString( "ping" ), arguments, QIODevice::ReadOnly ); + if( m_process.waitForFinished() ) + { + LogInfo( "[PingRunner::startPing]", QString( "Process for host %1 done...." ).arg( m_host_to_ping ) ) + QStringList result; + result << m_host_to_ping; + result << QString( m_process.readAll() ).split( "\n", SKIP_EMPTY_PARTS ); + + if( nullptr != m_shrBuf ) + { + m_shrBuf->setData( m_host_to_ping, result ); + } + } + LogInfo( "[PingRunner::startPing]", QString( "Closing Communicationchannels for host %1 process" ).arg( m_host_to_ping ) ) + m_process.close(); +} diff --git a/src/pingrunner.h b/src/pingrunner.h new file mode 100644 index 0000000..9523035 --- /dev/null +++ b/src/pingrunner.h @@ -0,0 +1,64 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_PINGER_H +#define OSDEV_COMPONENTS_PINGER_H + +#include +#include +#include + +#include "pingdataqueue.h" + +namespace osdev { +namespace components { + +class PingRunner : public QObject +{ + Q_OBJECT + +public: + PingRunner( QObject *parent = nullptr ); + PingRunner( PingDataQueue *l_shrBuffer, QObject *parent = nullptr ); + virtual ~PingRunner(); + + PingRunner( const PingRunner& ) = delete; + PingRunner( const PingRunner&& ) = delete; + PingRunner& operator=( const PingRunner& ) = delete; + PingRunner& operator=( const PingRunner&& ) = delete; + + void startPing( const QString &host_to_ping ); + + void setSharedBuffer( PingDataQueue *l_shrBuffer ); + +private: + void connectProcess(); + +private: + PingDataQueue *m_shrBuf; + QString m_host_to_ping; + +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_PINGER_H */ diff --git a/src/pingthread.cpp b/src/pingthread.cpp new file mode 100644 index 0000000..a826b73 --- /dev/null +++ b/src/pingthread.cpp @@ -0,0 +1,80 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "pingthread.h" +#include "pingrunner.h" + +#include "log.h" + +#include + +using namespace osdev::components; + +PingThread::PingThread( HostBuffer *input_queue, PingDataQueue *ping_buffer ) + : m_pHostBuffer( input_queue ) + , m_pQueue( ping_buffer ) +{ + connect( this, &PingThread::finished, this, &PingThread::slotThreadFinished ); +} + +PingThread::~PingThread() +{ + +} + +void PingThread::setPriority( QThread::Priority priority ) +{ + if( this->isRunning() ) + { + QThread::setPriority( priority ); + } +} + +void PingThread::run() +{ + LogInfo( "[PingThread::run]", QString( "Starting thread..." ) ) + QString host_name; + PingRunner l_pingRunner( m_pQueue ); + + while( m_pHostBuffer->count() > 0 ) + { + LogInfo( "[PingThread::run]", QString( "m_pHostBuffer has %1 entries.." ).arg( m_pHostBuffer->count() ) ) + host_name = m_pHostBuffer->getHost(); + LogInfo( "[PingThread::run]", QString( "m_pHostBuffer has %1 entries after getHost()" ).arg( m_pHostBuffer->count() ) ) + if( !host_name.isEmpty() ) + { + LogInfo( "[PingThread::run]", QString( "[OK] Alive thread for host %1 created." ).arg( host_name ) ); + l_pingRunner.startPing( host_name ); + } + } + LogInfo( "[PingThread::run]", QString( "m_pHostBuffer is empty..." ) ) +} + +void PingThread::slotStartThread() +{ + LogInfo( "[PingThread::slotStartThread]", QString( "Running thread." ) ); + this->start(); +} + +void PingThread::slotThreadFinished() +{ + emit signalThreadFinished( this ); +} diff --git a/src/pingthread.h b/src/pingthread.h new file mode 100644 index 0000000..638210c --- /dev/null +++ b/src/pingthread.h @@ -0,0 +1,67 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_PINGTHREAD_H +#define OSDEV_COMPONENTS_PINGTHREAD_H + +#include "pingdataqueue.h" +#include "hostbuffer.h" + +#include +#include + +namespace osdev { +namespace components { + +class PingThread : public QThread +{ + Q_OBJECT + +public: + PingThread( HostBuffer *input_buffer, PingDataQueue *ping_queue ); + + PingThread( const PingThread& ) = delete; + PingThread( const PingThread&& ) = delete; + PingThread& operator=( const PingThread& ) = delete; + PingThread& operator=( const PingThread&& ) = delete; + + virtual ~PingThread(); + + void setPriority( QThread::Priority priority = QThread::NormalPriority ); + void run() override; + +public slots: + void slotStartThread(); + + void slotThreadFinished(); + +signals: + void signalThreadFinished( QThread *thread ); + +private: + HostBuffer *m_pHostBuffer; + PingDataQueue *m_pQueue; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_PINGTHREAD */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3635320 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.0) +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) + +include(projectheader) +project_header(test_logutils) + +include_directories( SYSTEM + ${CMAKE_CURRENT_SOURCE_DIR}/../../src +) + +include(compiler) +set(SRC_LIST +) + +# add_executable( ${PROJECT_NAME} +# ${SRC_LIST} +# ) + +# target_link_libraries( +# ${PROJECT_NAME} +# ) + +# set_target_properties( ${PROJECT_NAME} PROPERTIES +# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +# LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib +# ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archive +# ) + +# include(installation) +# install_application() -- libgit2 0.21.4