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