diff --git a/core/src/Variable/VariableAcquisitionWorker.cpp b/core/src/Variable/VariableAcquisitionWorker.cpp index d595e5f..0b5132e 100644 --- a/core/src/Variable/VariableAcquisitionWorker.cpp +++ b/core/src/Variable/VariableAcquisitionWorker.cpp @@ -29,9 +29,6 @@ struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { void removeVariableRequest(QUuid vIdentifier); - /// Remove the current request and execute the next one if exist - void updateToNextRequest(QUuid vIdentifier); - /// Remove and/or abort all AcqRequest in link with varRequestId void cancelVarRequest(QUuid varRequestId); void removeAcqRequest(QUuid acqRequestId); @@ -41,7 +38,7 @@ struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { std::map > m_AcqIdentifierToAcqDataPacketVectorMap; std::map m_AcqIdentifierToAcqRequestMap; - std::map > m_VIdentifierToCurrrentAcqIdNextIdPairMap; + std::map m_VIdentifierToCurrrentAcqIdMap; VariableAcquisitionWorker *q; }; @@ -70,12 +67,13 @@ QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid v // Request creation auto acqRequest = AcquisitionRequest{}; - qCDebug(LOG_VariableAcquisitionWorker()) << tr("Add acqRequest ") << acqRequest.m_AcqIdentifier; acqRequest.m_VarRequestId = varRequestId; acqRequest.m_vIdentifier = vIdentifier; acqRequest.m_DataProviderParameters = parameters; acqRequest.m_Size = parameters.m_Times.size(); acqRequest.m_Provider = provider; + qCInfo(LOG_VariableAcquisitionWorker()) << tr("Add acqRequest ") << acqRequest.m_AcqIdentifier + << acqRequest.m_Size; // Register request @@ -83,33 +81,32 @@ QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid v impl->m_AcqIdentifierToAcqRequestMap.insert( std::make_pair(acqRequest.m_AcqIdentifier, acqRequest)); - auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); - if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { - // A current request already exists, we can replace the next one - auto oldAcqId = it->second.second; + auto it = impl->m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); + if (it != impl->m_VIdentifierToCurrrentAcqIdMap.cend()) { + // A current request already exists, we can cancel it + // remove old acqIdentifier from the worker + auto oldAcqId = it->second; auto acqIdentifierToAcqRequestMapIt = impl->m_AcqIdentifierToAcqRequestMap.find(oldAcqId); if (acqIdentifierToAcqRequestMapIt != impl->m_AcqIdentifierToAcqRequestMap.cend()) { auto oldAcqRequest = acqIdentifierToAcqRequestMapIt->second; varRequestIdCanceled = oldAcqRequest.m_VarRequestId; } - - it->second.second = acqRequest.m_AcqIdentifier; impl->unlock(); - - // remove old acqIdentifier from the worker impl->cancelVarRequest(varRequestIdCanceled); - // impl->m_AcqIdentifierToAcqRequestMap.erase(oldAcqId); } else { - // First request for the variable, it must be stored and executed - impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.insert( - std::make_pair(vIdentifier, std::make_pair(acqRequest.m_AcqIdentifier, QUuid()))); impl->unlock(); - - QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection, - Q_ARG(QUuid, acqRequest.m_AcqIdentifier)); } + // Request for the variable, it must be stored and executed + impl->lockWrite(); + impl->m_VIdentifierToCurrrentAcqIdMap.insert( + std::make_pair(vIdentifier, acqRequest.m_AcqIdentifier)); + impl->unlock(); + + QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection, + Q_ARG(QUuid, acqRequest.m_AcqIdentifier)); + return varRequestIdCanceled; } @@ -117,18 +114,15 @@ void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier) { impl->lockRead(); - auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); - if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { - auto currentAcqId = it->second.first; + auto it = impl->m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); + if (it != impl->m_VIdentifierToCurrrentAcqIdMap.cend()) { + auto currentAcqId = it->second; 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->updateToNextRequest(vIdentifier); - // notify the request aborting to the provider request.m_Provider->requestDataAborting(currentAcqId); } @@ -146,26 +140,39 @@ void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier) void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier, double progress) { - qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ") - << QThread::currentThread()->objectName() - << acqIdentifier << progress; + qCInfo(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ") + << QThread::currentThread()->objectName() + << acqIdentifier << progress; impl->lockRead(); auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) { - auto currentPartSize = (aIdToARit->second.m_Size != 0) ? 100 / aIdToARit->second.m_Size : 0; + auto progressPartSize + = (aIdToARit->second.m_Size != 0) ? 100 / aIdToARit->second.m_Size : 0; auto currentPartProgress - = std::isnan(progress) ? 0.0 : (progress * currentPartSize) / 100.0; - auto currentAlreadyProgress = aIdToARit->second.m_Progression * currentPartSize; + = std::isnan(progress) ? 0.0 : (progress * progressPartSize) / 100.0; + + // We can only give an approximation of the currentProgression since its upgrade is async. + qCInfo(LOG_VariableAcquisitionWorker()) + << tr("Progression: ") << aIdToARit->second.m_Progression << aIdToARit->second.m_Size; + auto currentProgression = aIdToARit->second.m_Progression; + if (currentProgression == aIdToARit->second.m_Size) { + currentProgression = aIdToARit->second.m_Size - 1; + } + + auto currentAlreadyProgress = progressPartSize * currentProgression; + auto finalProgression = currentAlreadyProgress + currentPartProgress; emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, finalProgression); - qCDebug(LOG_VariableAcquisitionWorker()) + qCInfo(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ") << QThread::currentThread()->objectName() << aIdToARit->second.m_vIdentifier - << currentPartSize << currentAlreadyProgress << currentPartProgress << finalProgression; + << progressPartSize << currentAlreadyProgress << currentPartProgress + << finalProgression; + if (finalProgression == 100.0) { - qCDebug(LOG_VariableAcquisitionWorker()) + qCInfo(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress : finished : 0.0 "); emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, 0.0); } @@ -221,6 +228,8 @@ void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier, // Decrement the counter of the request auto &acqRequest = aIdToARit->second; + qCInfo(LOG_VariableAcquisitionWorker()) << tr("TORM: +1 update progresson ") + << acqIdentifier; acqRequest.m_Progression = acqRequest.m_Progression + 1; // if the counter is 0, we can return data then run the next request if it exists and @@ -233,9 +242,6 @@ void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier, emit dataProvided(varId, aIdToADPVit->second); } impl->unlock(); - - // Update to the next request - impl->updateToNextRequest(acqRequest.m_vIdentifier); } else { impl->unlock(); @@ -289,68 +295,25 @@ void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariable QUuid vIdentifier) { lockWrite(); - auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); + auto it = m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); - if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + if (it != m_VIdentifierToCurrrentAcqIdMap.cend()) { // A current request already exists, we can replace the next one qCDebug(LOG_VariableAcquisitionWorker()) << "VariableAcquisitionWorkerPrivate::removeVariableRequest " - << QThread::currentThread()->objectName() << it->second.first; - m_AcqIdentifierToAcqRequestMap.erase(it->second.first); - m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.first); - - qCDebug(LOG_VariableAcquisitionWorker()) - << "VariableAcquisitionWorkerPrivate::removeVariableRequest " << it->second.second; - m_AcqIdentifierToAcqRequestMap.erase(it->second.second); - m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.second); + << QThread::currentThread()->objectName() << it->second; + m_AcqIdentifierToAcqRequestMap.erase(it->second); + m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second); } // stop any progression emit q->variableRequestInProgress(vIdentifier, 0.0); - m_VIdentifierToCurrrentAcqIdNextIdPairMap.erase(vIdentifier); + m_VIdentifierToCurrrentAcqIdMap.erase(vIdentifier); unlock(); } -void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::updateToNextRequest( - QUuid vIdentifier) -{ - lockRead(); - auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); - if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { - if (it->second.second.isNull()) { - unlock(); - // 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 - auto nextRequestId = it->second.second; - it->second.first = nextRequestId; - it->second.second = QUuid(); - unlock(); - // Remove AcquisitionRequest and results; - lockWrite(); - qCDebug(LOG_VariableAcquisitionWorker()) - << "VariableAcquisitionWorkerPrivate::updateToNextRequest removed: " - << acqIdentifierToRemove; - m_AcqIdentifierToAcqRequestMap.erase(acqIdentifierToRemove); - m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqIdentifierToRemove); - unlock(); - // Execute the current request - QMetaObject::invokeMethod(q, "onExecuteRequest", Qt::QueuedConnection, - Q_ARG(QUuid, nextRequestId)); - } - } - else { - unlock(); - qCCritical(LOG_VariableAcquisitionWorker()) - << tr("Impossible to execute the acquisition on an unfound variable "); - } -} - void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::cancelVarRequest( QUuid varRequestId) { @@ -388,22 +351,15 @@ void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeAcqReque vIdentifier = acqIt->second.m_vIdentifier; provider = acqIt->second.m_Provider; - auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); - if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { - if (it->second.first == acqRequestId) { + auto it = m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); + if (it != m_VIdentifierToCurrrentAcqIdMap.cend()) { + if (it->second == acqRequestId) { // acqRequest is currently running -> let's aborting it unlock(); - // Remove the current request from the worker - updateToNextRequest(vIdentifier); - // notify the request aborting to the provider provider->requestDataAborting(acqRequestId); } - else if (it->second.second == acqRequestId) { - it->second.second = QUuid(); - unlock(); - } else { unlock(); }