swift
Loading...
Searching...
No Matches
backgroundvalidation.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QDateTime>
7
8#include "misc/eventloop.h"
9#include "misc/logmessage.h"
12#include "misc/threadutils.h"
13
14using namespace swift::misc::network;
15using namespace swift::misc::simulation::data;
16
17namespace swift::misc::simulation
18{
24
26 : CContinuousWorker(owner, "Background validation"), m_updateTimer(this, "Background validation")
27 {
28 connect(&m_updateTimer, &CThreadedTimer::timeout, this, &CBackgroundValidation::doWork);
29 }
30
31 void CBackgroundValidation::setCurrentSimulator(const CSimulatorInfo &simulator, const QString &simDirectory,
32 const QStringList &modelDirList)
33 {
34 QWriteLocker l(&m_lock);
35 m_simulator = simulator;
36 m_simDirectory = simDirectory;
37 m_modelDirList = modelDirList;
38 }
39
41 {
42 QReadLocker l(&m_lock);
43 return m_checkedSimulatorMsgs.contains(simulator);
44 }
45
47 {
48 QWriteLocker l(&m_lock);
49 m_checkedSimulatorMsgs.remove(simulator);
50 }
51
53 {
54 QReadLocker l(&m_lock);
55 return m_simulator;
56 }
57
58 bool CBackgroundValidation::triggerValidation(const CSimulatorInfo &simulator, const QString &simDirectory)
59 {
60 const QPointer<CBackgroundValidation> myself(this);
61 if (simulator.isNoSimulator())
62 {
63 return this->requestLastValidationResults();
64 }
65
66 {
67 QWriteLocker l(&m_lock);
68 if (m_inWork)
69 {
70 return false;
71 }
72 m_simulator = simulator;
73 m_simDirectory = simDirectory;
74 m_checkedSimulatorMsgs.remove(simulator);
75 }
76 QTimer::singleShot(5, this, [=] {
77 if (!myself)
78 {
79 return;
80 }
81 myself->doWork();
82 });
83 return true;
84 }
85
87 {
89 CAircraftModelList invalid;
90 CAircraftModelList models;
92 CSimulatorInfo simulator;
93 bool wasStopped = false;
94 {
95 QReadLocker l(&m_lock);
96 simulator = m_lastResultSimulator;
97 valid = m_lastResultValid;
98 invalid = m_lastResultInvalid;
99 msgs = m_lastResultMsgs;
100 wasStopped = m_lastResultWasStopped;
101 }
102 if (m_lastResultSimulator.isUnspecified())
103 {
104 return false;
105 }
106 emit this->validated(simulator, valid, invalid, wasStopped, msgs);
107 return true;
108 }
109
110 void CBackgroundValidation::startUpdating(std::chrono::milliseconds ms)
111 {
112 Q_ASSERT_X(this->hasStarted(), Q_FUNC_INFO, "Worker not started yet");
113 m_updateTimer.startTimer(ms);
114 }
115
117 {
118 m_wasStopped = true; // stop in utility functions
119 m_updateTimer.stopTimer();
120 }
121
122 void CBackgroundValidation::doWork()
123 {
124 if (m_inWork)
125 {
126 return;
127 }
128 m_inWork = true;
129 emit this->validating(true);
130
131 const bool isTimerBased = (QObject::sender() == &m_updateTimer);
132 CAircraftModelList valid;
133 CAircraftModelList invalid;
135 bool validated = false;
136 bool onlyErrorsAndWarnings = false;
137 const CSimulatorInfo simulator = this->getCurrentSimulator();
138 const qint64 started = QDateTime::currentMSecsSinceEpoch();
139 m_wasStopped = false;
140
141 do
142 {
143 if (!simulator.isSingleSimulator())
144 {
145 break;
146 }
147 if (this->wasAlreadyChecked(simulator))
148 {
149 break;
150 }
151
152 const CAircraftMatcherSetup setup = m_matchingSettings.get();
153 if (!setup.doVerificationAtStartup())
154 {
155 break;
156 }
157
158 onlyErrorsAndWarnings = setup.onlyShowVerificationWarningsAndErrors();
159 const CAircraftModelList models = m_modelSets.getCachedModels(simulator);
160 const qint64 now = QDateTime::currentMSecsSinceEpoch();
161 validated = true;
162
163 if (models.isEmpty())
164 {
165 msgs.push_back(
166 CStatusMessage(this, CStatusMessage::SeverityWarning,
167 QStringLiteral("No models in set for '%1'").arg(simulator.toQString(true))));
168 }
169 else
170 {
171 msgs = CAircraftModelUtilities::validateModelFiles(simulator, models, valid, invalid, false, 25,
172 m_wasStopped, m_simDirectory);
173 }
174
175 // cppcheck-suppress duplicateExpression
176 const qint64 deltaTimeMs = now - started;
177 msgs.push_back(CStatusMessage(this, CStatusMessage::SeverityInfo,
178 QStringLiteral("Validated in %1ms").arg(deltaTimeMs)));
180 msgs.freezeOrder();
181
182 QWriteLocker l(&m_lock);
183 m_lastResultValid = valid;
184 m_lastResultInvalid = invalid;
185 m_lastResultWasStopped = m_wasStopped;
186 m_lastResultSimulator = simulator;
187 m_lastResultMsgs = msgs;
188 m_checkedSimulatorMsgs.insert(simulator, msgs);
189 }
190 while (false);
191
192 m_inWork = false;
193 if (isTimerBased)
194 {
195 m_timerBasedRuns++;
196
197 // stop timer after some runs
198 if (m_timerBasedRuns > 3)
199 {
200 m_updateTimer.stopTimer();
201 }
202 }
203
204 emit this->validating(false);
205 if (validated)
206 {
207 const bool e = !onlyErrorsAndWarnings || (!invalid.isEmpty() || msgs.hasWarningOrErrorMessages());
208 if (e || !isTimerBased)
209 {
210 emit this->validated(simulator, valid, invalid, m_wasStopped, msgs);
211 }
212 }
213 }
214} // namespace swift::misc::simulation
const QObject * owner() const
Owner of the worker.
Definition worker.h:352
CContinuousWorker(QObject *owner, const QString &name)
Constructor.
Definition worker.cpp:168
static const QString & worker()
Background task.
static const QString & modelSetCache()
Model set cache.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
iterator insert(iterator before, const T &value)
Inserts an element into the sequence.
Definition sequence.h:301
static constexpr auto SeverityInfo
Status severities.
static constexpr auto SeverityWarning
Status severities.
Status messages, e.g. from Core -> GUI.
bool hasWarningOrErrorMessages() const
Warning or error messages.
void sortBySeverityHighestFirst()
Sort by severity, highest first.
void timeout()
Timer timed out.
bool hasStarted() const
True if the worker has started.
Definition worker.h:200
void freezeOrder()
Current order of list will be new order values.
QString toQString(bool i18n=false) const
Cast as QString.
Definition mixinstring.h:74
bool onlyShowVerificationWarningsAndErrors() const
Verification only shown for warning/errors?
bool doVerificationAtStartup() const
Verification at startup?
Value object encapsulating a list of aircraft models.
static CStatusMessageList validateModelFiles(const CSimulatorInfo &simulator, const CAircraftModelList &models, CAircraftModelList &validModels, CAircraftModelList &invalidModels, bool ignoreEmpty, int stopAtFailedFiles, std::atomic_bool &wasStopped, const QString &simulatorDir)
Validate aircraft.cfg entries.
bool wasAlreadyChecked(const CSimulatorInfo &simulator) const
Was already checked for simulator?
swift::misc::simulation::CSimulatorInfo getCurrentSimulator() const
Corresponding simulator.
void validating(bool running)
Validating.
bool requestLastValidationResults()
Request last results (again), if there are any.
static const QStringList & getLogCategories()
Log categories.
void startUpdating(std::chrono::milliseconds ms)
Start the updating timer.
void setCurrentSimulator(const CSimulatorInfo &simulator, const QString &simDirectory, const QStringList &modelDirList)
Corresponding simulator.
void beforeQuit() noexcept
Called before quit is called.
void resetAlreadyChecked(const CSimulatorInfo &simulator)
Reset checked for simulator.
bool triggerValidation(const CSimulatorInfo &simulator, const QString &simDirectory)
Trigger a validation, returns false if "work in progress".
void validated(const CSimulatorInfo &simulator, const CAircraftModelList &validModels, const CAircraftModelList &invalidModels, bool stopped, const CStatusMessageList &msgs)
Validated for simulator.
Simple hardcoded info about the corresponding simulator.
bool isSingleSimulator() const
Single simulator selected.
bool isNoSimulator() const
No simulator?
qint64 currentMSecsSinceEpoch()
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)