summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/DownloadClient.cpp29
-rw-r--r--client/DownloadClient.h4
-rw-r--r--client/FileOpThread.cpp18
-rw-r--r--client/ProxyClientApp.h5
-rw-r--r--client/SslClient.cpp17
-rw-r--r--client/Utility.cpp29
-rw-r--r--client/Utility.h14
-rw-r--r--client/client.pro6
8 files changed, 103 insertions, 19 deletions
diff --git a/client/DownloadClient.cpp b/client/DownloadClient.cpp
index 3fe7b59..723dbcb 100644
--- a/client/DownloadClient.cpp
+++ b/client/DownloadClient.cpp
@@ -1,6 +1,7 @@
#include <QtCore>
#include "DownloadClient.h"
+#include "Utility.h"
DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId(-1)
@@ -9,7 +10,6 @@ DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId
DownloadClient::DownloadClient(QString addr): SslClient(addr), isDownloading(false), current(NULL), currentId(-1)
{
- DownloadClient();
}
void DownloadClient::Download(const string &filename)
@@ -73,7 +73,6 @@ void DownloadClient::DoDownload()
void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &data)
{
Logger::Trace("Recieved file data: %u bytes\n", data.size());
-
if (type != RegularFile)
{
Logger::Error("Invalid reply recieved: %x\n", type);
@@ -85,8 +84,6 @@ void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &
return;
}
- pair<string, string> file = files[currentId];
- string filename = file.first;
if (data.size() != 0)
{
current->write(data);
@@ -94,15 +91,27 @@ void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &
}
else
{
- Logger::Trace("Last (empty) packet recieved\n");
+ Logger::Trace("Last (empty) packet recieved\n");
current->close();
+
+ // check file's md5 hash value
+ pair<string, string> file = files[currentId];
+ QString md5string = QString::fromLocal8Bit(file.second.c_str());
+ QString filename = QString::fromLocal8Bit(file.first.c_str());
+ QByteArray md5 = md5_sum(filename);
+ if (md5 == md5string)
+ {
+ Logger::Trace("File %s was downloaded correctly!\n", filename.toStdString().c_str());
+ }
+ else
+ {
+ 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;
current = NULL;
-
- /**
- * @todo md5 hash check
- */
-
// proceed to the next file
DoDownload();
}
diff --git a/client/DownloadClient.h b/client/DownloadClient.h
index 0e6c510..8af8131 100644
--- a/client/DownloadClient.h
+++ b/client/DownloadClient.h
@@ -45,9 +45,7 @@ public:
/**
* @brief Download list of files
* @param files associative array of file names and it's md5 hashes
- * @note this is designated method among all
- * @todo It seems that std::map isn't best choice for this purpose
- * it's quite unsuitable to iterate through(
+ * @note this is designated method among all of three
*/
void Download(const vector<pair<string, string> > &files);
diff --git a/client/FileOpThread.cpp b/client/FileOpThread.cpp
index 3340db2..bd0b929 100644
--- a/client/FileOpThread.cpp
+++ b/client/FileOpThread.cpp
@@ -3,6 +3,7 @@
#include "DownloadClient.h"
#include "FileOpThread.h"
#include "UpdatedConfig.h"
+#include "Utility.h"
FileOpThread::~FileOpThread()
@@ -44,9 +45,20 @@ void FileOpThread::run()
if (fileInfo.exists())
{
Logger::Info("File \"%s\" already exists\n", downloadList[i].path.c_str());
- /**
- * @todo Compute md5 hash and check if they are the same
- */
+ QString filename = QString::fromLocal8Bit(downloadList[i].path.c_str());
+ QString md5string = QString::fromLocal8Bit(downloadList[i].md5.c_str());
+ QByteArray md5 = md5_sum(filename);
+
+ if (md5 == md5string)
+ {
+ Logger::Trace("File %s is correct!\n", filename.toStdString().c_str());
+ }
+ else
+ {
+ Logger::Error("File %s is incorrect or was updated. Need to download again!\n", filename.toStdString().c_str());
+ pair<string, string> file = make_pair(downloadList[i].path, downloadList[i].md5);
+ files.push_back(file);
+ }
}
else
{
diff --git a/client/ProxyClientApp.h b/client/ProxyClientApp.h
index b015344..3901adb 100644
--- a/client/ProxyClientApp.h
+++ b/client/ProxyClientApp.h
@@ -14,8 +14,11 @@ class ProxyClientApp: public QApplication
Q_OBJECT
public:
+ /**
+ * @brief create an instance of ProxyClientApp
+ */
ProxyClientApp(int &argc, char *argv[]);
-
+
private slots:
void trayActivated(QSystemTrayIcon::ActivationReason reason);
void showProxyDialog();
diff --git a/client/SslClient.cpp b/client/SslClient.cpp
index 2a599c8..9a39753 100644
--- a/client/SslClient.cpp
+++ b/client/SslClient.cpp
@@ -185,6 +185,23 @@ void SslClient::DataRecieved()
return;
}
+/*
+ fprintf(stderr, "[ ");
+ const char * tmp = data.constData();
+ for (int i = 0; i < data.size(); i++)
+ {
+ if (tmp[i] < 0x20)
+ {
+ fprintf(stderr, "0x%X ", tmp[i]);
+ }
+ else
+ {
+ fprintf(stderr, "%c ", tmp[i]);
+ }
+ }
+ fprintf(stderr, "]");
+*/
+
/* remove header and tail */
data.remove(data.size()-3, 3);
data.remove(0, 3);
diff --git a/client/Utility.cpp b/client/Utility.cpp
new file mode 100644
index 0000000..2faed74
--- /dev/null
+++ b/client/Utility.cpp
@@ -0,0 +1,29 @@
+
+#include <QCryptographicHash>
+#include "client.h"
+#include "Logger.h"
+#include "Utility.h"
+
+
+QByteArray md5_sum(QString &filename)
+{
+ QFile file(filename);
+ if (! file.exists())
+ {
+ Logger::Warn("Can't compute md5sum: file %s do not exist\n", filename.toStdString().c_str());
+ return NULL;
+ }
+
+ file.open(QIODevice::ReadOnly);
+ QCryptographicHash md5hash(QCryptographicHash::Md5);
+ while (! file.atEnd())
+ {
+ const size_t buf_sz = 4096;
+ md5hash.addData(file.read(buf_sz));
+ }
+ file.close();
+
+ QByteArray res = md5hash.result().toHex();
+ Logger::Debug("MD5 sum for %s is: %s\n", filename.toStdString().c_str(), res.data());
+ return res;
+}
diff --git a/client/Utility.h b/client/Utility.h
new file mode 100644
index 0000000..fef395b
--- /dev/null
+++ b/client/Utility.h
@@ -0,0 +1,14 @@
+
+
+#ifndef UTILITY_H
+#define UTILITY_H
+
+#include <QFile>
+#include <QString>
+
+/**
+ * @brief Calculate file's md5 sum value
+ */
+QByteArray md5_sum(QString &filename);
+
+#endif \ No newline at end of file
diff --git a/client/client.pro b/client/client.pro
index 6520c5d..ca6abf8 100644
--- a/client/client.pro
+++ b/client/client.pro
@@ -22,7 +22,8 @@ HEADERS += client.h \
UpdatedConfig.h \
SslClient.h \
FileOpThread.h \
- DownloadClient.h
+ DownloadClient.h \
+ Utility.h
SOURCES += Dialog.cpp \
main.cpp \
@@ -33,7 +34,8 @@ SOURCES += Dialog.cpp \
UpdatedConfig.cpp \
SslClient.cpp \
FileOpThread.cpp \
- DownloadClient.cpp
+ DownloadClient.cpp \
+ Utility.cpp
OTHER_FILES +=