mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 21:15:33 +08:00
134 lines
4.6 KiB
C++
134 lines
4.6 KiB
C++
/* Copyright (C) 2015
|
|
* swift project Community / Contributors
|
|
*
|
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
|
* contained in the LICENSE file.
|
|
*/
|
|
|
|
#include "databasewriter.h"
|
|
#include "blackcore/application.h"
|
|
#include "blackmisc/logmessage.h"
|
|
#include "blackmisc/datastoreutility.h"
|
|
#include "blackmisc/network/networkutils.h"
|
|
#include "blackmisc/project.h"
|
|
#include <QUrlQuery>
|
|
#include <QJsonDocument>
|
|
#include <QHttpPart>
|
|
#include <QHttpMultiPart>
|
|
|
|
using namespace BlackMisc;
|
|
using namespace BlackMisc::Network;
|
|
using namespace BlackMisc::Simulation;
|
|
|
|
namespace BlackCore
|
|
{
|
|
CDatabaseWriter::CDatabaseWriter(const Network::CUrl &baseUrl, QObject *parent) :
|
|
QObject(parent),
|
|
m_modelPublishUrl(getModelPublishUrl(baseUrl))
|
|
{
|
|
// void
|
|
}
|
|
|
|
CStatusMessageList CDatabaseWriter::asyncPublishModels(const CAircraftModelList &models)
|
|
{
|
|
CStatusMessageList msgs;
|
|
if (m_shutdown)
|
|
{
|
|
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Database writer shuts down"));
|
|
return msgs;
|
|
}
|
|
|
|
if (m_pendingReply)
|
|
{
|
|
msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Another write operation in progress"));
|
|
return msgs;
|
|
}
|
|
|
|
QUrl url(m_modelPublishUrl.toQUrl());
|
|
QNetworkRequest request(url);
|
|
CNetworkUtils::ignoreSslVerification(request);
|
|
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
|
|
multiPart->append(CNetworkUtils::getJsonTextMultipart(models.toDatabaseJson()));
|
|
if (sApp->getGlobalSetup().dbDebugFlag())
|
|
{
|
|
multiPart->append(CNetworkUtils::getMultipartWithDebugFlag());
|
|
}
|
|
|
|
m_pendingReply = sApp->postToNetwork(request, multiPart, { this, &CDatabaseWriter::ps_postResponse});
|
|
multiPart->setParent(m_pendingReply);
|
|
return msgs;
|
|
}
|
|
|
|
void CDatabaseWriter::gracefulShutdown()
|
|
{
|
|
m_shutdown = true;
|
|
if (m_pendingReply)
|
|
{
|
|
m_pendingReply->abort();
|
|
m_pendingReply = nullptr;
|
|
}
|
|
}
|
|
|
|
void CDatabaseWriter::ps_postResponse(QNetworkReply *nwReplyPtr)
|
|
{
|
|
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::swiftDbWebservice()}));
|
|
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
|
m_pendingReply = nullptr;
|
|
QUrl url(nwReply->url());
|
|
QString urlString(url.toString());
|
|
|
|
if (m_shutdown)
|
|
{
|
|
nwReply->abort();
|
|
return;
|
|
}
|
|
|
|
if (nwReply->error() == QNetworkReply::NoError)
|
|
{
|
|
const QString dataFileData(nwReply->readAll().trimmed());
|
|
nwReply->close(); // close asap
|
|
if (dataFileData.isEmpty())
|
|
{
|
|
const CStatusMessageList msgs({CStatusMessage(cats, CStatusMessage::SeverityError, "No response data from " + urlString)});
|
|
emit published(CAircraftModelList(), CAircraftModelList(), msgs);
|
|
return;
|
|
}
|
|
|
|
CAircraftModelList modelsPublished;
|
|
CAircraftModelList modelsSkipped;
|
|
CStatusMessageList msgs;
|
|
bool success = CDatastoreUtility::parseSwiftPublishResponse(dataFileData, modelsPublished, modelsSkipped, msgs);
|
|
emit published(modelsPublished, modelsSkipped, msgs);
|
|
Q_UNUSED(success);
|
|
}
|
|
else
|
|
{
|
|
QString error = nwReply->errorString();
|
|
nwReply->close(); // close asap
|
|
const CStatusMessageList msgs( {CStatusMessage(cats, CStatusMessage::SeverityError, "HTTP error: " + error)});
|
|
emit published(CAircraftModelList(), CAircraftModelList(), msgs);
|
|
}
|
|
}
|
|
|
|
CUrl CDatabaseWriter::getModelPublishUrl(const Network::CUrl &baseUrl)
|
|
{
|
|
return baseUrl.withAppendedPath("service/publishmodels.php");
|
|
}
|
|
|
|
QList<QByteArray> CDatabaseWriter::splitData(const QByteArray &data, int size)
|
|
{
|
|
if (data.size() <= size) { return QList<QByteArray>({data}); }
|
|
int pos = 0, arrsize = data.size();
|
|
QList<QByteArray> arrays;
|
|
while (pos < arrsize)
|
|
{
|
|
QByteArray arr = data.mid(pos, size);
|
|
arrays << arr;
|
|
pos += arr.size();
|
|
}
|
|
return arrays;
|
|
}
|
|
} // namespace
|