diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-09-04 12:29:59 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-09-04 12:29:59 +0300 |
commit | 6d8b331a37a025f4a16d8278da715e7a628b33d3 (patch) | |
tree | 11b7880b2afe9575f99a0a9eeb2ba97b8e6c88b9 | |
parent | 1052c8bd510b92827a61aba725083c9feb24e042 (diff) | |
parent | ad1559db52cf01869c0fb31252ad9f69af2ddd4c (diff) |
Merge branch 'master' of ssh://sss.chaoslab.ru//home/git/proxy_ui
-rw-r--r-- | client/Dialog.cpp | 2 | ||||
-rw-r--r-- | client/DownloadClient.cpp | 45 | ||||
-rw-r--r-- | client/DownloadClient.h | 17 | ||||
-rw-r--r-- | client/Proxifier.cpp | 16 | ||||
-rw-r--r-- | client/Proxifier.h | 4 | ||||
-rw-r--r-- | client/main.cpp | 6 |
6 files changed, 63 insertions, 27 deletions
diff --git a/client/Dialog.cpp b/client/Dialog.cpp index be04781..b3c67c1 100644 --- a/client/Dialog.cpp +++ b/client/Dialog.cpp @@ -261,7 +261,7 @@ void ProxyDialog::StaticProxyToggled(bool on) proxifier->TurnProxyOff(active[i]); } } - proxifier->TurnProxyOn(*proxy); + proxifier->TurnProxyOn(*proxy, true); } void ProxyDialog::CountryActivated(int index) diff --git a/client/DownloadClient.cpp b/client/DownloadClient.cpp index d25e6dc..c94109a 100644 --- a/client/DownloadClient.cpp +++ b/client/DownloadClient.cpp @@ -23,13 +23,14 @@ DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId { } -DownloadClient::DownloadClient(QString addr): SslClient(addr), isDownloading(false), current(NULL), currentId(-1) +DownloadClient::DownloadClient(QString addr): + SslClient(addr), isDownloading(false), current(NULL), currentId(-1) { } void DownloadClient::Download(const string &filename) { - pair<string, string> file(filename, 0); + pair<string, string> file(filename, "0"); vector<pair<string, string> > fileList; fileList.push_back(file); Download(fileList); @@ -57,18 +58,31 @@ void DownloadClient::Download(const vector<pair<string, string> > &fileList) connect(this, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)), this, SLOT(FileDataRecieved(SslClient::RequestType&, QByteArray&))); - isDownloading = true; + + isDownloading = true; files = fileList; + for(unsigned i = 0; i < files.size(); i++) + { + status[files[i].first] = 0; + } DoDownload(); } void DownloadClient::DoDownload() { currentId++; - + if (currentId < (int)files.size()) { - pair<string, string> file = files[currentId]; + pair<string, string> file = files[currentId]; + if (status[file.first] > MAX_DOWNLOAD_ATTEMPTS) + { + Logger::Warn("Maximum number of %d download retries exceeded. Skipping %s\n", + MAX_DOWNLOAD_ATTEMPTS, file.first.c_str()); + DoDownload(); + return; + } + QString filename = QString::fromLocal8Bit(file.first.c_str()); string md5 = file.second; current = new QFile(filename); @@ -76,14 +90,16 @@ void DownloadClient::DoDownload() { Logger::Info("Can't open file: path isn't available\n"); DoDownload(); + return; } Logger::Info("Downloading file (%d of %u): %s\n", currentId + 1, files.size(), file.first.c_str()); - SendFileRequest(file.first); + status[file.first]++; + SendFileRequest(file.first); } else { - Logger::Info("All files have been downloaded.\n"); + Logger::Info("All files have been downloaded.\n"); isDownloading = false; emit downloadFinished(); } @@ -118,15 +134,20 @@ void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray & QString md5string = QString::fromLocal8Bit(file.second.c_str()); QString filename = QString::fromLocal8Bit(file.first.c_str()); QByteArray md5 = md5_sum(filename); - if (md5 == md5string) - { + if (md5 == md5string) + { Logger::Trace("File %s was downloaded correctly!\n", filename.toStdString().c_str()); + if (QString::compare(filename, QString("client.bin.tmp")) == 0) + { + Logger::Trace("Copying client.bin.tmp => client.bin.latest\n"); + current->copy(QString("client.bin.latest")); + } } else { - Logger::Error("File %s was downloaded incorrectly! Removing and trying again!\n", filename.toStdString().c_str()); - current->remove(); - currentId--; + Logger::Error("File %s was downloaded incorrectly! Removing and trying again!\n", filename.toStdString().c_str()); + current->remove(); + currentId--; } // free pointer to the current file delete current; diff --git a/client/DownloadClient.h b/client/DownloadClient.h index 229074f..8488d47 100644 --- a/client/DownloadClient.h +++ b/client/DownloadClient.h @@ -23,6 +23,7 @@ #include <QObject> #include "SslClient.h" +using std::map; using std::pair; using std::string; using std::vector; @@ -70,7 +71,15 @@ signals: */ void downloadFinished(); +private slots: + void FileDataRecieved(SslClient::RequestType &type, QByteArray &confdata); + private: + /** + * @brief maximum number of retries when downloading files + */ + static const int MAX_DOWNLOAD_ATTEMPTS = 3; + /** * @brief indicates that program is downloading file(s) right now */ @@ -86,6 +95,11 @@ private: */ int currentId; + /** + * @brief number of failed download attempt per single file + */ + map<string, int> status; + /** * @brief List of file to */ @@ -95,9 +109,6 @@ private: * @brief start file-by-file downloading process */ void DoDownload(); - -private slots: - void FileDataRecieved(SslClient::RequestType &type, QByteArray &confdata); }; #endif diff --git a/client/Proxifier.cpp b/client/Proxifier.cpp index 8ce4fd5..dc134c0 100644 --- a/client/Proxifier.cpp +++ b/client/Proxifier.cpp @@ -267,7 +267,7 @@ vector<Proxy> Proxifier::GetActiveProxies() return result; } -bool Proxifier::TurnProxyOn(Proxy& proxy) +bool Proxifier::TurnProxyOn(Proxy& proxy, bool isStatic) { if (!valid) { @@ -317,15 +317,13 @@ bool Proxifier::TurnProxyOn(Proxy& proxy) { // proxy is not enabled - remove it and add again RemoveProxyFromChain(proxyId, chainId); - bool isHttp = (stricmp(proxy.type.c_str(), "http") == 0); - AddProxyToChain(proxyId, chainId, isHttp); + AddProxyToChain(proxyId, chainId, isStatic); } } else { // proxy not found in the target chain - add it - bool isHttp = (stricmp(proxy.type.c_str(), "http") == 0); - AddProxyToChain(proxyId, chainId, isHttp); + AddProxyToChain(proxyId, chainId, isStatic); } } @@ -954,7 +952,7 @@ bool Proxifier::AddChain(string& name, int *id) return true; } -bool Proxifier::AddProxyToChain(int proxyId, int chainId, bool isHttp) +bool Proxifier::AddProxyToChain(int proxyId, int chainId, bool isStatic) { Logger::Trace("Adding proxy to chain in Proxifier config\n"); // read XML DOM structure @@ -1013,14 +1011,14 @@ bool Proxifier::AddProxyToChain(int proxyId, int chainId, bool isHttp) QDomText nameTxt = configDom.createTextNode(idTxt); proxyElem.appendChild(nameTxt); // append Http proxies to then end, but prepend other ones - if (isHttp) + if (isStatic) { - chainElem.appendChild(proxyElem); + chainElem.insertAfter(proxyElem, chainElem.firstChildElement()); } else { // first child is 'Name' - chainElem.insertAfter(proxyElem, chainElem.firstChildElement()); + chainElem.appendChild(proxyElem); } break; } diff --git a/client/Proxifier.h b/client/Proxifier.h index 30aa0ce..5d558fd 100644 --- a/client/Proxifier.h +++ b/client/Proxifier.h @@ -72,7 +72,7 @@ public: * @note adds specified proxy to Proxifier's Proxy list and in "Client" chain<br/> * Also adds "Client" chain to "Default" rule if its not there */ - bool TurnProxyOn(Proxy& proxy); + bool TurnProxyOn(Proxy& proxy, bool isStatic = false); /** * @brief turn proxy off * @param proxy Proxy class that represents particular proxy @@ -249,7 +249,7 @@ private: * chainId chain ID to add proxy to * @return true on success or false otherwise */ - bool AddProxyToChain(int proxyId, int chainId, bool isHttp = false); + bool AddProxyToChain(int proxyId, int chainId, bool isStatic = false); /** * @brief remove proxy from proxy chain * @param proxyId proxy ID to remove to proxy chain diff --git a/client/main.cpp b/client/main.cpp index 0d0e1db..badd31c 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -74,6 +74,12 @@ int main(int argc, char *argv[]) Logger::Error("Can't launch updater to update client app\n"); } } + else + { + QFile* newClientFile = new QFile(newClient.fileName()); + newClientFile->remove(); + delete newClientFile; + } } // check if initial config exists (config.cfg) // without it application is useless |