From 6e10ca88d5ca2373ecb740836f67a3aee6c9c07b Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sat, 1 Apr 2023 00:46:29 -0500 Subject: [PATCH] Associate product refresh with a uuid Allows product manager to track which map widgets have enabled which product refreshes --- .../scwx/qt/manager/radar_product_manager.cpp | 72 +++++++++++++++++-- .../scwx/qt/manager/radar_product_manager.hpp | 18 ++++- scwx-qt/source/scwx/qt/map/map_widget.cpp | 10 ++- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp index 80514e49..7cd70f37 100644 --- a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp @@ -16,6 +16,7 @@ #pragma warning(push, 0) #include +#include #include #include #include @@ -166,7 +167,8 @@ public: std::shared_ptr GetLevel3ProviderManager(const std::string& product); - void EnableRefresh(std::shared_ptr providerManager, + void EnableRefresh(boost::uuids::uuid uuid, + std::shared_ptr providerManager, bool enabled); void RefreshData(std::shared_ptr providerManager); @@ -218,6 +220,12 @@ public: common::Level3ProductCategoryMap availableCategoryMap_; std::shared_mutex availableCategoryMutex_; + + std::unordered_map, + boost::hash> + refreshMap_ {}; + std::mutex refreshMapMutex_ {}; }; RadarProductManager::RadarProductManager(const std::string& radarId) : @@ -248,6 +256,8 @@ std::string ProviderManager::name() const void ProviderManager::Disable() { + logger_->debug("Disabling refresh: {}", name()); + std::unique_lock lock(refreshTimerMutex_); refreshEnabled_ = false; refreshTimer_.cancel(); @@ -413,11 +423,12 @@ RadarProductManagerImpl::GetLevel3ProviderManager(const std::string& product) void RadarProductManager::EnableRefresh(common::RadarProductGroup group, const std::string& product, - bool enabled) + bool enabled, + boost::uuids::uuid uuid) { if (group == common::RadarProductGroup::Level2) { - p->EnableRefresh(p->level2ProviderManager_, enabled); + p->EnableRefresh(uuid, p->level2ProviderManager_, enabled); } else { @@ -437,16 +448,65 @@ void RadarProductManager::EnableRefresh(common::RadarProductGroup group, availableProducts.cend(), product) != availableProducts.cend()) { - p->EnableRefresh(providerManager, enabled); + p->EnableRefresh(uuid, providerManager, enabled); } }); } } void RadarProductManagerImpl::EnableRefresh( - std::shared_ptr providerManager, bool enabled) + boost::uuids::uuid uuid, + std::shared_ptr providerManager, + bool enabled) { - if (providerManager->refreshEnabled_ != enabled) + // Lock the refresh map + std::unique_lock lock {refreshMapMutex_}; + + auto currentProviderManager = refreshMap_.find(uuid); + if (currentProviderManager != refreshMap_.cend()) + { + // If the enabling refresh for a different product, or disabling refresh + if (currentProviderManager->second != providerManager || !enabled) + { + // Determine number of entries in the map for the current provider + // manager + auto currentProviderManagerCount = std::count_if( + refreshMap_.cbegin(), + refreshMap_.cend(), + [&](const auto& provider) + { return provider.second == currentProviderManager->second; }); + + // If this is the last reference to the provider in the refresh map + if (currentProviderManagerCount == 1) + { + // Disable current provider + currentProviderManager->second->Disable(); + } + + // Dissociate uuid from current provider manager + refreshMap_.erase(currentProviderManager); + + // If we are enabling a new provider manager + if (enabled) + { + // Associate uuid to providerManager + refreshMap_.emplace(uuid, providerManager); + } + } + } + else if (enabled) + { + // We are enabling a new provider manager + // Associate uuid to provider manager + refreshMap_.emplace(uuid, providerManager); + } + + // Release the refresh map mutex + lock.unlock(); + + // We have already handled a disable request by this point. If enabling, and + // the provider manager refresh isn't already enabled, enable it. + if (enabled && providerManager->refreshEnabled_ != enabled) { providerManager->refreshEnabled_ = enabled; diff --git a/scwx-qt/source/scwx/qt/manager/radar_product_manager.hpp b/scwx-qt/source/scwx/qt/manager/radar_product_manager.hpp index 80a3cca7..cdce3a51 100644 --- a/scwx-qt/source/scwx/qt/manager/radar_product_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/radar_product_manager.hpp @@ -12,6 +12,7 @@ #include #include +#include #include namespace scwx @@ -38,9 +39,24 @@ public: std::shared_ptr radar_site() const; void Initialize(); + + /** + * @brief Enables or disables refresh associated with a unique identifier + * (UUID) for a given radar product group and product. + * + * Only a single product refresh can be enabled for a given UUID. If a second + * product refresh is enabled for the same UUID, the first product refresh is + * disabled (unless still enabled under a different UUID). + * + * @param [in] group Radar product group + * @param [in] product Radar product name + * @param [in] enabled Whether to enable refresh + * @param [in] uuid Unique identifier. Default is boost::uuids::nil_uuid(). + */ void EnableRefresh(common::RadarProductGroup group, const std::string& product, - bool enabled); + bool enabled, + boost::uuids::uuid uuid = boost::uuids::nil_uuid()); std::tuple, float, diff --git a/scwx-qt/source/scwx/qt/map/map_widget.cpp b/scwx-qt/source/scwx/qt/map/map_widget.cpp index 40d45910..273ceb55 100644 --- a/scwx-qt/source/scwx/qt/map/map_widget.cpp +++ b/scwx-qt/source/scwx/qt/map/map_widget.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -58,6 +59,7 @@ class MapWidgetImpl : public QObject public: explicit MapWidgetImpl(MapWidget* widget, const QMapLibreGL::Settings& settings) : + uuid_ {boost::uuids::random_generator()()}, context_ {std::make_shared()}, widget_ {widget}, settings_(settings), @@ -126,6 +128,8 @@ public: common::Level2Product GetLevel2ProductOrDefault(const std::string& productName) const; + boost::uuids::uuid uuid_; + std::shared_ptr context_; MapWidget* widget_; @@ -399,7 +403,8 @@ void MapWidget::SelectRadarProduct(common::RadarProductGroup group, if (p->autoRefreshEnabled_) { - p->radarProductManager_->EnableRefresh(group, productName, true); + p->radarProductManager_->EnableRefresh( + group, productName, true, p->uuid_); } } @@ -485,7 +490,8 @@ void MapWidget::SetAutoRefresh(bool enabled) p->radarProductManager_->EnableRefresh( radarProductView->GetRadarProductGroup(), radarProductView->GetRadarProductName(), - true); + true, + p->uuid_); } } }