Fixed adding the hardcoded test servers in a dev. environment

(issue found during debugging of #533)
Added some functions to find similar servers by address/port
This commit is contained in:
Klaus Basan
2015-12-06 05:40:50 +01:00
parent f4eec653c1
commit 22ca6ae922
7 changed files with 80 additions and 10 deletions

View File

@@ -35,19 +35,38 @@ namespace BlackMisc
m_name.startsWith(name, Qt::CaseInsensitive);
}
bool CServer::matchesAddressPort(const CServer &server) const
{
return server.getPort() == this->getPort() &&
server.matchesAddress(this->getAddress());
}
bool CServer::matchesAddress(const QString &address) const
{
return m_address.length() == address.length() &&
m_address.startsWith(address, Qt::CaseInsensitive);
}
bool CServer::isValidForLogin() const
{
return this->m_user.hasValidCredentials() && this->m_port > 0 && !this->m_address.isEmpty() && this->isAcceptingConnections();
return this->m_user.hasValidCredentials() && this->hasAddressAndPort() && this->isAcceptingConnections();
}
bool CServer::hasAddressAndPort() const
{
return m_port > 0 && !m_address.isEmpty();
}
CStatusMessageList CServer::validate() const
{
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
CStatusMessageList msgs;
if (this->getName().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing name")); }
if (this->getAddress().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing address")); }
if (this->getDescription().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing description")); }
if (this->getPort() < 1 || this->getPort() > 65535) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Wrong port")); }
msgs.push_back(this->getUser().validate());
msgs.addCategories(cats);
return msgs;
}

View File

@@ -64,6 +64,12 @@ namespace BlackMisc
//! Matches server name?
bool matchesName(const QString &name) const;
//! Same address and port?
bool matchesAddressPort(const CServer &server) const;
//! Same address?
bool matchesAddress(const QString &address) const;
//! Get description
const QString &getDescription() const { return m_description; }
@@ -85,6 +91,9 @@ namespace BlackMisc
//! Is valid for login?
bool isValidForLogin() const;
//! Address and port?
bool hasAddressAndPort() const;
//! Validate, provide details about issues
BlackMisc::CStatusMessageList validate() const;

View File

@@ -13,7 +13,6 @@ namespace BlackMisc
{
namespace Network
{
CServerList::CServerList() { }
CServerList::CServerList(const CSequence<CServer> &other) :
@@ -29,5 +28,30 @@ namespace BlackMisc
return false;
}
bool CServerList::containsAddressPort(const CServer &server)
{
for (const CServer &s : *this)
{
if (s.matchesAddressPort(server)) { return true; }
}
return false;
}
void CServerList::addIfAddressNotExists(const CServer &server)
{
if (!server.hasAddressAndPort() || server.getName().isEmpty()) { return; }
if (!this->containsAddressPort(server))
{
this->push_back(server);
}
}
void CServerList::addIfAddressNotExists(const CServerList &servers)
{
for (const CServer &s : servers)
{
this->addIfAddressNotExists(s);
}
}
} // namespace
} // namespace

View File

@@ -37,8 +37,16 @@ namespace BlackMisc
//! Contains name
bool containsName(const QString &name) const;
};
//! Contains server with same address/port
bool containsAddressPort(const CServer &server);
//! Add if address not already exists
void addIfAddressNotExists(const CServer &server);
//! Add if address not already exists
void addIfAddressNotExists(const CServerList &servers);
};
} //namespace
} // namespace