From 4038c5605b8fca7075e1f5802af76e235fd5a634 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 20 Oct 2018 21:05:57 +0200 Subject: [PATCH] Improved buildRandomList() function. --- src/UpdateChecker.cpp | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/UpdateChecker.cpp b/src/UpdateChecker.cpp index 33dd516..b975683 100644 --- a/src/UpdateChecker.cpp +++ b/src/UpdateChecker.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include "Mirrors.h" @@ -91,13 +92,12 @@ while(0) // Helper Functions //////////////////////////////////////////////////////////// -static QStringList buildRandomList(const char *const values[]) +static QQueue buildRandomList(const char *const *values) { - QStringList list; - for (int index = 0; values[index]; index++) + QQueue list; + while(*values) { - const int pos = MUtils::next_rand_u32() % (index + 1); - list.insert(pos, QString::fromLatin1(values[index])); + list.insert(MUtils::next_rand_u32(list.size() + 1), QString::fromLatin1(*(values++))); } return list; } @@ -222,32 +222,35 @@ void MUtils::UpdateChecker::checkForUpdates(void) // ----- Test Known Hosts Connectivity ----- // int connectionScore = 0; - QStringList mirrorList = buildRandomList(known_hosts); + QQueue mirrorList = buildRandomList(known_hosts); for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2) { QElapsedTimer elapsedTimer; - elapsedTimer.start(); const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout; - while (!elapsedTimer.hasExpired(globalTimout)) + elapsedTimer.start(); + do { - const QString hostName = mirrorList.takeFirst(); - if (tryContactHost(hostName, connectionTimout)) + if (!mirrorList.isEmpty()) { - setProgress(1 + (connectionScore += 1)); - elapsedTimer.restart(); - if (connectionScore >= MIN_CONNSCORE) + const QString hostName = mirrorList.dequeue(); + if (tryContactHost(hostName, connectionTimout)) { - goto endLoop; /*success*/ + setProgress(1 + (connectionScore += 1)); + elapsedTimer.restart(); + if (connectionScore >= MIN_CONNSCORE) + { + goto endLoop; /*success*/ + } + } + else + { + mirrorList.enqueue(hostName); /*re-schedule*/ } } - else - { - mirrorList.append(hostName); /*re-schedule*/ - } CHECK_CANCELLED(); - msleep(1); } + while(!elapsedTimer.hasExpired(globalTimout)); } endLoop: