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