-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadHandler.cpp
144 lines (122 loc) · 4.66 KB
/
DownloadHandler.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
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
//
// Created by warnelso on 6/6/16.
//
#include <iostream>
#include <regex>
#include "DownloadHandler.h"
#include "DownloadHash.h"
DownloadHandler::DownloadHandler(Filename filename) {
add_download(filename);
_current_download = nullptr;
}
DownloadHandler::DownloadHandler(std::list<Filename> files) {
for (auto &filename : files) {
add_download(filename);
}
_current_download = nullptr;
}
DownloadHandler::~DownloadHandler() {
for (auto &element: _completed_downloads) { // delete all completed downloads
delete element.second;
}
if (currently_downloading()) {
delete_current_download(); // delete incomplete download
}
}
bool DownloadHandler::add_download(const Filename &filename) {
if (downloaded(filename) // if downloaded
|| in_download_queue(filename) // or in queue
|| currently_downloading(filename))
return false; // or currently being downloaded
else {
_download_queue.push_back(filename);
std::cout << "File added to download queue: " << filename << "\n";
return true;
}
}
void DownloadHandler::add_downloads_from_config() {
if (!downloaded("pxelinux.cfg")) throw not_found_exception();
Download *file = _completed_downloads["pxelinux.cfg"];
std::string filestr((char *) file->data(), file->size());
std::smatch matches;
std::regex regex("(kernel [^ \\n\\r]*|initrd=[^ \\n\\r]*)");
while (std::regex_search(filestr, matches, regex)) {
add_download(matches.begin()->str().substr(7));
filestr = matches.suffix().str();
}
}
void DownloadHandler::rename_current_downlaod(const Filename &new_filename) {
if (!currently_downloading()) throw not_found_exception();
_current_download->first = new_filename;
_current_download->second->filename(new_filename);
}
void DownloadHandler::append_data(const Data &data, uint16_t block_number) {
if (currently_downloading()) {
_current_download->second->add_data(data, block_number);
}
}
void DownloadHandler::set_current_download_sizes(uint32_t total_size) {
if (currently_downloading()) {
_current_download->second->total_size(total_size);
}
}
void DownloadHandler::finalize_current_download() {
if (!currently_downloading()) throw not_found_exception();
else if (!downloaded(_current_download->first)) {
_current_download->second->finalize();
std::cout << "Finalized File Data: \n" <<
std::string((const char *) _current_download->second->data(), _current_download->second->size()) << "\n";
_completed_downloads[_current_download->first] = _current_download->second;
_current_download->second = nullptr;
delete _current_download;
_current_download = nullptr;
} else throw overwrite_exception();
}
void DownloadHandler::delete_current_download() {
if (!currently_downloading()) throw not_found_exception();
else {
delete _current_download->second;
_current_download->second = nullptr;
delete _current_download;
_current_download = nullptr;
}
}
void DownloadHandler::clear_queue() {
_download_queue.clear();
}
const Filename DownloadHandler::start_new_download_file() {
if (!currently_downloading()) {
Filename filename = _download_queue.front();
_download_queue.pop_front();
_current_download = new std::pair<Filename, Download *>(filename, new DownloadFile(filename));
return filename;
} else throw not_found_exception();
}
const Filename DownloadHandler::start_new_download_hash() {
if (!currently_downloading()) {
Filename filename = _download_queue.front();
_download_queue.pop_front();
_current_download = new std::pair<Filename, Download *>(filename, new DownloadHash(filename));
return filename;
} else throw not_found_exception();
}
bool DownloadHandler::downloaded(const Filename &filename) const {
return (bool) _completed_downloads.count(filename);
}
bool DownloadHandler::in_download_queue(const Filename &filename) const {
return std::find(_download_queue.begin(), _download_queue.end(), filename) != _download_queue.end();
}
bool DownloadHandler::currently_downloading(const Filename &filename) const {
return _current_download != nullptr && _current_download->first == filename;
}
bool DownloadHandler::currently_downloading() const {
return _current_download != nullptr;
}
bool DownloadHandler::complete() const {
return (!_current_download && _download_queue.empty());
}
bool DownloadHandler::current_download_complete() const {
if (currently_downloading()) {
return _current_download->second->complete();
} else throw not_found_exception();
}