-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydownload.cpp
108 lines (95 loc) · 2.67 KB
/
mydownload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// MyDownload.cpp: implementation of the MyDownload class.
//
//////////////////////////////////////////////////////////////////////
#include <afxsock.h>
#include "helper.h"
#include "Mydownload.h"
#include <io.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MyDownload::MyDownload()
{
onFinish = NULL;
}
MyDownload::~MyDownload()
{
}
//add download task, take the URL as identifier
bool MyDownload::addDownloadTask(const char* remoteUrl, const char* localFolder)
{
string remoteUrlString(remoteUrl);
string localFolderString(localFolder);
if (!existInVector(downloadListRemoteURLs, remoteUrlString)) {
downloadListRemoteURLs.push_back(remoteUrlString);
downloadListLocalFolders.push_back(localFolderString);
return true;
}
else {
return false;
}
}
//check whether the task exists.
bool existInVector(vector<string>& array, string& str) {
for (int k = 0; k<array.size(); k++) {
if (array[k].compare(str) == 0)
return true;
}
return false;
}
//start download
bool MyDownload::startDownload()
{
this->start();
return true;
}
//refactoring multithread downloading
void * MyDownload::run(void *)
{
unsigned long temp = 0;
unsigned long *downloaded = &temp;
unsigned long totalSize = 1024;
bool ret;
while (downloadListRemoteURLs.size()>0)
{
cout << downloadListRemoteURLs[0] << endl;
//default thread number is 3. The number can be changed but must be consistent.
while (true) {
//loop until the task is finished.
ret = DownloadHelper(downloadListRemoteURLs[0].data(),
downloadListLocalFolders[0].data(), downloaded, totalSize, THREAD_COUNT);
if (ret) {
cout << "True" << endl;
}
if (!exist(0)) {
//file doesn't exist.
cout << "Task failed,reconnecting..." << endl;
Sleep(RECONNECT_INTERVAL); //reconnecting after 10 seconds.
}
else {
//download finished. delete the first task.
vector<string>::iterator startIterator = downloadListRemoteURLs.begin();
downloadListRemoteURLs.erase(startIterator);
startIterator = downloadListLocalFolders.begin();
downloadListLocalFolders.erase(startIterator);
break;
}
}
}
if (onFinish != NULL) {
onFinish();
}
return NULL;
}
//check if the file is already in the list.
bool MyDownload::exist(int index)
{
string fileName = downloadListRemoteURLs[index].substr(downloadListRemoteURLs[index].find_last_of("/") + 1);
string file(downloadListLocalFolders[index].data());
file.append('\\' + fileName);
return (_access(file.data(), 0) == 0);;
}
//function pointer fired upon task finish
void MyDownload::setOnFinish(void(*func)()) {
onFinish = func;
}