mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +08:00
refs #568, allow to read incremental data based on timestamp
* signatures with QDateTime * flags
This commit is contained in:
@@ -24,11 +24,14 @@ namespace BlackCore
|
||||
connect(&m_watchdogTimer, &QTimer::timeout, this, &CDatabaseReader::ps_watchdog);
|
||||
}
|
||||
|
||||
void CDatabaseReader::readInBackgroundThread(CEntityFlags::Entity entities)
|
||||
void CDatabaseReader::readInBackgroundThread(CEntityFlags::Entity entities, const QDateTime &newerThan)
|
||||
{
|
||||
if (isAbandoned()) { return; }
|
||||
this->m_watchdogTimer.stop();
|
||||
bool s = QMetaObject::invokeMethod(this, "ps_read", Q_ARG(BlackMisc::Network::CEntityFlags::Entity, entities));
|
||||
// ps_read is implemented in the derived classes
|
||||
bool s = QMetaObject::invokeMethod(this, "ps_read",
|
||||
Q_ARG(BlackMisc::Network::CEntityFlags::Entity, entities),
|
||||
Q_ARG(QDateTime, newerThan));
|
||||
Q_ASSERT_X(s, Q_FUNC_INFO, "Invoke failed");
|
||||
Q_UNUSED(s);
|
||||
}
|
||||
@@ -63,6 +66,7 @@ namespace BlackCore
|
||||
datastoreResponse.jsonArray = responseObject["data"].toArray();
|
||||
QString ts(responseObject["latest"].toString());
|
||||
datastoreResponse.updated = ts.isEmpty() ? QDateTime::currentDateTimeUtc() : CDatastoreUtility::parseTimestamp(ts);
|
||||
datastoreResponse.restricted = responseObject["restricted"].toBool();
|
||||
}
|
||||
return datastoreResponse;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include <QDateTime>
|
||||
#include <QTimer>
|
||||
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
//! Specialized version of threaded reader for DB data
|
||||
@@ -32,8 +31,9 @@ namespace BlackCore
|
||||
//! Response from our database
|
||||
struct JsonDatastoreResponse
|
||||
{
|
||||
QJsonArray jsonArray; //!< JSON array data
|
||||
QDateTime updated; //!< when updated
|
||||
QJsonArray jsonArray; //!< JSON array data
|
||||
QDateTime updated; //!< when was the latest updated?
|
||||
bool restricted = false; //!< restricted reponse, only data changed
|
||||
|
||||
//! Any data?
|
||||
bool isEmpty() const { return jsonArray.isEmpty(); }
|
||||
@@ -50,12 +50,15 @@ namespace BlackCore
|
||||
//! Is response newer?
|
||||
bool isNewer(qint64 mSecsSinceEpoch) const { return updated.toMSecsSinceEpoch() > mSecsSinceEpoch; }
|
||||
|
||||
//! Incremental data
|
||||
bool isRestricted() const { return restricted; }
|
||||
|
||||
//! Implicit conversion
|
||||
operator QJsonArray() const { return jsonArray; }
|
||||
};
|
||||
|
||||
//! Start reading in own thread
|
||||
void readInBackgroundThread(BlackMisc::Network::CEntityFlags::Entity entities);
|
||||
void readInBackgroundThread(BlackMisc::Network::CEntityFlags::Entity entities, const QDateTime &newerThan);
|
||||
|
||||
//! Can connect to DB
|
||||
//! \threadsafe
|
||||
@@ -69,7 +72,7 @@ namespace BlackCore
|
||||
protected:
|
||||
BlackMisc::Network::CUrl m_watchdogUrl; //!< URL for checking if alive
|
||||
QTimer m_watchdogTimer { this }; //!< Timer for watchdog (DB available?)
|
||||
QString m_watchdogMessage; //!< Returned status message
|
||||
QString m_watchdogMessage; //!< Returned status message from watchdog
|
||||
bool m_canConnect = false; //!< Successful connection?
|
||||
mutable QReadWriteLock m_watchdogLock; //!< Lock
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ namespace BlackCore
|
||||
return m_countries.size();
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_read(BlackMisc::Network::CEntityFlags::Entity entities)
|
||||
void CIcaoDataReader::ps_read(BlackMisc::Network::CEntityFlags::Entity entities, const QDateTime &newerThan)
|
||||
{
|
||||
this->threadAssertCheck(); // runs in background thread
|
||||
Q_ASSERT(this->m_networkManagerAircraft);
|
||||
@@ -139,9 +139,10 @@ namespace BlackCore
|
||||
CEntityFlags::Entity entitiesTriggered = CEntityFlags::NoEntity;
|
||||
if (entities.testFlag(CEntityFlags::AircraftIcaoEntity))
|
||||
{
|
||||
QUrl url(getAircraftIcaoUrl());
|
||||
CUrl url(getAircraftIcaoUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull()) { url.appendQuery("newer=" + newerThan.toString(Qt::ISODate)); }
|
||||
QNetworkRequest requestAircraft(CNetworkUtils::getNetworkRequest(url));
|
||||
this->m_networkManagerAircraft->get(requestAircraft);
|
||||
entitiesTriggered |= CEntityFlags::AircraftIcaoEntity;
|
||||
@@ -154,9 +155,10 @@ namespace BlackCore
|
||||
|
||||
if (entities.testFlag(CEntityFlags::AirlineIcaoEntity))
|
||||
{
|
||||
QUrl url(getAirlineIcaoUrl());
|
||||
CUrl url(getAirlineIcaoUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull()) { url.appendQuery("newer=" + newerThan.toString(Qt::ISODate)); }
|
||||
QNetworkRequest requestAirline(CNetworkUtils::getNetworkRequest(url));
|
||||
this->m_networkManagerAirlines->get(requestAirline);
|
||||
entitiesTriggered |= CEntityFlags::AirlineIcaoEntity;
|
||||
@@ -169,9 +171,10 @@ namespace BlackCore
|
||||
|
||||
if (entities.testFlag(CEntityFlags::CountryEntity))
|
||||
{
|
||||
QUrl url(getCountryUrl());
|
||||
CUrl url(getCountryUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull()) { url.appendQuery("newer=" + newerThan.toString(Qt::ISODate)); }
|
||||
QNetworkRequest requestCountry(CNetworkUtils::getNetworkRequest(url));
|
||||
this->m_networkManagerCountries->get(requestCountry);
|
||||
entitiesTriggered |= CEntityFlags::CountryEntity;
|
||||
|
||||
@@ -120,7 +120,7 @@ namespace BlackCore
|
||||
void ps_parseCountryData(QNetworkReply *nwReply);
|
||||
|
||||
//! Read / re-read data file
|
||||
void ps_read(BlackMisc::Network::CEntityFlags::Entity entities);
|
||||
void ps_read(BlackMisc::Network::CEntityFlags::Entity entities, const QDateTime &newerThan);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *m_networkManagerAircraft = nullptr;
|
||||
|
||||
@@ -126,6 +126,18 @@ namespace BlackCore
|
||||
return m_models.size();
|
||||
}
|
||||
|
||||
QList<int> CModelDataReader::getModelDbKeys() const
|
||||
{
|
||||
QReadLocker l(&m_lockModels);
|
||||
return m_models.toDbKeyList();
|
||||
}
|
||||
|
||||
QStringList CModelDataReader::getModelStrings() const
|
||||
{
|
||||
QReadLocker l(&m_lockModels);
|
||||
return m_models.getModelStrings(false);
|
||||
}
|
||||
|
||||
bool CModelDataReader::areAllDataRead() const
|
||||
{
|
||||
return
|
||||
@@ -134,7 +146,7 @@ namespace BlackCore
|
||||
getDistributorsCount() > 0;
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_read(CEntityFlags::Entity entity)
|
||||
void CModelDataReader::ps_read(CEntityFlags::Entity entity, const QDateTime &newerThan)
|
||||
{
|
||||
this->threadAssertCheck();
|
||||
Q_ASSERT(this->m_networkManagerLivery);
|
||||
@@ -147,6 +159,11 @@ namespace BlackCore
|
||||
CUrl url(getLiveryUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull())
|
||||
{
|
||||
const QString tss(newerThan.toString(Qt::ISODate));
|
||||
url.appendQuery("newer=" + tss);
|
||||
}
|
||||
QNetworkRequest requestLivery(url);
|
||||
CNetworkUtils::ignoreSslVerification(requestLivery);
|
||||
this->m_networkManagerLivery->get(requestLivery);
|
||||
@@ -163,6 +180,11 @@ namespace BlackCore
|
||||
CUrl url(getDistributorUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull())
|
||||
{
|
||||
const QString tss(newerThan.toString(Qt::ISODate));
|
||||
url.appendQuery("newer=" + tss);
|
||||
}
|
||||
QNetworkRequest requestDistributor(url);
|
||||
CNetworkUtils::ignoreSslVerification(requestDistributor);
|
||||
this->m_networkManagerDistributor->get(requestDistributor);
|
||||
@@ -179,6 +201,11 @@ namespace BlackCore
|
||||
CUrl url(getModelUrl());
|
||||
if (!url.isEmpty())
|
||||
{
|
||||
if (!newerThan.isNull())
|
||||
{
|
||||
const QString tss(newerThan.toString(Qt::ISODate));
|
||||
url.appendQuery("newer=" + tss);
|
||||
}
|
||||
QNetworkRequest requestModel(url);
|
||||
CNetworkUtils::ignoreSslVerification(requestModel);
|
||||
this->m_networkManagerModel->get(requestModel);
|
||||
@@ -202,13 +229,25 @@ namespace BlackCore
|
||||
// required to use delete later as object is created in a different thread
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
CDatabaseReader::JsonDatastoreResponse res = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (res.isEmpty())
|
||||
{
|
||||
emit dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CLiveryList liveries = CLiveryList::fromDatabaseJson(array);
|
||||
|
||||
// get all or incremental set of distributor
|
||||
CLiveryList liveries;
|
||||
if (res.isRestricted())
|
||||
{
|
||||
// create full list if it was just incremental
|
||||
liveries = this->getLiveries();
|
||||
liveries.replaceOrAddObjectsByKey(CLiveryList::fromDatabaseJson(res));
|
||||
}
|
||||
else
|
||||
{
|
||||
liveries = CLiveryList::fromDatabaseJson(res);
|
||||
}
|
||||
|
||||
// this part needs to be synchronized
|
||||
int n = liveries.size();
|
||||
@@ -216,8 +255,10 @@ namespace BlackCore
|
||||
QWriteLocker wl(&this->m_lockLivery);
|
||||
this->m_liveries = liveries;
|
||||
}
|
||||
|
||||
// never emit when lock is held -> deadlock
|
||||
emit dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFinished, n);
|
||||
emit dataRead(CEntityFlags::LiveryEntity,
|
||||
res.isRestricted() ? CEntityFlags::ReadFinishedRestricted : CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::LiveryEntity) << urlString;
|
||||
}
|
||||
|
||||
@@ -225,13 +266,25 @@ namespace BlackCore
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
CDatabaseReader::JsonDatastoreResponse res = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (res.isEmpty())
|
||||
{
|
||||
emit dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CDistributorList distributors = CDistributorList::fromDatabaseJson(array);
|
||||
|
||||
// get all or incremental set of distributor
|
||||
CDistributorList distributors;
|
||||
if (res.isRestricted())
|
||||
{
|
||||
// create full list if it was just incremental
|
||||
distributors = this->getDistributors();
|
||||
distributors.replaceOrAddObjectsByKey(CDistributorList::fromDatabaseJson(res));
|
||||
}
|
||||
else
|
||||
{
|
||||
distributors = CDistributorList::fromDatabaseJson(res);
|
||||
}
|
||||
|
||||
// this part needs to be synchronized
|
||||
int n = distributors.size();
|
||||
@@ -239,7 +292,8 @@ namespace BlackCore
|
||||
QWriteLocker wl(&this->m_lockDistributor);
|
||||
this->m_distributors = distributors;
|
||||
}
|
||||
emit dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFinished, n);
|
||||
emit dataRead(CEntityFlags::DistributorEntity,
|
||||
res.isRestricted() ? CEntityFlags::ReadFinishedRestricted : CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::DistributorEntity) << urlString;
|
||||
}
|
||||
|
||||
@@ -247,21 +301,35 @@ namespace BlackCore
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
CDatabaseReader::JsonDatastoreResponse res = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (res.isEmpty())
|
||||
{
|
||||
emit dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CAircraftModelList models = CAircraftModelList::fromDatabaseJson(array);
|
||||
|
||||
// this part needs to be synchronized
|
||||
// get all or incremental set of models
|
||||
CAircraftModelList models;
|
||||
if (res.isRestricted())
|
||||
{
|
||||
// create full list if it was just incremental
|
||||
models = this->getModels();
|
||||
models.replaceOrAddObjectsByKey(CAircraftModelList::fromDatabaseJson(res));
|
||||
}
|
||||
else
|
||||
{
|
||||
models = CAircraftModelList::fromDatabaseJson(res);
|
||||
}
|
||||
|
||||
// syncronized update
|
||||
int n = models.size();
|
||||
{
|
||||
QWriteLocker wl(&this->m_lockModels);
|
||||
this->m_models = models;
|
||||
}
|
||||
emit dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFinished, n);
|
||||
|
||||
emit dataRead(CEntityFlags::ModelEntity,
|
||||
res.isRestricted() ? CEntityFlags::ReadFinishedRestricted : CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::ModelEntity) << urlString;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,6 +88,14 @@ namespace BlackCore
|
||||
//! \threadsafe
|
||||
int getModelsCount() const;
|
||||
|
||||
//! Get model keys
|
||||
//! \threadsafe
|
||||
QList<int> getModelDbKeys() const;
|
||||
|
||||
//! Get model keys
|
||||
//! \threadsafe
|
||||
QStringList getModelStrings() const;
|
||||
|
||||
//! All data read?
|
||||
//! \threadsafe
|
||||
bool areAllDataRead() const;
|
||||
@@ -116,7 +124,7 @@ namespace BlackCore
|
||||
void ps_parseModelData(QNetworkReply *nwReply);
|
||||
|
||||
//! Read / re-read data file
|
||||
void ps_read(BlackMisc::Network::CEntityFlags::Entity entity = BlackMisc::Network::CEntityFlags::DistributorLiveryModel);
|
||||
void ps_read(BlackMisc::Network::CEntityFlags::Entity entity = BlackMisc::Network::CEntityFlags::DistributorLiveryModel, const QDateTime &newerThan = QDateTime());
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *m_networkManagerLivery = nullptr;
|
||||
|
||||
@@ -177,7 +177,7 @@ namespace BlackCore
|
||||
return false;
|
||||
}
|
||||
|
||||
CEntityFlags::Entity CWebDataServices::triggerRead(CEntityFlags::Entity whatToRead)
|
||||
CEntityFlags::Entity CWebDataServices::triggerRead(CEntityFlags::Entity whatToRead, const QDateTime &newerThan)
|
||||
{
|
||||
m_initialRead = true;
|
||||
CEntityFlags::Entity triggeredRead = CEntityFlags::NoEntity;
|
||||
@@ -213,7 +213,7 @@ namespace BlackCore
|
||||
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity) || whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity) || whatToRead.testFlag(CEntityFlags::CountryEntity))
|
||||
{
|
||||
CEntityFlags::Entity icaoEntities = whatToRead & CEntityFlags::AllIcaoAndCountries;
|
||||
m_icaoDataReader->readInBackgroundThread(icaoEntities);
|
||||
m_icaoDataReader->readInBackgroundThread(icaoEntities, newerThan);
|
||||
triggeredRead |= icaoEntities;
|
||||
}
|
||||
}
|
||||
@@ -223,7 +223,7 @@ namespace BlackCore
|
||||
if (whatToRead.testFlag(CEntityFlags::LiveryEntity) || whatToRead.testFlag(CEntityFlags::DistributorEntity) || whatToRead.testFlag(CEntityFlags::ModelEntity))
|
||||
{
|
||||
CEntityFlags::Entity modelEntities = whatToRead & CEntityFlags::DistributorLiveryModel;
|
||||
m_modelDataReader->readInBackgroundThread(modelEntities);
|
||||
m_modelDataReader->readInBackgroundThread(modelEntities, newerThan);
|
||||
triggeredRead |= modelEntities;
|
||||
}
|
||||
}
|
||||
@@ -296,6 +296,18 @@ namespace BlackCore
|
||||
return 0;
|
||||
}
|
||||
|
||||
QList<int> CWebDataServices::getModelDbKeys() const
|
||||
{
|
||||
if (m_modelDataReader) { return m_modelDataReader->getModelDbKeys(); }
|
||||
return QList<int>();
|
||||
}
|
||||
|
||||
QStringList CWebDataServices::getModelStrings() const
|
||||
{
|
||||
if (m_modelDataReader) { return m_modelDataReader->getModelStrings(); }
|
||||
return QStringList();
|
||||
}
|
||||
|
||||
CAircraftModelList CWebDataServices::getModelsForAircraftDesignatorAndLiveryCombinedCode(const QString &aircraftDesignator, const QString &combinedCode) const
|
||||
{
|
||||
if (m_modelDataReader) { return m_modelDataReader->getModelsForAircraftDesignatorAndLiveryCombinedCode(aircraftDesignator, combinedCode); }
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace BlackCore
|
||||
|
||||
//! \copydoc BlackMisc::Network::IWebDataServicesProvider::triggerRead
|
||||
//! \ingroup webdatareaderprovider
|
||||
virtual BlackMisc::Network::CEntityFlags::Entity triggerRead(BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
virtual BlackMisc::Network::CEntityFlags::Entity triggerRead(BlackMisc::Network::CEntityFlags::Entity whatToRead, const QDateTime &newerThan = QDateTime()) override;
|
||||
|
||||
//! \copydoc BlackMisc::Network::IWebDataServicesProvider::getVatsimFsdServers
|
||||
//! \ingroup webdatareaderprovider
|
||||
@@ -145,6 +145,14 @@ namespace BlackCore
|
||||
//! \ingroup webdatareaderprovider
|
||||
virtual int getModelsCount() const override;
|
||||
|
||||
//! \copydoc BlackMisc::Network::IWebDataServicesProvider::getModelDbKeys
|
||||
//! \ingroup webdatareaderprovider
|
||||
virtual QList<int> getModelDbKeys() const override;
|
||||
|
||||
//! \copydoc BlackMisc::Network::IWebDataServicesProvider::getModelStrings
|
||||
//! \ingroup webdatareaderprovider
|
||||
virtual QStringList getModelStrings() const override;
|
||||
|
||||
//! \copydoc BlackMisc::Network::IWebDataServicesProvider::getModelsForAircraftDesignatorAndLiveryCombinedCode
|
||||
//! \ingroup webdatareaderprovider
|
||||
virtual BlackMisc::Simulation::CAircraftModelList getModelsForAircraftDesignatorAndLiveryCombinedCode(const QString &aircraftDesignator, const QString &combinedCode) const override;
|
||||
|
||||
Reference in New Issue
Block a user