mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
refactor: Move period usage of CThreadedReader in subclass
This commit is contained in:
@@ -237,6 +237,8 @@ add_library(core SHARED
|
|||||||
swiftcoreexport.h
|
swiftcoreexport.h
|
||||||
threadedreader.cpp
|
threadedreader.cpp
|
||||||
threadedreader.h
|
threadedreader.h
|
||||||
|
threadedreaderperiodic.cpp
|
||||||
|
threadedreaderperiodic.h
|
||||||
webdataservices.cpp
|
webdataservices.cpp
|
||||||
webdataservices.h
|
webdataservices.h
|
||||||
webdataservicesms.cpp
|
webdataservicesms.cpp
|
||||||
|
|||||||
@@ -28,11 +28,7 @@ namespace swift::core
|
|||||||
return cats;
|
return cats;
|
||||||
}
|
}
|
||||||
|
|
||||||
CThreadedReader::CThreadedReader(QObject *owner, const QString &name) : CContinuousWorker(owner, name)
|
CThreadedReader::CThreadedReader(QObject *owner, const QString &name) : CContinuousWorker(owner, name) {}
|
||||||
{
|
|
||||||
connect(&m_updateTimer, &QTimer::timeout, this, &CThreadedReader::doWork);
|
|
||||||
m_updateTimer.setSingleShot(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
qint64 CThreadedReader::lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const
|
qint64 CThreadedReader::lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const
|
||||||
{
|
{
|
||||||
@@ -51,12 +47,6 @@ namespace swift::core
|
|||||||
m_updateTimestamp = updateTimestamp;
|
m_updateTimestamp = updateTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CThreadedReader::startReader()
|
|
||||||
{
|
|
||||||
Q_ASSERT(m_initialTime > 0);
|
|
||||||
QTimer::singleShot(m_initialTime, this, [=] { this->doWork(); });
|
|
||||||
}
|
|
||||||
|
|
||||||
CUrlLogList CThreadedReader::getUrlLogList() const
|
CUrlLogList CThreadedReader::getUrlLogList() const
|
||||||
{
|
{
|
||||||
QReadLocker l(&m_lock);
|
QReadLocker l(&m_lock);
|
||||||
@@ -96,29 +86,6 @@ namespace swift::core
|
|||||||
Q_ASSERT_X(this->thread() == QThread::currentThread(), Q_FUNC_INFO, "Wrong object thread");
|
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
|
bool CThreadedReader::doWorkCheck() const
|
||||||
{
|
{
|
||||||
// sApp->hasWebDataServices() cannot be used, as some readers are already used during init phase
|
// sApp->hasWebDataServices() cannot be used, as some readers are already used during init phase
|
||||||
|
|||||||
@@ -61,10 +61,6 @@ namespace swift::core
|
|||||||
//! \threadsafe
|
//! \threadsafe
|
||||||
swift::misc::network::CUrlLogList getReadLog() const;
|
swift::misc::network::CUrlLogList getReadLog() const;
|
||||||
|
|
||||||
//! Starts the reader
|
|
||||||
//! \threadsafe
|
|
||||||
void startReader();
|
|
||||||
|
|
||||||
//! Used in unit test
|
//! Used in unit test
|
||||||
//! \remark needs to be done before started in different thread
|
//! \remark needs to be done before started in different thread
|
||||||
void markAsUsedInUnitTest() { m_unitTest = true; }
|
void markAsUsedInUnitTest() { m_unitTest = true; }
|
||||||
@@ -97,12 +93,6 @@ namespace swift::core
|
|||||||
//! \threadsafe
|
//! \threadsafe
|
||||||
bool didContentChange(const QString &content, int startPosition = -1);
|
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.?
|
//! Still enabled etc.?
|
||||||
//! \threadsafe under normal conditions
|
//! \threadsafe under normal conditions
|
||||||
bool doWorkCheck() const;
|
bool doWorkCheck() const;
|
||||||
@@ -124,13 +114,8 @@ namespace swift::core
|
|||||||
static void logInconsistentData(const swift::misc::CStatusMessage &msg, const char *funcInfo = nullptr);
|
static void logInconsistentData(const swift::misc::CStatusMessage &msg, const char *funcInfo = nullptr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Trigger doWorkImpl
|
|
||||||
void doWork();
|
|
||||||
|
|
||||||
static constexpr int OutdatedPendingCallMs = 30 * 1000; //!< when is a call considered "outdated"
|
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
|
QDateTime m_updateTimestamp; //!< when file/resource was read
|
||||||
size_t m_contentHash = 0; //!< has of the content given
|
size_t m_contentHash = 0; //!< has of the content given
|
||||||
std::atomic_bool m_markedAsFailed { false }; //!< marker if reading failed
|
std::atomic_bool m_markedAsFailed { false }; //!< marker if reading failed
|
||||||
|
|||||||
43
src/core/threadedreaderperiodic.cpp
Normal file
43
src/core/threadedreaderperiodic.cpp
Normal 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
|
||||||
48
src/core/threadedreaderperiodic.h
Normal file
48
src/core/threadedreaderperiodic.h
Normal 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
|
||||||
@@ -50,7 +50,7 @@ using namespace swift::core::data;
|
|||||||
|
|
||||||
namespace swift::core::vatsim
|
namespace swift::core::vatsim
|
||||||
{
|
{
|
||||||
CVatsimDataFileReader::CVatsimDataFileReader(QObject *owner) : CThreadedReader(owner, "CVatsimDataFileReader")
|
CVatsimDataFileReader::CVatsimDataFileReader(QObject *owner) : CThreadedReaderPeriodic(owner, "CVatsimDataFileReader")
|
||||||
{
|
{
|
||||||
this->reloadSettings();
|
this->reloadSettings();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
#include "core/data/vatsimsetup.h"
|
#include "core/data/vatsimsetup.h"
|
||||||
#include "core/swiftcoreexport.h"
|
#include "core/swiftcoreexport.h"
|
||||||
#include "core/threadedreader.h"
|
#include "core/threadedreaderperiodic.h"
|
||||||
#include "misc/aviation/aircrafticaocode.h"
|
#include "misc/aviation/aircrafticaocode.h"
|
||||||
#include "misc/aviation/airlineicaocode.h"
|
#include "misc/aviation/airlineicaocode.h"
|
||||||
#include "misc/aviation/atcstationlist.h"
|
#include "misc/aviation/atcstationlist.h"
|
||||||
@@ -34,7 +34,7 @@ namespace swift::misc::simulation
|
|||||||
namespace swift::core::vatsim
|
namespace swift::core::vatsim
|
||||||
{
|
{
|
||||||
//! Read vatsim data file
|
//! Read vatsim data file
|
||||||
class SWIFT_CORE_EXPORT CVatsimDataFileReader : public CThreadedReader
|
class SWIFT_CORE_EXPORT CVatsimDataFileReader : public CThreadedReaderPeriodic
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ using namespace swift::core::data;
|
|||||||
|
|
||||||
namespace swift::core::vatsim
|
namespace swift::core::vatsim
|
||||||
{
|
{
|
||||||
CVatsimMetarReader::CVatsimMetarReader(QObject *owner) : CThreadedReader(owner, "CVatsimMetarReader")
|
CVatsimMetarReader::CVatsimMetarReader(QObject *owner) : CThreadedReaderPeriodic(owner, "CVatsimMetarReader")
|
||||||
{
|
{
|
||||||
this->reloadSettings();
|
this->reloadSettings();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include "core/swiftcoreexport.h"
|
#include "core/swiftcoreexport.h"
|
||||||
#include "core/threadedreader.h"
|
#include "core/threadedreaderperiodic.h"
|
||||||
#include "misc/aviation/airporticaocode.h"
|
#include "misc/aviation/airporticaocode.h"
|
||||||
#include "misc/network/entityflags.h"
|
#include "misc/network/entityflags.h"
|
||||||
#include "misc/weather/metar.h"
|
#include "misc/weather/metar.h"
|
||||||
@@ -21,7 +21,7 @@ class QNetworkReply;
|
|||||||
namespace swift::core::vatsim
|
namespace swift::core::vatsim
|
||||||
{
|
{
|
||||||
//! Read METARs from 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
|
Q_OBJECT
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user