diff --git a/src/blackcore/network.h b/src/blackcore/network.h index 36364df0a..34011c477 100644 --- a/src/blackcore/network.h +++ b/src/blackcore/network.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include namespace BlackCore { @@ -120,6 +122,18 @@ namespace BlackCore */ virtual bool isConnected() const = 0; + /*! + * Returns a list of URLs where network status data can be found. + * To obtain the status, one of these URLs should be picked at random. + */ + virtual QList getStatusUrls() const = 0; + + /*! + * Returns a list of known servers which may be connected to. + * Not all servers may be accepting connections; consult the CServer::isAcceptingConnections method. + */ + virtual BlackMisc::Network::CServerList getKnownServers() const = 0; + public slots: //////////////////////////////////////////////////////////////// //! \name Network slots diff --git a/src/blackcore/network_vatlib.cpp b/src/blackcore/network_vatlib.cpp index 3634a011b..2121a7069 100644 --- a/src/blackcore/network_vatlib.cpp +++ b/src/blackcore/network_vatlib.cpp @@ -317,6 +317,61 @@ namespace BlackCore } } + QList CNetworkVatlib::getStatusUrls() const + { + QList result; + try + { + Cvatlib_Network *net = m_net.data(); + decltype(m_net) netPtr; + if (!net) + { + netPtr.reset(Cvatlib_Network::Create()); + net = netPtr.data(); + } + + auto urlsPtr = QSharedPointer(net->GetVatsimStatusUrls(), [=](const char *const *p){ net->GetVatsimStatusUrls_Free(p); }); + auto urls = urlsPtr.data(); + while (*urls) + { + result.push_back(QUrl(*urls++)); + } + } + catch (...) { exceptionDispatcher(Q_FUNC_INFO); } + return result; + } + + BlackMisc::Network::CServerList CNetworkVatlib::getKnownServers() const + { + BlackMisc::Network::CServerList result; + try + { + Cvatlib_Network *net = m_net.data(); + decltype(m_net) netPtr; + if (!net) + { + netPtr.reset(Cvatlib_Network::Create()); + net = netPtr.data(); + } + + auto namesPtr = QSharedPointer(net->GetVatsimFSDServerNames(), [=](const char *const *p){ net->GetVatsimFSDServerNames_Free(p); }); + auto ipsPtr = QSharedPointer(net->GetVatsimFSDServerIps(), [=](const char *const *p){ net->GetVatsimFSDServerIps_Free(p); }); + auto locationsPtr = QSharedPointer(net->GetVatsimFSDServerLocations(), [=](const char *const *p){ net->GetVatsimFSDServerLocations_Free(p); }); + auto acceptsPtr = QSharedPointer(net->GetVatsimFSDServerAcceptingConnections(), [=](const bool *p){ net->GetVatsimFSDServerAcceptingConnections_Free(p); }); + auto names = namesPtr.data(); + auto ips = ipsPtr.data(); + auto locations = locationsPtr.data(); + auto accepts = acceptsPtr.data(); + int port = 6809; // TODO hard-coded number? + while (*names) + { + result.push_back(BlackMisc::Network::CServer(*names++, *locations++, *ips++, port, BlackMisc::Network::CUser(), *accepts++)); + } + } + catch (...) { exceptionDispatcher(Q_FUNC_INFO); } + return result; + } + /********************************** * * * * * * * * * * * * * * * * * * * ************************************/ /********************************** INetwork slots ************************************/ /********************************** * * * * * * * * * * * * * * * * * * * ************************************/ diff --git a/src/blackcore/network_vatlib.h b/src/blackcore/network_vatlib.h index 1fd45a365..3a81d65c2 100644 --- a/src/blackcore/network_vatlib.h +++ b/src/blackcore/network_vatlib.h @@ -38,6 +38,12 @@ namespace BlackCore //! \copydoc INetwork::isConnected() virtual bool isConnected() const override { return m_status == Cvatlib_Network::connStatus_Connected; } + //! \copydoc INetwork::getStatusUrls() + virtual QList getStatusUrls() const override; + + //! \copydoc INetwork::getKnownServers() + virtual BlackMisc::Network::CServerList getKnownServers() const override; + // Network slots virtual void presetServer(const BlackMisc::Network::CServer &server) override; virtual void presetCallsign(const BlackMisc::Aviation::CCallsign &callsign) override; diff --git a/src/blackmisc/nwserver.cpp b/src/blackmisc/nwserver.cpp index 96f8b593b..8b6f736b2 100644 --- a/src/blackmisc/nwserver.cpp +++ b/src/blackmisc/nwserver.cpp @@ -16,6 +16,7 @@ namespace BlackMisc s.append(" ").append(this->m_address); s.append(" ").append(QString::number(this->m_port)); s.append(" ").append(this->m_user.toQString(i18n)); + s.append(" ").append(this->m_isAcceptingConnections ? "true" : "false"); return s; } @@ -68,7 +69,7 @@ namespace BlackMisc */ bool CServer::isValidForLogin() const { - return this->m_user.hasValidCredentials() && this->m_port > 0 && !this->m_address.isEmpty(); + return this->m_user.hasValidCredentials() && this->m_port > 0 && !this->m_address.isEmpty() && this->isAcceptingConnections(); } /* @@ -115,6 +116,8 @@ namespace BlackMisc return QVariant::fromValue(this->m_user.getId()); case IndexUserRealName: return QVariant::fromValue(this->m_user.getRealName()); + case IndexIsAcceptingConnections: + return QVariant::fromValue(this->m_isAcceptingConnections); default: break; } @@ -161,6 +164,9 @@ namespace BlackMisc case IndexUserRealName: this->m_user.setRealName(variant.value()); break; + case IndexIsAcceptingConnections: + this->setIsAcceptingConnections(variant.value()); + break; default: Q_ASSERT_X(false, "CServer", "index unknown"); break; diff --git a/src/blackmisc/nwserver.h b/src/blackmisc/nwserver.h index 9c019d39b..4567fb6ad 100644 --- a/src/blackmisc/nwserver.h +++ b/src/blackmisc/nwserver.h @@ -23,11 +23,11 @@ namespace BlackMisc { public: //! \brief Default constructor. - CServer() : m_port(-1) {} + CServer() : m_port(-1), m_isAcceptingConnections(true) {} //! \brief Constructor. - CServer(const QString &name, const QString &description, const QString &address, qint32 port, const CUser &user) - : m_name(name), m_description(description), m_address(address), m_port(port), m_user(user) {} + CServer(const QString &name, const QString &description, const QString &address, qint32 port, const CUser &user, bool isAcceptingConnections = true) + : m_name(name), m_description(description), m_address(address), m_port(port), m_user(user), m_isAcceptingConnections(isAcceptingConnections) {} //! \copydoc CValueObject::toQVariant virtual QVariant toQVariant() const override @@ -65,6 +65,12 @@ namespace BlackMisc //! \brief Set port void setPort(qint32 port) { m_port = port; } + //! \brief Server is accepting connections + bool isAcceptingConnections() const { return m_isAcceptingConnections; } + + //! \brief Set whether server is accepting connections + void setIsAcceptingConnections(bool value) { m_isAcceptingConnections = value; } + //! \brief Is valid for login? bool isValidForLogin() const; @@ -98,7 +104,8 @@ namespace BlackMisc IndexPort, IndexUserId, IndexUserRealName, - IndexUserPassword + IndexUserPassword, + IndexIsAcceptingConnections }; //! \copydoc CValueObject::propertyByIndex(int) @@ -136,11 +143,12 @@ namespace BlackMisc QString m_address; qint32 m_port; CUser m_user; + bool m_isAcceptingConnections; }; } // namespace } // namespace -BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CServer, (o.m_name, o.m_description, o.m_address, o.m_port, o.m_user)) +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CServer, (o.m_name, o.m_description, o.m_address, o.m_port, o.m_user, o.m_isAcceptingConnections)) Q_DECLARE_METATYPE(BlackMisc::Network::CServer) #endif // guard