From af3ebf71a34e1bb1531557e2b3e1e5f49d8eb786 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 25 Nov 2015 03:33:31 +0100 Subject: [PATCH] refs #521, improved URL list * allow to reset url list * remove duplicates * fixed CFailoverUrlList::getNextWorkingUrl --- src/blackmisc/network/urllist.cpp | 38 +++++++++++++++++++++++++++---- src/blackmisc/network/urllist.h | 13 +++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/blackmisc/network/urllist.cpp b/src/blackmisc/network/urllist.cpp index 929e72cda..7cff54d77 100644 --- a/src/blackmisc/network/urllist.cpp +++ b/src/blackmisc/network/urllist.cpp @@ -19,9 +19,11 @@ namespace BlackMisc { CUrlList::CUrlList() { } - CUrlList::CUrlList(const QStringList &listOfUrls) + CUrlList::CUrlList(const QStringList &listOfUrls, bool removeDuplicates) { - for (const QString &url : listOfUrls) + QStringList urlList(listOfUrls); + if (removeDuplicates) { urlList.removeDuplicates(); } + for (const QString &url : urlList) { this->push_back(CUrl(url)); } @@ -110,6 +112,27 @@ namespace BlackMisc return sl.join(separator); } + CUrlList CUrlList::getWithoutDuplicates() const + { + if (this->size() < 2) { return (*this); } + CUrlList withoutDuplicates; + for (const CUrl &url : (*this)) + { + withoutDuplicates.replaceOrAdd(url, url); + } + return withoutDuplicates; + } + + int CUrlList::removeDuplicates() + { + if (this->size() < 2) { return 0; } + CUrlList withoutDuplicates(getWithoutDuplicates()); + if (this->size() == withoutDuplicates.size()) { return 0; } + int r = this->size() - withoutDuplicates.size(); + (*this) = withoutDuplicates; + return r; + } + CFailoverUrlList::CFailoverUrlList(int maxTrials) : m_maxTrials(maxTrials) { } @@ -131,6 +154,7 @@ namespace BlackMisc bool CFailoverUrlList::addFailedUrl(const CUrl &failedUrl) { + Q_ASSERT_X(!failedUrl.isEmpty(), Q_FUNC_INFO, "empty URL as failed"); this->m_failedUrls.push_back(failedUrl); return hasMoreUrlsToTry(); } @@ -145,10 +169,16 @@ namespace BlackMisc { if (!failedUrl.isEmpty()) { this->addFailedUrl(failedUrl); } if (!hasMoreUrlsToTry()) { return CUrl(); } - CUrl url(this->getNextUrl(random)); + CUrl url(this->getNextUrlWithout(this->m_failedUrls, random)); if (CNetworkUtils::canConnect(url)) { return url; } - if (addFailedUrl(url)) { return getNextUrl(); } + if (addFailedUrl(url)) { return getNextWorkingUrl(); } return CUrl(); } + + void CFailoverUrlList::reset(int maxTrials) + { + this->m_failedUrls.clear(); + if (maxTrials >= 0) { this->m_maxTrials = maxTrials; } + } } // namespace } // namespace diff --git a/src/blackmisc/network/urllist.h b/src/blackmisc/network/urllist.h index 14476aeb8..d99184fb6 100644 --- a/src/blackmisc/network/urllist.h +++ b/src/blackmisc/network/urllist.h @@ -33,7 +33,7 @@ namespace BlackMisc CUrlList(); //! By list of URLs - explicit CUrlList(const QStringList &listOfUrls); + explicit CUrlList(const QStringList &listOfUrls, bool removeDuplicates = true); //! Construct from a base class object. CUrlList(const CSequence &other); @@ -59,8 +59,14 @@ namespace BlackMisc //! To formatted String QString convertToQString(const QString &separator, bool i18n = false) const; + //! URLs without duplicates + CUrlList getWithoutDuplicates() const; + + //! Remove duplicated URL and return number of removed elements + int removeDuplicates(); + private: - mutable int m_currentIndexDistributedLoad = -1; + mutable int m_currentIndexDistributedLoad = -1; //!< index for random access }; //! URL list with fail support @@ -91,6 +97,9 @@ namespace BlackMisc //! Next working URL, test if it can be connected CUrl getNextWorkingUrl(const CUrl &failedUrl = CUrl(), bool random = true); + //! Reset failed URL, allows to set an optional new number of max.trials + void reset(int maxTrials = -1); + private: int m_maxTrials = 2; //!< number of max trials CUrlList m_failedUrls;