##// END OF EJS Templates
Remove regression for aborting request during processRequest method
perrinel -
r827:83da67fa98c8
parent child
Show More
@@ -1,415 +1,416
1 #include "Variable/VariableAcquisitionWorker.h"
1 #include "Variable/VariableAcquisitionWorker.h"
2
2
3 #include "Variable/Variable.h"
3 #include "Variable/Variable.h"
4
4
5 #include <Data/AcquisitionRequest.h>
5 #include <Data/AcquisitionRequest.h>
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <unordered_map>
8 #include <unordered_map>
9 #include <utility>
9 #include <utility>
10
10
11 #include <QMutex>
11 #include <QMutex>
12 #include <QReadWriteLock>
12 #include <QReadWriteLock>
13 #include <QThread>
13 #include <QThread>
14
14
15 #include <cmath>
15 #include <cmath>
16
16
17 Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker")
17 Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker")
18
18
19 struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate {
19 struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate {
20
20
21 explicit VariableAcquisitionWorkerPrivate(VariableAcquisitionWorker *parent)
21 explicit VariableAcquisitionWorkerPrivate(VariableAcquisitionWorker *parent)
22 : m_Lock{QReadWriteLock::Recursive}, q{parent}
22 : m_Lock{QReadWriteLock::Recursive}, q{parent}
23 {
23 {
24 }
24 }
25
25
26 void lockRead() { m_Lock.lockForRead(); }
26 void lockRead() { m_Lock.lockForRead(); }
27 void lockWrite() { m_Lock.lockForWrite(); }
27 void lockWrite() { m_Lock.lockForWrite(); }
28 void unlock() { m_Lock.unlock(); }
28 void unlock() { m_Lock.unlock(); }
29
29
30 void removeVariableRequest(QUuid vIdentifier);
30 void removeVariableRequest(QUuid vIdentifier);
31
31
32 /// Remove the current request and execute the next one if exist
32 /// Remove the current request and execute the next one if exist
33 void updateToNextRequest(QUuid vIdentifier);
33 void updateToNextRequest(QUuid vIdentifier);
34
34
35 /// Remove and/or abort all AcqRequest in link with varRequestId
35 /// Remove and/or abort all AcqRequest in link with varRequestId
36 void cancelVarRequest(QUuid varRequestId);
36 void cancelVarRequest(QUuid varRequestId);
37 void removeAcqRequest(QUuid acqRequestId);
37 void removeAcqRequest(QUuid acqRequestId);
38
38
39 QMutex m_WorkingMutex;
39 QMutex m_WorkingMutex;
40 QReadWriteLock m_Lock;
40 QReadWriteLock m_Lock;
41
41
42 std::map<QUuid, QVector<AcquisitionDataPacket> > m_AcqIdentifierToAcqDataPacketVectorMap;
42 std::map<QUuid, QVector<AcquisitionDataPacket> > m_AcqIdentifierToAcqDataPacketVectorMap;
43 std::map<QUuid, AcquisitionRequest> m_AcqIdentifierToAcqRequestMap;
43 std::map<QUuid, AcquisitionRequest> m_AcqIdentifierToAcqRequestMap;
44 std::map<QUuid, std::pair<QUuid, QUuid> > m_VIdentifierToCurrrentAcqIdNextIdPairMap;
44 std::map<QUuid, std::pair<QUuid, QUuid> > m_VIdentifierToCurrrentAcqIdNextIdPairMap;
45 VariableAcquisitionWorker *q;
45 VariableAcquisitionWorker *q;
46 };
46 };
47
47
48
48
49 VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent)
49 VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent)
50 : QObject{parent}, impl{spimpl::make_unique_impl<VariableAcquisitionWorkerPrivate>(this)}
50 : QObject{parent}, impl{spimpl::make_unique_impl<VariableAcquisitionWorkerPrivate>(this)}
51 {
51 {
52 }
52 }
53
53
54 VariableAcquisitionWorker::~VariableAcquisitionWorker()
54 VariableAcquisitionWorker::~VariableAcquisitionWorker()
55 {
55 {
56 qCInfo(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker destruction")
56 qCInfo(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker destruction")
57 << QThread::currentThread();
57 << QThread::currentThread();
58 this->waitForFinish();
58 this->waitForFinish();
59 }
59 }
60
60
61
61
62 QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid vIdentifier,
62 QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid vIdentifier,
63 SqpRange rangeRequested,
63 SqpRange rangeRequested,
64 SqpRange cacheRangeRequested,
64 SqpRange cacheRangeRequested,
65 DataProviderParameters parameters,
65 DataProviderParameters parameters,
66 std::shared_ptr<IDataProvider> provider)
66 std::shared_ptr<IDataProvider> provider)
67 {
67 {
68 qCDebug(LOG_VariableAcquisitionWorker())
68 qCDebug(LOG_VariableAcquisitionWorker())
69 << tr("TORM VariableAcquisitionWorker::pushVariableRequest ") << cacheRangeRequested;
69 << tr("TORM VariableAcquisitionWorker::pushVariableRequest ") << cacheRangeRequested;
70 auto varRequestIdCanceled = QUuid();
70 auto varRequestIdCanceled = QUuid();
71
71
72 // Request creation
72 // Request creation
73 auto acqRequest = AcquisitionRequest{};
73 auto acqRequest = AcquisitionRequest{};
74 qCDebug(LOG_VariableAcquisitionWorker()) << tr("PushVariableRequest ") << vIdentifier
74 qCDebug(LOG_VariableAcquisitionWorker()) << tr("PushVariableRequest ") << vIdentifier
75 << varRequestId;
75 << varRequestId;
76 acqRequest.m_VarRequestId = varRequestId;
76 acqRequest.m_VarRequestId = varRequestId;
77 acqRequest.m_vIdentifier = vIdentifier;
77 acqRequest.m_vIdentifier = vIdentifier;
78 acqRequest.m_DataProviderParameters = parameters;
78 acqRequest.m_DataProviderParameters = parameters;
79 acqRequest.m_RangeRequested = rangeRequested;
79 acqRequest.m_RangeRequested = rangeRequested;
80 acqRequest.m_CacheRangeRequested = cacheRangeRequested;
80 acqRequest.m_CacheRangeRequested = cacheRangeRequested;
81 acqRequest.m_Size = parameters.m_Times.size();
81 acqRequest.m_Size = parameters.m_Times.size();
82 acqRequest.m_Provider = provider;
82 acqRequest.m_Provider = provider;
83
83
84
84
85 // Register request
85 // Register request
86 impl->lockWrite();
86 impl->lockWrite();
87 impl->m_AcqIdentifierToAcqRequestMap.insert(
87 impl->m_AcqIdentifierToAcqRequestMap.insert(
88 std::make_pair(acqRequest.m_AcqIdentifier, acqRequest));
88 std::make_pair(acqRequest.m_AcqIdentifier, acqRequest));
89
89
90 auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
90 auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
91 if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
91 if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
92 // A current request already exists, we can replace the next one
92 // A current request already exists, we can replace the next one
93 auto oldAcqId = it->second.second;
93 auto oldAcqId = it->second.second;
94 auto acqIdentifierToAcqRequestMapIt = impl->m_AcqIdentifierToAcqRequestMap.find(oldAcqId);
94 auto acqIdentifierToAcqRequestMapIt = impl->m_AcqIdentifierToAcqRequestMap.find(oldAcqId);
95 if (acqIdentifierToAcqRequestMapIt != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
95 if (acqIdentifierToAcqRequestMapIt != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
96 auto oldAcqRequest = acqIdentifierToAcqRequestMapIt->second;
96 auto oldAcqRequest = acqIdentifierToAcqRequestMapIt->second;
97 varRequestIdCanceled = oldAcqRequest.m_VarRequestId;
97 varRequestIdCanceled = oldAcqRequest.m_VarRequestId;
98 }
98 }
99
99
100 it->second.second = acqRequest.m_AcqIdentifier;
100 it->second.second = acqRequest.m_AcqIdentifier;
101 impl->unlock();
101 impl->unlock();
102
102
103 // remove old acqIdentifier from the worker
103 // remove old acqIdentifier from the worker
104 impl->cancelVarRequest(varRequestIdCanceled);
104 impl->cancelVarRequest(varRequestIdCanceled);
105 // impl->m_AcqIdentifierToAcqRequestMap.erase(oldAcqId);
105 }
106 }
106 else {
107 else {
107 // First request for the variable, it must be stored and executed
108 // First request for the variable, it must be stored and executed
108 impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.insert(
109 impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.insert(
109 std::make_pair(vIdentifier, std::make_pair(acqRequest.m_AcqIdentifier, QUuid())));
110 std::make_pair(vIdentifier, std::make_pair(acqRequest.m_AcqIdentifier, QUuid())));
110 impl->unlock();
111 impl->unlock();
111
112
112 QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection,
113 QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection,
113 Q_ARG(QUuid, acqRequest.m_AcqIdentifier));
114 Q_ARG(QUuid, acqRequest.m_AcqIdentifier));
114 }
115 }
115
116
116 return varRequestIdCanceled;
117 return varRequestIdCanceled;
117 }
118 }
118
119
119 void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier)
120 void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier)
120 {
121 {
121 impl->lockRead();
122 impl->lockRead();
122
123
123 auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
124 auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
124 if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
125 if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
125 auto currentAcqId = it->second.first;
126 auto currentAcqId = it->second.first;
126
127
127 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(currentAcqId);
128 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(currentAcqId);
128 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
129 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
129 auto request = it->second;
130 auto request = it->second;
130 impl->unlock();
131 impl->unlock();
131
132
132 // Remove the current request from the worker
133 // Remove the current request from the worker
133 impl->updateToNextRequest(vIdentifier);
134 impl->updateToNextRequest(vIdentifier);
134
135
135 // notify the request aborting to the provider
136 // notify the request aborting to the provider
136 request.m_Provider->requestDataAborting(currentAcqId);
137 request.m_Provider->requestDataAborting(currentAcqId);
137 }
138 }
138 else {
139 else {
139 impl->unlock();
140 impl->unlock();
140 qCWarning(LOG_VariableAcquisitionWorker())
141 qCWarning(LOG_VariableAcquisitionWorker())
141 << tr("Impossible to abort an unknown acquisition request") << currentAcqId;
142 << tr("Impossible to abort an unknown acquisition request") << currentAcqId;
142 }
143 }
143 }
144 }
144 else {
145 else {
145 impl->unlock();
146 impl->unlock();
146 }
147 }
147 }
148 }
148
149
149 void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier,
150 void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier,
150 double progress)
151 double progress)
151 {
152 {
152 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ")
153 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ")
153 << acqIdentifier << progress;
154 << acqIdentifier << progress;
154 impl->lockRead();
155 impl->lockRead();
155 auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
156 auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
156 if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
157 if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
157 auto currentPartSize = (aIdToARit->second.m_Size != 0) ? 100 / aIdToARit->second.m_Size : 0;
158 auto currentPartSize = (aIdToARit->second.m_Size != 0) ? 100 / aIdToARit->second.m_Size : 0;
158
159
159 auto currentPartProgress
160 auto currentPartProgress
160 = std::isnan(progress) ? 0.0 : (progress * currentPartSize) / 100.0;
161 = std::isnan(progress) ? 0.0 : (progress * currentPartSize) / 100.0;
161 auto currentAlreadyProgress = aIdToARit->second.m_Progression * currentPartSize;
162 auto currentAlreadyProgress = aIdToARit->second.m_Progression * currentPartSize;
162
163
163 auto finalProgression = currentAlreadyProgress + currentPartProgress;
164 auto finalProgression = currentAlreadyProgress + currentPartProgress;
164 emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, finalProgression);
165 emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, finalProgression);
165 qCDebug(LOG_VariableAcquisitionWorker())
166 qCDebug(LOG_VariableAcquisitionWorker())
166 << tr("TORM: onVariableRetrieveDataInProgress ")
167 << tr("TORM: onVariableRetrieveDataInProgress ")
167 << QThread::currentThread()->objectName() << aIdToARit->second.m_vIdentifier
168 << QThread::currentThread()->objectName() << aIdToARit->second.m_vIdentifier
168 << currentPartSize << currentAlreadyProgress << currentPartProgress << finalProgression;
169 << currentPartSize << currentAlreadyProgress << currentPartProgress << finalProgression;
169 if (finalProgression == 100.0) {
170 if (finalProgression == 100.0) {
170 emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, 0.0);
171 emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, 0.0);
171 }
172 }
172 }
173 }
173 impl->unlock();
174 impl->unlock();
174 }
175 }
175
176
176 void VariableAcquisitionWorker::onVariableAcquisitionFailed(QUuid acqIdentifier)
177 void VariableAcquisitionWorker::onVariableAcquisitionFailed(QUuid acqIdentifier)
177 {
178 {
178 qCDebug(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed")
179 qCDebug(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed")
179 << QThread::currentThread();
180 << QThread::currentThread();
180 impl->lockRead();
181 impl->lockRead();
181 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
182 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
182 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
183 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
183 auto request = it->second;
184 auto request = it->second;
184 impl->unlock();
185 impl->unlock();
185 qCInfo(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed")
186 qCInfo(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed")
186 << acqIdentifier << request.m_vIdentifier
187 << acqIdentifier << request.m_vIdentifier
187 << QThread::currentThread();
188 << QThread::currentThread();
188 emit variableCanceledRequested(request.m_vIdentifier);
189 emit variableCanceledRequested(request.m_vIdentifier);
189 }
190 }
190 else {
191 else {
191 impl->unlock();
192 impl->unlock();
192 // TODO log no acqIdentifier recognized
193 // TODO log no acqIdentifier recognized
193 }
194 }
194 }
195 }
195
196
196 void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier,
197 void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier,
197 std::shared_ptr<IDataSeries> dataSeries,
198 std::shared_ptr<IDataSeries> dataSeries,
198 SqpRange dataRangeAcquired)
199 SqpRange dataRangeAcquired)
199 {
200 {
200 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableDataAcquired on range ")
201 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableDataAcquired on range ")
201 << acqIdentifier << dataRangeAcquired;
202 << acqIdentifier << dataRangeAcquired;
202 impl->lockWrite();
203 impl->lockWrite();
203 auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
204 auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
204 if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
205 if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
205 // Store the result
206 // Store the result
206 auto dataPacket = AcquisitionDataPacket{};
207 auto dataPacket = AcquisitionDataPacket{};
207 dataPacket.m_Range = dataRangeAcquired;
208 dataPacket.m_Range = dataRangeAcquired;
208 dataPacket.m_DateSeries = dataSeries;
209 dataPacket.m_DateSeries = dataSeries;
209
210
210 auto aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier);
211 auto aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier);
211 if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) {
212 if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) {
212 // A current request result already exists, we can update it
213 // A current request result already exists, we can update it
213 aIdToADPVit->second.push_back(dataPacket);
214 aIdToADPVit->second.push_back(dataPacket);
214 }
215 }
215 else {
216 else {
216 // First request result for the variable, it must be stored
217 // First request result for the variable, it must be stored
217 impl->m_AcqIdentifierToAcqDataPacketVectorMap.insert(
218 impl->m_AcqIdentifierToAcqDataPacketVectorMap.insert(
218 std::make_pair(acqIdentifier, QVector<AcquisitionDataPacket>() << dataPacket));
219 std::make_pair(acqIdentifier, QVector<AcquisitionDataPacket>() << dataPacket));
219 }
220 }
220
221
221
222
222 // Decrement the counter of the request
223 // Decrement the counter of the request
223 auto &acqRequest = aIdToARit->second;
224 auto &acqRequest = aIdToARit->second;
224 acqRequest.m_Progression = acqRequest.m_Progression + 1;
225 acqRequest.m_Progression = acqRequest.m_Progression + 1;
225
226
226 // if the counter is 0, we can return data then run the next request if it exists and
227 // if the counter is 0, we can return data then run the next request if it exists and
227 // removed the finished request
228 // removed the finished request
228 if (acqRequest.m_Size == acqRequest.m_Progression) {
229 if (acqRequest.m_Size == acqRequest.m_Progression) {
229 auto varId = acqRequest.m_vIdentifier;
230 auto varId = acqRequest.m_vIdentifier;
230 auto rangeRequested = acqRequest.m_RangeRequested;
231 auto rangeRequested = acqRequest.m_RangeRequested;
231 auto cacheRangeRequested = acqRequest.m_CacheRangeRequested;
232 auto cacheRangeRequested = acqRequest.m_CacheRangeRequested;
232 // Return the data
233 // Return the data
233 aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier);
234 aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier);
234 if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) {
235 if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) {
235 emit dataProvided(varId, rangeRequested, cacheRangeRequested, aIdToADPVit->second);
236 emit dataProvided(varId, rangeRequested, cacheRangeRequested, aIdToADPVit->second);
236 }
237 }
237 impl->unlock();
238 impl->unlock();
238
239
239 // Update to the next request
240 // Update to the next request
240 impl->updateToNextRequest(acqRequest.m_vIdentifier);
241 impl->updateToNextRequest(acqRequest.m_vIdentifier);
241 }
242 }
242 else {
243 else {
243 impl->unlock();
244 impl->unlock();
244 }
245 }
245 }
246 }
246 else {
247 else {
247 impl->unlock();
248 impl->unlock();
248 qCWarning(LOG_VariableAcquisitionWorker())
249 qCWarning(LOG_VariableAcquisitionWorker())
249 << tr("Impossible to retrieve AcquisitionRequest for the incoming data.");
250 << tr("Impossible to retrieve AcquisitionRequest for the incoming data.");
250 }
251 }
251 }
252 }
252
253
253 void VariableAcquisitionWorker::onExecuteRequest(QUuid acqIdentifier)
254 void VariableAcquisitionWorker::onExecuteRequest(QUuid acqIdentifier)
254 {
255 {
255 qCDebug(LOG_VariableAcquisitionWorker()) << tr("onExecuteRequest") << QThread::currentThread();
256 qCDebug(LOG_VariableAcquisitionWorker()) << tr("onExecuteRequest") << QThread::currentThread();
256 impl->lockRead();
257 impl->lockRead();
257 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
258 auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier);
258 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
259 if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) {
259 auto request = it->second;
260 auto request = it->second;
260 impl->unlock();
261 impl->unlock();
261 emit variableRequestInProgress(request.m_vIdentifier, 0.1);
262 emit variableRequestInProgress(request.m_vIdentifier, 0.1);
262 request.m_Provider->requestDataLoading(acqIdentifier, request.m_DataProviderParameters);
263 request.m_Provider->requestDataLoading(acqIdentifier, request.m_DataProviderParameters);
263 }
264 }
264 else {
265 else {
265 impl->unlock();
266 impl->unlock();
266 // TODO log no acqIdentifier recognized
267 // TODO log no acqIdentifier recognized
267 }
268 }
268 }
269 }
269
270
270 void VariableAcquisitionWorker::initialize()
271 void VariableAcquisitionWorker::initialize()
271 {
272 {
272 qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init")
273 qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init")
273 << QThread::currentThread();
274 << QThread::currentThread();
274 impl->m_WorkingMutex.lock();
275 impl->m_WorkingMutex.lock();
275 qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init END");
276 qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init END");
276 }
277 }
277
278
278 void VariableAcquisitionWorker::finalize()
279 void VariableAcquisitionWorker::finalize()
279 {
280 {
280 impl->m_WorkingMutex.unlock();
281 impl->m_WorkingMutex.unlock();
281 }
282 }
282
283
283 void VariableAcquisitionWorker::waitForFinish()
284 void VariableAcquisitionWorker::waitForFinish()
284 {
285 {
285 QMutexLocker locker{&impl->m_WorkingMutex};
286 QMutexLocker locker{&impl->m_WorkingMutex};
286 }
287 }
287
288
288 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariableRequest(
289 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariableRequest(
289 QUuid vIdentifier)
290 QUuid vIdentifier)
290 {
291 {
291 lockWrite();
292 lockWrite();
292 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
293 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
293
294
294 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
295 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
295 // A current request already exists, we can replace the next one
296 // A current request already exists, we can replace the next one
296
297
297 m_AcqIdentifierToAcqRequestMap.erase(it->second.first);
298 m_AcqIdentifierToAcqRequestMap.erase(it->second.first);
298 m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.first);
299 m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.first);
299
300
300 m_AcqIdentifierToAcqRequestMap.erase(it->second.second);
301 m_AcqIdentifierToAcqRequestMap.erase(it->second.second);
301 m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.second);
302 m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.second);
302 }
303 }
303 m_VIdentifierToCurrrentAcqIdNextIdPairMap.erase(vIdentifier);
304 m_VIdentifierToCurrrentAcqIdNextIdPairMap.erase(vIdentifier);
304 unlock();
305 unlock();
305 }
306 }
306
307
307 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::updateToNextRequest(
308 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::updateToNextRequest(
308 QUuid vIdentifier)
309 QUuid vIdentifier)
309 {
310 {
310 lockRead();
311 lockRead();
311 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
312 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
312 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
313 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
313 if (it->second.second.isNull()) {
314 if (it->second.second.isNull()) {
314 unlock();
315 unlock();
315 // There is no next request, we can remove the variable request
316 // There is no next request, we can remove the variable request
316 removeVariableRequest(vIdentifier);
317 removeVariableRequest(vIdentifier);
317 }
318 }
318 else {
319 else {
319 auto acqIdentifierToRemove = it->second.first;
320 auto acqIdentifierToRemove = it->second.first;
320 // Move the next request to the current request
321 // Move the next request to the current request
321 auto nextRequestId = it->second.second;
322 auto nextRequestId = it->second.second;
322 it->second.first = nextRequestId;
323 it->second.first = nextRequestId;
323 it->second.second = QUuid();
324 it->second.second = QUuid();
324 unlock();
325 unlock();
325 // Remove AcquisitionRequest and results;
326 // Remove AcquisitionRequest and results;
326 lockWrite();
327 lockWrite();
327 m_AcqIdentifierToAcqRequestMap.erase(acqIdentifierToRemove);
328 m_AcqIdentifierToAcqRequestMap.erase(acqIdentifierToRemove);
328 m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqIdentifierToRemove);
329 m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqIdentifierToRemove);
329 unlock();
330 unlock();
330 // Execute the current request
331 // Execute the current request
331 QMetaObject::invokeMethod(q, "onExecuteRequest", Qt::QueuedConnection,
332 QMetaObject::invokeMethod(q, "onExecuteRequest", Qt::QueuedConnection,
332 Q_ARG(QUuid, nextRequestId));
333 Q_ARG(QUuid, nextRequestId));
333 }
334 }
334 }
335 }
335 else {
336 else {
336 unlock();
337 unlock();
337 qCCritical(LOG_VariableAcquisitionWorker())
338 qCCritical(LOG_VariableAcquisitionWorker())
338 << tr("Impossible to execute the acquisition on an unfound variable ");
339 << tr("Impossible to execute the acquisition on an unfound variable ");
339 }
340 }
340 }
341 }
341
342
342 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::cancelVarRequest(
343 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::cancelVarRequest(
343 QUuid varRequestId)
344 QUuid varRequestId)
344 {
345 {
345 qCDebug(LOG_VariableAcquisitionWorker())
346 qCDebug(LOG_VariableAcquisitionWorker())
346 << "VariableAcquisitionWorkerPrivate::cancelVarRequest 0";
347 << "VariableAcquisitionWorkerPrivate::cancelVarRequest 0";
347 lockRead();
348 lockRead();
348 // get all AcqIdentifier in link with varRequestId
349 // get all AcqIdentifier in link with varRequestId
349 QVector<QUuid> acqIdsToRm;
350 QVector<QUuid> acqIdsToRm;
350 auto cend = m_AcqIdentifierToAcqRequestMap.cend();
351 auto cend = m_AcqIdentifierToAcqRequestMap.cend();
351 for (auto it = m_AcqIdentifierToAcqRequestMap.cbegin(); it != cend; ++it) {
352 for (auto it = m_AcqIdentifierToAcqRequestMap.cbegin(); it != cend; ++it) {
352 if (it->second.m_VarRequestId == varRequestId) {
353 if (it->second.m_VarRequestId == varRequestId) {
353 acqIdsToRm << it->first;
354 acqIdsToRm << it->first;
354 }
355 }
355 }
356 }
356 unlock();
357 unlock();
357 // run aborting or removing of acqIdsToRm
358 // run aborting or removing of acqIdsToRm
358
359
359 for (auto acqId : acqIdsToRm) {
360 for (auto acqId : acqIdsToRm) {
360 removeAcqRequest(acqId);
361 removeAcqRequest(acqId);
361 }
362 }
362 qCDebug(LOG_VariableAcquisitionWorker())
363 qCDebug(LOG_VariableAcquisitionWorker())
363 << "VariableAcquisitionWorkerPrivate::cancelVarRequest end";
364 << "VariableAcquisitionWorkerPrivate::cancelVarRequest end";
364 }
365 }
365
366
366 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeAcqRequest(
367 void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeAcqRequest(
367 QUuid acqRequestId)
368 QUuid acqRequestId)
368 {
369 {
369 qCDebug(LOG_VariableAcquisitionWorker())
370 qCDebug(LOG_VariableAcquisitionWorker())
370 << "VariableAcquisitionWorkerPrivate::removeAcqRequest";
371 << "VariableAcquisitionWorkerPrivate::removeAcqRequest";
371 QUuid vIdentifier;
372 QUuid vIdentifier;
372 std::shared_ptr<IDataProvider> provider;
373 std::shared_ptr<IDataProvider> provider;
373 lockRead();
374 lockRead();
374 auto acqIt = m_AcqIdentifierToAcqRequestMap.find(acqRequestId);
375 auto acqIt = m_AcqIdentifierToAcqRequestMap.find(acqRequestId);
375 if (acqIt != m_AcqIdentifierToAcqRequestMap.cend()) {
376 if (acqIt != m_AcqIdentifierToAcqRequestMap.cend()) {
376 vIdentifier = acqIt->second.m_vIdentifier;
377 vIdentifier = acqIt->second.m_vIdentifier;
377 provider = acqIt->second.m_Provider;
378 provider = acqIt->second.m_Provider;
378
379
379 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
380 auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier);
380 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
381 if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) {
381 if (it->second.first == acqRequestId) {
382 if (it->second.first == acqRequestId) {
382 // acqRequest is currently running -> let's aborting it
383 // acqRequest is currently running -> let's aborting it
383 unlock();
384 unlock();
384
385
385 // Remove the current request from the worker
386 // Remove the current request from the worker
386 updateToNextRequest(vIdentifier);
387 updateToNextRequest(vIdentifier);
387
388
388 // notify the request aborting to the provider
389 // notify the request aborting to the provider
389 provider->requestDataAborting(acqRequestId);
390 provider->requestDataAborting(acqRequestId);
390 }
391 }
391 else if (it->second.second == acqRequestId) {
392 else if (it->second.second == acqRequestId) {
392 it->second.second = QUuid();
393 it->second.second = QUuid();
393 unlock();
394 unlock();
394 }
395 }
395 else {
396 else {
396 unlock();
397 unlock();
397 }
398 }
398 }
399 }
399 else {
400 else {
400 unlock();
401 unlock();
401 }
402 }
402 }
403 }
403 else {
404 else {
404 unlock();
405 unlock();
405 }
406 }
406
407
407 lockWrite();
408 lockWrite();
408
409
409 m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqRequestId);
410 m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqRequestId);
410 m_AcqIdentifierToAcqRequestMap.erase(acqRequestId);
411 m_AcqIdentifierToAcqRequestMap.erase(acqRequestId);
411
412
412 unlock();
413 unlock();
413 qCDebug(LOG_VariableAcquisitionWorker())
414 qCDebug(LOG_VariableAcquisitionWorker())
414 << "VariableAcquisitionWorkerPrivate::removeAcqRequest END";
415 << "VariableAcquisitionWorkerPrivate::removeAcqRequest END";
415 }
416 }
@@ -1,956 +1,958
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableAcquisitionWorker.h>
2 #include <Variable/VariableAcquisitionWorker.h>
3 #include <Variable/VariableCacheStrategy.h>
3 #include <Variable/VariableCacheStrategy.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
5 #include <Variable/VariableController.h>
5 #include <Variable/VariableController.h>
6 #include <Variable/VariableModel.h>
6 #include <Variable/VariableModel.h>
7 #include <Variable/VariableSynchronizationGroup.h>
7 #include <Variable/VariableSynchronizationGroup.h>
8
8
9 #include <Data/DataProviderParameters.h>
9 #include <Data/DataProviderParameters.h>
10 #include <Data/IDataProvider.h>
10 #include <Data/IDataProvider.h>
11 #include <Data/IDataSeries.h>
11 #include <Data/IDataSeries.h>
12 #include <Data/VariableRequest.h>
12 #include <Data/VariableRequest.h>
13 #include <Time/TimeController.h>
13 #include <Time/TimeController.h>
14
14
15 #include <QMutex>
15 #include <QMutex>
16 #include <QThread>
16 #include <QThread>
17 #include <QUuid>
17 #include <QUuid>
18 #include <QtCore/QItemSelectionModel>
18 #include <QtCore/QItemSelectionModel>
19
19
20 #include <deque>
20 #include <deque>
21 #include <set>
21 #include <set>
22 #include <unordered_map>
22 #include <unordered_map>
23
23
24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25
25
26 namespace {
26 namespace {
27
27
28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 const SqpRange &oldGraphRange)
29 const SqpRange &oldGraphRange)
30 {
30 {
31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32
32
33 auto varRangeRequested = varRange;
33 auto varRangeRequested = varRange;
34 switch (zoomType) {
34 switch (zoomType) {
35 case AcquisitionZoomType::ZoomIn: {
35 case AcquisitionZoomType::ZoomIn: {
36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 varRangeRequested.m_TStart += deltaLeft;
38 varRangeRequested.m_TStart += deltaLeft;
39 varRangeRequested.m_TEnd -= deltaRight;
39 varRangeRequested.m_TEnd -= deltaRight;
40 break;
40 break;
41 }
41 }
42
42
43 case AcquisitionZoomType::ZoomOut: {
43 case AcquisitionZoomType::ZoomOut: {
44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 varRangeRequested.m_TStart -= deltaLeft;
46 varRangeRequested.m_TStart -= deltaLeft;
47 varRangeRequested.m_TEnd += deltaRight;
47 varRangeRequested.m_TEnd += deltaRight;
48 break;
48 break;
49 }
49 }
50 case AcquisitionZoomType::PanRight: {
50 case AcquisitionZoomType::PanRight: {
51 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
51 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
52 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
52 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
53 varRangeRequested.m_TStart += deltaLeft;
53 varRangeRequested.m_TStart += deltaLeft;
54 varRangeRequested.m_TEnd += deltaRight;
54 varRangeRequested.m_TEnd += deltaRight;
55 break;
55 break;
56 }
56 }
57 case AcquisitionZoomType::PanLeft: {
57 case AcquisitionZoomType::PanLeft: {
58 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
58 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
59 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
59 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
60 varRangeRequested.m_TStart -= deltaLeft;
60 varRangeRequested.m_TStart -= deltaLeft;
61 varRangeRequested.m_TEnd -= deltaRight;
61 varRangeRequested.m_TEnd -= deltaRight;
62 break;
62 break;
63 }
63 }
64 case AcquisitionZoomType::Unknown: {
64 case AcquisitionZoomType::Unknown: {
65 qCCritical(LOG_VariableController())
65 qCCritical(LOG_VariableController())
66 << VariableController::tr("Impossible to synchronize: zoom type unknown");
66 << VariableController::tr("Impossible to synchronize: zoom type unknown");
67 break;
67 break;
68 }
68 }
69 default:
69 default:
70 qCCritical(LOG_VariableController()) << VariableController::tr(
70 qCCritical(LOG_VariableController()) << VariableController::tr(
71 "Impossible to synchronize: zoom type not take into account");
71 "Impossible to synchronize: zoom type not take into account");
72 // No action
72 // No action
73 break;
73 break;
74 }
74 }
75
75
76 return varRangeRequested;
76 return varRangeRequested;
77 }
77 }
78 }
78 }
79
79
80 struct VariableController::VariableControllerPrivate {
80 struct VariableController::VariableControllerPrivate {
81 explicit VariableControllerPrivate(VariableController *parent)
81 explicit VariableControllerPrivate(VariableController *parent)
82 : m_WorkingMutex{},
82 : m_WorkingMutex{},
83 m_VariableModel{new VariableModel{parent}},
83 m_VariableModel{new VariableModel{parent}},
84 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
84 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
85 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
85 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
86 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
86 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
87 CacheStrategy::SingleThreshold)},
87 CacheStrategy::SingleThreshold)},
88 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
88 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
89 q{parent}
89 q{parent}
90 {
90 {
91
91
92 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
92 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
93 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
93 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
94 }
94 }
95
95
96
96
97 virtual ~VariableControllerPrivate()
97 virtual ~VariableControllerPrivate()
98 {
98 {
99 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
99 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
100 m_VariableAcquisitionWorkerThread.quit();
100 m_VariableAcquisitionWorkerThread.quit();
101 m_VariableAcquisitionWorkerThread.wait();
101 m_VariableAcquisitionWorkerThread.wait();
102 }
102 }
103
103
104
104
105 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
105 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
106 QUuid varRequestId);
106 QUuid varRequestId);
107
107
108 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
108 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
109 std::shared_ptr<IDataSeries>
109 std::shared_ptr<IDataSeries>
110 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
110 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
111
111
112 void registerProvider(std::shared_ptr<IDataProvider> provider);
112 void registerProvider(std::shared_ptr<IDataProvider> provider);
113
113
114 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
114 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
115 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
115 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
116 void updateVariableRequest(QUuid varRequestId);
116 void updateVariableRequest(QUuid varRequestId);
117 void cancelVariableRequest(QUuid varRequestId);
117 void cancelVariableRequest(QUuid varRequestId);
118 void abortVariableRequest(QUuid varRequestId);
118 void abortVariableRequest(QUuid varRequestId);
119 SqpRange getLastRequestedRange(QUuid varId);
119 SqpRange getLastRequestedRange(QUuid varId);
120
120
121 QMutex m_WorkingMutex;
121 QMutex m_WorkingMutex;
122 /// Variable model. The VariableController has the ownership
122 /// Variable model. The VariableController has the ownership
123 VariableModel *m_VariableModel;
123 VariableModel *m_VariableModel;
124 QItemSelectionModel *m_VariableSelectionModel;
124 QItemSelectionModel *m_VariableSelectionModel;
125
125
126
126
127 TimeController *m_TimeController{nullptr};
127 TimeController *m_TimeController{nullptr};
128 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
128 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
129 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
129 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
130 QThread m_VariableAcquisitionWorkerThread;
130 QThread m_VariableAcquisitionWorkerThread;
131
131
132 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
132 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
133 m_VariableToProviderMap;
133 m_VariableToProviderMap;
134 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
134 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
135 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
135 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
136 m_GroupIdToVariableSynchronizationGroupMap;
136 m_GroupIdToVariableSynchronizationGroupMap;
137 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
137 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
138 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
138 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
139
139
140 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
140 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
141
141
142 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
142 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
143
143
144
145 VariableController *q;
144 VariableController *q;
146 };
145 };
147
146
148
147
149 VariableController::VariableController(QObject *parent)
148 VariableController::VariableController(QObject *parent)
150 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
149 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
151 {
150 {
152 qCDebug(LOG_VariableController()) << tr("VariableController construction")
151 qCDebug(LOG_VariableController()) << tr("VariableController construction")
153 << QThread::currentThread();
152 << QThread::currentThread();
154
153
155 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
154 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
156 &VariableController::onAbortProgressRequested);
155 &VariableController::onAbortProgressRequested);
157
156
158 connect(impl->m_VariableAcquisitionWorker.get(),
157 connect(impl->m_VariableAcquisitionWorker.get(),
159 &VariableAcquisitionWorker::variableCanceledRequested, this,
158 &VariableAcquisitionWorker::variableCanceledRequested, this,
160 &VariableController::onAbortAcquisitionRequested);
159 &VariableController::onAbortAcquisitionRequested);
161
160
162 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
161 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
163 &VariableController::onDataProvided);
162 &VariableController::onDataProvided);
164 connect(impl->m_VariableAcquisitionWorker.get(),
163 connect(impl->m_VariableAcquisitionWorker.get(),
165 &VariableAcquisitionWorker::variableRequestInProgress, this,
164 &VariableAcquisitionWorker::variableRequestInProgress, this,
166 &VariableController::onVariableRetrieveDataInProgress);
165 &VariableController::onVariableRetrieveDataInProgress);
167
166
168
167
169 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
168 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
170 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
169 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
171 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
170 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
172 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
171 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
173
172
174
173
175 impl->m_VariableAcquisitionWorkerThread.start();
174 impl->m_VariableAcquisitionWorkerThread.start();
176 }
175 }
177
176
178 VariableController::~VariableController()
177 VariableController::~VariableController()
179 {
178 {
180 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
179 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
181 << QThread::currentThread();
180 << QThread::currentThread();
182 this->waitForFinish();
181 this->waitForFinish();
183 }
182 }
184
183
185 VariableModel *VariableController::variableModel() noexcept
184 VariableModel *VariableController::variableModel() noexcept
186 {
185 {
187 return impl->m_VariableModel;
186 return impl->m_VariableModel;
188 }
187 }
189
188
190 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
189 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
191 {
190 {
192 return impl->m_VariableSelectionModel;
191 return impl->m_VariableSelectionModel;
193 }
192 }
194
193
195 void VariableController::setTimeController(TimeController *timeController) noexcept
194 void VariableController::setTimeController(TimeController *timeController) noexcept
196 {
195 {
197 impl->m_TimeController = timeController;
196 impl->m_TimeController = timeController;
198 }
197 }
199
198
200 std::shared_ptr<Variable>
199 std::shared_ptr<Variable>
201 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
200 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
202 {
201 {
203 if (impl->m_VariableModel->containsVariable(variable)) {
202 if (impl->m_VariableModel->containsVariable(variable)) {
204 // Clones variable
203 // Clones variable
205 auto duplicate = variable->clone();
204 auto duplicate = variable->clone();
206
205
207 // Adds clone to model
206 // Adds clone to model
208 impl->m_VariableModel->addVariable(duplicate);
207 impl->m_VariableModel->addVariable(duplicate);
209
208
210 // Generates clone identifier
209 // Generates clone identifier
211 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
210 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
212
211
213 // Registers provider
212 // Registers provider
214 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
213 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
215 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
214 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
216
215
217 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
216 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
218 if (duplicateProvider) {
217 if (duplicateProvider) {
219 impl->registerProvider(duplicateProvider);
218 impl->registerProvider(duplicateProvider);
220 }
219 }
221
220
222 return duplicate;
221 return duplicate;
223 }
222 }
224 else {
223 else {
225 qCCritical(LOG_VariableController())
224 qCCritical(LOG_VariableController())
226 << tr("Can't create duplicate of variable %1: variable not registered in the model")
225 << tr("Can't create duplicate of variable %1: variable not registered in the model")
227 .arg(variable->name());
226 .arg(variable->name());
228 return nullptr;
227 return nullptr;
229 }
228 }
230 }
229 }
231
230
232 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
231 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
233 {
232 {
234 if (!variable) {
233 if (!variable) {
235 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
234 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
236 return;
235 return;
237 }
236 }
238
237
239 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
238 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
240 // make some treatments before the deletion
239 // make some treatments before the deletion
241 emit variableAboutToBeDeleted(variable);
240 emit variableAboutToBeDeleted(variable);
242
241
243 // Deletes identifier
242 // Deletes identifier
244 impl->m_VariableToIdentifierMap.erase(variable);
243 impl->m_VariableToIdentifierMap.erase(variable);
245
244
246 // Deletes provider
245 // Deletes provider
247 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
246 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
248 qCDebug(LOG_VariableController())
247 qCDebug(LOG_VariableController())
249 << tr("Number of providers deleted for variable %1: %2")
248 << tr("Number of providers deleted for variable %1: %2")
250 .arg(variable->name(), QString::number(nbProvidersDeleted));
249 .arg(variable->name(), QString::number(nbProvidersDeleted));
251
250
252
251
253 // Deletes from model
252 // Deletes from model
254 impl->m_VariableModel->deleteVariable(variable);
253 impl->m_VariableModel->deleteVariable(variable);
255 }
254 }
256
255
257 void VariableController::deleteVariables(
256 void VariableController::deleteVariables(
258 const QVector<std::shared_ptr<Variable> > &variables) noexcept
257 const QVector<std::shared_ptr<Variable> > &variables) noexcept
259 {
258 {
260 for (auto variable : qAsConst(variables)) {
259 for (auto variable : qAsConst(variables)) {
261 deleteVariable(variable);
260 deleteVariable(variable);
262 }
261 }
263 }
262 }
264
263
265 std::shared_ptr<Variable>
264 std::shared_ptr<Variable>
266 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
265 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
267 std::shared_ptr<IDataProvider> provider) noexcept
266 std::shared_ptr<IDataProvider> provider) noexcept
268 {
267 {
269 if (!impl->m_TimeController) {
268 if (!impl->m_TimeController) {
270 qCCritical(LOG_VariableController())
269 qCCritical(LOG_VariableController())
271 << tr("Impossible to create variable: The time controller is null");
270 << tr("Impossible to create variable: The time controller is null");
272 return nullptr;
271 return nullptr;
273 }
272 }
274
273
275 auto range = impl->m_TimeController->dateTime();
274 auto range = impl->m_TimeController->dateTime();
276
275
277 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
276 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
278 auto identifier = QUuid::createUuid();
277 auto identifier = QUuid::createUuid();
279
278
280 // store the provider
279 // store the provider
281 impl->registerProvider(provider);
280 impl->registerProvider(provider);
282
281
283 // Associate the provider
282 // Associate the provider
284 impl->m_VariableToProviderMap[newVariable] = provider;
283 impl->m_VariableToProviderMap[newVariable] = provider;
285 qCInfo(LOG_VariableController()) << "createVariable: " << identifier;
286 impl->m_VariableToIdentifierMap[newVariable] = identifier;
284 impl->m_VariableToIdentifierMap[newVariable] = identifier;
287
285
288
286
289 auto varRequestId = QUuid::createUuid();
287 auto varRequestId = QUuid::createUuid();
288 qCInfo(LOG_VariableController()) << "createVariable: " << identifier << varRequestId;
290 impl->processRequest(newVariable, range, varRequestId);
289 impl->processRequest(newVariable, range, varRequestId);
291 impl->updateVariableRequest(varRequestId);
290 impl->updateVariableRequest(varRequestId);
292
291
293 return newVariable;
292 return newVariable;
294 }
293 }
295 }
294 }
296
295
297 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
296 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
298 {
297 {
299 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
298 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
300 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
299 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
301 << QThread::currentThread()->objectName();
300 << QThread::currentThread()->objectName();
302 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
301 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
303 auto variables = QVector<std::shared_ptr<Variable> >{};
302 auto variables = QVector<std::shared_ptr<Variable> >{};
304
303
305 for (const auto &selectedRow : qAsConst(selectedRows)) {
304 for (const auto &selectedRow : qAsConst(selectedRows)) {
306 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
305 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
307 variables << selectedVariable;
306 variables << selectedVariable;
308
307
309 // notify that rescale operation has to be done
308 // notify that rescale operation has to be done
310 emit rangeChanged(selectedVariable, dateTime);
309 emit rangeChanged(selectedVariable, dateTime);
311 }
310 }
312 }
311 }
313
312
314 if (!variables.isEmpty()) {
313 if (!variables.isEmpty()) {
315 this->onRequestDataLoading(variables, dateTime, true);
314 this->onRequestDataLoading(variables, dateTime, true);
316 }
315 }
317 }
316 }
318
317
319 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
318 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
320 const SqpRange &cacheRangeRequested,
319 const SqpRange &cacheRangeRequested,
321 QVector<AcquisitionDataPacket> dataAcquired)
320 QVector<AcquisitionDataPacket> dataAcquired)
322 {
321 {
323 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
322 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
324 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
323 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
325 if (!varRequestId.isNull()) {
324 if (!varRequestId.isNull()) {
326 impl->updateVariableRequest(varRequestId);
325 impl->updateVariableRequest(varRequestId);
327 }
326 }
328 }
327 }
329
328
330 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
329 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
331 {
330 {
332 qCDebug(LOG_VariableController())
331 qCDebug(LOG_VariableController())
333 << "TORM: variableController::onVariableRetrieveDataInProgress"
332 << "TORM: variableController::onVariableRetrieveDataInProgress"
334 << QThread::currentThread()->objectName() << progress;
333 << QThread::currentThread()->objectName() << progress;
335 if (auto var = impl->findVariable(identifier)) {
334 if (auto var = impl->findVariable(identifier)) {
336 impl->m_VariableModel->setDataProgress(var, progress);
335 impl->m_VariableModel->setDataProgress(var, progress);
337 }
336 }
338 else {
337 else {
339 qCCritical(LOG_VariableController())
338 qCCritical(LOG_VariableController())
340 << tr("Impossible to notify progression of a null variable");
339 << tr("Impossible to notify progression of a null variable");
341 }
340 }
342 }
341 }
343
342
344 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
343 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
345 {
344 {
346 auto it = impl->m_VariableToIdentifierMap.find(variable);
345 auto it = impl->m_VariableToIdentifierMap.find(variable);
347 if (it != impl->m_VariableToIdentifierMap.cend()) {
346 if (it != impl->m_VariableToIdentifierMap.cend()) {
348 impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second);
347 impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second);
349
348
350 QUuid varRequestId;
349 QUuid varRequestId;
351 auto varIdToVarRequestIdQueueMapIt = impl->m_VarIdToVarRequestIdQueueMap.find(it->second);
350 auto varIdToVarRequestIdQueueMapIt = impl->m_VarIdToVarRequestIdQueueMap.find(it->second);
352 if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) {
351 if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) {
353 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
352 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
354 varRequestId = varRequestIdQueue.front();
353 varRequestId = varRequestIdQueue.front();
355 impl->cancelVariableRequest(varRequestId);
354 impl->cancelVariableRequest(varRequestId);
356
355
357 // Finish the progression for the request
356 // Finish the progression for the request
358 impl->m_VariableModel->setDataProgress(variable, 0.0);
357 impl->m_VariableModel->setDataProgress(variable, 0.0);
359 }
358 }
360 else {
359 else {
361 qCWarning(LOG_VariableController())
360 qCWarning(LOG_VariableController())
362 << tr("Aborting progression of inexistant variable request detected !!!")
361 << tr("Aborting progression of inexistant variable request detected !!!")
363 << QThread::currentThread()->objectName();
362 << QThread::currentThread()->objectName();
364 }
363 }
365 }
364 }
366 else {
365 else {
367 qCWarning(LOG_VariableController())
366 qCWarning(LOG_VariableController())
368 << tr("Aborting progression of inexistant variable detected !!!")
367 << tr("Aborting progression of inexistant variable detected !!!")
369 << QThread::currentThread()->objectName();
368 << QThread::currentThread()->objectName();
370 }
369 }
371 }
370 }
372
371
373 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
372 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
374 {
373 {
375 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
374 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
376 << QThread::currentThread()->objectName() << vIdentifier;
375 << QThread::currentThread()->objectName() << vIdentifier;
377
376
378 if (auto var = impl->findVariable(vIdentifier)) {
377 if (auto var = impl->findVariable(vIdentifier)) {
379 this->onAbortProgressRequested(var);
378 this->onAbortProgressRequested(var);
380 }
379 }
381 else {
380 else {
382 qCCritical(LOG_VariableController())
381 qCCritical(LOG_VariableController())
383 << tr("Impossible to abort Acquisition Requestof a null variable");
382 << tr("Impossible to abort Acquisition Requestof a null variable");
384 }
383 }
385 }
384 }
386
385
387 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
386 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
388 {
387 {
389 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
388 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
390 << QThread::currentThread()->objectName()
389 << QThread::currentThread()->objectName()
391 << synchronizationGroupId;
390 << synchronizationGroupId;
392 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
391 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
393 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
392 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
394 std::make_pair(synchronizationGroupId, vSynchroGroup));
393 std::make_pair(synchronizationGroupId, vSynchroGroup));
395 }
394 }
396
395
397 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
396 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
398 {
397 {
399 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
398 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
400 }
399 }
401
400
402 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
401 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
403 QUuid synchronizationGroupId)
402 QUuid synchronizationGroupId)
404
403
405 {
404 {
406 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
405 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
407 << synchronizationGroupId;
406 << synchronizationGroupId;
408 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
407 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
409 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
408 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
410 auto groupIdToVSGIt
409 auto groupIdToVSGIt
411 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
410 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
412 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
411 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
413 impl->m_VariableIdGroupIdMap.insert(
412 impl->m_VariableIdGroupIdMap.insert(
414 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
413 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
415 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
414 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
416 }
415 }
417 else {
416 else {
418 qCCritical(LOG_VariableController())
417 qCCritical(LOG_VariableController())
419 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
418 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
420 << variable->name();
419 << variable->name();
421 }
420 }
422 }
421 }
423 else {
422 else {
424 qCCritical(LOG_VariableController())
423 qCCritical(LOG_VariableController())
425 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
424 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
426 }
425 }
427 }
426 }
428
427
429 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
428 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
430 QUuid synchronizationGroupId)
429 QUuid synchronizationGroupId)
431 {
430 {
432 // Gets variable id
431 // Gets variable id
433 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
432 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
434 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
433 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
435 qCCritical(LOG_VariableController())
434 qCCritical(LOG_VariableController())
436 << tr("Can't desynchronize variable %1: variable identifier not found")
435 << tr("Can't desynchronize variable %1: variable identifier not found")
437 .arg(variable->name());
436 .arg(variable->name());
438 return;
437 return;
439 }
438 }
440
439
441 // Gets synchronization group
440 // Gets synchronization group
442 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
441 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
443 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
442 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
444 qCCritical(LOG_VariableController())
443 qCCritical(LOG_VariableController())
445 << tr("Can't desynchronize variable %1: unknown synchronization group")
444 << tr("Can't desynchronize variable %1: unknown synchronization group")
446 .arg(variable->name());
445 .arg(variable->name());
447 return;
446 return;
448 }
447 }
449
448
450 auto variableId = variableIt->second;
449 auto variableId = variableIt->second;
451
450
452 // Removes variable from synchronization group
451 // Removes variable from synchronization group
453 auto synchronizationGroup = groupIt->second;
452 auto synchronizationGroup = groupIt->second;
454 synchronizationGroup->removeVariableId(variableId);
453 synchronizationGroup->removeVariableId(variableId);
455
454
456 // Removes link between variable and synchronization group
455 // Removes link between variable and synchronization group
457 impl->m_VariableIdGroupIdMap.erase(variableId);
456 impl->m_VariableIdGroupIdMap.erase(variableId);
458 }
457 }
459
458
460 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
459 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
461 const SqpRange &range, bool synchronise)
460 const SqpRange &range, bool synchronise)
462 {
461 {
463 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
462 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
464
463
465 // we want to load data of the variable for the dateTime.
464 // we want to load data of the variable for the dateTime.
466 // First we check if the cache contains some of them.
465 // First we check if the cache contains some of them.
467 // For the other, we ask the provider to give them.
466 // For the other, we ask the provider to give them.
468
467
469 auto varRequestId = QUuid::createUuid();
468 auto varRequestId = QUuid::createUuid();
470 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
469 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
471 << QThread::currentThread()->objectName() << varRequestId;
470 << QThread::currentThread()->objectName() << varRequestId;
472
471
473 for (const auto &var : variables) {
472 for (const auto &var : variables) {
474 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
473 qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
475 impl->processRequest(var, range, varRequestId);
474 impl->processRequest(var, range, varRequestId);
476 }
475 }
477
476
478 if (synchronise) {
477 if (synchronise) {
479 // Get the group ids
478 // Get the group ids
480 qCDebug(LOG_VariableController())
479 qCDebug(LOG_VariableController())
481 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
480 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
482 auto groupIds = std::set<QUuid>{};
481 auto groupIds = std::set<QUuid>{};
483 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
482 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
484 for (const auto &var : variables) {
483 for (const auto &var : variables) {
485 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
484 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
486 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
485 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
487 auto vId = varToVarIdIt->second;
486 auto vId = varToVarIdIt->second;
488 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
487 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
489 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
488 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
490 auto gId = varIdToGroupIdIt->second;
489 auto gId = varIdToGroupIdIt->second;
491 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
490 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
492 if (groupIds.find(gId) == groupIds.cend()) {
491 if (groupIds.find(gId) == groupIds.cend()) {
493 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
492 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
494 groupIds.insert(gId);
493 groupIds.insert(gId);
495 }
494 }
496 }
495 }
497 }
496 }
498 }
497 }
499
498
500 // We assume here all group ids exist
499 // We assume here all group ids exist
501 for (const auto &gId : groupIds) {
500 for (const auto &gId : groupIds) {
502 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
501 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
503 auto vSyncIds = vSynchronizationGroup->getIds();
502 auto vSyncIds = vSynchronizationGroup->getIds();
504 qCDebug(LOG_VariableController()) << "Var in synchro group ";
503 qCDebug(LOG_VariableController()) << "Var in synchro group ";
505 for (auto vId : vSyncIds) {
504 for (auto vId : vSyncIds) {
506 auto var = impl->findVariable(vId);
505 auto var = impl->findVariable(vId);
507
506
508 // Don't process already processed var
507 // Don't process already processed var
509 if (!variables.contains(var)) {
508 if (!variables.contains(var)) {
510 if (var != nullptr) {
509 if (var != nullptr) {
511 qCDebug(LOG_VariableController()) << "processRequest synchro for"
510 qCInfo(LOG_VariableController()) << "processRequest synchro for"
512 << var->name();
511 << var->name() << varRequestId;
513 auto vSyncRangeRequested = computeSynchroRangeRequested(
512 auto vSyncRangeRequested = computeSynchroRangeRequested(
514 var->range(), range, groupIdToOldRangeMap.at(gId));
513 var->range(), range, groupIdToOldRangeMap.at(gId));
515 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
514 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
516 impl->processRequest(var, vSyncRangeRequested, varRequestId);
515 impl->processRequest(var, vSyncRangeRequested, varRequestId);
517 }
516 }
518 else {
517 else {
519 qCCritical(LOG_VariableController())
518 qCCritical(LOG_VariableController())
520
519
521 << tr("Impossible to synchronize a null variable");
520 << tr("Impossible to synchronize a null variable");
522 }
521 }
523 }
522 }
524 }
523 }
525 }
524 }
526 }
525 }
527
526
528 impl->updateVariableRequest(varRequestId);
527 impl->updateVariableRequest(varRequestId);
529 }
528 }
530
529
531
530
532 void VariableController::initialize()
531 void VariableController::initialize()
533 {
532 {
534 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
533 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
535 impl->m_WorkingMutex.lock();
534 impl->m_WorkingMutex.lock();
536 qCDebug(LOG_VariableController()) << tr("VariableController init END");
535 qCDebug(LOG_VariableController()) << tr("VariableController init END");
537 }
536 }
538
537
539 void VariableController::finalize()
538 void VariableController::finalize()
540 {
539 {
541 impl->m_WorkingMutex.unlock();
540 impl->m_WorkingMutex.unlock();
542 }
541 }
543
542
544 void VariableController::waitForFinish()
543 void VariableController::waitForFinish()
545 {
544 {
546 QMutexLocker locker{&impl->m_WorkingMutex};
545 QMutexLocker locker{&impl->m_WorkingMutex};
547 }
546 }
548
547
549 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
548 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
550 {
549 {
551 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
550 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
552 auto zoomType = AcquisitionZoomType::Unknown;
551 auto zoomType = AcquisitionZoomType::Unknown;
553 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
552 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
554 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
553 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
555 zoomType = AcquisitionZoomType::ZoomOut;
554 zoomType = AcquisitionZoomType::ZoomOut;
556 }
555 }
557 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
556 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
558 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
557 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
559 zoomType = AcquisitionZoomType::PanRight;
558 zoomType = AcquisitionZoomType::PanRight;
560 }
559 }
561 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
560 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
562 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
561 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
563 zoomType = AcquisitionZoomType::PanLeft;
562 zoomType = AcquisitionZoomType::PanLeft;
564 }
563 }
565 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
564 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
566 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
565 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
567 zoomType = AcquisitionZoomType::ZoomIn;
566 zoomType = AcquisitionZoomType::ZoomIn;
568 }
567 }
569 else {
568 else {
570 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
569 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
571 }
570 }
572 return zoomType;
571 return zoomType;
573 }
572 }
574
573
575 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
574 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
576 const SqpRange &rangeRequested,
575 const SqpRange &rangeRequested,
577 QUuid varRequestId)
576 QUuid varRequestId)
578 {
577 {
579 auto varRequest = VariableRequest{};
578 auto varRequest = VariableRequest{};
580
579
581 auto it = m_VariableToIdentifierMap.find(var);
580 auto it = m_VariableToIdentifierMap.find(var);
582 if (it != m_VariableToIdentifierMap.cend()) {
581 if (it != m_VariableToIdentifierMap.cend()) {
583
582
584 auto varId = it->second;
583 auto varId = it->second;
585
584
586 auto oldRange = getLastRequestedRange(varId);
585 auto oldRange = getLastRequestedRange(varId);
587
586
588 // check for update oldRange to the last request range.
587 // check for update oldRange to the last request range.
589 if (oldRange == INVALID_RANGE) {
588 if (oldRange == INVALID_RANGE) {
590 oldRange = var->range();
589 oldRange = var->range();
591 }
590 }
592
591
593 auto varStrategyRangesRequested
592 auto varStrategyRangesRequested
594 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
593 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
595
594
596 // Use commented lines to remove the cache (run time)
595 // Use commented lines to remove the cache (run time)
597 // auto notInCacheRangeList = QVector<SqpRange>{varStrategyRangesRequested.second};
596 // auto notInCacheRangeList = QVector<SqpRange>{varStrategyRangesRequested.second};
598 // auto inCacheRangeList = QVector<SqpRange>{};
597 // auto inCacheRangeList = QVector<SqpRange>{};
599 auto notInCacheRangeList
598 auto notInCacheRangeList
600 = Variable::provideNotInCacheRangeList(oldRange, varStrategyRangesRequested.second);
599 = Variable::provideNotInCacheRangeList(oldRange, varStrategyRangesRequested.second);
601 auto inCacheRangeList
600 auto inCacheRangeList
602 = Variable::provideInCacheRangeList(oldRange, varStrategyRangesRequested.second);
601 = Variable::provideInCacheRangeList(oldRange, varStrategyRangesRequested.second);
603
602
604 if (!notInCacheRangeList.empty()) {
603 if (!notInCacheRangeList.empty()) {
605 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
604 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
606 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
605 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
607
606
608 // store VarRequest
607 // store VarRequest
609 storeVariableRequest(varId, varRequestId, varRequest);
608 storeVariableRequest(varId, varRequestId, varRequest);
610
609
611 auto varProvider = m_VariableToProviderMap.at(var);
610 auto varProvider = m_VariableToProviderMap.at(var);
612 if (varProvider != nullptr) {
611 if (varProvider != nullptr) {
613 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
612 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
614 varRequestId, varId, varStrategyRangesRequested.first,
613 varRequestId, varId, varStrategyRangesRequested.first,
615 varStrategyRangesRequested.second,
614 varStrategyRangesRequested.second,
616 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
615 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
617 varProvider);
616 varProvider);
618
617
619 if (!varRequestIdCanceled.isNull()) {
620 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
621 << varRequestIdCanceled;
622 // cancelVariableRequest(varRequestIdCanceled);
623 abortVariableRequest(varRequestIdCanceled);
624 }
625 }
618 }
626 else {
619 else {
627 qCCritical(LOG_VariableController())
620 qCCritical(LOG_VariableController())
628 << "Impossible to provide data with a null provider";
621 << "Impossible to provide data with a null provider";
629 }
622 }
630
623
631 if (!inCacheRangeList.empty()) {
624 if (!inCacheRangeList.empty()) {
632 emit q->updateVarDisplaying(var, inCacheRangeList.first());
625 emit q->updateVarDisplaying(var, inCacheRangeList.first());
633 }
626 }
634 }
627 }
635 else {
628 else {
636 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
629 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
637 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
630 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
638 // store VarRequest
631 // store VarRequest
639 storeVariableRequest(varId, varRequestId, varRequest);
632 storeVariableRequest(varId, varRequestId, varRequest);
640 acceptVariableRequest(
633 acceptVariableRequest(
641 varId, var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
634 varId, var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
642 }
635 }
643 }
636 }
644 }
637 }
645
638
646 std::shared_ptr<Variable>
639 std::shared_ptr<Variable>
647 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
640 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
648 {
641 {
649 std::shared_ptr<Variable> var;
642 std::shared_ptr<Variable> var;
650 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
643 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
651
644
652 auto end = m_VariableToIdentifierMap.cend();
645 auto end = m_VariableToIdentifierMap.cend();
653 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
646 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
654 if (it != end) {
647 if (it != end) {
655 var = it->first;
648 var = it->first;
656 }
649 }
657 else {
650 else {
658 qCCritical(LOG_VariableController())
651 qCCritical(LOG_VariableController())
659 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
652 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
660 }
653 }
661
654
662 return var;
655 return var;
663 }
656 }
664
657
665 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
658 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
666 const QVector<AcquisitionDataPacket> acqDataPacketVector)
659 const QVector<AcquisitionDataPacket> acqDataPacketVector)
667 {
660 {
668 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
661 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
669 << acqDataPacketVector.size();
662 << acqDataPacketVector.size();
670 std::shared_ptr<IDataSeries> dataSeries;
663 std::shared_ptr<IDataSeries> dataSeries;
671 if (!acqDataPacketVector.isEmpty()) {
664 if (!acqDataPacketVector.isEmpty()) {
672 dataSeries = acqDataPacketVector[0].m_DateSeries;
665 dataSeries = acqDataPacketVector[0].m_DateSeries;
673 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
666 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
674 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
667 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
675 }
668 }
676 }
669 }
677 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
670 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
678 << acqDataPacketVector.size();
671 << acqDataPacketVector.size();
679 return dataSeries;
672 return dataSeries;
680 }
673 }
681
674
682 void VariableController::VariableControllerPrivate::registerProvider(
675 void VariableController::VariableControllerPrivate::registerProvider(
683 std::shared_ptr<IDataProvider> provider)
676 std::shared_ptr<IDataProvider> provider)
684 {
677 {
685 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
678 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
686 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
679 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
687 << provider->objectName();
680 << provider->objectName();
688 m_ProviderSet.insert(provider);
681 m_ProviderSet.insert(provider);
689 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
682 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
690 &VariableAcquisitionWorker::onVariableDataAcquired);
683 &VariableAcquisitionWorker::onVariableDataAcquired);
691 connect(provider.get(), &IDataProvider::dataProvidedProgress,
684 connect(provider.get(), &IDataProvider::dataProvidedProgress,
692 m_VariableAcquisitionWorker.get(),
685 m_VariableAcquisitionWorker.get(),
693 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
686 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
694 connect(provider.get(), &IDataProvider::dataProvidedFailed,
687 connect(provider.get(), &IDataProvider::dataProvidedFailed,
695 m_VariableAcquisitionWorker.get(),
688 m_VariableAcquisitionWorker.get(),
696 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
689 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
697 }
690 }
698 else {
691 else {
699 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
692 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
700 }
693 }
701 }
694 }
702
695
703 void VariableController::VariableControllerPrivate::storeVariableRequest(
696 void VariableController::VariableControllerPrivate::storeVariableRequest(
704 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
697 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
705 {
698 {
706 // First request for the variable. we can create an entry for it
699 // First request for the variable. we can create an entry for it
707 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
700 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
708 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
701 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
709 auto varRequestIdQueue = std::deque<QUuid>{};
702 auto varRequestIdQueue = std::deque<QUuid>{};
710 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
703 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
711 varRequestIdQueue.push_back(varRequestId);
704 varRequestIdQueue.push_back(varRequestId);
712 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
705 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
713 }
706 }
714 else {
707 else {
715 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
708 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
716 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
709 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
717 varRequestIdQueue.push_back(varRequestId);
710 varRequestIdQueue.push_back(varRequestId);
718 }
711 }
719
712
720 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
713 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
721 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
714 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
722 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
715 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
723 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
716 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
724 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
717 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
725 m_VarRequestIdToVarIdVarRequestMap.insert(
718 m_VarRequestIdToVarIdVarRequestMap.insert(
726 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
719 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
727 }
720 }
728 else {
721 else {
729 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
722 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
730 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
723 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
731 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
724 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
732 }
725 }
733 }
726 }
734
727
735 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
728 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
736 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
729 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
737 {
730 {
738 QUuid varRequestId;
731 QUuid varRequestId;
739 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
732 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
740 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
733 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
741 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
734 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
742 varRequestId = varRequestIdQueue.front();
735 varRequestId = varRequestIdQueue.front();
743 auto varRequestIdToVarIdVarRequestMapIt
736 auto varRequestIdToVarIdVarRequestMapIt
744 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
737 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
745 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
738 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
746 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
739 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
747 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
740 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
748 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
741 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
749 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
742 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
750 auto &varRequest = varIdToVarRequestMapIt->second;
743 auto &varRequest = varIdToVarRequestMapIt->second;
751 varRequest.m_DataSeries = dataSeries;
744 varRequest.m_DataSeries = dataSeries;
752 varRequest.m_CanUpdate = true;
745 varRequest.m_CanUpdate = true;
753 }
746 }
754 else {
747 else {
755 qCDebug(LOG_VariableController())
748 qCDebug(LOG_VariableController())
756 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
749 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
757 "to a variableRequestId")
750 "to a variableRequestId")
758 << varRequestId << varId;
751 << varRequestId << varId;
759 }
752 }
760 }
753 }
761 else {
754 else {
762 qCCritical(LOG_VariableController())
755 qCCritical(LOG_VariableController())
763 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
756 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
764 << varRequestId;
757 << varRequestId;
765 }
758 }
766
759
767 varRequestIdQueue.pop_front();
760 varRequestIdQueue.pop_front();
768 if (varRequestIdQueue.empty()) {
761 if (varRequestIdQueue.empty()) {
769 qCDebug(LOG_VariableController())
762 qCDebug(LOG_VariableController())
770 << tr("TORM Erase REQUEST because it has been accepted") << varId;
763 << tr("TORM Erase REQUEST because it has been accepted") << varId;
771 m_VarIdToVarRequestIdQueueMap.erase(varId);
764 m_VarIdToVarRequestIdQueueMap.erase(varId);
772 }
765 }
773 }
766 }
774 else {
767 else {
775 qCCritical(LOG_VariableController())
768 qCCritical(LOG_VariableController())
776 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
769 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
777 }
770 }
778
771
779 return varRequestId;
772 return varRequestId;
780 }
773 }
781
774
782 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
775 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
783 {
776 {
784
777
785 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
778 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
786 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
779 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
787 bool processVariableUpdate = true;
780 bool processVariableUpdate = true;
788 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
781 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
789 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
782 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
790 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
783 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
791 ++varIdToVarRequestMapIt) {
784 ++varIdToVarRequestMapIt) {
792 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
785 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
793 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
786 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
794 << processVariableUpdate;
787 << processVariableUpdate;
795 }
788 }
796
789
797 if (processVariableUpdate) {
790 if (processVariableUpdate) {
798 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
791 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
799 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
792 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
800 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
793 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
801 auto &varRequest = varIdToVarRequestMapIt->second;
794 auto &varRequest = varIdToVarRequestMapIt->second;
802 var->setRange(varRequest.m_RangeRequested);
795 var->setRange(varRequest.m_RangeRequested);
803 var->setCacheRange(varRequest.m_CacheRangeRequested);
796 var->setCacheRange(varRequest.m_CacheRangeRequested);
804 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
797 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
805 << varRequest.m_RangeRequested
798 << varRequest.m_RangeRequested
806 << varRequest.m_CacheRangeRequested;
799 << varRequest.m_CacheRangeRequested;
807 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
800 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
808 << var->nbPoints()
801 << var->nbPoints()
809 << varRequest.m_DataSeries->nbPoints();
802 << varRequest.m_DataSeries->nbPoints();
810 var->mergeDataSeries(varRequest.m_DataSeries);
803 var->mergeDataSeries(varRequest.m_DataSeries);
811 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
804 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
812 << var->nbPoints();
805 << var->nbPoints();
813
806
814 emit var->updated();
807 emit var->updated();
815 }
808 }
816 else {
809 else {
817 qCCritical(LOG_VariableController())
810 qCCritical(LOG_VariableController())
818 << tr("Impossible to update data to a null variable");
811 << tr("Impossible to update data to a null variable");
819 }
812 }
820 }
813 }
821 // cleaning varRequestId
814 // cleaning varRequestId
822 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
815 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
823 << m_VarRequestIdToVarIdVarRequestMap.size();
816 << m_VarRequestIdToVarIdVarRequestMap.size();
824 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
817 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
825 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
818 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
826 << m_VarRequestIdToVarIdVarRequestMap.size();
819 << m_VarRequestIdToVarIdVarRequestMap.size();
827 }
820 }
828 }
821 }
829 else {
822 else {
830 qCCritical(LOG_VariableController())
823 qCCritical(LOG_VariableController())
831 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
824 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
832 }
825 }
833 }
826 }
834
827
835 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
828 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
836 {
829 {
837 // cleaning varRequestId
830 // cleaning varRequestId
838 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
831 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
839
832
840 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
833 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
841 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
834 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
842 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
835 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
843 varRequestIdQueue.erase(
836 varRequestIdQueue.erase(
844 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
837 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
845 varRequestIdQueue.end());
838 varRequestIdQueue.end());
846 if (varRequestIdQueue.empty()) {
839 if (varRequestIdQueue.empty()) {
847 varIdToVarRequestIdQueueMapIt
840 varIdToVarRequestIdQueueMapIt
848 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
841 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
849
842
850 // Recompute if there is any next request based on the removed request.
843 // Recompute if there is any next request based on the removed request.
851 }
844 }
852 else {
845 else {
853 ++varIdToVarRequestIdQueueMapIt;
846 ++varIdToVarRequestIdQueueMapIt;
854 }
847 }
855 }
848 }
856 }
849 }
857
850
858 void VariableController::VariableControllerPrivate::abortVariableRequest(QUuid varRequestId)
851 void VariableController::VariableControllerPrivate::abortVariableRequest(QUuid varRequestId)
859 {
852 {
860 auto varRequestIdsMap = std::map<QUuid, VariableRequest>{};
853 auto varRequestIdsMap = std::map<QUuid, VariableRequest>{};
861 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
854 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
862 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
855 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
863 varRequestIdsMap = varRequestIdToVarIdVarRequestMapIt->second;
856 varRequestIdsMap = varRequestIdToVarIdVarRequestMapIt->second;
864 }
857 }
858 qCCritical(LOG_VariableController())
859 << tr("abortVariableRequest : varRequestId and count of aborting") << varRequestId
860 << varRequestIdsMap.size();
865
861
866 auto nextUuidToRemove = QSet<QUuid>{};
862 auto nextUuidToRemove = QSet<QUuid>{};
867 auto varIdEnd = varRequestIdsMap.end();
863 auto varIdEnd = varRequestIdsMap.end();
868 for (auto varIdIt = varRequestIdsMap.begin(); varIdIt != varIdEnd;) {
864 for (auto varIdIt = varRequestIdsMap.begin(); varIdIt != varIdEnd;) {
869 auto currentVarId = varIdIt->first;
865 auto currentVarId = varIdIt->first;
870 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(currentVarId);
866 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(currentVarId);
871 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
867 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
872
868
873 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
869 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
874
870
875 auto varReqIdQueueEnd = varRequestIdQueue.end();
871 auto varReqIdQueueEnd = varRequestIdQueue.end();
876 for (auto varReqIdQueueIt = varRequestIdQueue.begin();
872 for (auto varReqIdQueueIt = varRequestIdQueue.begin();
877 varReqIdQueueIt != varRequestIdQueue.end(); ++varReqIdQueueIt) {
873 varReqIdQueueIt != varRequestIdQueue.end(); ++varReqIdQueueIt) {
878 if (*varReqIdQueueIt == varRequestId) {
874 if (*varReqIdQueueIt == varRequestId) {
879 auto nextVarRequestIdToRm = varReqIdQueueIt;
875 auto nextVarRequestIdToRm = varReqIdQueueIt;
880 ++nextVarRequestIdToRm;
876 ++nextVarRequestIdToRm;
881
877
882 if (nextVarRequestIdToRm == varReqIdQueueEnd) {
878 if (nextVarRequestIdToRm == varReqIdQueueEnd) {
883 // The varRequestId is in progress for the current var, let's aborting it.
879 // The varRequestId is in progress for the current var, let's aborting it.
884 m_VariableAcquisitionWorker->abortProgressRequested(currentVarId);
880 m_VariableAcquisitionWorker->abortProgressRequested(currentVarId);
885 }
881 }
886 else {
882 else {
887 // There is at least one Request after
883 // There is at least one Request after
888 // let's add only new id to remove
884 // let's add only new id to remove
889 if (!nextUuidToRemove.contains(*nextVarRequestIdToRm)) {
885 if (!nextUuidToRemove.contains(*nextVarRequestIdToRm)) {
890 nextUuidToRemove << *nextVarRequestIdToRm;
886 nextUuidToRemove << *nextVarRequestIdToRm;
891 }
887 }
892 }
888 }
893
889
890 qCCritical(LOG_VariableController()) << tr("remove the request : aborting")
891 << currentVarId;
892
894 varReqIdQueueIt = varRequestIdQueue.erase(varReqIdQueueIt);
893 varReqIdQueueIt = varRequestIdQueue.erase(varReqIdQueueIt);
895 // break is necessary here, we don"t need to iterate on the dequeue anymore and
894 // break is necessary here, we don"t need to iterate on the dequeue anymore and
896 // the iteration is broken because of the erase
895 // the iteration is broken because of the erase
897 break;
896 break;
898 }
897 }
899 }
898 }
900
899
901 // beacause the process can modify the map under the iteration, we need to update the
900 // beacause the process can modify the map under the iteration, we need to update the
902 // iterator only if no erase has been done
901 // iterator only if no erase has been done
903 if (varRequestIdQueue.empty()) {
902 if (varRequestIdQueue.empty()) {
904 varIdIt = varRequestIdsMap.erase(varIdIt);
903 varIdIt = varRequestIdsMap.erase(varIdIt);
905 }
904 }
906 else {
905 else {
907 ++varIdIt;
906 ++varIdIt;
908 }
907 }
909 }
908 }
910 }
909 }
911
910
912 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
911 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
912 qCCritical(LOG_VariableController()) << tr("remove the varRequestId");
913 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestIdToVarIdVarRequestMapIt);
913 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestIdToVarIdVarRequestMapIt);
914 }
914 }
915
915
916 for (auto nextVarRequestIdToRm : nextUuidToRemove) {
916 for (auto nextVarRequestIdToRm : nextUuidToRemove) {
917 qCCritical(LOG_VariableController()) << tr("Cancel automaticaly the next request:")
918 << nextVarRequestIdToRm;
917 abortVariableRequest(nextVarRequestIdToRm);
919 abortVariableRequest(nextVarRequestIdToRm);
918 }
920 }
919 }
921 }
920
922
921 SqpRange VariableController::VariableControllerPrivate::getLastRequestedRange(QUuid varId)
923 SqpRange VariableController::VariableControllerPrivate::getLastRequestedRange(QUuid varId)
922 {
924 {
923 auto lastRangeRequested = SqpRange{INVALID_RANGE};
925 auto lastRangeRequested = SqpRange{INVALID_RANGE};
924 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
926 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
925 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
927 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
926 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
928 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
927 auto varRequestId = varRequestIdQueue.back();
929 auto varRequestId = varRequestIdQueue.back();
928 auto varRequestIdToVarIdVarRequestMapIt
930 auto varRequestIdToVarIdVarRequestMapIt
929 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
931 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
930 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
932 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
931 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
933 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
932 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
934 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
933 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
935 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
934 auto &varRequest = varIdToVarRequestMapIt->second;
936 auto &varRequest = varIdToVarRequestMapIt->second;
935 lastRangeRequested = varRequest.m_RangeRequested;
937 lastRangeRequested = varRequest.m_RangeRequested;
936 }
938 }
937 else {
939 else {
938 qCDebug(LOG_VariableController())
940 qCDebug(LOG_VariableController())
939 << tr("Impossible to getLastRequestedRange of a unknown variable id attached "
941 << tr("Impossible to getLastRequestedRange of a unknown variable id attached "
940 "to a variableRequestId")
942 "to a variableRequestId")
941 << varRequestId << varId;
943 << varRequestId << varId;
942 }
944 }
943 }
945 }
944 else {
946 else {
945 qCCritical(LOG_VariableController())
947 qCCritical(LOG_VariableController())
946 << tr("Impossible to getLastRequestedRange of a unknown variableRequestId")
948 << tr("Impossible to getLastRequestedRange of a unknown variableRequestId")
947 << varRequestId;
949 << varRequestId;
948 }
950 }
949 }
951 }
950 else {
952 else {
951 qDebug(LOG_VariableController())
953 qDebug(LOG_VariableController())
952 << tr("Impossible to getLastRequestedRange of a unknown variable id") << varId;
954 << tr("Impossible to getLastRequestedRange of a unknown variable id") << varId;
953 }
955 }
954
956
955 return lastRangeRequested;
957 return lastRangeRequested;
956 }
958 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

Merge lasted acquisition developpement on main Sciqlop branch

You need to be logged in to leave comments. Login now