7 #include <QNetworkReply>
9 #include <QScopedPointerDeleteLater>
10 #include <QStringBuilder>
23 using namespace swift::misc::aviation;
24 using namespace swift::misc::network;
25 using namespace swift::misc::geo;
26 using namespace swift::misc::simulation;
27 using namespace swift::misc::physical_quantities;
30 namespace swift::core::vatsim
32 CVatsimServerFileReader::CVatsimServerFileReader(QObject *owner) :
CThreadedReader(owner,
"CVatsimServerFileReader")
39 Q_ASSERT_X(
hasStarted(), Q_FUNC_INFO,
"Thread was not started yet!");
40 QPointer<CVatsimServerFileReader> myself(
this);
42 if (!myself) {
return; }
47 void CVatsimServerFileReader::read()
52 Q_ASSERT_X(
sApp, Q_FUNC_INFO,
"Missing application");
54 if (url.isEmpty()) {
return; }
58 void CVatsimServerFileReader::parseVatsimFile(QNetworkReply *nwReplyPtr)
62 QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
74 const QUrl url = nwReply->url();
75 const QString urlString = url.toString();
77 if (nwReply->error() == QNetworkReply::NoError)
79 const QString dataFileData = nwReply->readAll();
82 if (dataFileData.isEmpty()) {
return; }
85 CLogMessage(
this).
info(u
"VATSIM file '%1' has same content, skipped") << urlString;
88 auto jsonDoc = QJsonDocument::fromJson(dataFileData.toUtf8());
89 if (jsonDoc.isEmpty()) {
return; }
94 for (QJsonValueRef server : jsonDoc.array())
101 fsdServers.
push_back(parseServer(server.toObject()));
106 fsdServers.
sortBy(&CServer::getName, &CServer::getDescription);
110 const bool changedSetup = vs.setServers(fsdServers, {});
113 vs.setCurrentUtcTime();
114 m_lastGoodSetup.
set(vs);
119 emit this->
dataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFinished, dataFileData.size() / 1000,
126 << nwReply->errorString() << urlString;
128 emit this->
dataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFailed, 0, url);
132 CServer CVatsimServerFileReader::parseServer(
const QJsonObject &server)
const
134 return CServer(server[
"name"].toString(), server[
"location"].toString(), server[
"hostname_or_ip"].toString(),
135 6809,
CUser(
"id",
"real name",
"email",
"password"), CFsdSetup::vatsimStandard(),
136 CEcosystem::VATSIM, CServer::FSDServerVatsim, server[
"clients_connection_allowed"].toInt());
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
swift::misc::network::CUrl getVatsimServerFileUrl() const
Get URL to file which contains the list of VATSIM servers.
Support for threaded based reading and parsing tasks such as data files via http, or file system and ...
void threadAssertCheck() const
Make sure everything runs correctly in own thread.
void logNetworkReplyReceived(QNetworkReply *reply)
Network reply received, mark in m_urlReadLog.
bool didContentChange(const QString &content, int startPosition=-1)
Stores new content hash and returns if content changed (based on hash value.
QNetworkReply * getFromNetworkAndLog(const swift::misc::network::CUrl &url, const swift::misc::CSlot< void(QNetworkReply *)> &callback)
Get request from network, and log with m_urlReadLog.
bool doWorkCheck() const
Still enabled etc.?
VATSIM data (servers, URLs) cached as last known good setup.
void dataRead(swift::misc::network::CEntityFlags::Entity entity, swift::misc::network::CEntityFlags::ReadState state, int number, const QUrl &url)
Data have been read.
void readInBackgroundThread()
Start reading in own thread.
void dataFileRead(int bytes)
Data have been read.
swift::misc::network::CServerList getFsdServers() const
Get all VATSIM FSD servers.
T get() const
Get a copy of the current value.
CStatusMessage set(const typename Trait::type &value, qint64 timestamp=0)
Write a new value. Must be called from the thread in which the owner lives.
Class for emitting a log message.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
void sortBy(K1 key1, Keys... keys)
In-place sort by some particular key(s).
void pop_back()
Removes an element at the end of the sequence.
void push_back(const T &value)
Appends an element at the end of the sequence.
reference back()
Access the last element.
bool hasStarted() const
True if the worker has started.
Value object encapsulating information of a server.
bool hasName() const
Has name?
Value object encapsulating a list of servers.
Value object encapsulating information of a user.
Core data traits (aka cached values) and classes.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...