mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
refs #380, use CWorker instead of QConcurrent for threaded reader (bookings/VATSIM file)
This commit is contained in:
@@ -123,8 +123,8 @@ namespace BlackCore
|
||||
|
||||
void CContextNetwork::gracefulShutdown()
|
||||
{
|
||||
if (this->m_vatsimBookingReader) { this->m_vatsimBookingReader->quit(); }
|
||||
if (this->m_vatsimDataFileReader) { this->m_vatsimDataFileReader->quit(); }
|
||||
if (this->m_vatsimBookingReader) { this->m_vatsimBookingReader->requestStop(); this->m_vatsimBookingReader->quit(); }
|
||||
if (this->m_vatsimDataFileReader) { this->m_vatsimDataFileReader->requestStop(); this->m_vatsimDataFileReader->quit(); }
|
||||
if (this->isConnected()) { this->disconnectFromNetwork(); }
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include "vatsimbookingreader.h"
|
||||
|
||||
#include <QtXml/QDomElement>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
@@ -23,46 +22,46 @@ using namespace BlackMisc::Network;
|
||||
namespace BlackCore
|
||||
{
|
||||
CVatsimBookingReader::CVatsimBookingReader(QObject *owner, const QString &url) :
|
||||
CThreadedReader(owner),
|
||||
m_serviceUrl(url), m_networkManager(nullptr)
|
||||
CThreadedReader(owner, "CVatsimBookingReader"),
|
||||
m_serviceUrl(url)
|
||||
{
|
||||
this->m_networkManager = new QNetworkAccessManager(this);
|
||||
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CVatsimBookingReader::ps_loadFinished);
|
||||
this->connect(this->m_updateTimer, &QTimer::timeout, this, &CVatsimBookingReader::read);
|
||||
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CVatsimBookingReader::ps_parseBookings);
|
||||
this->connect(this->m_updateTimer, &QTimer::timeout, this, &CVatsimBookingReader::ps_read);
|
||||
}
|
||||
|
||||
void CVatsimBookingReader::read()
|
||||
void CVatsimBookingReader::readInBackgroundThread()
|
||||
{
|
||||
if (QThread::currentThread() == QObject::thread())
|
||||
{
|
||||
ps_read();
|
||||
}
|
||||
else
|
||||
{
|
||||
bool s = QMetaObject::invokeMethod(this, "ps_read", Qt::BlockingQueuedConnection);
|
||||
Q_ASSERT(s);
|
||||
Q_UNUSED(s);
|
||||
}
|
||||
}
|
||||
|
||||
void CVatsimBookingReader::ps_read()
|
||||
{
|
||||
this->threadAssertCheck();
|
||||
QUrl url(this->m_serviceUrl);
|
||||
if (url.isEmpty()) return;
|
||||
Q_ASSERT(this->m_networkManager);
|
||||
QNetworkRequest request(url);
|
||||
QNetworkReply *reply = this->m_networkManager->get(request);
|
||||
this->setPendingNetworkReply(reply);
|
||||
this->m_networkManager->get(request);
|
||||
}
|
||||
|
||||
/*
|
||||
* Bookings read from XML
|
||||
*/
|
||||
void CVatsimBookingReader::ps_loadFinished(QNetworkReply *nwReply)
|
||||
{
|
||||
this->setPendingNetworkReply(nullptr);
|
||||
if (!this->isFinished())
|
||||
{
|
||||
QFuture<void> f = QtConcurrent::run(this, &CVatsimBookingReader::parseBookings, nwReply);
|
||||
this->setPendingFuture(f);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse bookings
|
||||
*/
|
||||
void CVatsimBookingReader::parseBookings(QNetworkReply *nwReplyPtr)
|
||||
void CVatsimBookingReader::ps_parseBookings(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
// wrap pointer, make sure any exit cleans up reply
|
||||
// required to use delete later as object is created in a different thread
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
|
||||
this->threadAssertCheck();
|
||||
|
||||
// Worker thread, make sure to write no members here!
|
||||
if (this->isFinished())
|
||||
{
|
||||
@@ -103,7 +102,7 @@ namespace BlackCore
|
||||
if (this->isFinished())
|
||||
{
|
||||
CLogMessage(this).debug() << Q_FUNC_INFO;
|
||||
CLogMessage(this).info("terminated booking parsing process"); // for users
|
||||
CLogMessage(this).info("Terminated booking parsing process"); // for users
|
||||
return; // stop, terminate straight away, ending thread
|
||||
}
|
||||
|
||||
@@ -148,8 +147,8 @@ namespace BlackCore
|
||||
}
|
||||
// time checks
|
||||
QDateTime now = QDateTime::currentDateTimeUtc();
|
||||
if (now.msecsTo(bookedStation.getBookedUntilUtc()) < (1000 * 60 * 15)) continue; // until n mins in past
|
||||
if (now.msecsTo(bookedStation.getBookedFromUtc()) > (1000 * 60 * 60 * 24)) continue; // to far in the future, n hours
|
||||
if (now.msecsTo(bookedStation.getBookedUntilUtc()) < (1000 * 60 * 15)) { continue; } // until n mins in past
|
||||
if (now.msecsTo(bookedStation.getBookedFromUtc()) > (1000 * 60 * 60 * 24)) { continue; } // to far in the future, n hours
|
||||
bookedStation.setController(user);
|
||||
bookedStations.push_back(bookedStation);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace BlackCore
|
||||
/*!
|
||||
* Read bookings from VATSIM
|
||||
*/
|
||||
class CVatsimBookingReader : public BlackMisc::CThreadedReader<void>
|
||||
class CVatsimBookingReader : public BlackMisc::CThreadedReader
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -34,21 +34,21 @@ namespace BlackCore
|
||||
explicit CVatsimBookingReader(QObject *owner, const QString &url);
|
||||
|
||||
//! Read / re-read bookings
|
||||
void read();
|
||||
void readInBackgroundThread();
|
||||
|
||||
private slots:
|
||||
//! Bookings have been read
|
||||
void ps_loadFinished(QNetworkReply *nwReply);
|
||||
//! \threadsafe
|
||||
void ps_parseBookings(QNetworkReply *nwReply);
|
||||
|
||||
//! Do reading
|
||||
void ps_read();
|
||||
|
||||
private:
|
||||
QString m_serviceUrl; /*!< URL of the service */
|
||||
QNetworkAccessManager *m_networkManager;
|
||||
QNetworkAccessManager *m_networkManager = nullptr;
|
||||
|
||||
//! Parse received bookings
|
||||
//! \threadsafe
|
||||
void parseBookings(QNetworkReply *nwReplyPtr);
|
||||
|
||||
signals:
|
||||
signals:
|
||||
//! Bookings have been read and converted to BlackMisc::Aviation::CAtcStationList
|
||||
void dataRead(const BlackMisc::Aviation::CAtcStationList &bookedStations);
|
||||
};
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "vatsimdatafilereader.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
@@ -26,30 +25,12 @@ using namespace BlackMisc::PhysicalQuantities;
|
||||
namespace BlackCore
|
||||
{
|
||||
CVatsimDataFileReader::CVatsimDataFileReader(QObject *owner, const QStringList &urls) :
|
||||
CThreadedReader(owner),
|
||||
m_serviceUrls(urls), m_currentUrlIndex(0), m_networkManager(nullptr)
|
||||
CThreadedReader(owner, "CVatsimDataFileReader"),
|
||||
m_serviceUrls(urls), m_currentUrlIndex(0)
|
||||
{
|
||||
this->m_networkManager = new QNetworkAccessManager(this);
|
||||
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CVatsimDataFileReader::ps_loadFinished);
|
||||
this->connect(this->m_updateTimer, &QTimer::timeout, this, &CVatsimDataFileReader::read);
|
||||
}
|
||||
|
||||
void CVatsimDataFileReader::read()
|
||||
{
|
||||
if (this->m_serviceUrls.isEmpty()) return;
|
||||
|
||||
// round robin for load distribution
|
||||
this->m_currentUrlIndex++;
|
||||
if (this->m_serviceUrls.size() >= this->m_currentUrlIndex) this->m_currentUrlIndex = 0;
|
||||
|
||||
// remark: Don't use QThread to run network operations in the background
|
||||
// see http://qt-project.org/doc/qt-4.7/qnetworkaccessmanager.html
|
||||
QUrl url(this->m_serviceUrls.at(this->m_currentUrlIndex));
|
||||
if (url.isEmpty()) return;
|
||||
Q_ASSERT(this->m_networkManager);
|
||||
QNetworkRequest request(url);
|
||||
QNetworkReply *r = this->m_networkManager->get(request);
|
||||
this->setPendingNetworkReply(r);
|
||||
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CVatsimDataFileReader::ps_parseVatsimFile);
|
||||
this->connect(this->m_updateTimer, &QTimer::timeout, this, &CVatsimDataFileReader::ps_read);
|
||||
}
|
||||
|
||||
CAircraftList CVatsimDataFileReader::getAircraft() const
|
||||
@@ -143,34 +124,51 @@ namespace BlackCore
|
||||
return users;
|
||||
}
|
||||
|
||||
/*
|
||||
* Data file read from XML
|
||||
*/
|
||||
void CVatsimDataFileReader::ps_loadFinished(QNetworkReply *nwReply)
|
||||
void CVatsimDataFileReader::readInBackgroundThread()
|
||||
{
|
||||
this->setPendingNetworkReply(nullptr);
|
||||
if (!this->isFinished())
|
||||
if (QThread::currentThread() == QObject::thread())
|
||||
{
|
||||
QFuture<void> f = QtConcurrent::run(this, &CVatsimDataFileReader::parseVatsimFileInBackground, nwReply);
|
||||
this->setPendingFuture(f);
|
||||
ps_read();
|
||||
}
|
||||
else
|
||||
{
|
||||
bool s = QMetaObject::invokeMethod(this, "ps_read", Qt::BlockingQueuedConnection);
|
||||
Q_ASSERT(s);
|
||||
Q_UNUSED(s);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Data file read from XML
|
||||
* Example: http://info.vroute.net/vatsim-data.txt
|
||||
*/
|
||||
void CVatsimDataFileReader::parseVatsimFileInBackground(QNetworkReply *nwReplyPtr)
|
||||
void CVatsimDataFileReader::ps_read()
|
||||
{
|
||||
this->threadAssertCheck();
|
||||
if (this->m_serviceUrls.isEmpty()) { return; }
|
||||
|
||||
// round robin for load distribution
|
||||
this->m_currentUrlIndex++;
|
||||
if (this->m_serviceUrls.size() >= this->m_currentUrlIndex) this->m_currentUrlIndex = 0;
|
||||
|
||||
// remark: Don't use QThread to run network operations in the background
|
||||
// see http://qt-project.org/doc/qt-4.7/qnetworkaccessmanager.html
|
||||
QUrl url(this->m_serviceUrls.at(this->m_currentUrlIndex));
|
||||
if (url.isEmpty()) { return; }
|
||||
Q_ASSERT(this->m_networkManager);
|
||||
QNetworkRequest request(url);
|
||||
this->m_networkManager->get(request);
|
||||
}
|
||||
|
||||
void CVatsimDataFileReader::ps_parseVatsimFile(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
// wrap pointer, make sure any exit cleans up reply
|
||||
// required to use delete later as object is created in a different thread
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
|
||||
this->threadAssertCheck();
|
||||
|
||||
// Worker thread, make sure to write only synced here!
|
||||
if (this->isFinished())
|
||||
{
|
||||
CLogMessage(this).debug() << Q_FUNC_INFO;
|
||||
CLogMessage(this).info("terminated VATSIM file parsing process"); // for users
|
||||
CLogMessage(this).info("Terminated VATSIM file parsing process"); // for users
|
||||
return; // stop, terminate straight away, ending thread
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace BlackCore
|
||||
/*!
|
||||
* Read bookings from VATSIM
|
||||
*/
|
||||
class CVatsimDataFileReader : public BlackMisc::CThreadedReader<void>
|
||||
class CVatsimDataFileReader : public BlackMisc::CThreadedReader
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -38,9 +38,6 @@ namespace BlackCore
|
||||
//! Constructor
|
||||
explicit CVatsimDataFileReader(QObject *owner, const QStringList &urls);
|
||||
|
||||
//! Read / re-read data file
|
||||
void read();
|
||||
|
||||
//! Get aircrafts
|
||||
//! \threadsafe
|
||||
BlackMisc::Aviation::CAircraftList getAircraft() const;
|
||||
@@ -93,14 +90,20 @@ namespace BlackCore
|
||||
//! \threadsafe
|
||||
void updateWithVatsimDataFileData(BlackMisc::Aviation::CAircraft &aircraftToBeUdpated) const;
|
||||
|
||||
//! Start reading in own thread
|
||||
void readInBackgroundThread();
|
||||
|
||||
private slots:
|
||||
//! Data have been read
|
||||
void ps_loadFinished(QNetworkReply *nwReply);
|
||||
//! Data have been read, parse VATSIM file
|
||||
void ps_parseVatsimFile(QNetworkReply *nwReply);
|
||||
|
||||
//! Read / re-read data file
|
||||
void ps_read();
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *m_networkManager = nullptr;
|
||||
QStringList m_serviceUrls; /*!< URL of the service */
|
||||
int m_currentUrlIndex;
|
||||
QNetworkAccessManager *m_networkManager;
|
||||
BlackMisc::Network::CServerList m_voiceServers;
|
||||
BlackMisc::Network::CServerList m_fsdServers;
|
||||
BlackMisc::Aviation::CAtcStationList m_atcStations;
|
||||
@@ -123,9 +126,6 @@ namespace BlackCore
|
||||
//! Get current section
|
||||
static Section currentLineToSection(const QString ¤tLine);
|
||||
|
||||
//! Parse the VATSIM data file in backgroun
|
||||
void parseVatsimFileInBackground(QNetworkReply *nwReplyPtr);
|
||||
|
||||
signals:
|
||||
//! Data have been read
|
||||
void dataRead();
|
||||
|
||||
Reference in New Issue
Block a user