refs #507, checks if URLs / DBus can be connected / reached

* new class CFailoverUrlList
* improved utility methods
* CDBusServer: utility function to check server / DBus can be connected
* check in swift GUI if DBus is available
This commit is contained in:
Klaus Basan
2015-11-17 01:22:29 +01:00
committed by Mathew Sutcliffe
parent b24cd2e9c7
commit 6dd8fb333e
7 changed files with 271 additions and 62 deletions

View File

@@ -134,9 +134,21 @@ namespace BlackMisc
return canConnect(host, p, message, timeoutMs);
}
bool CNetworkUtils::canConnect(const CUrl &location, QString &message, int timeoutMs)
bool CNetworkUtils::canConnect(const QUrl &url, int timeoutMs)
{
return canConnect(location.getHost(), location.getPort(), message, timeoutMs);
QString m;
return canConnect(url, m, timeoutMs);
}
bool CNetworkUtils::canConnect(const CUrl &url, QString &message, int timeoutMs)
{
return canConnect(url.getHost(), url.getPort(), message, timeoutMs);
}
bool CNetworkUtils::canConnect(const CUrl &url, int timeoutMs)
{
QString m;
return canConnect(url, m, timeoutMs);
}
bool CNetworkUtils::isValidIPv4Address(const QString &candidate)

View File

@@ -66,7 +66,13 @@ namespace BlackMisc
static bool canConnect(const QUrl &url, QString &message, int timeoutMs = 1500);
//! Can connect to URL?
static bool canConnect(const BlackMisc::Network::CUrl &location, QString &message, int timeoutMs = 1500);
static bool canConnect(const QUrl &url, int timeoutMs = 1500);
//! Can connect to URL?
static bool canConnect(const BlackMisc::Network::CUrl &url, QString &message, int timeoutMs = 1500);
//! Can connect to URL?
static bool canConnect(const BlackMisc::Network::CUrl &url, int timeoutMs = 1500);
//! Find out my IPv4 address, empty if not possible
static QStringList getKnownIpAddresses();

View File

@@ -8,6 +8,7 @@
*/
#include "blackmisc/network/urllist.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/math/mathutils.h"
using namespace BlackMisc::Math;
@@ -38,6 +39,22 @@ namespace BlackMisc
return (*this)[i];
}
CUrl CUrlList::getRandomWorkingUrl(int maxTrials) const
{
if (this->isEmpty()) { return CUrl(); }
if (maxTrials < 1) { return CUrl();}
CUrlList trials;
for (int t = 0; t < maxTrials && t < this->size(); t++)
{
CUrl url(getRandomWithout(trials));
trials.push_back(url);
QString message;
if (CNetworkUtils::canConnect(url, message)) { return url; }
}
return CUrl();
}
CUrl CUrlList::getRandomWithout(const CUrlList &exclude) const
{
CUrlList copy(*this);
@@ -87,5 +104,51 @@ namespace BlackMisc
return urls;
}
QString CUrlList::convertToQString(const QString &separator, bool i18n) const
{
const QStringList sl(toStringList(i18n));
return sl.join(separator);
}
CFailoverUrlList::CFailoverUrlList(int maxTrials) :
m_maxTrials(maxTrials)
{ }
CFailoverUrlList::CFailoverUrlList(const QStringList &listOfUrls, int maxTrials) :
CUrlList(listOfUrls), m_maxTrials(maxTrials)
{ }
CFailoverUrlList::CFailoverUrlList(const CSequence<CUrl> &other, int maxTrials) :
CUrlList(other), m_maxTrials(maxTrials)
{ }
CUrlList CFailoverUrlList::getWithoutFailed() const
{
CUrlList urls(*this);
urls.removeIfIn(m_failedUrls);
return urls;
}
bool CFailoverUrlList::addFailedUrl(const CUrl &failedUrl)
{
this->m_failedUrls.push_back(failedUrl);
return hasMoreUrlsToTry();
}
bool CFailoverUrlList::hasMoreUrlsToTry() const
{
if (this->isEmpty()) { return false; }
return (m_failedUrls.size() < this->size() && m_failedUrls.size() < m_maxTrials);
}
CUrl CFailoverUrlList::getNextWorkingUrl(const CUrl &failedUrl, bool random)
{
if (!failedUrl.isEmpty()) { this->addFailedUrl(failedUrl); }
if (!hasMoreUrlsToTry()) { return CUrl(); }
CUrl url(this->getNextUrl(random));
if (CNetworkUtils::canConnect(url)) { return url; }
if (addFailedUrl(url)) { return getNextUrl(); }
return CUrl();
}
} // namespace
} // namespace

View File

@@ -41,6 +41,9 @@ namespace BlackMisc
//! Random location for distributed load
CUrl getRandomUrl() const;
//! Random location for distributed load, tested
CUrl getRandomWorkingUrl(int maxTrials = 2) const;
//! Random location for distributed load
CUrl getRandomWithout(const CUrlList &exclude) const;
@@ -53,9 +56,46 @@ namespace BlackMisc
//! Append path to all URLs
CUrlList appendPath(const QString &path) const;
//! To formatted String
QString convertToQString(const QString &separator, bool i18n = false) const;
private:
mutable int m_currentIndexDistributedLoad = -1;
};
//! URL list with fail support
class BLACKMISC_EXPORT CFailoverUrlList : public CUrlList
{
public:
//! Default constructor.
CFailoverUrlList(int maxTrials = 2);
//! By list of URLs
explicit CFailoverUrlList(const QStringList &listOfUrls, int maxTrials = 2);
//! Construct from a base class object.
CFailoverUrlList(const CSequence<CUrl> &other, int maxTrials = 2);
//! All failed URLs
const CUrlList &getFailedUrls() const { return m_failedUrls; }
//! Get without the failed URLs
CUrlList getWithoutFailed() const;
//! Failed URL
bool addFailedUrl(const CUrl &failedUrl);
//! More URLs to try
bool hasMoreUrlsToTry() const;
//! Next working URL, test if it can be connected
CUrl getNextWorkingUrl(const CUrl &failedUrl = CUrl(), bool random = true);
private:
int m_maxTrials = 2; //!< number of max trials
CUrlList m_failedUrls;
};
} //namespace
} // namespace