refs #521, improved URL list

* allow to reset url list
* remove duplicates
* fixed CFailoverUrlList::getNextWorkingUrl
This commit is contained in:
Klaus Basan
2015-11-25 03:33:31 +01:00
parent 1f77d8844a
commit af3ebf71a3
2 changed files with 45 additions and 6 deletions

View File

@@ -19,9 +19,11 @@ namespace BlackMisc
{ {
CUrlList::CUrlList() { } 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)); this->push_back(CUrl(url));
} }
@@ -110,6 +112,27 @@ namespace BlackMisc
return sl.join(separator); 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) : CFailoverUrlList::CFailoverUrlList(int maxTrials) :
m_maxTrials(maxTrials) m_maxTrials(maxTrials)
{ } { }
@@ -131,6 +154,7 @@ namespace BlackMisc
bool CFailoverUrlList::addFailedUrl(const CUrl &failedUrl) bool CFailoverUrlList::addFailedUrl(const CUrl &failedUrl)
{ {
Q_ASSERT_X(!failedUrl.isEmpty(), Q_FUNC_INFO, "empty URL as failed");
this->m_failedUrls.push_back(failedUrl); this->m_failedUrls.push_back(failedUrl);
return hasMoreUrlsToTry(); return hasMoreUrlsToTry();
} }
@@ -145,10 +169,16 @@ namespace BlackMisc
{ {
if (!failedUrl.isEmpty()) { this->addFailedUrl(failedUrl); } if (!failedUrl.isEmpty()) { this->addFailedUrl(failedUrl); }
if (!hasMoreUrlsToTry()) { return CUrl(); } if (!hasMoreUrlsToTry()) { return CUrl(); }
CUrl url(this->getNextUrl(random)); CUrl url(this->getNextUrlWithout(this->m_failedUrls, random));
if (CNetworkUtils::canConnect(url)) { return url; } if (CNetworkUtils::canConnect(url)) { return url; }
if (addFailedUrl(url)) { return getNextUrl(); } if (addFailedUrl(url)) { return getNextWorkingUrl(); }
return CUrl(); return CUrl();
} }
void CFailoverUrlList::reset(int maxTrials)
{
this->m_failedUrls.clear();
if (maxTrials >= 0) { this->m_maxTrials = maxTrials; }
}
} // namespace } // namespace
} // namespace } // namespace

View File

@@ -33,7 +33,7 @@ namespace BlackMisc
CUrlList(); CUrlList();
//! By list of URLs //! By list of URLs
explicit CUrlList(const QStringList &listOfUrls); explicit CUrlList(const QStringList &listOfUrls, bool removeDuplicates = true);
//! Construct from a base class object. //! Construct from a base class object.
CUrlList(const CSequence<CUrl> &other); CUrlList(const CSequence<CUrl> &other);
@@ -59,8 +59,14 @@ namespace BlackMisc
//! To formatted String //! To formatted String
QString convertToQString(const QString &separator, bool i18n = false) const; 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: private:
mutable int m_currentIndexDistributedLoad = -1; mutable int m_currentIndexDistributedLoad = -1; //!< index for random access
}; };
//! URL list with fail support //! URL list with fail support
@@ -91,6 +97,9 @@ namespace BlackMisc
//! Next working URL, test if it can be connected //! Next working URL, test if it can be connected
CUrl getNextWorkingUrl(const CUrl &failedUrl = CUrl(), bool random = true); 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: private:
int m_maxTrials = 2; //!< number of max trials int m_maxTrials = 2; //!< number of max trials
CUrlList m_failedUrls; CUrlList m_failedUrls;