OSDN Git Service

Updated the mirror URL.
[mutilities/MUtilities.git] / src / UpdateChecker.cpp
index 8df81dd..d0fdcd7 100644 (file)
@@ -53,7 +53,7 @@ static const char *update_mirrors[] =
        "http://muldersoft.com/",
        "http://mulder.bplaced.net/",
        "http://mulder.6te.net/",
-       "http://mulder.webuda.com/",
+       "http://mulder.000webhostapp.com/",     //"http://mulder.webuda.com/",
        "http://mulder.pe.hu/",
        "http://muldersoft.square7.ch/",
        "http://muldersoft.co.nf/",
@@ -102,6 +102,7 @@ static const char *known_hosts[] =          //Taken form: http://www.alexa.com/topsites
        "www.citizeninsomniac.com",
        "www.cnet.com",
        "cnzz.com",
+       "www.cuhk.edu.hk",
        "www.codeplex.com",
        "www.codeproject.com",
        "www.der-postillon.com",
@@ -125,12 +126,14 @@ static const char *known_hosts[] =                //Taken form: http://www.alexa.com/topsites
        "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",
@@ -154,6 +157,7 @@ static const char *known_hosts[] =          //Taken form: http://www.alexa.com/topsites
        "www.msn.com",
        "wiki.multimedia.cx",
        "www.nch.com.au",
+       "neocities.org",
        "mirror.netcologne.de",
        "oss.netfarm.it",
        "blog.netflix.com",
@@ -211,21 +215,39 @@ static const char *known_hosts[] =                //Taken form: http://www.alexa.com/topsites
 };
 
 static const int MIN_CONNSCORE = 5;
-static const int MAX_CONN_TIMEOUT = 8000;
+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(MUTILS_BOOLIFY(m_cancelled)) \
+       { \
+               m_success.fetchAndStoreOrdered(0); \
+               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[])
@@ -276,11 +298,11 @@ bool UpdateCheckerInfo::isComplete(void)
 // Constructor & Destructor
 ////////////////////////////////////////////////////////////
 
-UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binNC, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
+UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binMCat, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
 :
        m_updateInfo(new UpdateCheckerInfo()),
        m_binaryWGet(binWGet),
-       m_binaryNC(binNC),
+       m_binaryMCat(binMCat),
        m_binaryGnuPG(binGnuPG),
        m_binaryKeys(binKeys),
        m_applicationId(applicationId),
@@ -289,7 +311,6 @@ UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binNC, const
        m_testMode(testMode),
        m_maxProgress(getMaxProgress())
 {
-       m_success = false;
        m_status = UpdateStatus_NotStartedYet;
        m_progress = 0;
 
@@ -304,6 +325,17 @@ UpdateChecker::~UpdateChecker(void)
 }
 
 ////////////////////////////////////////////////////////////
+// Public slots
+////////////////////////////////////////////////////////////
+
+void UpdateChecker::start(Priority priority)
+{
+       m_success.fetchAndStoreOrdered(0);
+       m_cancelled.fetchAndStoreOrdered(0);
+       QThread::start(priority);
+}
+
+////////////////////////////////////////////////////////////
 // Protected functions
 ////////////////////////////////////////////////////////////
 
@@ -318,7 +350,6 @@ void UpdateChecker::checkForUpdates(void)
 {
        // ----- Initialization ----- //
 
-       m_success = false;
        m_updateInfo->resetInfo();
        setProgress(0);
 
@@ -340,22 +371,17 @@ void UpdateChecker::checkForUpdates(void)
 
        // ----- Test Known Hosts Connectivity ----- //
 
-       int connectionScore = 0, connectionTimout = 125;
-       QStringList hostList = buildRandomList(known_hosts);
+       int connectionScore = 0;
+       QStringList mirrorList = buildRandomList(known_hosts);
 
-       while (connectionScore < MIN_CONNSCORE)
+       for(int connectionTimout = 250; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
        {
-               connectionTimout *= 2;
-               if (connectionTimout > MAX_CONN_TIMEOUT)
-               {
-                       break;
-               }
                QElapsedTimer elapsedTimer;
-               const int globalTimout = 250 + (13 * connectionTimout);
                elapsedTimer.start();
+               const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout;
                while (!elapsedTimer.hasExpired(globalTimout))
                {
-                       const QString hostName = hostList.takeFirst();
+                       const QString hostName = mirrorList.takeFirst();
                        if (tryContactHost(hostName, connectionTimout))
                        {
                                connectionScore += 1;
@@ -363,17 +389,19 @@ void UpdateChecker::checkForUpdates(void)
                                elapsedTimer.restart();
                                if (connectionScore >= MIN_CONNSCORE)
                                {
-                                       break; /*success*/
+                                       goto endLoop; /*success*/
                                }
                        }
                        else
                        {
-                               hostList.append(hostName); /*re-schedule*/
+                               mirrorList.append(hostName); /*re-schedule*/
                        }
+                       CHECK_CANCELLED();
                        msleep(1);
                }
        }
 
+endLoop:
        if(connectionScore < MIN_CONNSCORE)
        {
                log("", "Connectivity test has failed: Internet connection appears to be broken!");
@@ -387,27 +415,37 @@ void UpdateChecker::checkForUpdates(void)
        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.ref(); /*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)
+       if(MUTILS_BOOLIFY(m_success))
        {
                if(m_updateInfo->m_buildNo > m_installedBuildNo)
                {
@@ -483,14 +521,14 @@ void UpdateChecker::log(const QString &str1, const QString &str2, const QString
        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, "");
 
-       if (!tryContactHost(QUrl(url).host(), MAX_CONN_TIMEOUT))
+       if (!tryContactHost(QUrl(url).host(), quick ? (MAX_CONN_TIMEOUT / 10) : MAX_CONN_TIMEOUT))
        {
-               log("", "Mirror is unreachable!");
+               log("", quick ? "Mirror is too slow!" :"Mirror is unreachable!");
                return false;
        }
 
@@ -503,7 +541,7 @@ bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString
                log("", "Download okay, checking signature:");
                if (checkSignature(outFileVers, outFileSign))
                {
-                       log("", "Signature okay, parsing info:");
+                       log("", "Signature okay, parsing info:", "");
                        success = parseVersionInfo(outFileVers, updateInfo);
                }
                else
@@ -524,19 +562,19 @@ bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString
 
 bool UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
 {
-       log("Downloading update info:");
-       if(!getFile(QString("%1%2"     ).arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
+       log("Downloading update info:", "");
+       if(getFile(QString("%1%2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
        {
-               return false;
-       }
-
-       log("", "Downloading signature:");
-       if(!getFile(QString("%1%2.sig2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileSign))
-       {
-               return false;
+               if (!m_cancelled)
+               {
+                       log("", "Downloading signature:", "");
+                       if (getFile(QString("%1%2.sig2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileSign))
+                       {
+                               return true;
+                       }
+               }
        }
-
-       return true;
+       return false;
 }
 
 bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo)
@@ -654,6 +692,10 @@ bool UpdateChecker::getFile(const QString &url, const QString &outFile, const un
                {
                        return true;
                }
+               if (MUTILS_BOOLIFY(m_cancelled))
+               {
+                       break; /*cancelled*/
+               }
        }
        return false;
 }
@@ -715,12 +757,12 @@ bool UpdateChecker::getFile(const QString &url, const bool forceIp4, const QStri
                        const QString line = QString::fromLatin1(process.readLine()).simplified();
                        log(line);
                }
-               if (bTimeOut)
+               if (bTimeOut || MUTILS_BOOLIFY(m_cancelled))
                {
                        qWarning("WGet process timed out <-- killing!");
                        process.kill();
                        process.waitForFinished();
-                       log("!!! TIMEOUT !!!");
+                       log(bTimeOut ? "!!! TIMEOUT !!!": "!!! CANCELLED !!!");
                        return false;
                }
        }
@@ -734,13 +776,13 @@ bool UpdateChecker::getFile(const QString &url, const bool forceIp4, const QStri
 
 bool UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
 {
-       log(QString("Connecting to host: %1").arg(hostname));
+       log(QString("Connecting to host: %1").arg(hostname), "");
 
        QProcess process;
        init_process(process, temp_folder());
 
        QStringList args;
-       args << "-z" << hostname << QString::number(80);
+       args << "--retry" << QString::number(3) << hostname << QString::number(80);
 
        QEventLoop loop;
        connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
@@ -751,7 +793,7 @@ bool UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMs
        timer.setSingleShot(true);
        connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
 
-       process.start(m_binaryNC, args);
+       process.start(m_binaryMCat, args);
 
        if (!process.waitForStarted())
        {
@@ -769,12 +811,12 @@ bool UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMs
                        QString line = QString::fromLatin1(process.readLine()).simplified();
                        log(line);
                }
-               if (bTimeOut)
+               if (bTimeOut || MUTILS_BOOLIFY(m_cancelled))
                {
-                       qWarning("NC process timed out <-- killing!");
+                       qWarning("MCat process timed out <-- killing!");
                        process.kill();
                        process.waitForFinished();
-                       log("!!! TIMEOUT !!!");
+                       log(bTimeOut ? "!!! TIMEOUT !!!" : "!!! CANCELLED !!!");
                        return false;
                }
        }