swift
server.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/network/server.h"
5 
6 #include <Qt>
7 #include <QtGlobal>
8 
10 #include "misc/logcategories.h"
11 #include "misc/obfuscation.h"
12 #include "misc/propertyindexref.h"
13 #include "misc/statusmessage.h"
14 #include "misc/stringutils.h"
15 #include "misc/verify.h"
16 
17 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CServer)
18 
19 namespace swift::misc::network
20 {
21  const QList<int> &CServer::allServerTypes()
22  {
23  static const QList<int> all(
24  { FSDServerVatsim, VoiceServerVatsim, FSDServer, VoiceServer, WebService, Unspecified });
25  return all;
26  }
27 
28  CServer::CServer(const QString &name, const QString &description, const QString &address, int port,
29  const CUser &user, const CFsdSetup &fsdSetup, const CEcosystem &ecosytem, ServerType serverType,
30  bool isAcceptingConnections)
31  : m_name(CObfuscation::decode(name)), m_description(CObfuscation::decode(description)),
32  m_address(CObfuscation::decode(address)), m_port(port), m_user(user), m_ecosystem(ecosytem),
33  m_serverType(serverType), m_isAcceptingConnections(isAcceptingConnections), m_fsdSetup(fsdSetup)
34  {}
35 
36  CServer::CServer(const QString &address, int port, const CUser &user)
37  : m_name("no name"), m_description("min.configuration"), m_address(address), m_port(port), m_user(user)
38  {}
39 
40  CServer::CServer(const CEcosystem &ecosystem) { this->setEcosystem(ecosystem); }
41 
42  CServer::CServer(CServer::ServerType serverType) { this->setServerType(serverType); }
43 
44  QString CServer::convertToQString(bool i18n) const
45  {
46  return QStringLiteral("%1 %2 %3:%4 %5 %6 accepting: %7 FSD: %8 con.since: %9")
47  .arg(m_name, m_description, m_address)
48  .arg(m_port)
49  .arg(m_user.toQString(i18n), m_ecosystem.getSystemString(), boolToYesNo(m_isAcceptingConnections),
50  m_fsdSetup.toQString(i18n), this->isConnected() ? this->getFormattedUtcTimestampHms() : "not con.");
51  }
52 
54  {
55  static const CServer fsc = [] {
56  CServer s = CServer("FSC", "FSC e.V.", "OBF:AwJIKfgkQDJEIRnno29DJlB+UK0=", 6809, CUser(), CFsdSetup(),
57  CEcosystem(CEcosystem::privateFsd()), CServer::FSDServer);
59  return s;
60  }();
61  return fsc;
62  }
63 
65  {
66  static const CServer s =
67  CServer("ES Tower", "Euroscope Tower view", "localhost", 6809, CUser(), CFsdSetup::vatsimStandard(),
68  CEcosystem(CEcosystem::vatsim()), CServer::VoiceServerVatsim);
69  return s;
70  }
71 
72  bool CServer::matchesName(const QString &name) const
73  {
74  return m_name.length() == name.length() && m_name.startsWith(name, Qt::CaseInsensitive);
75  }
76 
77  bool CServer::matchesAddressPort(const CServer &server) const
78  {
79  return server.getPort() == this->getPort() && server.matchesAddress(this->getAddress());
80  }
81 
82  bool CServer::matchesAddress(const QString &address) const
83  {
84  return m_address.length() == address.length() && m_address.startsWith(address, Qt::CaseInsensitive);
85  }
86 
87  bool CServer::setEcosystem(const CEcosystem &ecosystem)
88  {
89  if (m_ecosystem == ecosystem) { return false; } // avoid cross dependency
90  m_ecosystem = ecosystem;
91 
92  // cross dependency
93  if (ecosystem.isSystem(CEcosystem::VATSIM)) { m_serverType = FSDServerVatsim; }
94  if (ecosystem.isSystem(CEcosystem::SwiftTest)) { m_serverType = FSDServerVatsim; }
95  return true;
96  }
97 
99  {
100  return m_user.hasCredentials() && this->hasAddressAndPort() && this->isAcceptingConnections();
101  }
102 
103  bool CServer::hasAddressAndPort() const { return m_port > 0 && !m_address.isEmpty(); }
104 
105  bool CServer::isFsdServer() const
106  {
107  return (this->getServerType() == FSDServerVatsim || this->getServerType() == FSDServer);
108  }
109 
111  {
112  if (m_serverType == serverType) { return false; } // avoid x-dependency
113  // disabled x-dependency
114  m_serverType = static_cast<int>(serverType);
115  return true;
116  }
117 
118  bool CServer::hasUnspecifiedServerType() const { return this->getServerType() == Unspecified; }
119 
121 
122  bool CServer::isConnected() const { return m_timestampMSecsSinceEpoch >= 0; }
123 
124  bool CServer::isNull() const { return this->hasUnspecifiedServerType() && !this->hasName() && m_port < 0; }
125 
127  {
128  static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
129  CStatusMessageList msgs;
130  if (this->getName().isEmpty())
131  {
133  }
134  if (this->getAddress().isEmpty())
135  {
136  msgs.push_back(CStatusMessage(CStatusMessage::SeverityError, u"Missing address"));
137  }
138  if (this->getDescription().isEmpty())
139  {
140  msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Missing description"));
141  }
142  if (this->getPort() < 1 || this->getPort() > 65535)
143  {
145  }
146  msgs.push_back(this->getUser().validate());
147  msgs.push_back(this->getFsdSetup().validate());
148  msgs.addCategories(cats);
149  msgs.sortBySeverity();
150  return msgs;
151  }
152 
153  QString CServer::getServerSessionId(bool onlyConnected) const
154  {
155  const bool isConnected = this->isConnected();
156  if (onlyConnected && !isConnected) { return {}; }
157  static const QString session("%1 %2 %3:%4 [%5] %6 %7");
158  return session.arg(isConnected ? u"connected" : u"disconnected")
159  .arg(this->getName(), this->getAddress())
160  .arg(this->getPort())
161  .arg(this->getEcosystem().getSystemString(), this->getUser().getRealName(),
163  }
164 
166  {
167  if (index.isMyself()) { return QVariant::fromValue(*this); }
169 
170  const ColumnIndex i = index.frontCasted<ColumnIndex>();
171  switch (i)
172  {
173  case IndexAddress: return QVariant::fromValue(m_address);
174  case IndexDescription: return QVariant::fromValue(m_description);
175  case IndexName: return QVariant::fromValue(m_name);
176  case IndexPort: return QVariant::fromValue(m_port);
177  case IndexUser: return m_user.propertyByIndex(index.copyFrontRemoved());
178  case IndexFsdSetup: return m_fsdSetup.propertyByIndex(index.copyFrontRemoved());
179  case IndexEcosystem: return m_ecosystem.propertyByIndex(index.copyFrontRemoved());
180  case IndexIsAcceptingConnections: return QVariant::fromValue(m_isAcceptingConnections);
181  case IndexServerType: return QVariant::fromValue(m_serverType);
182  case IndexServerTypeAsString: return QVariant::fromValue(getServerTypeAsString());
183  default: return CValueObject::propertyByIndex(index);
184  }
185  }
186 
187  void CServer::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
188  {
189  if (index.isMyself())
190  {
191  (*this) = variant.value<CServer>();
192  return;
193  }
195  {
196  ITimestampBased::setPropertyByIndex(index, variant);
197  return;
198  }
199 
200  const ColumnIndex i = index.frontCasted<ColumnIndex>();
201  switch (i)
202  {
203  case IndexAddress: this->setAddress(variant.value<QString>()); break;
204  case IndexPort: this->setPort(variant.value<qint32>()); break;
205  case IndexDescription: this->setDescription(variant.value<QString>()); break;
206  case IndexName: this->setName(variant.value<QString>()); break;
207  case IndexUser: m_user.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
208  case IndexFsdSetup: m_fsdSetup.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
209  case IndexEcosystem: m_ecosystem.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
210  case IndexServerType: this->setServerType(static_cast<ServerType>(variant.toInt())); break;
211  case IndexIsAcceptingConnections: this->setIsAcceptingConnections(variant.value<bool>()); break;
212  default: CValueObject::setPropertyByIndex(index, variant); break;
213  }
214  }
215 
216  int CServer::comparePropertyByIndex(CPropertyIndexRef index, const CServer &compareValue) const
217  {
218  if (index.isMyself()) { return this->getName().compare(compareValue.getName()); }
220  {
221  return ITimestampBased::comparePropertyByIndex(index, compareValue);
222  }
223  const ColumnIndex i = index.frontCasted<ColumnIndex>();
224  switch (i)
225  {
226  case IndexAddress: return this->getAddress().compare(compareValue.getAddress(), Qt::CaseInsensitive);
227  case IndexDescription:
228  return this->getDescription().compare(compareValue.getDescription(), Qt::CaseInsensitive);
229  case IndexFsdSetup:
230  return m_fsdSetup.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getFsdSetup());
231  case IndexName: return this->getName().compare(compareValue.getName(), Qt::CaseInsensitive);
232  case IndexPort: return Compare::compare(this->getPort(), compareValue.getPort());
233  case IndexUser: return this->getUser().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getUser());
234  case IndexEcosystem:
235  return this->getEcosystem().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getEcosystem());
236  case IndexIsAcceptingConnections:
237  return Compare::compare(this->isAcceptingConnections(), compareValue.isAcceptingConnections());
238  case IndexServerType:
239  case IndexServerTypeAsString:
240  return this->getServerTypeAsString().compare(compareValue.getServerTypeAsString(), Qt::CaseInsensitive);
241  default: break;
242  }
243  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
244  return 0;
245  }
246 
248  {
249  static const QString fsdVatsim("FSD [VATSIM]");
250  static const QString voiceVatsim("voice [VATSIM]");
251  static const QString fsdLegacy("FSD (legacy)");
252  static const QString voice("voice");
253  static const QString webService("web service");
254  static const QString unspecified("unspecified");
255 
256  switch (server)
257  {
258  case FSDServerVatsim: return fsdVatsim;
259  case VoiceServerVatsim: return voiceVatsim;
260  case FSDServer: return fsdLegacy;
261  case VoiceServer: return voice;
262  case WebService: return webService;
263  case Unspecified:
264  default: return unspecified;
265  }
266  }
267 
268  void CServer::setAddress(const QString &address) { m_address = CObfuscation::decode(address); }
269 
270  void CServer::setName(const QString &name) { m_name = CObfuscation::decode(name); }
271 
272  void CServer::setDescription(const QString &description)
273  {
274  m_description = CObfuscation::decode(description).simplified();
275  }
276 } // namespace swift::misc::network
A sequence of log categories.
Utility class to obfuscate strings in the source code to make them unreadable.
Definition: obfuscation.h:19
static QString decode(const QString &inString, bool trimmed=true)
Decode string if it has the prefix, otherwise do nothing with it.
Definition: obfuscation.cpp:12
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Streamable status message, e.g.
constexpr static auto SeverityError
Status severities.
constexpr static auto SeverityWarning
Status severities.
Status messages, e.g. from Core -> GUI.
void addCategories(const CLogCategoryList &categories)
Add some categories to all messages in the list.
void sortBySeverity()
Sort by severity, lowest first.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampBased &compareValue) const
Compare for index.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
qint64 m_timestampMSecsSinceEpoch
timestamp value
QString getFormattedUtcTimestampHms() const
As hh:mm:ss.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Ecosystem of server belonging together.
Definition: ecosystem.h:21
@ SwiftTest
swift test server
Definition: ecosystem.h:36
int comparePropertyByIndex(CPropertyIndexRef index, const CEcosystem &compareValue) const
Compare for index.
Definition: ecosystem.cpp:98
static const CEcosystem & vatsim()
VATSIM eco system.
Definition: ecosystem.cpp:26
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: ecosystem.cpp:83
bool isSystem(System s) const
Is system?
Definition: ecosystem.h:54
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: ecosystem.cpp:71
static const CEcosystem & privateFsd()
FSD private.
Definition: ecosystem.cpp:44
const QString & getSystemString() const
Get the system string.
Definition: ecosystem.cpp:50
Value object for a FSD setup.
Definition: fsdsetup.h:24
static const CFsdSetup & vatsimStandard()
Standard FSD setup for official VATSIM servers.
Definition: fsdsetup.cpp:76
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: fsdsetup.cpp:110
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: fsdsetup.cpp:98
int comparePropertyByIndex(CPropertyIndexRef index, const CFsdSetup &compareValue) const
Compare for index.
Definition: fsdsetup.cpp:126
@ AllInterimPositions
all interim positions
Definition: fsdsetup.h:53
Value object encapsulating information of a server.
Definition: server.h:28
bool hasAddressAndPort() const
Address and port?
Definition: server.cpp:103
void setName(const QString &name)
Set name.
Definition: server.cpp:270
void setDescription(const QString &description)
Set description.
Definition: server.cpp:272
void setAddress(const QString &address)
Set address (e.g. myserver.foo.com)
Definition: server.cpp:268
bool setServerType(ServerType serverType)
Set server type.
Definition: server.cpp:110
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: server.cpp:165
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: server.cpp:44
const QString & getDescription() const
Get description.
Definition: server.h:110
const QString & getServerTypeAsString() const
Get server type as string.
Definition: server.cpp:120
bool matchesAddress(const QString &address) const
Same address?
Definition: server.cpp:82
bool hasName() const
Has name?
Definition: server.h:95
static const CServer & esTowerView()
ES tower server.
Definition: server.cpp:64
int getPort() const
Get port.
Definition: server.h:116
bool isConnected() const
Is connected?
Definition: server.cpp:122
bool matchesName(const QString &name) const
Matches server name?
Definition: server.cpp:72
CStatusMessageList validate() const
Validate, provide details about issues.
Definition: server.cpp:126
bool matchesAddressPort(const CServer &server) const
Same address and port?
Definition: server.cpp:77
bool isValidForLogin() const
Is valid for login?
Definition: server.cpp:98
const QString & getName() const
Get name.
Definition: server.h:92
const CFsdSetup & getFsdSetup() const
Get FSD setup.
Definition: server.h:140
static const QList< int > & allServerTypes()
Allows to iterate over all ServerType.
Definition: server.cpp:21
void setIsAcceptingConnections(bool value)
Set whether server is accepting connections (allows to disable server temporarily or generally)
Definition: server.h:131
static const QString & serverTypeToString(ServerType server)
Enum to string.
Definition: server.cpp:247
int comparePropertyByIndex(CPropertyIndexRef index, const CServer &compareValue) const
Compare for index.
Definition: server.cpp:216
const QString & getAddress() const
Get address.
Definition: server.h:80
void setPort(int port)
Set port.
Definition: server.h:119
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: server.cpp:187
const CEcosystem & getEcosystem() const
Get the ecosystem.
Definition: server.h:122
void removeSendReceiveDetails(CFsdSetup::SendReceiveDetails sendReceive)
Remove send / receive details.
Definition: server.h:152
static const CServer & fscFsdServer()
FSC server.
Definition: server.cpp:53
CServer()
Default constructor.
Definition: server.h:63
bool isAcceptingConnections() const
Server is accepting connections (allows to disable server temporarily or generally)
Definition: server.h:128
bool setEcosystem(const CEcosystem &ecosystem)
Set the ecosystem.
Definition: server.cpp:87
const CUser & getUser() const
Get user.
Definition: server.h:86
bool isNull() const
NULL?
Definition: server.cpp:124
bool isFsdServer() const
A FSD server?
Definition: server.cpp:105
bool hasUnspecifiedServerType() const
Unspecified?
Definition: server.cpp:118
ServerType getServerType() const
Get server type.
Definition: server.h:164
QString getServerSessionId(bool onlyConnected) const
Identifying a session, if not connected empty.
Definition: server.cpp:153
Value object encapsulating information of a user.
Definition: user.h:28
int comparePropertyByIndex(CPropertyIndexRef index, const CUser &compareValue) const
Compare for index.
Definition: user.cpp:281
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: user.cpp:241
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: user.cpp:259
bool hasCredentials() const
Valid credentials?
Definition: user.h:77
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26