Improved VATSIM status file reading

This commit is contained in:
Klaus Basan
2019-09-10 21:58:39 +02:00
committed by Mat Sutcliffe
parent e8b14c435f
commit b3945adc98
4 changed files with 44 additions and 12 deletions

View File

@@ -45,11 +45,11 @@ namespace BlackCore
void CGlobalSetup::initDefaultValues() void CGlobalSetup::initDefaultValues()
{ {
m_mappingMinimumVersion = CBuildConfig::getVersionString(); m_mappingMinimumVersion = CBuildConfig::getVersionString();
m_dbRootDirectoryUrl = CUrl("https://datastore.swift-project.org/"); m_dbRootDirectoryUrl = CUrl("https://datastore.swift-project.org/");
m_vatsimBookingsUrl = CUrl("http://vatbook.euroutepro.com/xml2.php"); m_vatsimBookingsUrl = CUrl("http://vatbook.euroutepro.com/xml2.php");
m_vatsimMetarsUrls = CUrlList({"http://metar.vatsim.net/metar.php"}); m_vatsimMetarsUrls = CUrlList({"http://metar.vatsim.net/metar.php"});
m_vatsimStatusFileUrls = CUrlList({ "https://status.vatsim.net" }); m_vatsimStatusFileUrls = CUrlList({ "https://status.vatsim.net" });
m_vatsimDataFileUrls = CUrlList({ "http://info.vroute.net/vatsim-data.txt" }); m_vatsimDataFileUrls = CUrlList({ "http://info.vroute.net/vatsim-data.txt" });
m_sharedUrls = CUrlList( m_sharedUrls = CUrlList(
{ {
"https://datastore.swift-project.net/shared/", "https://datastore.swift-project.net/shared/",

View File

@@ -71,10 +71,28 @@ namespace BlackCore
if (!this->isInternetAccessible("No network/internet access, cannot read VATSIM status file")) { return; } if (!this->isInternetAccessible("No network/internet access, cannot read VATSIM status file")) { return; }
Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing application"); Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing application");
CFailoverUrlList urls(sApp->getGlobalSetup().getVatsimStatusFileUrls()); const CUrlList urls(sApp->getGlobalSetup().getVatsimStatusFileUrls());
const CUrl url(urls.obtainNextWorkingUrl(true)); // random working URL const CUrl url = urls.getRandomUrl();
if (url.isEmpty()) { return; } if (url.isEmpty()) { return; }
CLogMessage(this).info(u"Trigger read of VATSIM status file from '%1'") << url.toQString(true);
this->getFromNetworkAndLog(url, { this, &CVatsimStatusFileReader::parseVatsimFile}); this->getFromNetworkAndLog(url, { this, &CVatsimStatusFileReader::parseVatsimFile});
if (urls.size() < 2) { return; }
const CUrl secondary = urls.getRandomWithout(url);
if (secondary.isEmpty()) { return; }
constexpr int DelayMs = 5000;
const QPointer<CVatsimStatusFileReader> myself(this);
QTimer::singleShot(DelayMs, this, [ = ]
{
if (!myself) { return; }
const CVatsimSetup vs(m_lastGoodSetup.get());
if (vs.getTimeDifferenceToNowMs() > 2 * DelayMs)
{
// not yet read
this->getFromNetworkAndLog(url, { this, &CVatsimStatusFileReader::parseVatsimFile});
}
});
} }
void CVatsimStatusFileReader::parseVatsimFile(QNetworkReply *nwReplyPtr) void CVatsimStatusFileReader::parseVatsimFile(QNetworkReply *nwReplyPtr)
@@ -93,6 +111,8 @@ namespace BlackCore
} }
this->logNetworkReplyReceived(nwReplyPtr); this->logNetworkReplyReceived(nwReplyPtr);
const QString urlString = nwReply->url().toString();
if (nwReply->error() == QNetworkReply::NoError) if (nwReply->error() == QNetworkReply::NoError)
{ {
const QString dataFileData = nwReply->readAll(); const QString dataFileData = nwReply->readAll();
@@ -148,12 +168,15 @@ namespace BlackCore
// cache itself is thread safe, avoid writing with unchanged data // cache itself is thread safe, avoid writing with unchanged data
CVatsimSetup vs(m_lastGoodSetup.get()); CVatsimSetup vs(m_lastGoodSetup.get());
const bool changed = vs.setUrls(dataFileUrls, serverFileUrls, metarFileUrls); const bool changed = vs.setUrls(dataFileUrls, serverFileUrls, metarFileUrls);
if (changed) vs.setUtcTimestamp(QDateTime::currentDateTime());
const CStatusMessage cacheMsg = m_lastGoodSetup.set(vs);
if (cacheMsg.isFailure()) { CLogMessage::preformatted(cacheMsg); }
else
{ {
vs.setUtcTimestamp(QDateTime::currentDateTime()); CLogMessage(this).info(u"Read VATSIM status file from '%1', %2 data file URLs, %3 server file URLs, %4 METAR file URLs")
const CStatusMessage cacheMsg = m_lastGoodSetup.set(vs); << urlString << dataFileUrls.size() << serverFileUrls.size() << metarFileUrls.size();
if (cacheMsg.isFailure()) { CLogMessage::preformatted(cacheMsg); }
} }
Q_UNUSED(changed);
// data read finished // data read finished
emit this->dataFileRead(lines.count()); emit this->dataFileRead(lines.count());
@@ -162,7 +185,7 @@ namespace BlackCore
else else
{ {
// network error // network error
CLogMessage(this).warning(u"Reading VATSIM status file failed '%1' '%2'") << nwReply->errorString() << nwReply->url().toString(); CLogMessage(this).warning(u"Reading VATSIM status file failed '%1' '%2'") << nwReply->errorString() << urlString;
nwReply->abort(); nwReply->abort();
emit this->dataRead(CEntityFlags::VatsimStatusFile, CEntityFlags::ReadFailed, 0); emit this->dataRead(CEntityFlags::VatsimStatusFile, CEntityFlags::ReadFailed, 0);
} }

View File

@@ -61,6 +61,12 @@ namespace BlackMisc
return CUrl(); return CUrl();
} }
CUrl CUrlList::getRandomWithout(const CUrl &exclude) const
{
const CUrlList excludes({ exclude });
return this->getRandomWithout(excludes);
}
CUrl CUrlList::getRandomWithout(const CUrlList &exclude) const CUrl CUrlList::getRandomWithout(const CUrlList &exclude) const
{ {
CUrlList copy(*this); CUrlList copy(*this);

View File

@@ -49,6 +49,9 @@ namespace BlackMisc
//! Random location for distributed load, tested //! Random location for distributed load, tested
CUrl getRandomWorkingUrl(int maxTrials = 2, int timeoutMs = -1) const; CUrl getRandomWorkingUrl(int maxTrials = 2, int timeoutMs = -1) const;
//! Random location for distributed load
CUrl getRandomWithout(const CUrl &exclude) const;
//! Random location for distributed load //! Random location for distributed load
CUrl getRandomWithout(const CUrlList &exclude) const; CUrl getRandomWithout(const CUrlList &exclude) const;