refactor: Move period usage of CThreadedReader in subclass

This commit is contained in:
Lars Toenning
2025-06-09 13:53:33 +02:00
parent b553ef77c6
commit c45f0fac2d
9 changed files with 100 additions and 55 deletions

View File

@@ -237,6 +237,8 @@ add_library(core SHARED
swiftcoreexport.h
threadedreader.cpp
threadedreader.h
threadedreaderperiodic.cpp
threadedreaderperiodic.h
webdataservices.cpp
webdataservices.h
webdataservicesms.cpp

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 <QObject>
#include <QString>
#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

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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();
}

View File

@@ -9,7 +9,7 @@
#include <QObject>
#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