From 13937fa7979a857a44f9a0f3fa27248648a5c1e5 2017-07-10 15:49:16 From: mperrinel Date: 2017-07-10 15:49:16 Subject: [PATCH] Implement the network controller to permit the execution of a request with a callback given by plugin --- diff --git a/core/include/Network/NetworkController.h b/core/include/Network/NetworkController.h index bbcb27e..dd4fca5 100644 --- a/core/include/Network/NetworkController.h +++ b/core/include/Network/NetworkController.h @@ -3,12 +3,15 @@ #include #include +#include #include +#include Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController) class QNetworkReply; +class QNetworkRequest; /** * @brief The NetworkController class aims to handle all network connection of SciQlop. @@ -18,14 +21,17 @@ class NetworkController : public QObject { public: explicit NetworkController(QObject *parent = 0); - void execute(QNetworkReply *reply); - - void initialize(); void finalize(); +public slots: + void onProcessRequested(const QNetworkRequest &request, QUuid identifier, + std::function callback); + void onReplyCanceled(QUuid identifier); + signals: - replyToRead(); + void replyFinished(QNetworkReply *reply, QUuid identifier); + void replyDownloadProgress(QUuid identifier); private: void waitForFinish(); diff --git a/core/src/Network/NetworkController.cpp b/core/src/Network/NetworkController.cpp index aa25b51..eadc56b 100644 --- a/core/src/Network/NetworkController.cpp +++ b/core/src/Network/NetworkController.cpp @@ -3,42 +3,66 @@ #include #include #include +#include #include +#include + Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") struct NetworkController::NetworkControllerPrivate { - explicit NetworkControllerPrivate(NetworkController *parent) - : m_WorkingMutex{}, m_AccessManager{std::make_unique()} - { - } + explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {} QMutex m_WorkingMutex; + std::unordered_map m_NetworkReplyToVariableId; std::unique_ptr m_AccessManager{nullptr}; }; NetworkController::NetworkController(QObject *parent) : QObject(parent), impl{spimpl::make_unique_impl(this)} { - } -void NetworkController::execute(QNetworkReply *reply) +void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier, + std::function callback) { - auto replyReadyToRead =[reply, this] () { - auto content = reply->readAll(); + qCDebug(LOG_NetworkController()) << tr("NetworkController registered") + << QThread::currentThread(); + auto reply = impl->m_AccessManager->get(request); + + // Store the couple reply id + impl->m_NetworkReplyToVariableId[reply] = identifier; + + auto onReplyFinished = [reply, this, identifier, callback]() { + + qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished") + << QThread::currentThread(); + auto it = impl->m_NetworkReplyToVariableId.find(reply); + if (it != impl->m_NetworkReplyToVariableId.cend()) { + callback(reply, identifier); + } + }; + + auto onReplyDownloadProgress = [reply, this]() { - emit this->replyToRead(); - }; + qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyDownloadProgress") + << QThread::currentThread(); + auto it = impl->m_NetworkReplyToVariableId.find(reply); + if (it != impl->m_NetworkReplyToVariableId.cend()) { + emit this->replyDownloadProgress(it->second); + } + }; - connect(impl->m_Reply, &QNetworkReply::finished, this, replyReadyToRead); - connect(impl->m_Reply, &QNetworkReply::aboutToClose, this, replyReadyToRead); + + connect(reply, &QNetworkReply::finished, this, onReplyFinished); + connect(reply, &QNetworkReply::downloadProgress, this, onReplyDownloadProgress); } void NetworkController::initialize() { qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); impl->m_WorkingMutex.lock(); + impl->m_AccessManager = std::make_unique(); qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); } @@ -47,6 +71,17 @@ void NetworkController::finalize() impl->m_WorkingMutex.unlock(); } +void NetworkController::onReplyCanceled(QUuid identifier) +{ + auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; + + auto end = impl->m_NetworkReplyToVariableId.cend(); + auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply); + if (it != end) { + it->first->abort(); + } +} + void NetworkController::waitForFinish() { QMutexLocker locker{&impl->m_WorkingMutex}; diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 24bebc2..3039def 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -38,7 +38,7 @@ struct VariableController::VariableControllerPrivate { std::unordered_map, std::shared_ptr > m_VariableToProviderMap; - std::unordered_map, QUuid> m_VariableToToken; + std::unordered_map, QUuid> m_VariableToIdentifier; }; VariableController::VariableController(QObject *parent) @@ -120,18 +120,18 @@ void VariableController::createVariable(const QString &name, /// in sciqlop auto dateTime = impl->m_TimeController->dateTime(); if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { - auto token = QUuid::createUuid(); + auto identifier = QUuid::createUuid(); // store the provider impl->m_VariableToProviderMap[newVariable] = provider; - impl->m_VariableToToken[newVariable] = token; + impl->m_VariableToIdentifier[newVariable] = identifier; auto addDateTimeAcquired = [ this, varW = std::weak_ptr{newVariable} ]( - QUuid token, auto dataSeriesAcquired, auto dateTimeToPutInCache) + QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) { if (auto variable = varW.lock()) { - auto varToken = impl->m_VariableToToken.at(variable); - if (varToken == token) { + auto varIdentifier = impl->m_VariableToIdentifier.at(variable); + if (varIdentifier == identifier) { impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); variable->setDataSeries(dataSeriesAcquired); } @@ -173,9 +173,9 @@ void VariableController::onRequestDataLoading(std::shared_ptr variable if (!dateTimeListNotInCache.empty()) { // Ask the provider for each data on the dateTimeListNotInCache - auto token = impl->m_VariableToToken.at(variable); + auto identifier = impl->m_VariableToIdentifier.at(variable); impl->m_VariableToProviderMap.at(variable)->requestDataLoading( - token, std::move(dateTimeListNotInCache)); + identifier, std::move(dateTimeListNotInCache)); } else { emit variable->updated(); diff --git a/gui/src/SqpApplication.cpp b/gui/src/SqpApplication.cpp index a50fa86..597a3a3 100644 --- a/gui/src/SqpApplication.cpp +++ b/gui/src/SqpApplication.cpp @@ -78,7 +78,7 @@ public: SqpApplication::SqpApplication(int &argc, char **argv) : QApplication{argc, argv}, impl{spimpl::make_unique_impl()} { - qCInfo(LOG_SqpApplication()) << tr("SqpApplication construction"); + qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread(); connect(&impl->m_DataSourceControllerThread, &QThread::started, impl->m_DataSourceController.get(), &DataSourceController::initialize);