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