mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 01:40:04 +00:00 
			
		
		
		
	Add boost steady_timer for refresh, and application cleanup
This commit is contained in:
		
							parent
							
								
									bb7793d309
								
							
						
					
					
						commit
						eb576c5172
					
				
					 3 changed files with 68 additions and 16 deletions
				
			
		|  | @ -1,5 +1,6 @@ | ||||||
| #include <scwx/qt/config/radar_site.hpp> | #include <scwx/qt/config/radar_site.hpp> | ||||||
| #include <scwx/qt/main/main_window.hpp> | #include <scwx/qt/main/main_window.hpp> | ||||||
|  | #include <scwx/qt/manager/radar_product_manager.hpp> | ||||||
| #include <scwx/qt/manager/resource_manager.hpp> | #include <scwx/qt/manager/resource_manager.hpp> | ||||||
| #include <scwx/qt/manager/settings_manager.hpp> | #include <scwx/qt/manager/settings_manager.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
|  | @ -53,10 +54,16 @@ int main(int argc, char* argv[]) | ||||||
|    scwx::qt::manager::ResourceManager::PreLoad(); |    scwx::qt::manager::ResourceManager::PreLoad(); | ||||||
| 
 | 
 | ||||||
|    // Run Qt main loop
 |    // Run Qt main loop
 | ||||||
|    QApplication               a(argc, argv); |    int result; | ||||||
|    scwx::qt::main::MainWindow w; |    { | ||||||
|    w.show(); |       QApplication               a(argc, argv); | ||||||
|    int result = a.exec(); |       scwx::qt::main::MainWindow w; | ||||||
|  |       w.show(); | ||||||
|  |       result = a.exec(); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    // Deinitialize application
 | ||||||
|  |    scwx::qt::manager::RadarProductManager::Cleanup(); | ||||||
| 
 | 
 | ||||||
|    // Gracefully stop the io_context main loop
 |    // Gracefully stop the io_context main loop
 | ||||||
|    work.reset(); |    work.reset(); | ||||||
|  |  | ||||||
|  | @ -12,12 +12,12 @@ | ||||||
| #include <mutex> | #include <mutex> | ||||||
| #include <shared_mutex> | #include <shared_mutex> | ||||||
| 
 | 
 | ||||||
|  | #include <boost/asio/steady_timer.hpp> | ||||||
| #include <boost/range/irange.hpp> | #include <boost/range/irange.hpp> | ||||||
| #include <boost/timer/timer.hpp> | #include <boost/timer/timer.hpp> | ||||||
| #include <fmt/chrono.h> | #include <fmt/chrono.h> | ||||||
| #include <GeographicLib/Geodesic.hpp> | #include <GeographicLib/Geodesic.hpp> | ||||||
| #include <QMapbox> | #include <QMapbox> | ||||||
| #include <QTimer> |  | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
| { | { | ||||||
|  | @ -49,7 +49,8 @@ static constexpr std::chrono::seconds kRetryInterval_ {15}; | ||||||
| 
 | 
 | ||||||
| // TODO: Find a way to garbage collect this
 | // TODO: Find a way to garbage collect this
 | ||||||
| static std::unordered_map<std::string, std::shared_ptr<RadarProductManager>> | static std::unordered_map<std::string, std::shared_ptr<RadarProductManager>> | ||||||
|    instanceMap_; |                   instanceMap_; | ||||||
|  | static std::mutex instanceMutex_; | ||||||
| 
 | 
 | ||||||
| static std::unordered_map<std::string, | static std::unordered_map<std::string, | ||||||
|                           std::shared_ptr<types::RadarProductRecord>> |                           std::shared_ptr<types::RadarProductRecord>> | ||||||
|  | @ -74,7 +75,8 @@ public: | ||||||
|        level2ProductRecordMutex_ {}, |        level2ProductRecordMutex_ {}, | ||||||
|        level3ProductRecordMutex_ {}, |        level3ProductRecordMutex_ {}, | ||||||
|        level2DataRefreshEnabled_ {false}, |        level2DataRefreshEnabled_ {false}, | ||||||
|        level2DataRefreshTimer_ {std::make_shared<QTimer>()}, |        level2DataRefreshTimer_ {util::io_context()}, | ||||||
|  |        level2DataRefreshTimerMutex_ {}, | ||||||
|        level2DataProvider_ { |        level2DataProvider_ { | ||||||
|           provider::Level2DataProviderFactory::Create(radarId)}, |           provider::Level2DataProviderFactory::Create(radarId)}, | ||||||
|        initializeMutex_ {}, |        initializeMutex_ {}, | ||||||
|  | @ -85,10 +87,15 @@ public: | ||||||
|          logger_->warn("Radar site not found: \"{}\"", radarId_); |          logger_->warn("Radar site not found: \"{}\"", radarId_); | ||||||
|          radarSite_ = std::make_shared<config::RadarSite>(); |          radarSite_ = std::make_shared<config::RadarSite>(); | ||||||
|       } |       } | ||||||
| 
 |  | ||||||
|       level2DataRefreshTimer_->setSingleShot(true); |  | ||||||
|    } |    } | ||||||
|    ~RadarProductManagerImpl() = default; |    ~RadarProductManagerImpl() | ||||||
|  |    { | ||||||
|  |       { | ||||||
|  |          std::unique_lock lock(level2DataRefreshTimerMutex_); | ||||||
|  |          level2DataRefreshEnabled_ = false; | ||||||
|  |          level2DataRefreshTimer_.cancel(); | ||||||
|  |       } | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    RadarProductManager* self_; |    RadarProductManager* self_; | ||||||
| 
 | 
 | ||||||
|  | @ -122,7 +129,8 @@ public: | ||||||
|    std::shared_mutex level3ProductRecordMutex_; |    std::shared_mutex level3ProductRecordMutex_; | ||||||
| 
 | 
 | ||||||
|    bool                                          level2DataRefreshEnabled_; |    bool                                          level2DataRefreshEnabled_; | ||||||
|    std::shared_ptr<QTimer>                       level2DataRefreshTimer_; |    boost::asio::steady_timer                     level2DataRefreshTimer_; | ||||||
|  |    std::mutex                                    level2DataRefreshTimerMutex_; | ||||||
|    std::shared_ptr<provider::Level2DataProvider> level2DataProvider_; |    std::shared_ptr<provider::Level2DataProvider> level2DataProvider_; | ||||||
| 
 | 
 | ||||||
|    std::mutex initializeMutex_; |    std::mutex initializeMutex_; | ||||||
|  | @ -135,6 +143,19 @@ RadarProductManager::RadarProductManager(const std::string& radarId) : | ||||||
| } | } | ||||||
| RadarProductManager::~RadarProductManager() = default; | RadarProductManager::~RadarProductManager() = default; | ||||||
| 
 | 
 | ||||||
|  | void RadarProductManager::Cleanup() | ||||||
|  | { | ||||||
|  |    { | ||||||
|  |       std::unique_lock lock(fileIndexMutex_); | ||||||
|  |       fileIndex_.clear(); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    { | ||||||
|  |       std::unique_lock lock(instanceMutex_); | ||||||
|  |       instanceMap_.clear(); | ||||||
|  |    } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| const std::vector<float>& | const std::vector<float>& | ||||||
| RadarProductManager::coordinates(common::RadialSize radialSize) const | RadarProductManager::coordinates(common::RadialSize radialSize) const | ||||||
| { | { | ||||||
|  | @ -274,7 +295,10 @@ void RadarProductManagerImpl::RefreshLevel2Data() | ||||||
| { | { | ||||||
|    logger_->debug("RefreshLevel2Data()"); |    logger_->debug("RefreshLevel2Data()"); | ||||||
| 
 | 
 | ||||||
|    level2DataRefreshTimer_->stop(); |    { | ||||||
|  |       std::unique_lock lock(level2DataRefreshTimerMutex_); | ||||||
|  |       level2DataRefreshTimer_.cancel(); | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    util::async( |    util::async( | ||||||
|       [&]() |       [&]() | ||||||
|  | @ -301,14 +325,34 @@ void RadarProductManagerImpl::RefreshLevel2Data() | ||||||
|             emit self_->NewLevel2DataAvailable(latestTime); |             emit self_->NewLevel2DataAvailable(latestTime); | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|  |          std::unique_lock lock(level2DataRefreshTimerMutex_); | ||||||
|  | 
 | ||||||
|          if (level2DataRefreshEnabled_) |          if (level2DataRefreshEnabled_) | ||||||
|          { |          { | ||||||
|             logger_->debug( |             logger_->debug( | ||||||
|                "Scheduled refresh in {:%M:%S}", |                "Scheduled refresh in {:%M:%S}", | ||||||
|                std::chrono::duration_cast<std::chrono::seconds>(interval)); |                std::chrono::duration_cast<std::chrono::seconds>(interval)); | ||||||
| 
 | 
 | ||||||
|             // TODO: This doesn't work from an async thread
 |             { | ||||||
|             level2DataRefreshTimer_->start(interval); |                level2DataRefreshTimer_.expires_after(interval); | ||||||
|  |                level2DataRefreshTimer_.async_wait( | ||||||
|  |                   [this](const boost::system::error_code& e) | ||||||
|  |                   { | ||||||
|  |                      if (e == boost::system::errc::success) | ||||||
|  |                      { | ||||||
|  |                         RefreshLevel2Data(); | ||||||
|  |                      } | ||||||
|  |                      else if (e == boost::asio::error::operation_aborted) | ||||||
|  |                      { | ||||||
|  |                         logger_->debug("Level 2 data refresh timer cancelled"); | ||||||
|  |                      } | ||||||
|  |                      else | ||||||
|  |                      { | ||||||
|  |                         logger_->warn("Level 2 data refresh timer error: {}", | ||||||
|  |                                       e.message()); | ||||||
|  |                      } | ||||||
|  |                   }); | ||||||
|  |             } | ||||||
|          } |          } | ||||||
|       }); |       }); | ||||||
| } | } | ||||||
|  | @ -580,8 +624,7 @@ RadarProductManager::GetLevel3Data(const std::string& product, | ||||||
| std::shared_ptr<RadarProductManager> | std::shared_ptr<RadarProductManager> | ||||||
| RadarProductManager::Instance(const std::string& radarSite) | RadarProductManager::Instance(const std::string& radarSite) | ||||||
| { | { | ||||||
|    static std::mutex           instanceMutex; |    std::lock_guard<std::mutex> guard(instanceMutex_); | ||||||
|    std::lock_guard<std::mutex> guard(instanceMutex); |  | ||||||
| 
 | 
 | ||||||
|    if (!instanceMap_.contains(radarSite)) |    if (!instanceMap_.contains(radarSite)) | ||||||
|    { |    { | ||||||
|  |  | ||||||
|  | @ -29,6 +29,8 @@ public: | ||||||
|    explicit RadarProductManager(const std::string& radarId); |    explicit RadarProductManager(const std::string& radarId); | ||||||
|    ~RadarProductManager(); |    ~RadarProductManager(); | ||||||
| 
 | 
 | ||||||
|  |    static void Cleanup(); | ||||||
|  | 
 | ||||||
|    const std::vector<float>& coordinates(common::RadialSize radialSize) const; |    const std::vector<float>& coordinates(common::RadialSize radialSize) const; | ||||||
|    float                     gate_size() const; |    float                     gate_size() const; | ||||||
|    std::shared_ptr<config::RadarSite> radar_site() const; |    std::shared_ptr<config::RadarSite> radar_site() const; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat