Ref T172, added ecosystem in CServer class

This commit is contained in:
Klaus Basan
2017-12-28 18:17:33 +01:00
parent 8724e3dc3a
commit c1df30fc55
2 changed files with 164 additions and 100 deletions

View File

@@ -15,6 +15,7 @@
#include "blackmisc/statusmessage.h"
#include "blackmisc/comparefunctions.h"
#include "blackmisc/variant.h"
#include "blackmisc/verify.h"
#include <Qt>
#include <QtGlobal>
@@ -25,19 +26,52 @@ namespace BlackMisc
{
const QList<int> &CServer::allServerTypes()
{
static const QList<int> all({ FSDServerVatsim, FSDServerFSC, FSDServerLegacy, WebService, Unspecified });
static const QList<int> all({ FSDServerVatsim, VoiceServerVatsim, FSDServer, VoiceServer, WebService, Unspecified });
return all;
}
CServer::CServer(const QString &name, const QString &description, const QString &address, int port, const CUser &user, ServerType serverType, bool isAcceptingConnections)
: m_name(name), m_description(description), m_address(address), m_port(port), m_user(user), m_serverType(serverType), m_isAcceptingConnections(isAcceptingConnections) {}
CServer::CServer(
const QString &name, const QString &description, const QString &address, int port, const CUser &user,
const CFsdSetup &setup, const CEcosystem &ecosytem, ServerType serverType, bool isAcceptingConnections)
: m_name(name), m_description(description), m_address(address), m_port(port), m_user(user),
m_fsdSetup(setup), m_ecosystem(ecosytem),
m_serverType(serverType), m_isAcceptingConnections(isAcceptingConnections)
{}
CServer::CServer(
const QString &address, int port, const CUser &user)
: m_name("no name"), m_description("min.configuration"), m_address(address), m_port(port), m_user(user)
{}
CServer::CServer(const CEcosystem &ecosystem)
{
this->setEcosystem(ecosystem);
}
CServer::CServer(CServer::ServerType serverType)
{
this->setServerType(serverType);
}
QString CServer::convertToQString(bool i18n) const
{
static const QString str("%1 %2 %3:%4 %5 accepting: %6 FSD: %7 con.since: %8");
static const QString str("%1 %2 %3:%4 %5 %6 accepting: %7 FSD: %8 con.since: %9");
return str.
arg(this->m_name, this->m_description, this->m_address).arg(this->m_port).
arg(this->m_user.toQString(i18n), boolToYesNo(this->m_isAcceptingConnections), this->m_fsdSetup.toQString(i18n), this->isConnected() ? this->getFormattedUtcTimestampHms() : "not con.");
arg(m_name, m_description, m_address).arg(m_port).
arg(m_user.toQString(i18n), m_ecosystem.getSystemString(),
boolToYesNo(m_isAcceptingConnections), m_fsdSetup.toQString(i18n), this->isConnected() ? this->getFormattedUtcTimestampHms() : "not con.");
}
const CServer &CServer::swiftFsdTestServer(bool withPw)
{
// CUser("guest", "Guest Client project", "", "guest")
static const CServer dvp("Testserver", "Client project testserver", "fsd.swift-project.org", 6809,
CUser("1234567", "Test User", "", "123456"),
CFsdSetup(), CEcosystem(CEcosystem::swiftTest()), CServer::FSDServerVatsim);
static const CServer dvnp("Testserver", "Client project testserver", "fsd.swift-project.org", 6809,
CUser("1234567", "Test User", "", ""),
CFsdSetup(), CEcosystem(CEcosystem::swiftTest()), CServer::FSDServerVatsim);
return withPw ? dvp : dvnp;
}
bool CServer::matchesName(const QString &name) const
@@ -58,9 +92,21 @@ namespace BlackMisc
m_address.startsWith(address, Qt::CaseInsensitive);
}
bool CServer::setEcosystem(const CEcosystem &ecosystem)
{
if (m_ecosystem == ecosystem) { return false; } // avoid cross dependency
m_ecosystem = ecosystem;
// cross dependency
if (ecosystem.isSystem(CEcosystem::VATSIM)) { m_serverType = FSDServerVatsim; }
if (ecosystem.isSystem(CEcosystem::PrivateFSD)) { m_serverType = FSDServer; }
if (ecosystem.isSystem(CEcosystem::SwiftTest)) { m_serverType = FSDServerVatsim; }
return true;
}
bool CServer::isValidForLogin() const
{
return this->m_user.hasValidCredentials() && this->hasAddressAndPort() && this->isAcceptingConnections();
return m_user.hasValidCredentials() && this->hasAddressAndPort() && this->isAcceptingConnections();
}
bool CServer::hasAddressAndPort() const
@@ -71,18 +117,39 @@ namespace BlackMisc
bool CServer::isFsdServer() const
{
return (this->getServerType() == FSDServerVatsim ||
this->getServerType() == FSDServerLegacy ||
this->getServerType() == FSDServerFSC);
this->getServerType() == FSDServer);
}
bool CServer::setServerType(CServer::ServerType serverType)
{
if (m_serverType == serverType) { return false; } // avoid x-dependency
m_serverType = static_cast<int>(serverType);
switch (m_serverType)
{
case FSDServerVatsim : m_ecosystem = CEcosystem(CEcosystem::VATSIM); break;
case FSDServer: m_ecosystem = CEcosystem(CEcosystem::PrivateFSD); break;
case VoiceServerVatsim: m_ecosystem = CEcosystem(CEcosystem::VATSIM); break;
case VoiceServer: m_ecosystem = CEcosystem(CEcosystem::PrivateFSD); break;
case WebService: m_ecosystem = CEcosystem(CEcosystem::NoSystem); break;
case Unspecified: m_ecosystem = CEcosystem(CEcosystem::Unspecified); break;
default: break;
}
return true;
}
bool CServer::hasUnspecifiedServerType() const
{
return this->getServerType() == Unspecified;
}
const QString &CServer::getServerTypeAsString() const
{
return serverTypeToString(getServerType());
return CServer::serverTypeToString(getServerType());
}
bool CServer::isConnected() const
{
return this->m_timestampMSecsSinceEpoch >= 0;
return m_timestampMSecsSinceEpoch >= 0;
}
CStatusMessageList CServer::validate() const
@@ -107,7 +174,7 @@ namespace BlackMisc
return session.arg(this->getName(), this->getAddress()).arg(this->getPort()).arg(this->getUser().getRealName(), this->getFormattedUtcTimestampHms());
}
CVariant CServer::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
CVariant CServer::propertyByIndex(const CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); }
@@ -115,26 +182,17 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
return CVariant::fromValue(this->m_address);
case IndexDescription:
return CVariant::fromValue(this->m_description);
case IndexName:
return CVariant::fromValue(this->m_name);
case IndexPort:
return CVariant::fromValue(this->m_port);
case IndexUser:
return this->m_user.propertyByIndex(index.copyFrontRemoved());
case IndexFsdSetup:
return this->m_fsdSetup.propertyByIndex(index.copyFrontRemoved());
case IndexIsAcceptingConnections:
return CVariant::fromValue(this->m_isAcceptingConnections);
case IndexServerType:
return CVariant::fromValue(this->m_serverType);
case IndexServerTypeAsString:
return CVariant::fromValue(getServerTypeAsString());
default:
return CValueObject::propertyByIndex(index);
case IndexAddress: return CVariant::fromValue(m_address);
case IndexDescription: return CVariant::fromValue(m_description);
case IndexName: return CVariant::fromValue(m_name);
case IndexPort: return CVariant::fromValue(m_port);
case IndexUser: return m_user.propertyByIndex(index.copyFrontRemoved());
case IndexFsdSetup: return m_fsdSetup.propertyByIndex(index.copyFrontRemoved());
case IndexEcosystem: return m_ecosystem.propertyByIndex(index.copyFrontRemoved());
case IndexIsAcceptingConnections: return CVariant::fromValue(m_isAcceptingConnections);
case IndexServerType: return CVariant::fromValue(m_serverType);
case IndexServerTypeAsString: return CVariant::fromValue(getServerTypeAsString());
default: return CValueObject::propertyByIndex(index);
}
}
@@ -146,33 +204,16 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
this->setAddress(variant.value<QString>());
break;
case IndexPort:
this->setPort(variant.value<qint32>());
break;
case IndexDescription:
this->setDescription(variant.value<QString>());
break;
case IndexName:
this->setName(variant.value<QString>());
break;
case IndexUser:
this->m_user.setPropertyByIndex(index.copyFrontRemoved(), variant);
break;
case IndexFsdSetup:
this->m_fsdSetup.setPropertyByIndex(index.copyFrontRemoved(), variant);
break;
case IndexIsAcceptingConnections:
this->setIsAcceptingConnections(variant.value<bool>());
break;
case IndexServerType:
this->setServerType(static_cast<ServerType>(variant.toInt()));
break;
default:
CValueObject::setPropertyByIndex(index, variant);
break;
case IndexAddress: this->setAddress(variant.value<QString>()); break;
case IndexPort: this->setPort(variant.value<qint32>()); break;
case IndexDescription: this->setDescription(variant.value<QString>()); break;
case IndexName: this->setName(variant.value<QString>()); break;
case IndexUser: m_user.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
case IndexFsdSetup: m_fsdSetup.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
case IndexEcosystem: m_ecosystem.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
case IndexIsAcceptingConnections: this->setIsAcceptingConnections(variant.value<bool>()); break;
case IndexServerType: this->setServerType(static_cast<ServerType>(variant.toInt())); break;
default: CValueObject::setPropertyByIndex(index, variant); break;
}
}
@@ -183,45 +224,40 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
return this->getAddress().compare(compareValue.getAddress(), Qt::CaseInsensitive);
case IndexDescription:
return this->getDescription().compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexFsdSetup:
return this->getFsdSetup().toQString().compare(compareValue.getFsdSetup().toQString());
case IndexName:
return this->getName().compare(compareValue.getName(), Qt::CaseInsensitive);
case IndexIsAcceptingConnections:
return Compare::compare(this->isAcceptingConnections(), compareValue.isAcceptingConnections());
case IndexPort:
return Compare::compare(this->getPort(), compareValue.getPort());
case IndexUser:
return this->getUser().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getUser());
case IndexAddress: return this->getAddress().compare(compareValue.getAddress(), Qt::CaseInsensitive);
case IndexDescription: return this->getDescription().compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexFsdSetup: return this->getFsdSetup().toQString().compare(compareValue.getFsdSetup().toQString());
case IndexName: return this->getName().compare(compareValue.getName(), Qt::CaseInsensitive);
case IndexIsAcceptingConnections: return Compare::compare(this->isAcceptingConnections(), compareValue.isAcceptingConnections());
case IndexPort: return Compare::compare(this->getPort(), compareValue.getPort());
case IndexUser: return this->getUser().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getUser());
case IndexEcosystem: return this->getEcosystem().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getEcosystem());
case IndexServerType:
case IndexServerTypeAsString:
return this->getServerTypeAsString().compare(compareValue.getServerTypeAsString(), Qt::CaseInsensitive);
default:
break;
default: break;
}
Q_ASSERT_X(false, Q_FUNC_INFO, "No compare function");
BLACK_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
return 0;
}
const QString &CServer::serverTypeToString(CServer::ServerType server)
{
static const QString fsdVatsim("FSD [VATSIM]");
static const QString fsdFsc("FSD [FSC]");
static const QString voiceVatsim("voice [VATSIM]");
static const QString fsdLegacy("FSD (legacy)");
static const QString voice("voice");
static const QString webService("web service");
static const QString unspecified("unspecified");
switch (server)
{
case FSDServerVatsim: return fsdVatsim;
case FSDServerFSC: return fsdFsc;
case FSDServerLegacy: return fsdLegacy;
case VoiceServerVatsim: return voiceVatsim;
case FSDServer: return fsdLegacy;
case VoiceServer: return voice;
case WebService: return webService;
case Unspecified: return unspecified;
case Unspecified:
default: return unspecified;
}
}

View File

@@ -16,6 +16,7 @@
#include "blackmisc/metaclass.h"
#include "blackmisc/network/user.h"
#include "blackmisc/network/fsdsetup.h"
#include "blackmisc/network/ecosystem.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/timestampbased.h"
@@ -32,18 +33,19 @@ namespace BlackMisc
//! Value object encapsulating information of a server
class BLACKMISC_EXPORT CServer :
public CValueObject<CServer>,
public BlackMisc::ITimestampBased
public ITimestampBased
{
public:
//! Properties by index
enum ColumnIndex
{
IndexName = BlackMisc::CPropertyIndex::GlobalIndexCServer,
IndexName = CPropertyIndex::GlobalIndexCServer,
IndexDescription,
IndexAddress,
IndexPort,
IndexUser,
IndexFsdSetup,
IndexEcosystem,
IndexIsAcceptingConnections,
IndexServerType,
IndexServerTypeAsString
@@ -52,11 +54,12 @@ namespace BlackMisc
//! Server Type
enum ServerType
{
Unspecified,
FSDServerVatsim,
FSDServerFSC,
FSDServerLegacy,
FSDServer,
VoiceServerVatsim,
VoiceServer,
WebService,
Unspecified
};
//! Allows to iterate over all ServerType
@@ -70,7 +73,18 @@ namespace BlackMisc
//! Constructor.
CServer(const QString &name, const QString &description, const QString &address, int port,
const CUser &user, ServerType serverType = FSDServerVatsim, bool isAcceptingConnections = true);
const CUser &user,
const CFsdSetup &setup, const CEcosystem &ecosytem, ServerType serverType,
bool isAcceptingConnections = true);
//! Constructor (minimal for testing)
CServer(const QString &address, int port, const CUser &user);
//! Constructor by ecosystem
CServer(const CEcosystem &ecosystem);
//! Constructor by server type
CServer(ServerType serverType);
//! Get address.
const QString &getAddress() const { return m_address; }
@@ -111,6 +125,12 @@ namespace BlackMisc
//! Set port
void setPort(int port) { m_port = port; }
//! Get the ecosystem
const CEcosystem &getEcosystem() const { return m_ecosystem; }
//! Set the ecosystem
bool setEcosystem(const CEcosystem &ecosystem);
//! Server is accepting connections (allows to disable server temporarily or generally)
bool isAcceptingConnections() const { return m_isAcceptingConnections; }
@@ -124,20 +144,23 @@ namespace BlackMisc
bool hasAddressAndPort() const;
//! Get setup
const CFsdSetup &getFsdSetup() const { return this->m_fsdSetup; }
const CFsdSetup &getFsdSetup() const { return m_fsdSetup; }
//! A FSD server?
bool isFsdServer() const;
//! Set setup
void setFsdSetup(const CFsdSetup &setup) { this->m_fsdSetup = setup; }
void setFsdSetup(const CFsdSetup &setup) { m_fsdSetup = setup; }
//! Set server type
void setServerType(ServerType serverType) { m_serverType = static_cast<int>(serverType); }
bool setServerType(ServerType serverType);
//! Get server type
ServerType getServerType() const { return static_cast<ServerType>(m_serverType); }
//! Unspecified?
bool hasUnspecifiedServerType() const;
//! Get server type as string
const QString &getServerTypeAsString() const;
@@ -154,16 +177,16 @@ namespace BlackMisc
bool isConnected() const;
//! Validate, provide details about issues
BlackMisc::CStatusMessageList validate() const;
CStatusMessageList validate() const;
//! Identifying a session, if not connected empty
QString getServerSessionId() const;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
CVariant propertyByIndex(const CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant);
//! Compare by index
int comparePropertyByIndex(const CPropertyIndex &index, const CServer &compareValue) const;
@@ -171,15 +194,19 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::String::toQString()
QString convertToQString(bool i18n = false) const;
//! swift FSD test server
static const CServer &swiftFsdTestServer(bool withPw = false);
private:
QString m_name;
QString m_description;
QString m_address;
int m_port = -1;
CUser m_user;
CFsdSetup m_fsdSetup;
int m_serverType = FSDServerVatsim;
bool m_isAcceptingConnections = true; //!< disable server for connections
QString m_name;
QString m_description;
QString m_address;
int m_port = -1;
CUser m_user;
CFsdSetup m_fsdSetup;
CEcosystem m_ecosystem;
int m_serverType = static_cast<int>(Unspecified);
bool m_isAcceptingConnections = true; //!< disable server for connections
BLACK_METACLASS(
CServer,
@@ -189,6 +216,7 @@ namespace BlackMisc
BLACK_METAMEMBER(port),
BLACK_METAMEMBER(user),
BLACK_METAMEMBER(fsdSetup),
BLACK_METAMEMBER(ecosystem),
BLACK_METAMEMBER(serverType),
BLACK_METAMEMBER(isAcceptingConnections),
BLACK_METAMEMBER(timestampMSecsSinceEpoch, 0, DisabledForJson | DisabledForComparison)