diff --git a/src/blackcore/threadedreader.cpp b/src/blackcore/threadedreader.cpp index 2b4828e32..2d43278ec 100644 --- a/src/blackcore/threadedreader.cpp +++ b/src/blackcore/threadedreader.cpp @@ -85,17 +85,33 @@ namespace BlackCore void CThreadedReader::setInterval(int updatePeriodMs) { Q_ASSERT(this->m_updateTimer); - bool s; - if (updatePeriodMs < 1) + QTimer::singleShot(0, this, [this, updatePeriodMs] { - s = QMetaObject::invokeMethod(m_updateTimer, "stop"); - } - else + QWriteLocker wl(&this->m_lock); + if (updatePeriodMs < 1) + { + this->m_updateTimer->stop(); + } + else { + this->m_updateTimer->start(updatePeriodMs); + } + }); + } + + bool CThreadedReader::didContentChange(const QString &content, int startPosition) + { + uint oldHash = 0; { - s = QMetaObject::invokeMethod(m_updateTimer, "start", Q_ARG(int, updatePeriodMs)); + QReadLocker rl(&this->m_lock); + oldHash = this->m_contentHash; } - Q_ASSERT_X(s, Q_FUNC_INFO, "Failed invoke"); - Q_UNUSED(s); + uint newHash = qHash(startPosition < 0 ? content : content.mid(startPosition)); + if (oldHash == newHash) { return false; } + { + QWriteLocker wl(&this->m_lock); + this->m_contentHash = newHash; + } + return true; } void CThreadedReader::ps_toggleInterval() diff --git a/src/blackcore/threadedreader.h b/src/blackcore/threadedreader.h index f5d020b85..671ef8829 100644 --- a/src/blackcore/threadedreader.h +++ b/src/blackcore/threadedreader.h @@ -94,8 +94,13 @@ namespace BlackCore //! \threadsafe void setInterval(int updatePeriodMs); + //! Stores new content hash and returns if content changed (based on hash value + //! \threadsafe + bool didContentChange(const QString &content, int startPosition = -1); + private: QDateTime m_updateTimestamp; //!< when file/resource was read + uint m_contentHash = 0; //!< has of the content given QMetaObject::Connection m_toggleConnection; //!< connection to switch interval from initial to periodic private slots: diff --git a/src/blackcore/vatsim/vatsimbookingreader.cpp b/src/blackcore/vatsim/vatsimbookingreader.cpp index 89b458705..679d4e90c 100644 --- a/src/blackcore/vatsim/vatsimbookingreader.cpp +++ b/src/blackcore/vatsim/vatsimbookingreader.cpp @@ -83,22 +83,22 @@ namespace BlackCore this->threadAssertCheck(); - // Worker thread, make sure to write no members here! + // Worker thread, make sure to write no members here od do it threadsafe if (this->isAbandoned()) { CLogMessage(this).debug() << Q_FUNC_INFO; - CLogMessage(this).info("terminated booking parsing process"); // for users + CLogMessage(this).info("Terminated booking parsing process"); return; // stop, terminate straight away, ending thread } if (nwReply->error() == QNetworkReply::NoError) { static const QString timestampFormat("yyyy-MM-dd HH:mm:ss"); - QString xmlData = nwReply->readAll(); + const QString xmlData = nwReply->readAll(); nwReply->close(); // close asap - QDomDocument doc; - QDateTime updateTimestamp = QDateTime::currentDateTimeUtc(); + QDomDocument doc; + QDateTime updateTimestamp = QDateTime::currentDateTimeUtc(); // default if (doc.setContent(xmlData)) { QDomNode timestamp = doc.elementsByTagName("timestamp").at(0); @@ -112,6 +112,14 @@ namespace BlackCore updateTimestamp = QDateTime::fromString(ts, timestampFormat); updateTimestamp.setTimeSpec(Qt::UTC); if (this->getUpdateTimestamp() == updateTimestamp) return; // nothing to do + + // save parsing and all follow up actions if nothing changed + bool changed = this->didContentChange(xmlData, xmlData.indexOf("")); + if (!changed) + { + CLogMessage(this).info("Bookings unchanged, skipped"); + return; // stop, terminate straight away, ending thread + } } QDomNode atc = doc.elementsByTagName("atcs").at(0); diff --git a/src/blackcore/vatsim/vatsimdatafilereader.cpp b/src/blackcore/vatsim/vatsimdatafilereader.cpp index 281d67e2f..cb5bd4517 100644 --- a/src/blackcore/vatsim/vatsimdatafilereader.cpp +++ b/src/blackcore/vatsim/vatsimdatafilereader.cpp @@ -211,7 +211,7 @@ namespace BlackCore if (this->isAbandoned()) { CLogMessage(this).debug() << Q_FUNC_INFO; - CLogMessage(this).info("Terminated VATSIM file parsing process"); // for users + CLogMessage(this).info("Terminated VATSIM file parsing process"); return; // stop, terminate straight away, ending thread } @@ -221,7 +221,13 @@ namespace BlackCore const QString dataFileData = nwReply->readAll(); nwReply->close(); // close asap - if (dataFileData.isEmpty()) return; + if (dataFileData.isEmpty()) { return; } + // Quick check by hash + if (!this->didContentChange(dataFileData)) + { + CLogMessage(this).info("VATSIM file has same content, skipped"); + return; + } const QStringList lines = dataFileData.split(QRegExp("[\r\n]"), QString::SkipEmptyParts); if (lines.isEmpty()) { return; } @@ -240,7 +246,7 @@ namespace BlackCore if (this->isAbandoned()) { CLogMessage(this).debug() << Q_FUNC_INFO; - CLogMessage(this).info("Terminated booking parsing process"); // for users + CLogMessage(this).info("Terminated VATSIM file parsing process"); // for users return; // stop, terminate straight away, ending thread } @@ -347,7 +353,11 @@ namespace BlackCore updateTimestampFromFile = QDateTime::fromString(dts, "yyyyMMddHHmmss"); updateTimestampFromFile.setOffsetFromUtc(0); bool alreadyRead = (updateTimestampFromFile == this->getUpdateTimestamp()); - if (alreadyRead) { return; }// still same data, terminate + if (alreadyRead) + { + CLogMessage(this).info("VATSIM file has same timestamp, skipped"); + return; + } } } break; @@ -388,10 +398,15 @@ namespace BlackCore this->m_atcStations = atcStations; this->m_voiceCapabilities = voiceCapabilities; CVatsimSetup vs(this->m_lastGoodSetup.getThreadLocal()); - vs.setVoiceServers(voiceServers); - vs.setFsdServers(fsdServers); - vs.setUtcTimestamp(updateTimestampFromFile); - this->m_lastGoodSetup.set(vs); + + // check if we need to save in cache + if (vs.getVoiceServers() != voiceServers || vs.getFsdServers() != fsdServers) + { + vs.setVoiceServers(voiceServers); + vs.setFsdServers(fsdServers); + vs.setUtcTimestamp(updateTimestampFromFile); + this->m_lastGoodSetup.set(vs); + } } // warnings, if required diff --git a/src/blackcore/vatsim/vatsimmetarreader.cpp b/src/blackcore/vatsim/vatsimmetarreader.cpp index e9105918c..d579cfd05 100644 --- a/src/blackcore/vatsim/vatsimmetarreader.cpp +++ b/src/blackcore/vatsim/vatsimmetarreader.cpp @@ -109,8 +109,15 @@ namespace BlackCore { QString metarData = nwReply->readAll(); nwReply->close(); // close asap - CMetarSet metars; + // Quick check by hash + if (!this->didContentChange(metarData)) + { + CLogMessage(this).info("METAR file has same content, skipped"); + return; + } + + CMetarSet metars; QString invalidMetars; int invalidLineCount = 0; QTextStream lineReader(&metarData);