From d7e5b5206c473023e2be2f8ff8de80ce7245a7c2 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 27 Feb 2016 22:26:06 +0100 Subject: [PATCH] refs #485, adjusted setup reader * log categories as discussed * made some functions thread safe --- src/blackcore/setupreader.cpp | 44 +++++++++++++++++++++++------------ src/blackcore/setupreader.h | 41 ++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/blackcore/setupreader.cpp b/src/blackcore/setupreader.cpp index 6d1f51bd1..1f26a87e1 100644 --- a/src/blackcore/setupreader.cpp +++ b/src/blackcore/setupreader.cpp @@ -52,16 +52,16 @@ namespace BlackCore { // initialized by local file for testing emit this->setupSynchronized(true); - return CStatusMessage(cats(), CStatusMessage::SeverityInfo, "Using local bootstrap file: " + this->m_localSetupFileValue); + return CStatusMessage(this, CStatusMessage::SeverityInfo, "Using local bootstrap file: " + this->m_localSetupFileValue); } else if (this->m_bootstrapMode == CacheOnly) { m_setup.synchronize(); - CGlobalSetup currentSetup = m_setup.get(); + CGlobalSetup currentSetup = m_setup.getCopy(); this->m_updateInfoUrls = currentSetup.updateInfoFileUrls(); emit this->setupSynchronized(true); emit this->updateInfoSynchronized(true); - return CStatusMessage(cats(), CStatusMessage::SeverityInfo, "Cache only setup, using it as it is"); + return CStatusMessage(this, CStatusMessage::SeverityInfo, "Cache only setup, using it as it is"); } else { @@ -74,7 +74,7 @@ namespace BlackCore // if ever loaded add those URLs m_setup.synchronize(); - CGlobalSetup currentSetup = m_setup.get(); + CGlobalSetup currentSetup = m_setup.getCopy(); if (currentSetup.wasLoaded()) { if (this->m_bootstrapMode != Explicit || this->m_bootstrapUrls.isEmpty()) @@ -86,13 +86,13 @@ namespace BlackCore if (this->m_bootstrapUrls.isEmpty()) { - return CStatusMessage(cats(), CStatusMessage::SeverityError, "No bootstrap URLs, cannot load setup"); + return CStatusMessage(this, CStatusMessage::SeverityError, "No bootstrap URLs, cannot load setup"); } else { this->m_bootstrapUrls.removeDuplicates(); this->ps_readSetup(); // start reading - return CStatusMessage(cats(), CStatusMessage::SeverityInfo, "Will start loading setup"); + return CStatusMessage(this, CStatusMessage::SeverityInfo, "Will start loading setup"); } } } @@ -139,10 +139,12 @@ namespace BlackCore void CSetupReader::ps_readSetup() { - CUrl url(this->m_bootstrapUrls.obtainNextWorkingUrl()); + const CUrl url(this->m_bootstrapUrls.obtainNextWorkingUrl()); if (url.isEmpty()) { - CLogMessage(this).warning("Cannot read setup, failed URLs: %1") << this->m_bootstrapUrls.getFailedUrls(); + CLogMessage(this).warning("Cannot read setup, URLs: %1, failed URLs: %2") + << this->m_bootstrapUrls + << this->m_bootstrapUrls.getFailedUrls(); emit setupSynchronized(false); return; } @@ -152,10 +154,12 @@ namespace BlackCore void CSetupReader::ps_readUpdateInfo() { - CUrl url(this->m_updateInfoUrls.obtainNextWorkingUrl()); + const CUrl url(this->m_updateInfoUrls.obtainNextWorkingUrl()); if (url.isEmpty()) { - CLogMessage(this).warning("Cannot read update info, failed URLs: %1") << this->m_updateInfoUrls.getFailedUrls(); + CLogMessage(this).warning("Cannot read update info, URLs: %1, failed URLs: %2") + << this->m_updateInfoUrls + << this->m_updateInfoUrls.getFailedUrls(); emit updateInfoSynchronized(false); return; } @@ -252,7 +256,8 @@ namespace BlackCore } else { - CGlobalSetup currentSetup = m_setup.get(); + const CGlobalSetup currentSetup = m_setup.get(); + this->m_updateInfoUrls = currentSetup.updateInfoFileUrls(); CGlobalSetup loadedSetup; loadedSetup.convertFromJson(Json::jsonObjectFromString(setupJson)); loadedSetup.markAsLoaded(true); @@ -278,7 +283,7 @@ namespace BlackCore CStatusMessage m = m_setup.set(loadedSetup, loadedSetup.getMSecsSinceEpoch()); if (m.isWarningOrAbove()) { - m.setCategories(cats()); + m.setCategories(getLogCategories()); CLogMessage(this).preformatted(m); emit setupSynchronized(false); return; // issue with cache @@ -361,7 +366,7 @@ namespace BlackCore CStatusMessage m = m_updateInfo.set(loadedUpdateInfo, loadedUpdateInfo.getMSecsSinceEpoch()); if (!m.isEmpty()) { - m.setCategories(cats()); + m.setCategories(getLogCategories()); CLogMessage(this).preformatted(m); emit updateInfoSynchronized(false); return; // issue with cache @@ -394,10 +399,19 @@ namespace BlackCore } } // function - const CLogCategoryList &CSetupReader::cats() + const CLogCategoryList &CSetupReader::getLogCategories() { - static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::webservice()})); + static const CLogCategoryList cats({ CLogCategory("swift.setupreader"), CLogCategory::webservice()}); return cats; } + CGlobalSetup CSetupReader::getSetup() const + { + return m_setup.getCopy(); + } + + CUpdateInfo CSetupReader::getUpdateInfo() const + { + return m_updateInfo.getCopy(); + } } // namespace diff --git a/src/blackcore/setupreader.h b/src/blackcore/setupreader.h index 5b20b3dad..023bbad29 100644 --- a/src/blackcore/setupreader.h +++ b/src/blackcore/setupreader.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace BlackCore { @@ -34,9 +35,27 @@ namespace BlackCore class BLACKCORE_EXPORT CSetupReader : public QObject { Q_OBJECT - friend class CApplication; //!< only using class + public: + //! Categories + static const BlackMisc::CLogCategoryList &getLogCategories(); + + //! Current setup + //! \threadsafe + BlackCore::Data::CGlobalSetup getSetup() const; + + //! Update info + //! \threadsafe + BlackCore::Data::CUpdateInfo getUpdateInfo() const; + + signals: + //! Setup has been read + void setupSynchronized(bool success); + + //! Version bas been read + void updateInfoSynchronized(bool success); + protected: //! Constructor explicit CSetupReader(QObject *parent); @@ -54,18 +73,13 @@ namespace BlackCore void gracefulShutdown(); //! Setup loaded? + //! \threadsafe bool isSetupSyncronized() const { return m_setupSyncronized; } //! Version info loaded? + //! \threadsafe bool isUpdateSyncronized() const { return m_updateInfoSyncronized; } - signals: - //! Setup has been read - void setupSynchronized(bool success); - - //! Version bas been read - void updateInfoSynchronized(bool success); - private slots: //! Setup has been read void ps_parseSetupFile(QNetworkReply *nwReply); @@ -98,13 +112,13 @@ namespace BlackCore }; bool m_shutdown = false; - bool m_setupSyncronized = false; - bool m_updateInfoSyncronized = false; + std::atomic m_setupSyncronized { false }; + std::atomic m_updateInfoSyncronized { false }; QString m_localSetupFileValue; QString m_bootsrapUrlFileValue; BootsrapMode m_bootstrapMode; - BlackMisc::Network::CFailoverUrlList m_bootstrapUrls; - BlackMisc::Network::CFailoverUrlList m_updateInfoUrls; + BlackMisc::Network::CFailoverUrlList m_bootstrapUrls; //!< location of setup files + BlackMisc::Network::CFailoverUrlList m_updateInfoUrls; //!< location of info files BlackMisc::CData m_setup {this, &CSetupReader::ps_setupChanged}; //!< data cache setup BlackMisc::CData m_updateInfo {this}; //!< data cache update info @@ -129,9 +143,6 @@ namespace BlackCore //! Read for development environment? static bool isForDevelopment(); - - //! Categories - const BlackMisc::CLogCategoryList &cats(); }; } // ns