From a62da56d5f97bddf44ae4c788357c6689cd0ab87 Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Mon, 27 Feb 2023 17:59:50 +0100 Subject: [PATCH] Use HTTP to get VATSIM IP in automatic mode --- .../share/shared/bootstrap/bootstrap.json | 3 ++ src/blackcore/application.cpp | 7 ++++ src/blackcore/application.h | 3 ++ src/blackcore/data/globalsetup.cpp | 3 ++ src/blackcore/data/globalsetup.h | 8 ++++- src/blackcore/fsd/fsdclient.cpp | 34 ++++++++++++++++++- src/blackcore/fsd/fsdclient.h | 4 +++ 7 files changed, 60 insertions(+), 2 deletions(-) diff --git a/resources/share/shared/bootstrap/bootstrap.json b/resources/share/shared/bootstrap/bootstrap.json index 104bc8a74..951dad087 100644 --- a/resources/share/shared/bootstrap/bootstrap.json +++ b/resources/share/shared/bootstrap/bootstrap.json @@ -61,6 +61,9 @@ "vatsimServerFileUrl": { "url": "https://data.vatsim.net/v3/vatsim-servers.json" }, + "vatsimFsdHttpUrl": { + "url": "http://fsd-http.connect.vatsim.net" + }, "vatsimMetarsUrls": { "containerbase": [ { diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index d5c7fb40a..eea113b17 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -1744,6 +1744,13 @@ namespace BlackCore return m_setupReader->getSetup().getVatsimServerFileUrl(); } + CUrl CApplication::getVatsimFsdHttpUrl() const + { + if (m_shutdown || !m_setupReader) { return {}; } + + return m_setupReader->getSetup().getVatsimFsdHttpUrl(); + } + void CApplication::onCrashDumpUploadEnabledChanged() { const bool enabled = CBuildConfig::isReleaseBuild() && m_crashDumpSettings.getThreadLocal().isEnabled(); diff --git a/src/blackcore/application.h b/src/blackcore/application.h index c7cce70a6..e0549d39d 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -442,6 +442,9 @@ namespace BlackCore //! Get URL to file which contains the list of VATSIM servers BlackMisc::Network::CUrl getVatsimServerFileUrl() const; + //! Get VATSIM FSD HTTP URL + BlackMisc::Network::CUrl getVatsimFsdHttpUrl() const; + //! Start services, if not yet parsed call CApplication::parse virtual bool start(); diff --git a/src/blackcore/data/globalsetup.cpp b/src/blackcore/data/globalsetup.cpp index ed55e19a1..a33611b72 100644 --- a/src/blackcore/data/globalsetup.cpp +++ b/src/blackcore/data/globalsetup.cpp @@ -53,6 +53,7 @@ namespace BlackCore::Data m_vatsimStatusFileUrls = CUrlList{ "https://status.vatsim.net" }; m_vatsimDataFileUrls = CUrlList{ "https://data.vatsim.net/v3/vatsim-data.json" }; m_vatsimServerFileUrl = CUrl("https://data.vatsim.net/v3/vatsim-servers.json"); + m_vatsimFsdHttpUrl = CUrl("http://fsd-http.connect.vatsim.net"); m_sharedUrls = CUrlList { "http://download.swift-project.org/shared/", @@ -373,6 +374,7 @@ namespace BlackCore::Data case IndexVatsimData: return QVariant::fromValue(m_vatsimDataFileUrls); case IndexVatsimBookings: return QVariant::fromValue(m_vatsimBookingsUrl); case IndexVatsimServer: return QVariant::fromValue(m_vatsimServerFileUrl); + case IndexVatsimHttpFsd: return QVariant::fromValue(m_vatsimFsdHttpUrl); case IndexVatsimMetars: return QVariant::fromValue(m_vatsimMetarsUrls); case IndexBootstrapFileUrls: return QVariant::fromValue(this->getSwiftBootstrapFileUrls()); case IndexUpdateInfoFileUrls: return QVariant::fromValue(this->getSwiftUpdateInfoFileUrls()); @@ -407,6 +409,7 @@ namespace BlackCore::Data case IndexVatsimData: m_vatsimDataFileUrls = variant.value(); break; case IndexVatsimBookings: m_vatsimBookingsUrl.setPropertyByIndex(index.copyFrontRemoved(), variant); break; case IndexVatsimServer: m_vatsimServerFileUrl = variant.value(); break; + case IndexVatsimHttpFsd: m_vatsimFsdHttpUrl = variant.value(); break; case IndexVatsimMetars: m_vatsimMetarsUrls = variant.value(); break; case IndexSharedUrls: m_sharedUrls = variant.value(); break; case IndexOnlineHelpUrls: m_onlineHelpUrls = variant.value(); break; diff --git a/src/blackcore/data/globalsetup.h b/src/blackcore/data/globalsetup.h index 99a04e956..cb1e7a7e2 100644 --- a/src/blackcore/data/globalsetup.h +++ b/src/blackcore/data/globalsetup.h @@ -51,6 +51,7 @@ namespace BlackCore::Data IndexVatsimMetars, IndexVatsimData, IndexVatsimServer, + IndexVatsimHttpFsd, IndexSwiftDbFiles, IndexBootstrapFileUrls, IndexUpdateInfoFileUrls, @@ -185,6 +186,9 @@ namespace BlackCore::Data //! VATSIM server file URL BlackMisc::Network::CUrl getVatsimServerFileUrl() const { return m_vatsimServerFileUrl; } + //! VATSIM server file URL + BlackMisc::Network::CUrl getVatsimFsdHttpUrl() const { return m_vatsimFsdHttpUrl; } + //! Help page URL //! \remark working URL evaluated at runtime, based on getOnlineHelpUrls BlackMisc::Network::CUrl getHelpPageUrl(const QString &context = {}) const; @@ -253,7 +257,8 @@ namespace BlackCore::Data BlackMisc::Network::CUrlList m_vatsimMetarsUrls; //!< METAR data BlackMisc::Network::CUrlList m_vatsimStatusFileUrls; //!< Status file, where to find the VATSIM files (METAR, data, ATIS, other status files) BlackMisc::Network::CUrlList m_vatsimDataFileUrls; //!< Overall VATSIM data file / merely for bootstrapping the first time - BlackMisc::Network::CUrl m_vatsimServerFileUrl; //!< UR to list of VATSIM servers + BlackMisc::Network::CUrl m_vatsimServerFileUrl; //!< URL to list of VATSIM servers + BlackMisc::Network::CUrl m_vatsimFsdHttpUrl; //!< URL to HTTP FSD server (for load-balancing and automatic server selection) BlackMisc::Network::CUrlList m_sharedUrls; //!< where we can obtain shared info files such as bootstrap, .. BlackMisc::Network::CUrlList m_onlineHelpUrls; //!< online help URLs BlackMisc::Network::CServerList m_predefinedServers; //!< Predefined servers loaded from setup file @@ -279,6 +284,7 @@ namespace BlackCore::Data BLACK_METAMEMBER(vatsimStatusFileUrls), BLACK_METAMEMBER(vatsimDataFileUrls), BLACK_METAMEMBER(vatsimServerFileUrl), + BLACK_METAMEMBER(vatsimFsdHttpUrl), BLACK_METAMEMBER(vatsimBookingsUrl), BLACK_METAMEMBER(vatsimMetarsUrls), BLACK_METAMEMBER(sharedUrls), diff --git a/src/blackcore/fsd/fsdclient.cpp b/src/blackcore/fsd/fsdclient.cpp index e51602bc5..47d49b190 100644 --- a/src/blackcore/fsd/fsdclient.cpp +++ b/src/blackcore/fsd/fsdclient.cpp @@ -270,7 +270,39 @@ namespace BlackCore::Fsd this->updateConnectionStatus(CConnectionStatus::Connecting); const CServer s = this->getServer(); - const QString host = s.getAddress(); + + QHostAddress serverAddress(s.getAddress()); + + if (serverAddress.isNull() && s.getName() == "AUTOMATIC" && s.getEcosystem() == CEcosystem::VATSIM) + { + // Not an IP -> Get IP for loadbalancing via HTTP + Q_ASSERT_X(sApp, Q_FUNC_INFO, "Need app"); + CUrl url = sApp->getVatsimFsdHttpUrl(); + sApp->getFromNetwork(url, { this, &CFSDClient::handleVatsimServerIpResponse }); + } + else + { + const QString host = s.getAddress(); + const quint16 port = static_cast(s.getPort()); + m_socket->connectToHost(host, port); + this->startPositionTimers(); + } + } + + void CFSDClient::handleVatsimServerIpResponse(QNetworkReply *nwReplyPtr) + { + QScopedPointer nwReply(nwReplyPtr); + const CServer s = this->getServer(); + + QString host = s.getAddress(); + + if (nwReply->error() == QNetworkReply::NoError) + { + QHostAddress addr(static_cast(nwReply->readAll())); + if (!addr.isNull()) { host = addr.toString(); } + + } + const quint16 port = static_cast(s.getPort()); m_socket->connectToHost(host, port); this->startPositionTimers(); diff --git a/src/blackcore/fsd/fsdclient.h b/src/blackcore/fsd/fsdclient.h index b978fe168..e32c0828e 100644 --- a/src/blackcore/fsd/fsdclient.h +++ b/src/blackcore/fsd/fsdclient.h @@ -58,6 +58,8 @@ #define PROTOCOL_REVISION_VATSIM_VELOCITY 101 //! @} +class QNetworkReply; + namespace BlackFsdTest { class CTestFSDClient; } namespace BlackCore::Fsd { @@ -387,6 +389,8 @@ namespace BlackCore::Fsd void handleUnknownPacket(const QStringList &tokens); //! @} + void handleVatsimServerIpResponse(QNetworkReply *nwReplyPtr); + void printSocketError(QAbstractSocket::SocketError socketError); void handleSocketError(QAbstractSocket::SocketError socketError); void handleSocketConnected();