From 83c2495f8eafa8f79186530979083d689481fad7 2017-07-10 15:49:16 From: mperrinel Date: 2017-07-10 15:49:16 Subject: [PATCH] Modify the AmdaProvider to remove from it all network controller logical. --- diff --git a/plugins/amda/include/AmdaProvider.h b/plugins/amda/include/AmdaProvider.h index 38c5924..d6c0c59 100644 --- a/plugins/amda/include/AmdaProvider.h +++ b/plugins/amda/include/AmdaProvider.h @@ -12,6 +12,8 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaProvider) +class QNetworkReply; + /** * @brief The AmdaProvider class is an example of how a data provider can generate data */ @@ -22,15 +24,15 @@ public: void requestDataLoading(QUuid token, const QVector &dateTimeList) override; private: - void retrieveData(QUuid token, const DataProviderParameters ¶meters) const; + void retrieveData(QUuid token, const DataProviderParameters ¶meters); class AmdaProviderPrivate; spimpl::unique_impl_ptr impl; -private slots: - void httpFinished() noexcept; - void httpDownloadFinished() noexcept; - void httpDownloadReadyRead() noexcept; + // private slots: + // void httpFinished(QNetworkReply *reply, QUuid dataId) noexcept; + // void httpDownloadFinished(QNetworkReply *reply, QUuid dataId) noexcept; + // void httpDownloadReadyRead(QNetworkReply *reply, QUuid dataId) noexcept; }; #endif // SCIQLOP_AMDAPROVIDER_H diff --git a/plugins/amda/src/AmdaProvider.cpp b/plugins/amda/src/AmdaProvider.cpp index 88777fc..a7a6911 100644 --- a/plugins/amda/src/AmdaProvider.cpp +++ b/plugins/amda/src/AmdaProvider.cpp @@ -2,10 +2,14 @@ #include "AmdaResultParser.h" #include +#include +#include +#include #include #include #include +#include Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") @@ -30,18 +34,26 @@ QString dateFormat(double sqpDateTime) noexcept return dateTime.toString(AMDA_TIME_FORMAT); } + } // namespace struct AmdaProvider::AmdaProviderPrivate { DataProviderParameters m_Params{}; std::unique_ptr m_AccessManager{nullptr}; QNetworkReply *m_Reply{nullptr}; - std::unique_ptr m_File{nullptr}; + // std::unique_ptr m_File{nullptr}; QUuid m_Token; }; AmdaProvider::AmdaProvider() : impl{spimpl::make_unique_impl()} { + qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") + << QThread::currentThread(); + if (auto app = sqpApp) { + auto &networkController = app->networkController(); + connect(this, &AmdaProvider::requestConstructed, &networkController, + &NetworkController::onProcessRequested); + } } void AmdaProvider::requestDataLoading(QUuid token, const QVector &dateTimeList) @@ -52,7 +64,7 @@ void AmdaProvider::requestDataLoading(QUuid token, const QVector &d } } -void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶meters) const +void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶meters) { // /////////// // // Creates URL // @@ -64,71 +76,53 @@ void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶m auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; - // //////////////// // - // Executes request // - // //////////////// // + auto tempFile = std::make_shared(); - impl->m_Token = token; - impl->m_Params = parameters; - impl->m_AccessManager = std::make_unique(); - impl->m_Reply = impl->m_AccessManager->get(QNetworkRequest{url}); - connect(impl->m_Reply, &QNetworkReply::finished, this, &AmdaProvider::httpFinished); -} -void AmdaProvider::httpFinished() noexcept -{ - // ////////////////////// // - // Gets download file url // - // ////////////////////// // - - auto downloadFileUrl = QUrl{QString{impl->m_Reply->readAll()}}; - - // ///////////////////////////////////// // - // Executes request for downloading file // - // ///////////////////////////////////// // - - // Deletes old reply - impl->m_Reply->deleteLater(); - impl->m_Reply = nullptr; - - // Creates destination file - impl->m_File = std::make_unique(); - if (impl->m_File->open()) { - qCDebug(LOG_AmdaProvider()) << "Temp file: " << impl->m_File->fileName(); - - // Executes request - impl->m_AccessManager = std::make_unique(); - impl->m_Reply = impl->m_AccessManager->get(QNetworkRequest{downloadFileUrl}); - connect(impl->m_Reply, &QNetworkReply::finished, this, - &AmdaProvider::httpDownloadReadyRead); - connect(impl->m_Reply, &QNetworkReply::finished, this, &AmdaProvider::httpDownloadFinished); - } -} + // LAMBDA + auto httpDownloadFinished = [this, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { -void AmdaProvider::httpDownloadFinished() noexcept -{ - if (impl->m_File) { - impl->m_File->close(); + if (tempFile) { + auto replyReadAll = reply->readAll(); + if (!replyReadAll.isEmpty()) { + tempFile->write(replyReadAll); + } + tempFile->close(); - // Parse results file - if (auto dataSeries = AmdaResultParser::readTxt(impl->m_File->fileName())) { - emit dataProvided(impl->m_Token, dataSeries, impl->m_Params.m_Time); - } - else { - /// @todo ALX : debug + // Parse results file + if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { + emit dataProvided(impl->m_Token, dataSeries, impl->m_Params.m_Time); + } + else { + /// @todo ALX : debug + } } - impl->m_File = nullptr; - } + // Deletes reply + reply->deleteLater(); + reply = nullptr; + }; + auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, + QUuid dataId) noexcept { - // Deletes reply - impl->m_Reply->deleteLater(); - impl->m_Reply = nullptr; -} + auto downloadFileUrl = QUrl{QString{reply->readAll()}}; + // Deletes old reply + reply->deleteLater(); -void AmdaProvider::httpDownloadReadyRead() noexcept -{ - if (impl->m_File) { - impl->m_File->write(impl->m_Reply->readAll()); - } + // Executes request for downloading file // + + // Creates destination file + if (tempFile->open()) { + // Executes request + emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); + } + }; + + // //////////////// // + // Executes request // + // //////////////// // + + impl->m_Token = token; + impl->m_Params = parameters; + emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); }