Ref T554, publishing function for "auto publish data"

* writer
* services
This commit is contained in:
Klaus Basan
2019-02-26 23:04:29 +01:00
committed by Mat Sutcliffe
parent 4fb0db01bd
commit 4c33e4d792
6 changed files with 144 additions and 38 deletions

View File

@@ -425,6 +425,12 @@ namespace BlackCore
return CDatabaseUtils::getJsonTextMultipart(bytes, compress); return CDatabaseUtils::getJsonTextMultipart(bytes, compress);
} }
QHttpPart CDatabaseUtils::getJsonTextMultipart(const QString &json, bool compress)
{
const QByteArray bytes(json.toUtf8());
return CDatabaseUtils::getJsonTextMultipart(bytes, compress);
}
QHttpPart CDatabaseUtils::getJsonTextMultipart(const QJsonArray &json, bool compress) QHttpPart CDatabaseUtils::getJsonTextMultipart(const QJsonArray &json, bool compress)
{ {
const QByteArray bytes(QJsonDocument(json).toJson(QJsonDocument::Compact)); const QByteArray bytes(QJsonDocument(json).toJson(QJsonDocument::Compact));

View File

@@ -93,6 +93,9 @@ namespace BlackCore
//! Multipart for JSON //! Multipart for JSON
static QHttpPart getJsonTextMultipart(const QJsonObject &json, bool compress); static QHttpPart getJsonTextMultipart(const QJsonObject &json, bool compress);
//! Multipart for JSON
static QHttpPart getJsonTextMultipart(const QString &json, bool compress);
//! Multipart for JSON //! Multipart for JSON
static QHttpPart getJsonTextMultipart(const QJsonArray &json, bool compress); static QHttpPart getJsonTextMultipart(const QJsonArray &json, bool compress);

View File

@@ -6,14 +6,15 @@
* or distributed except according to the terms contained in the LICENSE file. * or distributed except according to the terms contained in the LICENSE file.
*/ */
#include "blackcore/application.h"
#include "blackcore/data/globalsetup.h" #include "blackcore/data/globalsetup.h"
#include "blackcore/db/databasewriter.h" #include "blackcore/db/databasewriter.h"
#include "blackcore/db/databaseutils.h" #include "blackcore/db/databaseutils.h"
#include "blackmisc/db/datastoreutility.h" #include "blackcore/application.h"
#include "blackmisc/logcategory.h" #include "blackmisc/simulation/autopublishdata.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/network/networkutils.h" #include "blackmisc/network/networkutils.h"
#include "blackmisc/db/datastoreutility.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/logcategory.h"
#include "blackmisc/statusmessage.h" #include "blackmisc/statusmessage.h"
#include <QStringBuilder> #include <QStringBuilder>
@@ -38,7 +39,8 @@ namespace BlackCore
{ {
CDatabaseWriter::CDatabaseWriter(const Network::CUrl &baseUrl, QObject *parent) : CDatabaseWriter::CDatabaseWriter(const Network::CUrl &baseUrl, QObject *parent) :
QObject(parent), QObject(parent),
m_modelPublishUrl(getModelPublishUrl(baseUrl)) m_modelPublishUrl(CDatabaseWriter::getModelPublishUrl(baseUrl)),
m_autoPublishUrl(CDatabaseWriter::getAutoPublishUrl(baseUrl))
{ {
// void // void
} }
@@ -51,15 +53,15 @@ namespace BlackCore
CStatusMessageList CDatabaseWriter::asyncPublishModels(const CAircraftModelList &models) CStatusMessageList CDatabaseWriter::asyncPublishModels(const CAircraftModelList &models)
{ {
CStatusMessageList msgs; CStatusMessageList msgs;
if (m_shutdown) if (m_shutdown || !sApp)
{ {
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Database writer shuts down")); msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Database writer shutting down"));
return msgs; return msgs;
} }
if (this->isReplyOverdue()) if (this->isModelReplyOverdue())
{ {
const bool killed = this->killPendingReply(); const bool killed = this->killPendingModelReply();
if (killed) if (killed)
{ {
const CStatusMessage msg(CStatusMessage::SeverityWarning, u"Aborted outdated pending reply"); const CStatusMessage msg(CStatusMessage::SeverityWarning, u"Aborted outdated pending reply");
@@ -69,7 +71,7 @@ namespace BlackCore
} }
} }
if (m_pendingReply) if (m_pendingModelPublishReply)
{ {
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Another write operation in progress")); msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Another write operation in progress"));
return msgs; return msgs;
@@ -88,15 +90,49 @@ namespace BlackCore
QNetworkRequest request(url); QNetworkRequest request(url);
CNetworkUtils::ignoreSslVerification(request); CNetworkUtils::ignoreSslVerification(request);
const int logId = m_writeLog.addPendingUrl(url); const int logId = m_writeLog.addPendingUrl(url);
m_pendingReply = sApp->postToNetwork(request, logId, multiPart, { this, &CDatabaseWriter::postedModelsResponse}); m_pendingModelPublishReply = sApp->postToNetwork(request, logId, multiPart, { this, &CDatabaseWriter::postedModelsResponse});
m_replyPendingSince = QDateTime::currentMSecsSinceEpoch(); m_modelReplyPendingSince = QDateTime::currentMSecsSinceEpoch();
return msgs;
}
CStatusMessageList CDatabaseWriter::asyncAutoPublish(const CAutoPublishData &data)
{
CStatusMessageList msgs;
if (m_shutdown || !sApp)
{
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Database writer shutting down"));
return msgs;
}
if (data.isEmpty())
{
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"No auto update data"));
return msgs;
}
const QString json = data.toDatabaseJson();
const bool compress = json.size() > 2048;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
multiPart->append(CDatabaseUtils::getJsonTextMultipart(json, compress));
if (sApp->getGlobalSetup().dbDebugFlag())
{
multiPart->append(CDatabaseUtils::getMultipartWithDebugFlag());
}
QUrl url(m_modelPublishUrl.toQUrl());
if (compress) { url.setQuery(CDatabaseUtils::getCompressedQuery()); }
QNetworkRequest request(url);
CNetworkUtils::ignoreSslVerification(request);
const int logId = m_writeLog.addPendingUrl(url);
m_pendingAutoPublishReply = sApp->postToNetwork(request, logId, multiPart, { this, &CDatabaseWriter::postedAutoPublishResponse});
m_autoPublishReplyPendingSince = QDateTime::currentMSecsSinceEpoch();
return msgs; return msgs;
} }
void CDatabaseWriter::gracefulShutdown() void CDatabaseWriter::gracefulShutdown()
{ {
m_shutdown = true; m_shutdown = true;
this->killPendingReply(); this->killPendingModelReply();
} }
const QString &CDatabaseWriter::getName() const QString &CDatabaseWriter::getName()
@@ -109,20 +145,20 @@ namespace BlackCore
{ {
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::swiftDbWebservice()})); static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::swiftDbWebservice()}));
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr); QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
if (m_shutdown) if (m_shutdown || !sApp)
{ {
nwReply->abort(); nwReply->abort();
return; return;
} }
m_pendingReply = nullptr; m_pendingModelPublishReply = nullptr;
const QUrl url(nwReply->url()); const QUrl url(nwReply->url());
const QString urlString(url.toString()); const QString urlString(url.toString());
if (nwReply->error() == QNetworkReply::NoError) if (nwReply->error() == QNetworkReply::NoError)
{ {
const QString dataFileData(nwReply->readAll().trimmed()); const QString responseData(nwReply->readAll().trimmed());
nwReply->close(); // close asap nwReply->close(); // close asap
if (dataFileData.isEmpty()) if (responseData.isEmpty())
{ {
const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, u"No response data from " % urlString)}); const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, u"No response data from " % urlString)});
emit this->publishedModels(CAircraftModelList(), CAircraftModelList(), msgs, false, false); emit this->publishedModels(CAircraftModelList(), CAircraftModelList(), msgs, false, false);
@@ -133,7 +169,7 @@ namespace BlackCore
CAircraftModelList modelsSkipped; CAircraftModelList modelsSkipped;
CStatusMessageList msgs; CStatusMessageList msgs;
bool directWrite; bool directWrite;
const bool sendingSuccessful = CDatastoreUtility::parseSwiftPublishResponse(dataFileData, modelsPublished, modelsSkipped, msgs, directWrite); const bool sendingSuccessful = CDatastoreUtility::parseSwiftPublishResponse(responseData, modelsPublished, modelsSkipped, msgs, directWrite);
const int c = CDatabaseUtils::fillInMissingAircraftAndLiveryEntities(modelsPublished); const int c = CDatabaseUtils::fillInMissingAircraftAndLiveryEntities(modelsPublished);
emit this->publishedModels(modelsPublished, modelsSkipped, msgs, sendingSuccessful, directWrite); emit this->publishedModels(modelsPublished, modelsSkipped, msgs, sendingSuccessful, directWrite);
if (!modelsPublished.isEmpty()) if (!modelsPublished.isEmpty())
@@ -146,24 +182,55 @@ namespace BlackCore
{ {
const QString error = nwReply->errorString(); const QString error = nwReply->errorString();
nwReply->close(); // close asap nwReply->close(); // close asap
const CStatusMessageList msgs( {CStatusMessage(cats, CStatusMessage::SeverityError, u"HTTP error: " % error)}); const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, u"HTTP error: " % error)});
emit this->publishedModels(CAircraftModelList(), CAircraftModelList(), msgs, false, false); emit this->publishedModels(CAircraftModelList(), CAircraftModelList(), msgs, false, false);
} }
} }
bool CDatabaseWriter::killPendingReply() void CDatabaseWriter::postedAutoPublishResponse(QNetworkReply *nwReplyPtr)
{ {
if (!m_pendingReply) { return false; } static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::swiftDbWebservice()}));
m_pendingReply->abort(); QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
m_pendingReply = nullptr; if (m_shutdown || !sApp)
m_replyPendingSince = -1; {
nwReply->abort();
return;
}
m_pendingAutoPublishReply = nullptr;
const QUrl url(nwReply->url());
const QString urlString(url.toString());
if (nwReply->error() == QNetworkReply::NoError)
{
const QString responseData(nwReply->readAll().trimmed());
nwReply->close(); // close asap
if (responseData.isEmpty())
{
const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, u"No response data from " % urlString)});
return;
}
}
else
{
const QString error = nwReply->errorString();
nwReply->close(); // close asap
const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, u"HTTP error: " % error)});
}
}
bool CDatabaseWriter::killPendingModelReply()
{
if (!m_pendingModelPublishReply) { return false; }
m_pendingModelPublishReply->abort();
m_pendingModelPublishReply = nullptr;
m_modelReplyPendingSince = -1;
return true; return true;
} }
bool CDatabaseWriter::isReplyOverdue() const bool CDatabaseWriter::isModelReplyOverdue() const
{ {
if (m_replyPendingSince < 0 || !m_pendingReply) { return false; } if (m_modelReplyPendingSince < 0 || !m_pendingModelPublishReply) { return false; }
const qint64 ms = QDateTime::currentMSecsSinceEpoch() - m_replyPendingSince; const qint64 ms = QDateTime::currentMSecsSinceEpoch() - m_modelReplyPendingSince;
return ms > 7500; return ms > 7500;
} }
@@ -172,6 +239,11 @@ namespace BlackCore
return baseUrl.withAppendedPath("service/publishmodels.php"); return baseUrl.withAppendedPath("service/publishmodels.php");
} }
CUrl CDatabaseWriter::getAutoPublishUrl(const CUrl &baseUrl)
{
return baseUrl.withAppendedPath("service/publishauto.php");
}
QList<QByteArray> CDatabaseWriter::splitData(const QByteArray &data, int size) QList<QByteArray> CDatabaseWriter::splitData(const QByteArray &data, int size)
{ {
if (data.size() <= size) { return QList<QByteArray>({data}); } if (data.size() <= size) { return QList<QByteArray>({data}); }

View File

@@ -12,9 +12,9 @@
#define BLACKCORE_DATABASE_WRITER_H #define BLACKCORE_DATABASE_WRITER_H
#include "blackcore/blackcoreexport.h" #include "blackcore/blackcoreexport.h"
#include "blackmisc/network/url.h"
#include "blackmisc/network/urlloglist.h"
#include "blackmisc/simulation/aircraftmodellist.h" #include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/network/urlloglist.h"
#include "blackmisc/network/url.h"
#include "blackmisc/statusmessagelist.h" #include "blackmisc/statusmessagelist.h"
#include <QByteArray> #include <QByteArray>
@@ -23,6 +23,7 @@
class QNetworkReply; class QNetworkReply;
namespace BlackMisc { namespace Simulation { class CAutoPublishData; }}
namespace BlackCore namespace BlackCore
{ {
namespace Db namespace Db
@@ -42,6 +43,9 @@ namespace BlackCore
//! Write models to DB //! Write models to DB
BlackMisc::CStatusMessageList asyncPublishModels(const BlackMisc::Simulation::CAircraftModelList &models); BlackMisc::CStatusMessageList asyncPublishModels(const BlackMisc::Simulation::CAircraftModelList &models);
//! Write auto publis data
BlackMisc::CStatusMessageList asyncAutoPublish(const BlackMisc::Simulation::CAutoPublishData &data);
//! Shutdown //! Shutdown
void gracefulShutdown(); void gracefulShutdown();
@@ -66,23 +70,32 @@ namespace BlackCore
private: private:
BlackMisc::Network::CUrlLogList m_writeLog; BlackMisc::Network::CUrlLogList m_writeLog;
BlackMisc::Network::CUrl m_modelPublishUrl; BlackMisc::Network::CUrl m_modelPublishUrl; //!< model publishing
QNetworkReply *m_pendingReply = nullptr; BlackMisc::Network::CUrl m_autoPublishUrl; //!< auto publish data
qint64 m_replyPendingSince = -1; QNetworkReply *m_pendingModelPublishReply = nullptr;
QNetworkReply *m_pendingAutoPublishReply = nullptr;
qint64 m_modelReplyPendingSince = -1;
qint64 m_autoPublishReplyPendingSince = -1;
bool m_shutdown = false; bool m_shutdown = false;
//! Post response //! Post response for models
void postedModelsResponse(QNetworkReply *nwReplyPtr); void postedModelsResponse(QNetworkReply *nwReplyPtr);
//! Post response for auto publish
void postedAutoPublishResponse(QNetworkReply *nwReplyPtr);
//! Kill the pending reply //! Kill the pending reply
bool killPendingReply(); bool killPendingModelReply();
//! Reply timed out? //! Reply timed out?
bool isReplyOverdue() const; bool isModelReplyOverdue() const;
//! URL model web service //! URL model web service
static BlackMisc::Network::CUrl getModelPublishUrl(const BlackMisc::Network::CUrl &baseUrl); static BlackMisc::Network::CUrl getModelPublishUrl(const BlackMisc::Network::CUrl &baseUrl);
//! URL auto publish web service
static BlackMisc::Network::CUrl getAutoPublishUrl(const BlackMisc::Network::CUrl &baseUrl);
//! Split data array //! Split data array
static QList<QByteArray> splitData(const QByteArray &data, int size); static QList<QByteArray> splitData(const QByteArray &data, int size);
}; };

View File

@@ -167,6 +167,12 @@ namespace BlackCore
return CStatusMessageList(); return CStatusMessageList();
} }
CStatusMessageList CWebDataServices::asyncAutoPublish(const CAutoPublishData &data) const
{
if (m_databaseWriter) { return m_databaseWriter->asyncAutoPublish(data);}
return CStatusMessageList();
}
void CWebDataServices::triggerReadOfDbInfoObjects() void CWebDataServices::triggerReadOfDbInfoObjects()
{ {
initDbInfoObjectReaderAndTriggerRead(); initDbInfoObjectReaderAndTriggerRead();
@@ -1242,8 +1248,7 @@ namespace BlackCore
void CWebDataServices::initWriters() void CWebDataServices::initWriters()
{ {
m_databaseWriter = new CDatabaseWriter(sApp->getGlobalSetup().getDbRootDirectoryUrl(), m_databaseWriter = new CDatabaseWriter(sApp->getGlobalSetup().getDbRootDirectoryUrl(), this);
this);
} }
bool CWebDataServices::signalEntitiesAlreadyRead(CEntityFlags::Entity entities) bool CWebDataServices::signalEntitiesAlreadyRead(CEntityFlags::Entity entities)

View File

@@ -50,7 +50,11 @@ namespace BlackMisc
template <typename T> class Restricted; template <typename T> class Restricted;
namespace Aviation { class CCallsign; } namespace Aviation { class CCallsign; }
namespace Simulation { class CSimulatedAircraft; } namespace Simulation
{
class CSimulatedAircraft;
class CAutoPublishData;
}
} }
namespace BlackCore namespace BlackCore
@@ -387,6 +391,9 @@ namespace BlackCore
//! Publish models to database //! Publish models to database
BlackMisc::CStatusMessageList asyncPublishModels(const BlackMisc::Simulation::CAircraftModelList &modelsToBePublished) const; BlackMisc::CStatusMessageList asyncPublishModels(const BlackMisc::Simulation::CAircraftModelList &modelsToBePublished) const;
//! Auto publish to database
BlackMisc::CStatusMessageList asyncAutoPublish(const BlackMisc::Simulation::CAutoPublishData &data) const;
//! Trigger read of DB info objects //! Trigger read of DB info objects
void triggerReadOfDbInfoObjects(); void triggerReadOfDbInfoObjects();