refs #853, JSON exception handling adjustments for ICAO reader

This commit is contained in:
Klaus Basan
2017-01-05 23:55:47 +01:00
committed by Mathew Sutcliffe
parent b0bef3264c
commit e3197ce375
3 changed files with 93 additions and 39 deletions

View File

@@ -302,57 +302,107 @@ namespace BlackCore
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString; CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString;
} }
bool CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead) CStatusMessageList CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
{ {
QDir directory(dir); QDir directory(dir);
if (!directory.exists()) { return false; } if (!directory.exists())
{
return CStatusMessage(this).error("Missing directory '%1'") << dir;
}
// Hint: Do not emit while locked -> deadlock
CStatusMessageList msgs;
whatToRead &= CEntityFlags::AllIcaoAndCountries;
CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity; CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
if (whatToRead.testFlag(CEntityFlags::CountryEntity)) if (whatToRead.testFlag(CEntityFlags::CountryEntity))
{ {
QString countriesJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "countries.json"))); const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "countries.json");
if (!countriesJson.isEmpty()) const QString countriesJson(CFileUtils::readFileToString(fileName));
if (countriesJson.isEmpty())
{
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
}
else
{
try
{ {
CCountryList countries; CCountryList countries;
countries.convertFromJson(Json::jsonObjectFromString(countriesJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow countries.convertFromJson(Json::jsonObjectFromString(countriesJson));
const int c = countries.size(); const int c = countries.size();
this->m_countryCache.set(countries); this->m_countryCache.set(countries);
// Do not emit while locked -> deadlock
reallyRead |= CEntityFlags::CountryEntity; reallyRead |= CEntityFlags::CountryEntity;
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, c); emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, c);
} }
catch (const CJsonException &ex)
{
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFailed, 0);
msgs.push_back(ex.toStatusMessage(this, QString("Reading countries from '%1'").arg(fileName)));
}
}
} }
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity)) if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity))
{ {
QString aircraftJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "aircrafticao.json"))); const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "aircrafticao.json");
if (!aircraftJson.isEmpty()) const QString aircraftJson(fileName);
if (aircraftJson.isEmpty())
{
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
}
else
{
try
{ {
CAircraftIcaoCodeList aircraftIcaos; CAircraftIcaoCodeList aircraftIcaos;
aircraftIcaos.convertFromJson(Json::jsonObjectFromString(aircraftJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow aircraftIcaos.convertFromJson(Json::jsonObjectFromString(aircraftJson));
const int c = aircraftIcaos.size(); const int c = aircraftIcaos.size();
this->m_aircraftIcaoCache.set(aircraftIcaos); this->m_aircraftIcaoCache.set(aircraftIcaos);
reallyRead |= CEntityFlags::AircraftIcaoEntity; reallyRead |= CEntityFlags::AircraftIcaoEntity;
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished, c); emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished, c);
} }
catch (const CJsonException &ex)
{
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFailed, 0);
msgs.push_back(ex.toStatusMessage(this, QString("Reading aircraft ICAOs from '%1'").arg(fileName)));
}
}
} }
if (whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity)) if (whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity))
{ {
QString airlineJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "airlineicao.json"))); const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "airlineicao.json");
if (!airlineJson.isEmpty()) const QString airlineJson(fileName);
if (airlineJson.isEmpty())
{
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
}
else
{
try
{ {
CAirlineIcaoCodeList airlineIcaos; CAirlineIcaoCodeList airlineIcaos;
airlineIcaos.convertFromJson(Json::jsonObjectFromString(airlineJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow airlineIcaos.convertFromJson(Json::jsonObjectFromString(airlineJson));
const int c = airlineIcaos.size(); const int c = airlineIcaos.size();
this->m_airlineIcaoCache.set(airlineIcaos); this->m_airlineIcaoCache.set(airlineIcaos);
reallyRead |= CEntityFlags::AirlineIcaoEntity; reallyRead |= CEntityFlags::AirlineIcaoEntity;
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, c); emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, c);
} }
catch (const CJsonException &ex)
{
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFailed, 0);
msgs.push_back(ex.toStatusMessage(this, QString("Reading airline ICAOs from '%1'").arg(fileName)));
}
}
}
if (msgs.isSuccess() && (reallyRead & CEntityFlags::DistributorLiveryModel) == whatToRead)
{
return CStatusMessage(this).info("Updated caches for '%1' from '%2'") << CEntityFlags::flagToString(reallyRead) << dir;
}
else
{
return msgs;
} }
return (whatToRead & CEntityFlags::AllIcaoAndCountries) == reallyRead;
} }
bool CIcaoDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead) bool CIcaoDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead)
@@ -360,8 +410,11 @@ namespace BlackCore
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; } if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
QTimer::singleShot(0, this, [this, dir, whatToRead]() QTimer::singleShot(0, this, [this, dir, whatToRead]()
{ {
bool s = this->readFromJsonFiles(dir, whatToRead); const CStatusMessageList msgs = this->readFromJsonFiles(dir, whatToRead);
Q_UNUSED(s); if (msgs.isFailure())
{
CLogMessage::preformatted(msgs);
}
}); });
return true; return true;
} }

View File

@@ -110,7 +110,7 @@ namespace BlackCore
bool areAllDataRead() const; bool areAllDataRead() const;
//! Read from static DB data file //! Read from static DB data file
bool readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries); BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
//! Read from static DB data file //! Read from static DB data file
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries); bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);

View File

@@ -1197,23 +1197,24 @@ namespace BlackCore
bool s = false; bool s = false;
if (this->m_icaoDataReader) if (this->m_icaoDataReader)
{ {
s = inBackground ? if (inBackground) { return this->m_icaoDataReader->readFromJsonFilesInBackground(dir); }
this->m_icaoDataReader->readFromJsonFilesInBackground(dir) : const CStatusMessageList msgs = this->m_icaoDataReader->readFromJsonFiles(dir);
this->m_icaoDataReader->readFromJsonFiles(dir); if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
s = msgs.isSuccess();
} }
if (s && this->m_modelDataReader) if (s && this->m_modelDataReader)
{ {
if (inBackground) { return this->m_modelDataReader->readFromJsonFilesInBackground(dir); } if (inBackground) { return this->m_modelDataReader->readFromJsonFilesInBackground(dir); }
const CStatusMessageList msgs = this->m_modelDataReader->readFromJsonFiles(dir); const CStatusMessageList msgs = this->m_modelDataReader->readFromJsonFiles(dir);
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); } if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
return msgs.isSuccess(); s = msgs.isSuccess();
} }
if (s && this->m_airportDataReader) if (s && this->m_airportDataReader)
{ {
if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); } if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); }
const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir); const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir);
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); } if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
return msgs.isSuccess(); s = msgs.isSuccess();
} }
return s; return s;
} }