diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index 1fd9c9c..722afb7 100644 --- a/core/include/Variable/VariableController.h +++ b/core/include/Variable/VariableController.h @@ -68,10 +68,6 @@ public: */ void deleteVariables(const QVector > &variables) noexcept; - /** - * @brief abort the variable retrieve data progression - */ - void abortProgress(std::shared_ptr variable); static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange); signals: diff --git a/core/src/Variable/VariableAcquisitionWorker.cpp b/core/src/Variable/VariableAcquisitionWorker.cpp index 5a3299b..0d095fc 100644 --- a/core/src/Variable/VariableAcquisitionWorker.cpp +++ b/core/src/Variable/VariableAcquisitionWorker.cpp @@ -18,7 +18,10 @@ Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker") struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { - explicit VariableAcquisitionWorkerPrivate() : m_Lock{QReadWriteLock::Recursive} {} + explicit VariableAcquisitionWorkerPrivate(VariableAcquisitionWorker *parent) + : m_Lock{QReadWriteLock::Recursive}, q{parent} + { + } void lockRead() { m_Lock.lockForRead(); } void lockWrite() { m_Lock.lockForWrite(); } @@ -26,17 +29,21 @@ struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { void removeVariableRequest(QUuid vIdentifier); + /// Remove the current request and execute the next one if exist + void updateToNextRequest(QUuid vIdentifier); + QMutex m_WorkingMutex; QReadWriteLock m_Lock; std::map > m_AcqIdentifierToAcqDataPacketVectorMap; std::map m_AcqIdentifierToAcqRequestMap; std::map > m_VIdentifierToCurrrentAcqIdNextIdPairMap; + VariableAcquisitionWorker *q; }; VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent) - : QObject{parent}, impl{spimpl::make_unique_impl()} + : QObject{parent}, impl{spimpl::make_unique_impl(this)} { } @@ -103,6 +110,35 @@ QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid v void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier) { // TODO + impl->lockRead(); + + auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); + if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + auto currentAcqId = it->second.first; + + auto it = impl->m_AcqIdentifierToAcqRequestMap.find(currentAcqId); + if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) { + auto request = it->second; + impl->unlock(); + + // Remove the current request from the worker + + impl->lockWrite(); + impl->updateToNextRequest(vIdentifier); + impl->unlock(); + + // notify the request aborting to the provider + request.m_Provider->requestDataAborting(currentAcqId); + } + else { + impl->unlock(); + qCWarning(LOG_VariableAcquisitionWorker()) + << tr("Impossible to abort an unknown acquisition request") << currentAcqId; + } + } + else { + impl->unlock(); + } } void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier, @@ -196,8 +232,8 @@ void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier, } } else { - qCCritical(LOG_VariableAcquisitionWorker()) - << tr("Impossible to retrieve AcquisitionRequest for the incoming data"); + qCWarning(LOG_VariableAcquisitionWorker()) + << tr("Impossible to retrieve AcquisitionRequest for the incoming data."); } impl->unlock(); } @@ -255,3 +291,31 @@ void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariable m_VIdentifierToCurrrentAcqIdNextIdPairMap.erase(vIdentifier); unlock(); } + +void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::updateToNextRequest( + QUuid vIdentifier) +{ + auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); + if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + if (it->second.second.isNull()) { + // There is no next request, we can remove the variable request + removeVariableRequest(vIdentifier); + } + else { + auto acqIdentifierToRemove = it->second.first; + // Move the next request to the current request + it->second.first = it->second.second; + it->second.second = QUuid(); + // Remove AcquisitionRequest and results; + m_AcqIdentifierToAcqRequestMap.erase(acqIdentifierToRemove); + m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqIdentifierToRemove); + // Execute the current request + QMetaObject::invokeMethod(q, "onExecuteRequest", Qt::QueuedConnection, + Q_ARG(QUuid, it->second.first)); + } + } + else { + qCCritical(LOG_VariableAcquisitionWorker()) + << tr("Impossible to execute the acquisition on an unfound variable "); + } +} diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 2510e13..3c8dde4 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -253,10 +253,6 @@ void VariableController::deleteVariables( } } -void VariableController::abortProgress(std::shared_ptr variable) -{ -} - std::shared_ptr VariableController::createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr provider) noexcept @@ -281,7 +277,6 @@ VariableController::createVariable(const QString &name, const QVariantHash &meta auto varRequestId = QUuid::createUuid(); - qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId; impl->processRequest(newVariable, range, varRequestId); impl->updateVariableRequest(varRequestId); @@ -336,12 +331,25 @@ void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, doub void VariableController::onAbortProgressRequested(std::shared_ptr variable) { - qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested" - << QThread::currentThread()->objectName(); - auto it = impl->m_VariableToIdentifierMap.find(variable); if (it != impl->m_VariableToIdentifierMap.cend()) { - impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second); + impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second); + + QUuid varRequestId; + auto varIdToVarRequestIdQueueMapIt = impl->m_VarIdToVarRequestIdQueueMap.find(it->second); + if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) { + auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second; + varRequestId = varRequestIdQueue.front(); + impl->cancelVariableRequest(varRequestId); + + // Finish the progression for the request + impl->m_VariableModel->setDataProgress(variable, 0.0); + } + else { + qCWarning(LOG_VariableController()) + << tr("Aborting progression of inexistant variable request detected !!!") + << QThread::currentThread()->objectName(); + } } else { qCWarning(LOG_VariableController()) @@ -434,8 +442,8 @@ void VariableController::onRequestDataLoading(QVector // For the other, we ask the provider to give them. auto varRequestId = QUuid::createUuid(); - qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading" - << QThread::currentThread()->objectName() << varRequestId; + qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" + << QThread::currentThread()->objectName() << varRequestId; for (const auto &var : variables) { qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId; @@ -552,11 +560,7 @@ void VariableController::VariableControllerPrivate::processRequest(std::shared_p if (!notInCacheRangeList.empty()) { varRequest.m_RangeRequested = varStrategyRangesRequested.first; varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second; - qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested; - qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ") - << varStrategyRangesRequested.first; - qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ") - << varStrategyRangesRequested.second; + // store VarRequest storeVariableRequest(varId, varRequestId, varRequest); @@ -569,8 +573,8 @@ void VariableController::VariableControllerPrivate::processRequest(std::shared_p varProvider); if (!varRequestIdCanceled.isNull()) { - qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ") - << varRequestIdCanceled; + qCDebug(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ") + << varRequestIdCanceled; cancelVariableRequest(varRequestIdCanceled); } } @@ -762,9 +766,11 @@ void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid /// @todo MPL: confirm // Variable update is notified only if there is no pending request for it - if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) { - emit var->updated(); - } + // if + // (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) + // == 0) { + emit var->updated(); + // } } else { qCCritical(LOG_VariableController())