mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
refs #521, improved URL list
* allow to reset url list * remove duplicates * fixed CFailoverUrlList::getNextWorkingUrl
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<CUrl> &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;
|
||||
|
||||
Reference in New Issue
Block a user