Quit and wait for GFS parser thread to be finished before closing

refs #854
This commit is contained in:
Roland Winklmeier
2017-01-12 22:56:35 +01:00
committed by Mathew Sutcliffe
parent bed1542fe4
commit 2bd9120c1c
2 changed files with 22 additions and 5 deletions

View File

@@ -9,7 +9,6 @@
#include "weatherdatagfs.h"
#include "blackcore/application.h"
#include "blackmisc/worker.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/math/mathutils.h"
#include "blackmisc/geo/coordinategeodetic.h"
@@ -55,6 +54,11 @@ namespace BlackWxPlugin
IWeatherData(parent)
{ }
CWeatherDataGfs::~CWeatherDataGfs()
{
if (m_parseGribFileWorker && !m_parseGribFileWorker->isFinished()) { m_parseGribFileWorker->abandonAndWait(); }
}
void CWeatherDataGfs::fetchWeatherData(const CWeatherGrid &grid, const CLength &range)
{
m_grid = grid;
@@ -70,11 +74,12 @@ namespace BlackWxPlugin
else
{
CLogMessage(this).debug() << "Using cached data";
CWorker *worker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]()
Q_ASSERT_X(!m_parseGribFileWorker, Q_FUNC_INFO, "Worker already running");
m_parseGribFileWorker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]()
{
parseGfsFileImpl(m_gribData);
});
worker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished);
m_parseGribFileWorker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished);
}
}
@@ -92,11 +97,12 @@ namespace BlackWxPlugin
void CWeatherDataGfs::ps_parseGfsFile(QNetworkReply *reply)
{
m_gribData = reply->readAll();
CWorker *worker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]()
Q_ASSERT_X(!m_parseGribFileWorker, Q_FUNC_INFO, "Worker already running");
m_parseGribFileWorker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]()
{
parseGfsFileImpl(m_gribData);
});
worker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished);
m_parseGribFileWorker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished);
reply->deleteLater();
}
@@ -199,6 +205,8 @@ namespace BlackWxPlugin
g2int iseek = 0;
for(;;)
{
if(QThread::currentThread()->isInterruptionRequested()) { return; }
// Search next grib field
g2int lskip = 0;
g2int lgrib = 0;
@@ -246,6 +254,8 @@ namespace BlackWxPlugin
for (const GfsGridPoint &gfsGridPoint : m_gfsWeatherGrid)
{
if(QThread::currentThread()->isInterruptionRequested()) { return; }
CTemperatureLayerList temperatureLayers;
CAltitude surfaceAltitude (0, CAltitude::AboveGround, CLengthUnit::defaultUnit());

View File

@@ -14,6 +14,7 @@
#include "g2clib/grib2.h"
#include "blackmisc/weather/gridpoint.h"
#include "blackmisc/worker.h"
#include "blackcore/weatherdata.h"
#include <QReadWriteLock>
#include <QHash>
@@ -21,6 +22,7 @@
#include <QUrl>
#include <QByteArray>
#include <QNetworkReply>
#include <QPointer>
#include <array>
class QNetworkAccessManager;
@@ -42,6 +44,9 @@ namespace BlackWxPlugin
//! Constructor
CWeatherDataGfs(QObject *parent = nullptr);
//! Destructor
~CWeatherDataGfs();
//! \copydoc BlackCore::IWeatherData::fetchWeatherData
virtual void fetchWeatherData(const BlackMisc::Weather::CWeatherGrid &grid,
const BlackMisc::PhysicalQuantities::CLength &range) override;
@@ -161,6 +166,8 @@ namespace BlackWxPlugin
QVector<GfsGridPoint> m_gfsWeatherGrid;
BlackMisc::Weather::CWeatherGrid m_weatherGrid;
QPointer<BlackMisc::CWorker> m_parseGribFileWorker; //!< worker will destroy itself, so weak pointer
using Grib2ParameterKey = std::array<g2int, 2>;
using Grib2ParameterTable = QMap<Grib2ParameterKey, Grib2ParameterValue>;
static const Grib2ParameterTable m_grib2ParameterTable;