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 QPointer<CVatsimServerFileReader> myself(
this);
41 if (!myself) {
return; }
48 void CVatsimServerFileReader::read()
53 Q_ASSERT_X(
sApp, Q_FUNC_INFO,
"Missing application");
55 if (url.isEmpty()) {
return; }
59 void CVatsimServerFileReader::parseVatsimFile(QNetworkReply *nwReplyPtr)
63 QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
75 const QUrl url = nwReply->url();
76 const QString urlString = url.toString();
78 if (nwReply->error() == QNetworkReply::NoError)
80 const QString dataFileData = nwReply->readAll();
83 if (dataFileData.isEmpty()) {
return; }
86 CLogMessage(
this).
info(u
"VATSIM file '%1' has same content, skipped") << urlString;
89 auto jsonDoc = QJsonDocument::fromJson(dataFileData.toUtf8());
90 if (jsonDoc.isEmpty()) {
return; }
95 for (QJsonValueRef server : jsonDoc.array())
102 fsdServers.
push_back(parseServer(server.toObject()));
107 fsdServers.
sortBy(&CServer::getName, &CServer::getDescription);
111 const bool changedSetup = vs.setServers(fsdServers, {});
114 vs.setCurrentUtcTime();
115 m_lastGoodSetup.
set(vs);
120 emit this->
dataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFinished, dataFileData.size() / 1000,
127 << nwReply->errorString() << urlString;
129 emit this->
dataRead(CEntityFlags::VatsimDataFile, CEntityFlags::ReadFailed, 0, url);
133 CServer CVatsimServerFileReader::parseServer(
const QJsonObject &server)
const
135 return CServer(server[
"name"].toString(), server[
"location"].toString(), server[
"hostname_or_ip"].toString(),
136 6809,
CUser(
"id",
"real name",
"email",
"password"), CFsdSetup::vatsimStandard(),
137 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 everthing 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.
virtual void doWorkImpl()
This method does the actual work in the derived class.
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.
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...