##// END OF EJS Templates
Blocks UI update of a variable while it has pending requests
Alexandre Leroux -
r649:a482710ef34e
parent child
Show More
@@ -1,738 +1,743
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/VariableController.h>
4 #include <Variable/VariableController.h>
5 #include <Variable/VariableModel.h>
5 #include <Variable/VariableModel.h>
6 #include <Variable/VariableSynchronizationGroup.h>
6 #include <Variable/VariableSynchronizationGroup.h>
7
7
8 #include <Data/DataProviderParameters.h>
8 #include <Data/DataProviderParameters.h>
9 #include <Data/IDataProvider.h>
9 #include <Data/IDataProvider.h>
10 #include <Data/IDataSeries.h>
10 #include <Data/IDataSeries.h>
11 #include <Data/VariableRequest.h>
11 #include <Data/VariableRequest.h>
12 #include <Time/TimeController.h>
12 #include <Time/TimeController.h>
13
13
14 #include <QMutex>
14 #include <QMutex>
15 #include <QThread>
15 #include <QThread>
16 #include <QUuid>
16 #include <QUuid>
17 #include <QtCore/QItemSelectionModel>
17 #include <QtCore/QItemSelectionModel>
18
18
19 #include <deque>
19 #include <deque>
20 #include <set>
20 #include <set>
21 #include <unordered_map>
21 #include <unordered_map>
22
22
23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24
24
25 namespace {
25 namespace {
26
26
27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 const SqpRange &oldGraphRange)
28 const SqpRange &oldGraphRange)
29 {
29 {
30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31
31
32 auto varRangeRequested = varRange;
32 auto varRangeRequested = varRange;
33 switch (zoomType) {
33 switch (zoomType) {
34 case AcquisitionZoomType::ZoomIn: {
34 case AcquisitionZoomType::ZoomIn: {
35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 varRangeRequested.m_TStart += deltaLeft;
37 varRangeRequested.m_TStart += deltaLeft;
38 varRangeRequested.m_TEnd -= deltaRight;
38 varRangeRequested.m_TEnd -= deltaRight;
39 break;
39 break;
40 }
40 }
41
41
42 case AcquisitionZoomType::ZoomOut: {
42 case AcquisitionZoomType::ZoomOut: {
43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 varRangeRequested.m_TStart -= deltaLeft;
45 varRangeRequested.m_TStart -= deltaLeft;
46 varRangeRequested.m_TEnd += deltaRight;
46 varRangeRequested.m_TEnd += deltaRight;
47 break;
47 break;
48 }
48 }
49 case AcquisitionZoomType::PanRight: {
49 case AcquisitionZoomType::PanRight: {
50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
51 varRangeRequested.m_TStart += deltaRight;
51 varRangeRequested.m_TStart += deltaRight;
52 varRangeRequested.m_TEnd += deltaRight;
52 varRangeRequested.m_TEnd += deltaRight;
53 break;
53 break;
54 }
54 }
55 case AcquisitionZoomType::PanLeft: {
55 case AcquisitionZoomType::PanLeft: {
56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
57 varRangeRequested.m_TStart -= deltaLeft;
57 varRangeRequested.m_TStart -= deltaLeft;
58 varRangeRequested.m_TEnd -= deltaLeft;
58 varRangeRequested.m_TEnd -= deltaLeft;
59 break;
59 break;
60 }
60 }
61 case AcquisitionZoomType::Unknown: {
61 case AcquisitionZoomType::Unknown: {
62 qCCritical(LOG_VariableController())
62 qCCritical(LOG_VariableController())
63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
64 break;
64 break;
65 }
65 }
66 default:
66 default:
67 qCCritical(LOG_VariableController()) << VariableController::tr(
67 qCCritical(LOG_VariableController()) << VariableController::tr(
68 "Impossible to synchronize: zoom type not take into account");
68 "Impossible to synchronize: zoom type not take into account");
69 // No action
69 // No action
70 break;
70 break;
71 }
71 }
72
72
73 return varRangeRequested;
73 return varRangeRequested;
74 }
74 }
75 }
75 }
76
76
77 struct VariableController::VariableControllerPrivate {
77 struct VariableController::VariableControllerPrivate {
78 explicit VariableControllerPrivate(VariableController *parent)
78 explicit VariableControllerPrivate(VariableController *parent)
79 : m_WorkingMutex{},
79 : m_WorkingMutex{},
80 m_VariableModel{new VariableModel{parent}},
80 m_VariableModel{new VariableModel{parent}},
81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
84 q{parent}
84 q{parent}
85 {
85 {
86
86
87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
89 }
89 }
90
90
91
91
92 virtual ~VariableControllerPrivate()
92 virtual ~VariableControllerPrivate()
93 {
93 {
94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
95 m_VariableAcquisitionWorkerThread.quit();
95 m_VariableAcquisitionWorkerThread.quit();
96 m_VariableAcquisitionWorkerThread.wait();
96 m_VariableAcquisitionWorkerThread.wait();
97 }
97 }
98
98
99
99
100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
101 QUuid varRequestId);
101 QUuid varRequestId);
102
102
103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
104 const SqpRange &dateTime);
104 const SqpRange &dateTime);
105
105
106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
107 std::shared_ptr<IDataSeries>
107 std::shared_ptr<IDataSeries>
108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
109
109
110 void registerProvider(std::shared_ptr<IDataProvider> provider);
110 void registerProvider(std::shared_ptr<IDataProvider> provider);
111
111
112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
114 void updateVariableRequest(QUuid varRequestId);
114 void updateVariableRequest(QUuid varRequestId);
115 void cancelVariableRequest(QUuid varRequestId);
115 void cancelVariableRequest(QUuid varRequestId);
116
116
117 QMutex m_WorkingMutex;
117 QMutex m_WorkingMutex;
118 /// Variable model. The VariableController has the ownership
118 /// Variable model. The VariableController has the ownership
119 VariableModel *m_VariableModel;
119 VariableModel *m_VariableModel;
120 QItemSelectionModel *m_VariableSelectionModel;
120 QItemSelectionModel *m_VariableSelectionModel;
121
121
122
122
123 TimeController *m_TimeController{nullptr};
123 TimeController *m_TimeController{nullptr};
124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
126 QThread m_VariableAcquisitionWorkerThread;
126 QThread m_VariableAcquisitionWorkerThread;
127
127
128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
129 m_VariableToProviderMap;
129 m_VariableToProviderMap;
130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
132 m_GroupIdToVariableSynchronizationGroupMap;
132 m_GroupIdToVariableSynchronizationGroupMap;
133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
135
135
136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
137
137
138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
139
139
140
140
141 VariableController *q;
141 VariableController *q;
142 };
142 };
143
143
144
144
145 VariableController::VariableController(QObject *parent)
145 VariableController::VariableController(QObject *parent)
146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
147 {
147 {
148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
149 << QThread::currentThread();
149 << QThread::currentThread();
150
150
151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
152 &VariableController::onAbortProgressRequested);
152 &VariableController::onAbortProgressRequested);
153
153
154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
155 &VariableController::onDataProvided);
155 &VariableController::onDataProvided);
156 connect(impl->m_VariableAcquisitionWorker.get(),
156 connect(impl->m_VariableAcquisitionWorker.get(),
157 &VariableAcquisitionWorker::variableRequestInProgress, this,
157 &VariableAcquisitionWorker::variableRequestInProgress, this,
158 &VariableController::onVariableRetrieveDataInProgress);
158 &VariableController::onVariableRetrieveDataInProgress);
159
159
160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
164
164
165
165
166 impl->m_VariableAcquisitionWorkerThread.start();
166 impl->m_VariableAcquisitionWorkerThread.start();
167 }
167 }
168
168
169 VariableController::~VariableController()
169 VariableController::~VariableController()
170 {
170 {
171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
172 << QThread::currentThread();
172 << QThread::currentThread();
173 this->waitForFinish();
173 this->waitForFinish();
174 }
174 }
175
175
176 VariableModel *VariableController::variableModel() noexcept
176 VariableModel *VariableController::variableModel() noexcept
177 {
177 {
178 return impl->m_VariableModel;
178 return impl->m_VariableModel;
179 }
179 }
180
180
181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
182 {
182 {
183 return impl->m_VariableSelectionModel;
183 return impl->m_VariableSelectionModel;
184 }
184 }
185
185
186 void VariableController::setTimeController(TimeController *timeController) noexcept
186 void VariableController::setTimeController(TimeController *timeController) noexcept
187 {
187 {
188 impl->m_TimeController = timeController;
188 impl->m_TimeController = timeController;
189 }
189 }
190
190
191 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
191 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
192 {
192 {
193 if (!variable) {
193 if (!variable) {
194 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
194 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
195 return;
195 return;
196 }
196 }
197
197
198 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
198 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
199 // make some treatments before the deletion
199 // make some treatments before the deletion
200 emit variableAboutToBeDeleted(variable);
200 emit variableAboutToBeDeleted(variable);
201
201
202 // Deletes identifier
202 // Deletes identifier
203 impl->m_VariableToIdentifierMap.erase(variable);
203 impl->m_VariableToIdentifierMap.erase(variable);
204
204
205 // Deletes provider
205 // Deletes provider
206 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
206 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
207 qCDebug(LOG_VariableController())
207 qCDebug(LOG_VariableController())
208 << tr("Number of providers deleted for variable %1: %2")
208 << tr("Number of providers deleted for variable %1: %2")
209 .arg(variable->name(), QString::number(nbProvidersDeleted));
209 .arg(variable->name(), QString::number(nbProvidersDeleted));
210
210
211
211
212 // Deletes from model
212 // Deletes from model
213 impl->m_VariableModel->deleteVariable(variable);
213 impl->m_VariableModel->deleteVariable(variable);
214 }
214 }
215
215
216 void VariableController::deleteVariables(
216 void VariableController::deleteVariables(
217 const QVector<std::shared_ptr<Variable> > &variables) noexcept
217 const QVector<std::shared_ptr<Variable> > &variables) noexcept
218 {
218 {
219 for (auto variable : qAsConst(variables)) {
219 for (auto variable : qAsConst(variables)) {
220 deleteVariable(variable);
220 deleteVariable(variable);
221 }
221 }
222 }
222 }
223
223
224 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
224 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
225 {
225 {
226 }
226 }
227
227
228 std::shared_ptr<Variable>
228 std::shared_ptr<Variable>
229 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
229 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
230 std::shared_ptr<IDataProvider> provider) noexcept
230 std::shared_ptr<IDataProvider> provider) noexcept
231 {
231 {
232 if (!impl->m_TimeController) {
232 if (!impl->m_TimeController) {
233 qCCritical(LOG_VariableController())
233 qCCritical(LOG_VariableController())
234 << tr("Impossible to create variable: The time controller is null");
234 << tr("Impossible to create variable: The time controller is null");
235 return nullptr;
235 return nullptr;
236 }
236 }
237
237
238 auto range = impl->m_TimeController->dateTime();
238 auto range = impl->m_TimeController->dateTime();
239
239
240 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
240 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
241 auto identifier = QUuid::createUuid();
241 auto identifier = QUuid::createUuid();
242
242
243 // store the provider
243 // store the provider
244 impl->registerProvider(provider);
244 impl->registerProvider(provider);
245
245
246 // Associate the provider
246 // Associate the provider
247 impl->m_VariableToProviderMap[newVariable] = provider;
247 impl->m_VariableToProviderMap[newVariable] = provider;
248 impl->m_VariableToIdentifierMap[newVariable] = identifier;
248 impl->m_VariableToIdentifierMap[newVariable] = identifier;
249
249
250
250
251 auto varRequestId = QUuid::createUuid();
251 auto varRequestId = QUuid::createUuid();
252 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
252 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
253 impl->processRequest(newVariable, range, varRequestId);
253 impl->processRequest(newVariable, range, varRequestId);
254 impl->updateVariableRequest(varRequestId);
254 impl->updateVariableRequest(varRequestId);
255
255
256 return newVariable;
256 return newVariable;
257 }
257 }
258 }
258 }
259
259
260 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
260 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
261 {
261 {
262 // TODO check synchronisation and Rescale
262 // TODO check synchronisation and Rescale
263 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
263 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
264 << QThread::currentThread()->objectName();
264 << QThread::currentThread()->objectName();
265 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
265 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
266 auto varRequestId = QUuid::createUuid();
266 auto varRequestId = QUuid::createUuid();
267
267
268 for (const auto &selectedRow : qAsConst(selectedRows)) {
268 for (const auto &selectedRow : qAsConst(selectedRows)) {
269 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
269 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
270 selectedVariable->setRange(dateTime);
270 selectedVariable->setRange(dateTime);
271 impl->processRequest(selectedVariable, dateTime, varRequestId);
271 impl->processRequest(selectedVariable, dateTime, varRequestId);
272
272
273 // notify that rescale operation has to be done
273 // notify that rescale operation has to be done
274 emit rangeChanged(selectedVariable, dateTime);
274 emit rangeChanged(selectedVariable, dateTime);
275 }
275 }
276 }
276 }
277 impl->updateVariableRequest(varRequestId);
277 impl->updateVariableRequest(varRequestId);
278 }
278 }
279
279
280 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
280 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
281 const SqpRange &cacheRangeRequested,
281 const SqpRange &cacheRangeRequested,
282 QVector<AcquisitionDataPacket> dataAcquired)
282 QVector<AcquisitionDataPacket> dataAcquired)
283 {
283 {
284 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
284 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
285 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
285 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
286 if (!varRequestId.isNull()) {
286 if (!varRequestId.isNull()) {
287 impl->updateVariableRequest(varRequestId);
287 impl->updateVariableRequest(varRequestId);
288 }
288 }
289 }
289 }
290
290
291 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
291 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
292 {
292 {
293 if (auto var = impl->findVariable(identifier)) {
293 if (auto var = impl->findVariable(identifier)) {
294 impl->m_VariableModel->setDataProgress(var, progress);
294 impl->m_VariableModel->setDataProgress(var, progress);
295 }
295 }
296 else {
296 else {
297 qCCritical(LOG_VariableController())
297 qCCritical(LOG_VariableController())
298 << tr("Impossible to notify progression of a null variable");
298 << tr("Impossible to notify progression of a null variable");
299 }
299 }
300 }
300 }
301
301
302 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
302 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
303 {
303 {
304 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
304 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
305 << QThread::currentThread()->objectName();
305 << QThread::currentThread()->objectName();
306
306
307 auto it = impl->m_VariableToIdentifierMap.find(variable);
307 auto it = impl->m_VariableToIdentifierMap.find(variable);
308 if (it != impl->m_VariableToIdentifierMap.cend()) {
308 if (it != impl->m_VariableToIdentifierMap.cend()) {
309 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
309 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
310 }
310 }
311 else {
311 else {
312 qCWarning(LOG_VariableController())
312 qCWarning(LOG_VariableController())
313 << tr("Aborting progression of inexistant variable detected !!!")
313 << tr("Aborting progression of inexistant variable detected !!!")
314 << QThread::currentThread()->objectName();
314 << QThread::currentThread()->objectName();
315 }
315 }
316 }
316 }
317
317
318 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
318 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
319 {
319 {
320 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
320 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
321 << QThread::currentThread()->objectName()
321 << QThread::currentThread()->objectName()
322 << synchronizationGroupId;
322 << synchronizationGroupId;
323 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
323 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
324 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
324 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
325 std::make_pair(synchronizationGroupId, vSynchroGroup));
325 std::make_pair(synchronizationGroupId, vSynchroGroup));
326 }
326 }
327
327
328 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
328 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
329 {
329 {
330 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
330 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
331 }
331 }
332
332
333 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
333 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
334 QUuid synchronizationGroupId)
334 QUuid synchronizationGroupId)
335
335
336 {
336 {
337 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
337 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
338 << synchronizationGroupId;
338 << synchronizationGroupId;
339 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
339 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
340 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
340 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
341 auto groupIdToVSGIt
341 auto groupIdToVSGIt
342 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
342 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
343 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
343 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
344 impl->m_VariableIdGroupIdMap.insert(
344 impl->m_VariableIdGroupIdMap.insert(
345 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
345 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
346 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
346 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
347 }
347 }
348 else {
348 else {
349 qCCritical(LOG_VariableController())
349 qCCritical(LOG_VariableController())
350 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
350 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
351 << variable->name();
351 << variable->name();
352 }
352 }
353 }
353 }
354 else {
354 else {
355 qCCritical(LOG_VariableController())
355 qCCritical(LOG_VariableController())
356 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
356 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
357 }
357 }
358 }
358 }
359
359
360
360
361 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
361 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
362 const SqpRange &range, const SqpRange &oldRange,
362 const SqpRange &range, const SqpRange &oldRange,
363 bool synchronise)
363 bool synchronise)
364 {
364 {
365 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
365 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
366
366
367 // we want to load data of the variable for the dateTime.
367 // we want to load data of the variable for the dateTime.
368 // First we check if the cache contains some of them.
368 // First we check if the cache contains some of them.
369 // For the other, we ask the provider to give them.
369 // For the other, we ask the provider to give them.
370
370
371 auto varRequestId = QUuid::createUuid();
371 auto varRequestId = QUuid::createUuid();
372 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
372 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
373 << QThread::currentThread()->objectName() << varRequestId;
373 << QThread::currentThread()->objectName() << varRequestId;
374
374
375 for (const auto &var : variables) {
375 for (const auto &var : variables) {
376 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
376 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
377 impl->processRequest(var, range, varRequestId);
377 impl->processRequest(var, range, varRequestId);
378 }
378 }
379
379
380 if (synchronise) {
380 if (synchronise) {
381 // Get the group ids
381 // Get the group ids
382 qCDebug(LOG_VariableController())
382 qCDebug(LOG_VariableController())
383 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
383 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
384 auto groupIds = std::set<QUuid>{};
384 auto groupIds = std::set<QUuid>{};
385 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
385 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
386 for (const auto &var : variables) {
386 for (const auto &var : variables) {
387 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
387 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
388 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
388 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
389 auto vId = varToVarIdIt->second;
389 auto vId = varToVarIdIt->second;
390 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
390 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
391 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
391 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
392 auto gId = varIdToGroupIdIt->second;
392 auto gId = varIdToGroupIdIt->second;
393 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
393 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
394 if (groupIds.find(gId) == groupIds.cend()) {
394 if (groupIds.find(gId) == groupIds.cend()) {
395 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
395 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
396 groupIds.insert(gId);
396 groupIds.insert(gId);
397 }
397 }
398 }
398 }
399 }
399 }
400 }
400 }
401
401
402 // We assume here all group ids exist
402 // We assume here all group ids exist
403 for (const auto &gId : groupIds) {
403 for (const auto &gId : groupIds) {
404 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
404 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
405 auto vSyncIds = vSynchronizationGroup->getIds();
405 auto vSyncIds = vSynchronizationGroup->getIds();
406 qCDebug(LOG_VariableController()) << "Var in synchro group ";
406 qCDebug(LOG_VariableController()) << "Var in synchro group ";
407 for (auto vId : vSyncIds) {
407 for (auto vId : vSyncIds) {
408 auto var = impl->findVariable(vId);
408 auto var = impl->findVariable(vId);
409
409
410 // Don't process already processed var
410 // Don't process already processed var
411 if (!variables.contains(var)) {
411 if (!variables.contains(var)) {
412 if (var != nullptr) {
412 if (var != nullptr) {
413 qCDebug(LOG_VariableController()) << "processRequest synchro for"
413 qCDebug(LOG_VariableController()) << "processRequest synchro for"
414 << var->name();
414 << var->name();
415 auto vSyncRangeRequested = computeSynchroRangeRequested(
415 auto vSyncRangeRequested = computeSynchroRangeRequested(
416 var->range(), range, groupIdToOldRangeMap.at(gId));
416 var->range(), range, groupIdToOldRangeMap.at(gId));
417 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
417 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
418 impl->processRequest(var, vSyncRangeRequested, varRequestId);
418 impl->processRequest(var, vSyncRangeRequested, varRequestId);
419 }
419 }
420 else {
420 else {
421 qCCritical(LOG_VariableController())
421 qCCritical(LOG_VariableController())
422
422
423 << tr("Impossible to synchronize a null variable");
423 << tr("Impossible to synchronize a null variable");
424 }
424 }
425 }
425 }
426 }
426 }
427 }
427 }
428 }
428 }
429
429
430 impl->updateVariableRequest(varRequestId);
430 impl->updateVariableRequest(varRequestId);
431 }
431 }
432
432
433
433
434 void VariableController::initialize()
434 void VariableController::initialize()
435 {
435 {
436 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
436 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
437 impl->m_WorkingMutex.lock();
437 impl->m_WorkingMutex.lock();
438 qCDebug(LOG_VariableController()) << tr("VariableController init END");
438 qCDebug(LOG_VariableController()) << tr("VariableController init END");
439 }
439 }
440
440
441 void VariableController::finalize()
441 void VariableController::finalize()
442 {
442 {
443 impl->m_WorkingMutex.unlock();
443 impl->m_WorkingMutex.unlock();
444 }
444 }
445
445
446 void VariableController::waitForFinish()
446 void VariableController::waitForFinish()
447 {
447 {
448 QMutexLocker locker{&impl->m_WorkingMutex};
448 QMutexLocker locker{&impl->m_WorkingMutex};
449 }
449 }
450
450
451 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
451 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
452 {
452 {
453 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
453 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
454 auto zoomType = AcquisitionZoomType::Unknown;
454 auto zoomType = AcquisitionZoomType::Unknown;
455 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
455 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
456 zoomType = AcquisitionZoomType::ZoomOut;
456 zoomType = AcquisitionZoomType::ZoomOut;
457 }
457 }
458 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
458 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
459 zoomType = AcquisitionZoomType::PanRight;
459 zoomType = AcquisitionZoomType::PanRight;
460 }
460 }
461 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
461 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
462 zoomType = AcquisitionZoomType::PanLeft;
462 zoomType = AcquisitionZoomType::PanLeft;
463 }
463 }
464 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
464 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
465 zoomType = AcquisitionZoomType::ZoomIn;
465 zoomType = AcquisitionZoomType::ZoomIn;
466 }
466 }
467 else {
467 else {
468 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
468 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
469 }
469 }
470 return zoomType;
470 return zoomType;
471 }
471 }
472
472
473 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
473 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
474 const SqpRange &rangeRequested,
474 const SqpRange &rangeRequested,
475 QUuid varRequestId)
475 QUuid varRequestId)
476 {
476 {
477
477
478 // TODO: protect at
478 // TODO: protect at
479 auto varRequest = VariableRequest{};
479 auto varRequest = VariableRequest{};
480 auto varId = m_VariableToIdentifierMap.at(var);
480 auto varId = m_VariableToIdentifierMap.at(var);
481
481
482 auto varStrategyRangesRequested
482 auto varStrategyRangesRequested
483 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
483 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
484 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
484 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
485 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
485 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
486
486
487 if (!notInCacheRangeList.empty()) {
487 if (!notInCacheRangeList.empty()) {
488 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
488 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
489 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
489 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
490 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
490 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
491 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
491 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
492 << varStrategyRangesRequested.first;
492 << varStrategyRangesRequested.first;
493 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
493 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
494 << varStrategyRangesRequested.second;
494 << varStrategyRangesRequested.second;
495 // store VarRequest
495 // store VarRequest
496 storeVariableRequest(varId, varRequestId, varRequest);
496 storeVariableRequest(varId, varRequestId, varRequest);
497
497
498 auto varProvider = m_VariableToProviderMap.at(var);
498 auto varProvider = m_VariableToProviderMap.at(var);
499 if (varProvider != nullptr) {
499 if (varProvider != nullptr) {
500 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
500 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
501 varRequestId, varId, varStrategyRangesRequested.first,
501 varRequestId, varId, varStrategyRangesRequested.first,
502 varStrategyRangesRequested.second,
502 varStrategyRangesRequested.second,
503 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
503 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
504 varProvider);
504 varProvider);
505
505
506 if (!varRequestIdCanceled.isNull()) {
506 if (!varRequestIdCanceled.isNull()) {
507 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
507 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
508 << varRequestIdCanceled;
508 << varRequestIdCanceled;
509 cancelVariableRequest(varRequestIdCanceled);
509 cancelVariableRequest(varRequestIdCanceled);
510 }
510 }
511 }
511 }
512 else {
512 else {
513 qCCritical(LOG_VariableController())
513 qCCritical(LOG_VariableController())
514 << "Impossible to provide data with a null provider";
514 << "Impossible to provide data with a null provider";
515 }
515 }
516
516
517 if (!inCacheRangeList.empty()) {
517 if (!inCacheRangeList.empty()) {
518 emit q->updateVarDisplaying(var, inCacheRangeList.first());
518 emit q->updateVarDisplaying(var, inCacheRangeList.first());
519 }
519 }
520 }
520 }
521 else {
521 else {
522
522
523 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
523 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
524 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
524 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
525 // store VarRequest
525 // store VarRequest
526 storeVariableRequest(varId, varRequestId, varRequest);
526 storeVariableRequest(varId, varRequestId, varRequest);
527 acceptVariableRequest(varId,
527 acceptVariableRequest(varId,
528 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
528 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
529 }
529 }
530 }
530 }
531
531
532 std::shared_ptr<Variable>
532 std::shared_ptr<Variable>
533 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
533 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
534 {
534 {
535 std::shared_ptr<Variable> var;
535 std::shared_ptr<Variable> var;
536 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
536 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
537
537
538 auto end = m_VariableToIdentifierMap.cend();
538 auto end = m_VariableToIdentifierMap.cend();
539 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
539 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
540 if (it != end) {
540 if (it != end) {
541 var = it->first;
541 var = it->first;
542 }
542 }
543 else {
543 else {
544 qCCritical(LOG_VariableController())
544 qCCritical(LOG_VariableController())
545 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
545 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
546 }
546 }
547
547
548 return var;
548 return var;
549 }
549 }
550
550
551 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
551 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
552 const QVector<AcquisitionDataPacket> acqDataPacketVector)
552 const QVector<AcquisitionDataPacket> acqDataPacketVector)
553 {
553 {
554 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
554 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
555 << acqDataPacketVector.size();
555 << acqDataPacketVector.size();
556 std::shared_ptr<IDataSeries> dataSeries;
556 std::shared_ptr<IDataSeries> dataSeries;
557 if (!acqDataPacketVector.isEmpty()) {
557 if (!acqDataPacketVector.isEmpty()) {
558 dataSeries = acqDataPacketVector[0].m_DateSeries;
558 dataSeries = acqDataPacketVector[0].m_DateSeries;
559 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
559 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
560 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
560 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
561 }
561 }
562 }
562 }
563 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
563 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
564 << acqDataPacketVector.size();
564 << acqDataPacketVector.size();
565 return dataSeries;
565 return dataSeries;
566 }
566 }
567
567
568 void VariableController::VariableControllerPrivate::registerProvider(
568 void VariableController::VariableControllerPrivate::registerProvider(
569 std::shared_ptr<IDataProvider> provider)
569 std::shared_ptr<IDataProvider> provider)
570 {
570 {
571 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
571 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
572 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
572 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
573 << provider->objectName();
573 << provider->objectName();
574 m_ProviderSet.insert(provider);
574 m_ProviderSet.insert(provider);
575 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
575 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
576 &VariableAcquisitionWorker::onVariableDataAcquired);
576 &VariableAcquisitionWorker::onVariableDataAcquired);
577 connect(provider.get(), &IDataProvider::dataProvidedProgress,
577 connect(provider.get(), &IDataProvider::dataProvidedProgress,
578 m_VariableAcquisitionWorker.get(),
578 m_VariableAcquisitionWorker.get(),
579 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
579 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
580 }
580 }
581 else {
581 else {
582 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
582 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
583 }
583 }
584 }
584 }
585
585
586 void VariableController::VariableControllerPrivate::storeVariableRequest(
586 void VariableController::VariableControllerPrivate::storeVariableRequest(
587 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
587 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
588 {
588 {
589 // First request for the variable. we can create an entry for it
589 // First request for the variable. we can create an entry for it
590 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
590 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
591 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
591 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
592 auto varRequestIdQueue = std::deque<QUuid>{};
592 auto varRequestIdQueue = std::deque<QUuid>{};
593 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
593 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
594 varRequestIdQueue.push_back(varRequestId);
594 varRequestIdQueue.push_back(varRequestId);
595 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
595 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
596 }
596 }
597 else {
597 else {
598 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
598 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
599 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
599 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
600 varRequestIdQueue.push_back(varRequestId);
600 varRequestIdQueue.push_back(varRequestId);
601 }
601 }
602
602
603 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
603 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
604 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
604 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
605 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
605 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
606 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
606 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
607 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
607 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
608 m_VarRequestIdToVarIdVarRequestMap.insert(
608 m_VarRequestIdToVarIdVarRequestMap.insert(
609 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
609 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
610 }
610 }
611 else {
611 else {
612 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
612 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
613 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
613 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
614 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
614 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
615 }
615 }
616 }
616 }
617
617
618 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
618 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
619 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
619 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
620 {
620 {
621 QUuid varRequestId;
621 QUuid varRequestId;
622 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
622 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
623 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
623 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
624 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
624 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
625 varRequestId = varRequestIdQueue.front();
625 varRequestId = varRequestIdQueue.front();
626 auto varRequestIdToVarIdVarRequestMapIt
626 auto varRequestIdToVarIdVarRequestMapIt
627 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
627 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
628 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
628 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
629 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
629 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
630 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
630 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
631 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
631 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
632 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
632 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
633 auto &varRequest = varIdToVarRequestMapIt->second;
633 auto &varRequest = varIdToVarRequestMapIt->second;
634 varRequest.m_DataSeries = dataSeries;
634 varRequest.m_DataSeries = dataSeries;
635 varRequest.m_CanUpdate = true;
635 varRequest.m_CanUpdate = true;
636 }
636 }
637 else {
637 else {
638 qCDebug(LOG_VariableController())
638 qCDebug(LOG_VariableController())
639 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
639 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
640 "to a variableRequestId")
640 "to a variableRequestId")
641 << varRequestId << varId;
641 << varRequestId << varId;
642 }
642 }
643 }
643 }
644 else {
644 else {
645 qCCritical(LOG_VariableController())
645 qCCritical(LOG_VariableController())
646 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
646 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
647 << varRequestId;
647 << varRequestId;
648 }
648 }
649
649
650 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
650 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
651 << varRequestIdQueue.size();
651 << varRequestIdQueue.size();
652 varRequestIdQueue.pop_front();
652 varRequestIdQueue.pop_front();
653 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
653 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
654 << varRequestIdQueue.size();
654 << varRequestIdQueue.size();
655 if (varRequestIdQueue.empty()) {
655 if (varRequestIdQueue.empty()) {
656 m_VarIdToVarRequestIdQueueMap.erase(varId);
656 m_VarIdToVarRequestIdQueueMap.erase(varId);
657 }
657 }
658 }
658 }
659 else {
659 else {
660 qCCritical(LOG_VariableController())
660 qCCritical(LOG_VariableController())
661 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
661 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
662 }
662 }
663
663
664 return varRequestId;
664 return varRequestId;
665 }
665 }
666
666
667 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
667 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
668 {
668 {
669
669
670 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
670 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
671 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
671 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
672 bool processVariableUpdate = true;
672 bool processVariableUpdate = true;
673 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
673 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
674 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
674 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
675 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
675 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
676 ++varIdToVarRequestMapIt) {
676 ++varIdToVarRequestMapIt) {
677 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
677 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
678 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
678 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
679 << processVariableUpdate;
679 << processVariableUpdate;
680 }
680 }
681
681
682 if (processVariableUpdate) {
682 if (processVariableUpdate) {
683 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
683 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
684 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
684 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
685 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
685 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
686 auto &varRequest = varIdToVarRequestMapIt->second;
686 auto &varRequest = varIdToVarRequestMapIt->second;
687 var->setRange(varRequest.m_RangeRequested);
687 var->setRange(varRequest.m_RangeRequested);
688 var->setCacheRange(varRequest.m_CacheRangeRequested);
688 var->setCacheRange(varRequest.m_CacheRangeRequested);
689 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
689 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
690 << varRequest.m_RangeRequested;
690 << varRequest.m_RangeRequested;
691 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
691 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
692 << varRequest.m_CacheRangeRequested;
692 << varRequest.m_CacheRangeRequested;
693 var->mergeDataSeries(varRequest.m_DataSeries);
693 var->mergeDataSeries(varRequest.m_DataSeries);
694 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
694 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
695 << varRequest.m_DataSeries->range();
695 << varRequest.m_DataSeries->range();
696 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
696 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
697
698 /// @todo MPL: confirm
699 // Variable update is notified only if there is no pending request for it
700 if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) {
697 emit var->updated();
701 emit var->updated();
698 }
702 }
703 }
699 else {
704 else {
700 qCCritical(LOG_VariableController())
705 qCCritical(LOG_VariableController())
701 << tr("Impossible to update data to a null variable");
706 << tr("Impossible to update data to a null variable");
702 }
707 }
703 }
708 }
704
709
705 // cleaning varRequestId
710 // cleaning varRequestId
706 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
711 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
707 << m_VarRequestIdToVarIdVarRequestMap.size();
712 << m_VarRequestIdToVarIdVarRequestMap.size();
708 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
713 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
709 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
714 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
710 << m_VarRequestIdToVarIdVarRequestMap.size();
715 << m_VarRequestIdToVarIdVarRequestMap.size();
711 }
716 }
712 }
717 }
713 else {
718 else {
714 qCCritical(LOG_VariableController())
719 qCCritical(LOG_VariableController())
715 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
720 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
716 }
721 }
717 }
722 }
718
723
719 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
724 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
720 {
725 {
721 // cleaning varRequestId
726 // cleaning varRequestId
722 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
727 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
723
728
724 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
729 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
725 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
730 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
726 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
731 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
727 varRequestIdQueue.erase(
732 varRequestIdQueue.erase(
728 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
733 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
729 varRequestIdQueue.end());
734 varRequestIdQueue.end());
730 if (varRequestIdQueue.empty()) {
735 if (varRequestIdQueue.empty()) {
731 varIdToVarRequestIdQueueMapIt
736 varIdToVarRequestIdQueueMapIt
732 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
737 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
733 }
738 }
734 else {
739 else {
735 ++varIdToVarRequestIdQueueMapIt;
740 ++varIdToVarRequestIdQueueMapIt;
736 }
741 }
737 }
742 }
738 }
743 }
General Comments 0
You need to be logged in to leave comments. Login now