aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/DownloadManager.h
blob: 08cd7d86b2d1e20e6c78c08e2c0155bee292b4d2 (plain)
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
#pragma once

#include <string>
#include <thread>
#include <vector>

/** @brief manage threaded downloads */
class DownloadManager {
private:
	const unsigned max_files = 50;

	/** @brief download thread list */
	std::vector<std::thread*> download_queue = {};
	/** @brief fstream pointer list for limiting amount of files open at once */
	std::vector<std::fstream*> file_queue = {};

	/**
	 * @brief open file in DownloadManager (THREAD-ONLY)
	 * 
	 * this function opens a file, but blocks until a slot is available, so
	 * should only be used in threads
	 */
	std::fstream* open_file(std::string filename);
	/**
	 * @brief close file in DownloadManager (THREAD-ONLY)
	 *
	 * this function closes a file and removes it's pointer from the file_queue
	 * vector
	 */
	void close_file(std::fstream* file_ptr);

public:
	DownloadManager();
	virtual ~DownloadManager();

	/** @brief downlaod `url` to `file` and close `file` when done */
	virtual std::thread* wget(std::string url, std::string filename);
};