#include <QUrl>
#include <QEventLoop>
#include <QTimer>
+#include <QElapsedTimer>
using namespace MUtils;
"www.bucketheadpikes.com",
"www.buckethead-coop.com",
"www.buzzfeed.com",
+ "www.cam.ac.uk",
"www.ccc.de",
+ "home.cern",
"www.citizeninsomniac.com",
"www.cnet.com",
"cnzz.com",
+ "www.cuhk.edu.hk",
"www.codeplex.com",
"www.codeproject.com",
"www.der-postillon.com",
"blog.fefe.de",
"www.ffmpeg.org",
"blog.flickr.net",
+ "www.fraunhofer.de",
"free-codecs.com",
"git-scm.com",
"doc.gitlab.com",
"go.com",
"code.google.com",
"haali.su",
+ "www.harvard.edu",
"www.heise.de",
+ "www.helmholtz.de",
"www.huffingtonpost.co.uk",
+ "www.hu-berlin.de",
"www.iana.org",
"www.imdb.com",
"www.imgburn.com",
"imgur.com",
+ "www.iuj.ac.jp",
"www.jd.com",
"www.jiscdigitalmedia.ac.uk",
"kannmanumdieuhrzeitschonnbierchentrinken.de",
"go.mail.ru",
"marknelson.us",
"www.mediafire.com",
+ "web.mit.edu",
"www.mod-technologies.com",
"ftp.mozilla.org",
+ "www.mpg.de",
"mplayerhq.hu",
"www.msn.com",
"wiki.multimedia.cx",
"www.nch.com.au",
+ "neocities.org",
"mirror.netcologne.de",
"oss.netfarm.it",
"blog.netflix.com",
"netrenderer.de",
"www.nytimes.com",
"www.opera.com",
+ "www.oxford.gov.uk",
"www.partha.com",
"pastebin.com",
"pastie.org",
"www.qt.io",
"www.quakelive.com",
"rationalqm.us",
+ "www.rwth-aachen.de",
"www.seamonkey-project.org",
"selfhtml.org",
"www.sina.com.cn",
"www.spiegel.de",
"www.sputnikmusic.com",
"stackoverflow.com",
+ "www.stanford.edu",
"www.t-online.de",
"www.tagesschau.de",
"tdm-gcc.tdragon.net",
"www.tdrsmusic.com",
+ "tu-dresden.de",
"www.ubuntu.com",
"www.uol.com.br",
"www.videohelp.com",
};
static const int MIN_CONNSCORE = 5;
+static const int QUICK_MIRRORS = 3;
+static const int MAX_CONN_TIMEOUT = 8000;
+static const int DOWNLOAD_TIMEOUT = 30000;
+
static const int VERSION_INFO_EXPIRES_MONTHS = 6;
static char *USER_AGENT_STR = "Mozilla/5.0 (X11; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0"; /*use something innocuous*/
+#define CHECK_CANCELLED() do \
+{ \
+ if(m_cancelled) \
+ { \
+ m_success = false; \
+ log("", "Update check has been cancelled by user!"); \
+ setProgress(m_maxProgress); \
+ setStatus(UpdateStatus_CancelledByUser); \
+ return; \
+ } \
+} \
+while(0)
+
////////////////////////////////////////////////////////////
// Helper Functions
////////////////////////////////////////////////////////////
static int getMaxProgress(void)
{
- int counter = MIN_CONNSCORE + 2;
- for(int i = 0; update_mirrors[i]; i++) counter++;
- return counter;
+ int counter = 0;
+ while (update_mirrors[counter])
+ {
+ counter++;
+ }
+ counter += MIN_CONNSCORE + QUICK_MIRRORS + 2;
+ return counter; ;
}
static QStringList buildRandomList(const char *const values[])
m_testMode(testMode),
m_maxProgress(getMaxProgress())
{
- m_success = false;
+ m_success = m_cancelled = false;
m_status = UpdateStatus_NotStartedYet;
m_progress = 0;
}
////////////////////////////////////////////////////////////
+// Public slots
+////////////////////////////////////////////////////////////
+
+void UpdateChecker::start(Priority priority)
+{
+ m_cancelled = m_success = false;
+ QThread::start(priority);
+}
+
+////////////////////////////////////////////////////////////
// Protected functions
////////////////////////////////////////////////////////////
{
// ----- Initialization ----- //
- m_success = false;
m_updateInfo->resetInfo();
setProgress(0);
// ----- Test Known Hosts Connectivity ----- //
- int connectionScore = 0, connectionRetry = MIN_CONNSCORE;
+ int connectionScore = 0;
+ QStringList mirrorList = buildRandomList(known_hosts);
- QStringList hostList = buildRandomList(known_hosts);
- while((!hostList.isEmpty()) && (connectionScore < MIN_CONNSCORE) && (connectionRetry > 0))
+ for(int connectionTimout = 125; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
{
- connectionRetry--;
- if(tryContactHost(hostList.takeFirst()))
+ QElapsedTimer elapsedTimer;
+ elapsedTimer.start();
+ const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout;
+ while (!elapsedTimer.hasExpired(globalTimout))
{
- connectionScore += 1;
- connectionRetry = MIN_CONNSCORE;
+ const QString hostName = mirrorList.takeFirst();
+ if (tryContactHost(hostName, connectionTimout))
+ {
+ connectionScore += 1;
+ setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
+ elapsedTimer.restart();
+ if (connectionScore >= MIN_CONNSCORE)
+ {
+ goto endLoop; /*success*/
+ }
+ }
+ else
+ {
+ mirrorList.append(hostName); /*re-schedule*/
+ }
+ CHECK_CANCELLED();
+ msleep(1);
}
- setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
- msleep(25);
}
+endLoop:
if(connectionScore < MIN_CONNSCORE)
{
log("", "Connectivity test has failed: Internet connection appears to be broken!");
log("----", "", "Checking for updates online...");
setStatus(UpdateStatus_FetchingUpdates);
- QStringList mirrorList = buildRandomList(update_mirrors);
+ int mirrorCount = 0;
+ mirrorList = buildRandomList(update_mirrors);
+
while(!mirrorList.isEmpty())
{
- QString currentMirror = mirrorList.takeFirst();
setProgress(m_progress + 1);
- if(!m_success)
+ const QString currentMirror = mirrorList.takeFirst();
+ const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
+ if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
{
- if(tryUpdateMirror(m_updateInfo.data(), currentMirror))
- {
- m_success = true;
- }
+ m_success = true; /*success*/
+ break;
}
- else
+ if (isQuick)
{
- msleep(25);
+ mirrorList.append(currentMirror); /*re-schedule*/
}
+ CHECK_CANCELLED();
+ msleep(1);
}
- setProgress(m_maxProgress);
+ while (m_progress < m_maxProgress)
+ {
+ msleep(16);
+ setProgress(m_progress + 1);
+ CHECK_CANCELLED();
+ }
+
+ // ----- Generate final result ----- //
if(m_success)
{
QString currentHost = hostList.takeFirst();
qDebug("Testing: %s", currentHost.toLatin1().constData());
log("", "Testing:", currentHost, "");
- if (!tryContactHost(currentHost))
+ if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
{
qWarning("\nConnectivity test FAILED on the following host:\n%s\n", currentHost.toLatin1().constData());
}
if(!str4.isNull()) emit messageLogged(str4);
}
-bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url)
+bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
{
bool success = false;
- log("", "Trying mirror:", url);
+ log("", "Trying mirror:", url, "");
+
+ if (!tryContactHost(QUrl(url).host(), quick ? (MAX_CONN_TIMEOUT / 10) : MAX_CONN_TIMEOUT))
+ {
+ log("", quick ? "Mirror is too slow!" :"Mirror is unreachable!");
+ return false;
+ }
const QString randPart = next_rand_str();
const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
- if(getUpdateInfo(url, outFileVers, outFileSign))
+ if (getUpdateInfo(url, outFileVers, outFileSign))
{
log("", "Download okay, checking signature:");
- if(checkSignature(outFileVers, outFileSign))
+ if (checkSignature(outFileVers, outFileSign))
{
log("", "Signature okay, parsing info:");
success = parseVersionInfo(outFileVers, updateInfo);
bool UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
{
- log("", "Downloading update info:");
+ log("Downloading update info:");
if(!getFile(QString("%1%2" ).arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
{
return false;
// EXTERNAL TOOLS
//----------------------------------------------------------
-bool UpdateChecker::getFile(const QString &url, const QString &outFile, const unsigned int maxRedir, bool *httpOk)
+bool UpdateChecker::getFile(const QString &url, const QString &outFile, const unsigned int maxRedir)
{
for (int i = 0; i < 2; i++)
{
- if (getFile(url, (i > 0), outFile, maxRedir, httpOk))
+ if (getFile(url, (i > 0), outFile, maxRedir))
{
return true;
}
return false;
}
-bool UpdateChecker::getFile(const QString &url, const bool forceIp4, const QString &outFile, const unsigned int maxRedir, bool *httpOk)
+bool UpdateChecker::getFile(const QString &url, const bool forceIp4, const QString &outFile, const unsigned int maxRedir)
{
QFileInfo output(outFile);
output.setCaching(false);
- if (httpOk) *httpOk = false;
if (output.exists())
{
}
args << "--no-config" << "--no-cache" << "--no-dns-cache" << "--no-check-certificate" << "--no-hsts";
- args << QString().sprintf("--max-redirect=%u", maxRedir) << "--timeout=15";
+ args << QString().sprintf("--max-redirect=%u", maxRedir) << QString().sprintf("--timeout=%u", DOWNLOAD_TIMEOUT / 1000);
args << QString("--referer=%1://%2/").arg(QUrl(url).scheme(), QUrl(url).host()) << "-U" << USER_AGENT_STR;
args << "-O" << output.fileName() << url;
return false;
}
- timer.start(25000);
+ timer.start(DOWNLOAD_TIMEOUT);
while (process.state() != QProcess::NotRunning)
{
const bool bTimeOut = (!timer.isActive());
while (process.canReadLine())
{
- QString line = QString::fromLatin1(process.readLine()).simplified();
- if (line.contains(httpResponseOK))
- {
- line.append(" [OK]");
- if (httpOk) *httpOk = true;
- }
+ const QString line = QString::fromLatin1(process.readLine()).simplified();
log(line);
}
- if (bTimeOut)
+ if (bTimeOut || m_cancelled)
{
qWarning("WGet process timed out <-- killing!");
process.kill();
process.waitForFinished();
- log("!!! TIMEOUT !!!");
+ log(bTimeOut ? "!!! TIMEOUT !!!": "!!! CANCELLED !!!");
return false;
}
}
return (process.exitCode() == 0) && output.exists() && output.isFile();
}
-bool UpdateChecker::tryContactHost(const QString &hostname)
+bool UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
{
log(QString("Connecting to host: %1").arg(hostname));
return false;
}
- timer.start(10000);
+ timer.start(timeoutMsec);
while (process.state() != QProcess::NotRunning)
{
QString line = QString::fromLatin1(process.readLine()).simplified();
log(line);
}
- if (bTimeOut)
+ if (bTimeOut || m_cancelled)
{
qWarning("NC process timed out <-- killing!");
process.kill();
process.waitForFinished();
- log("!!! TIMEOUT !!!");
+ log(bTimeOut ? "!!! TIMEOUT !!!" : "!!! CANCELLED !!!");
return false;
}
}