-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
235 lines (208 loc) · 7.23 KB
/
main.cpp
File metadata and controls
235 lines (208 loc) · 7.23 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <vector>
#include "DownloadManager.hpp"
#include "DownloadTask.hpp"
#include "FileWritter.hpp"
#include "TaskQueue.hpp"
#include "ThreadPool.hpp"
#include <curl/curl.h>
#include <thread>
#include <algorithm>
class DownloadApplication {
public:
DownloadApplication(): stopFlag(false), manager(5) {
curl_global_init(CURL_GLOBAL_DEFAULT);
// Run the cleanup thread in background
t2 = std::thread(&DownloadApplication::usualCleanup, this);
}
~DownloadApplication() {
stopFlag = true;
if (t1.joinable()) {
t1.join();
}
if (t2.joinable()) {
t2.join();
}
manager.waitForCompletion();
curl_global_cleanup();
}
void startAllDownloads() {
if (t1.joinable()) {
t1.join();
}
t1 = std::thread([this](){
manager.startDownloads();
});
}
void startDownload() {
int urlNo;
if (t1.joinable()) {
t1.join();
}
showDownloadList();
std::cout << "Enter the url number to start: ";
std::cin >> urlNo;
if(urlNo > filesToDownload.size() || urlNo < 1)
{
std::cout << "Invalid url number\n";
return;
}
t1 = std::thread([this, urlNo](){
manager.startDownload(filesToDownload[urlNo-1]);
});
}
void pauseDownload() {
int urlNo;
showDownloadList();
std::cout << "Enter the url number to pause: ";
std::cin >> urlNo;
if(urlNo > filesToDownload.size() || urlNo < 1)
{
std::cout << "Invalid url number\n";
return;
}
manager.pauseDownload(filesToDownload[urlNo-1]);
}
void resumeDownload() {
int urlNo;
showDownloadList();
std::cout << "Enter the url number to resume: ";
std::cin >> urlNo;
if(urlNo > filesToDownload.size() || urlNo < 1)
{
std::cout << "Invalid url number\n";
return;
}
manager.resumeDownload(filesToDownload[urlNo-1]);
}
void cancelDownload() {
int urlNo;
showDownloadList();
std::cout << "Enter the url number to cancel: ";
std::cin >> urlNo;
if(urlNo > filesToDownload.size() || urlNo < 1)
{
std::cout << "Invalid url number\n";
return;
}
manager.cancelDownload(filesToDownload[urlNo-1]);
}
void removeDownload() {
int urlNo;
std::vector<std::string>::iterator it;
showDownloadList();
std::cout << "Enter the url number to remove: ";
std::cin >> urlNo;
it = std::find(filesToDownload.begin(), filesToDownload.end(), filesToDownload[urlNo-1]);
// If element is found, erase it
if (it != filesToDownload.end()) {
filesToDownload.erase(it);
}else {
std::cout << "Url not found in download list\n";
}
}
void addFilesToDownload(std::string url) {
filesToDownload.push_back(url);
manager.addDownload(url, url.substr(url.find_last_of('/') + 1));
}
void CLITest() {
std::string url;
while(1) {
printMenu();
int option;
std::cin >> option;
switch(option) {
case 1:
std::cout << "Enter the url to add: ";
std::cin >> url;
addFilesToDownload(url);
showDownloadList();
break;
case 2:
removeDownload();
break;
case 3:
startAllDownloads();
break;
case 4:
startDownload();
break;
case 5:
pauseDownload();
break;
case 6:
resumeDownload();
break;
case 7:
cancelDownload();
break;
case 8:
stopFlag = true;
return;
default:
std::cout << "Invalid option. Exiting...\n";
stopFlag = true;
return;
}
}
}
private:
DownloadManager manager; // Start with 5 threads in the thread pool
std::vector<std::string> filesToDownload;
std::thread t1, t2;
bool stopFlag;
std::string downloadStatusToString(DownloadStatus status) {
switch (status) {
case DownloadStatus::Starting:
return "Starting";
case DownloadStatus::Pending:
return "Pending";
case DownloadStatus::Downloading:
return "Downloading";
case DownloadStatus::Paused:
return "Paused";
case DownloadStatus::Completed:
return "Completed";
case DownloadStatus::Failed:
return "Failed";
// Add other cases as needed
default:
return "Unknown";
}
}
void showDownloadList() {
int i = 1;
for (const auto& url : filesToDownload) {
if(manager.getDownloadStatus(url) == DownloadStatus::Completed)
{
// Remove the completed downloads from the list
filesToDownload.erase(std::remove(filesToDownload.begin(), filesToDownload.end(), url), filesToDownload.end());
}
}
for (const auto& url : filesToDownload) {
std::cout << i++ << ". " << url << " "<<downloadStatusToString(manager.getDownloadStatus(url))<<"\n";
}
}
void usualCleanup() {
while(!stopFlag) {
//clear the downloaded or faliled tasks
manager.clearTasks();
}
}
void printMenu() {
std::cout << "1. Add file to download list\n";
std::cout << "2. Remove file from download list\n";
std::cout << "3. Start all downloads\n";
std::cout << "4. Start a download\n";
std::cout << "5. Pause a download\n";
std::cout << "6. Resume a download\n";
std::cout << "7. Cancel a download\n";
std::cout << "8. Exit\n";
std::cout << "Enter your choice: ";
}
};
int main() {
DownloadApplication app;
app.CLITest();
return 0;
}