From c45f0fac2dcae7a0e1db192858fa6b5d32e892fd Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Mon, 9 Jun 2025 13:53:33 +0200 Subject: [PATCH] refactor: Move period usage of CThreadedReader in subclass --- src/core/CMakeLists.txt | 2 + src/core/threadedreader.cpp | 35 +---------------- src/core/threadedreader.h | 15 -------- src/core/threadedreaderperiodic.cpp | 43 +++++++++++++++++++++ src/core/threadedreaderperiodic.h | 48 ++++++++++++++++++++++++ src/core/vatsim/vatsimdatafilereader.cpp | 2 +- src/core/vatsim/vatsimdatafilereader.h | 4 +- src/core/vatsim/vatsimmetarreader.cpp | 2 +- src/core/vatsim/vatsimmetarreader.h | 4 +- 9 files changed, 100 insertions(+), 55 deletions(-) create mode 100644 src/core/threadedreaderperiodic.cpp create mode 100644 src/core/threadedreaderperiodic.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c19bb7f89..d8b3478ca 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -237,6 +237,8 @@ add_library(core SHARED swiftcoreexport.h threadedreader.cpp threadedreader.h + threadedreaderperiodic.cpp + threadedreaderperiodic.h webdataservices.cpp webdataservices.h webdataservicesms.cpp diff --git a/src/core/threadedreader.cpp b/src/core/threadedreader.cpp index 3e4d0da3a..0ee29c5a3 100644 --- a/src/core/threadedreader.cpp +++ b/src/core/threadedreader.cpp @@ -28,11 +28,7 @@ namespace swift::core return cats; } - CThreadedReader::CThreadedReader(QObject *owner, const QString &name) : CContinuousWorker(owner, name) - { - connect(&m_updateTimer, &QTimer::timeout, this, &CThreadedReader::doWork); - m_updateTimer.setSingleShot(true); - } + CThreadedReader::CThreadedReader(QObject *owner, const QString &name) : CContinuousWorker(owner, name) {} qint64 CThreadedReader::lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const { @@ -51,12 +47,6 @@ namespace swift::core m_updateTimestamp = updateTimestamp; } - void CThreadedReader::startReader() - { - Q_ASSERT(m_initialTime > 0); - QTimer::singleShot(m_initialTime, this, [=] { this->doWork(); }); - } - CUrlLogList CThreadedReader::getUrlLogList() const { QReadLocker l(&m_lock); @@ -96,29 +86,6 @@ namespace swift::core Q_ASSERT_X(this->thread() == QThread::currentThread(), Q_FUNC_INFO, "Wrong object thread"); } - void CThreadedReader::setInitialAndPeriodicTime(int initialTime, int periodicTime) - { - m_initialTime = initialTime; - m_periodicTime = periodicTime; - - // if timer is active start with delta time - // remark: will be reset in doWork - if (m_updateTimer.isActive()) - { - const int oldPeriodicTime = m_updateTimer.interval(); - const int delta = m_periodicTime - oldPeriodicTime + m_updateTimer.remainingTime(); - m_updateTimer.start(qMax(delta, 0)); - } - } - - void CThreadedReader::doWork() - { - if (!doWorkCheck()) { return; } - this->doWorkImpl(); - Q_ASSERT(m_periodicTime > 0); - m_updateTimer.start(m_periodicTime); // restart - } - bool CThreadedReader::doWorkCheck() const { // sApp->hasWebDataServices() cannot be used, as some readers are already used during init phase diff --git a/src/core/threadedreader.h b/src/core/threadedreader.h index 0016d4ea9..43b2331c1 100644 --- a/src/core/threadedreader.h +++ b/src/core/threadedreader.h @@ -61,10 +61,6 @@ namespace swift::core //! \threadsafe swift::misc::network::CUrlLogList getReadLog() const; - //! Starts the reader - //! \threadsafe - void startReader(); - //! Used in unit test //! \remark needs to be done before started in different thread void markAsUsedInUnitTest() { m_unitTest = true; } @@ -97,12 +93,6 @@ namespace swift::core //! \threadsafe bool didContentChange(const QString &content, int startPosition = -1); - //! Set initial and periodic times - void setInitialAndPeriodicTime(int initialTime, int periodicTime); - - //! This method does the actual work in the derived class - virtual void doWorkImpl() {} - //! Still enabled etc.? //! \threadsafe under normal conditions bool doWorkCheck() const; @@ -124,13 +114,8 @@ namespace swift::core static void logInconsistentData(const swift::misc::CStatusMessage &msg, const char *funcInfo = nullptr); private: - //! Trigger doWorkImpl - void doWork(); - static constexpr int OutdatedPendingCallMs = 30 * 1000; //!< when is a call considered "outdated" - int m_initialTime = -1; //!< Initial start delay - int m_periodicTime = -1; //!< Periodic time after which the task is repeated QDateTime m_updateTimestamp; //!< when file/resource was read size_t m_contentHash = 0; //!< has of the content given std::atomic_bool m_markedAsFailed { false }; //!< marker if reading failed diff --git a/src/core/threadedreaderperiodic.cpp b/src/core/threadedreaderperiodic.cpp new file mode 100644 index 000000000..0c4bf3f0f --- /dev/null +++ b/src/core/threadedreaderperiodic.cpp @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: Copyright (C) 2025 swift Project Community / Contributors +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1 + +#include "core/threadedreaderperiodic.h" + +namespace swift::core +{ + CThreadedReaderPeriodic::CThreadedReaderPeriodic(QObject *owner, const QString &name) : CThreadedReader(owner, name) + { + connect(&m_updateTimer, &QTimer::timeout, this, &CThreadedReaderPeriodic::doWork); + m_updateTimer.setSingleShot(true); + } + + void CThreadedReaderPeriodic::startReader() + { + Q_ASSERT(m_initialTime > 0); + QTimer::singleShot(m_initialTime, this, [=] { this->doWork(); }); + } + + void CThreadedReaderPeriodic::setInitialAndPeriodicTime(int initialTime, int periodicTime) + { + m_initialTime = initialTime; + m_periodicTime = periodicTime; + + // if timer is active start with delta time + // remark: will be reset in doWork + if (m_updateTimer.isActive()) + { + const int oldPeriodicTime = m_updateTimer.interval(); + const int delta = m_periodicTime - oldPeriodicTime + m_updateTimer.remainingTime(); + m_updateTimer.start(qMax(delta, 0)); + } + } + + void CThreadedReaderPeriodic::doWork() + { + if (!doWorkCheck()) { return; } + this->doWorkImpl(); + Q_ASSERT(m_periodicTime > 0); + m_updateTimer.start(m_periodicTime); // restart + } + +} // namespace swift::core diff --git a/src/core/threadedreaderperiodic.h b/src/core/threadedreaderperiodic.h new file mode 100644 index 000000000..7ca6d95b1 --- /dev/null +++ b/src/core/threadedreaderperiodic.h @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: Copyright (C) 2025 swift Project Community / Contributors +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1 + +//! \file + +#ifndef SWIFT_CORE_THREADED_READER_PERIODIC_H +#define SWIFT_CORE_THREADED_READER_PERIODIC_H + +#include +#include + +#include "core/swiftcoreexport.h" +#include "core/threadedreader.h" + +namespace swift::core +{ + //! Periodically executes doWorkImpl() in a separate thread + class SWIFT_CORE_EXPORT CThreadedReaderPeriodic : public swift::core::CThreadedReader + { + Q_OBJECT + public: + //! Destructor + ~CThreadedReaderPeriodic() override = default; + + //! Starts the reader + //! \threadsafe + void startReader(); + + protected: + //! Constructor + CThreadedReaderPeriodic(QObject *owner, const QString &name); + + //! This method does the actual work in the derived class + virtual void doWorkImpl() = 0; + + //! Set initial and periodic times + void setInitialAndPeriodicTime(int initialTime, int periodicTime); + + private: + //! Trigger doWorkImpl + void doWork(); + + int m_initialTime = -1; //!< Initial start delay + int m_periodicTime = -1; //!< Periodic time after which the task is repeated + }; +} // namespace swift::core + +#endif // SWIFT_CORE_THREADED_READER_PERIODIC_H \ No newline at end of file diff --git a/src/core/vatsim/vatsimdatafilereader.cpp b/src/core/vatsim/vatsimdatafilereader.cpp index 8f341779a..3de5fee10 100644 --- a/src/core/vatsim/vatsimdatafilereader.cpp +++ b/src/core/vatsim/vatsimdatafilereader.cpp @@ -50,7 +50,7 @@ using namespace swift::core::data; namespace swift::core::vatsim { - CVatsimDataFileReader::CVatsimDataFileReader(QObject *owner) : CThreadedReader(owner, "CVatsimDataFileReader") + CVatsimDataFileReader::CVatsimDataFileReader(QObject *owner) : CThreadedReaderPeriodic(owner, "CVatsimDataFileReader") { this->reloadSettings(); } diff --git a/src/core/vatsim/vatsimdatafilereader.h b/src/core/vatsim/vatsimdatafilereader.h index c892808d8..ae6692c07 100644 --- a/src/core/vatsim/vatsimdatafilereader.h +++ b/src/core/vatsim/vatsimdatafilereader.h @@ -13,7 +13,7 @@ #include "core/data/vatsimsetup.h" #include "core/swiftcoreexport.h" -#include "core/threadedreader.h" +#include "core/threadedreaderperiodic.h" #include "misc/aviation/aircrafticaocode.h" #include "misc/aviation/airlineicaocode.h" #include "misc/aviation/atcstationlist.h" @@ -34,7 +34,7 @@ namespace swift::misc::simulation namespace swift::core::vatsim { //! Read vatsim data file - class SWIFT_CORE_EXPORT CVatsimDataFileReader : public CThreadedReader + class SWIFT_CORE_EXPORT CVatsimDataFileReader : public CThreadedReaderPeriodic { Q_OBJECT diff --git a/src/core/vatsim/vatsimmetarreader.cpp b/src/core/vatsim/vatsimmetarreader.cpp index 28e42244b..8a094c4f0 100644 --- a/src/core/vatsim/vatsimmetarreader.cpp +++ b/src/core/vatsim/vatsimmetarreader.cpp @@ -30,7 +30,7 @@ using namespace swift::core::data; namespace swift::core::vatsim { - CVatsimMetarReader::CVatsimMetarReader(QObject *owner) : CThreadedReader(owner, "CVatsimMetarReader") + CVatsimMetarReader::CVatsimMetarReader(QObject *owner) : CThreadedReaderPeriodic(owner, "CVatsimMetarReader") { this->reloadSettings(); } diff --git a/src/core/vatsim/vatsimmetarreader.h b/src/core/vatsim/vatsimmetarreader.h index 50b6b0c7a..c7934e3de 100644 --- a/src/core/vatsim/vatsimmetarreader.h +++ b/src/core/vatsim/vatsimmetarreader.h @@ -9,7 +9,7 @@ #include #include "core/swiftcoreexport.h" -#include "core/threadedreader.h" +#include "core/threadedreaderperiodic.h" #include "misc/aviation/airporticaocode.h" #include "misc/network/entityflags.h" #include "misc/weather/metar.h" @@ -21,7 +21,7 @@ class QNetworkReply; namespace swift::core::vatsim { //! Read METARs from VATSIM - class SWIFT_CORE_EXPORT CVatsimMetarReader : public swift::core::CThreadedReader + class SWIFT_CORE_EXPORT CVatsimMetarReader : public swift::core::CThreadedReaderPeriodic { Q_OBJECT