mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-03 10:09:40 -06:00
Multithreading the downloadrequests
This commit is contained in:
@@ -131,6 +131,8 @@ SyncWidget::SyncWidget(QWidget* parent, Qt::WindowFlags f)
|
||||
QTimer* timer = new QTimer(this);
|
||||
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(handleTimer()));
|
||||
timer->start(100);
|
||||
|
||||
_mutex.clear();
|
||||
}
|
||||
|
||||
SyncWidget::~SyncWidget() {
|
||||
@@ -205,23 +207,30 @@ void SyncWidget::handleFileRequest() {
|
||||
std::string identifier = f.identifier.toStdString();
|
||||
std::string path = absPath("${SCENE}/" + f.module.toStdString() + "/" + f.destination.toStdString());
|
||||
int version = f.version;
|
||||
std::vector<openspace::DownloadManager::FileFuture*> futures =
|
||||
DlManager.downloadRequestFiles(
|
||||
|
||||
DlManager.downloadRequestFilesAsync(
|
||||
identifier,
|
||||
path,
|
||||
version,
|
||||
OverwriteFiles
|
||||
);
|
||||
OverwriteFiles,
|
||||
std::bind(&SyncWidget::handleFileFutureAddition, this, std::placeholders::_1)
|
||||
);
|
||||
|
||||
_futures.insert(_futures.end(), futures.begin(), futures.end());
|
||||
for (openspace::DownloadManager::FileFuture* f : futures) {
|
||||
InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1);
|
||||
_downloadLayout->addWidget(w);
|
||||
//std::vector<openspace::DownloadManager::FileFuture*> futures =
|
||||
// DlManager.downloadRequestFiles(
|
||||
// identifier,
|
||||
// path,
|
||||
// version,
|
||||
// OverwriteFiles
|
||||
// );
|
||||
|
||||
_futureInfoWidgetMap[f] = w;
|
||||
}
|
||||
//_futures.insert(_futures.end(), futures.begin(), futures.end());
|
||||
//for (openspace::DownloadManager::FileFuture* f : futures) {
|
||||
// InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1);
|
||||
// _downloadLayout->addWidget(w);
|
||||
|
||||
qApp->processEvents();
|
||||
// _futureInfoWidgetMap[f] = w;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,7 +280,6 @@ void SyncWidget::handleTorrentFiles() {
|
||||
_torrentInfoWidgetMap[h] = w;
|
||||
|
||||
FileSys.setCurrentDirectory(d);
|
||||
qApp->processEvents();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,6 +502,18 @@ void SyncWidget::handleTimer() {
|
||||
delete f;
|
||||
}
|
||||
|
||||
while (_mutex.test_and_set()) {}
|
||||
for (openspace::DownloadManager::FileFuture* f : _futuresToAdd) {
|
||||
InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1);
|
||||
_downloadLayout->addWidget(w);
|
||||
|
||||
_futureInfoWidgetMap[f] = w;
|
||||
_futures.push_back(f);
|
||||
}
|
||||
_futuresToAdd.clear();
|
||||
_mutex.clear();
|
||||
|
||||
|
||||
std::vector<torrent_handle> handles = _session->get_torrents();
|
||||
for (torrent_handle h : handles) {
|
||||
torrent_status s = h.status();
|
||||
@@ -570,3 +590,19 @@ void SyncWidget::handleTimer() {
|
||||
|
||||
// qDebug() << "";
|
||||
}
|
||||
|
||||
void SyncWidget::handleFileFutureAddition(
|
||||
const std::vector<openspace::DownloadManager::FileFuture*>& futures)
|
||||
{
|
||||
while (_mutex.test_and_set()) {}
|
||||
_futuresToAdd.insert(_futuresToAdd.end(), futures.begin(), futures.end());
|
||||
_mutex.clear();
|
||||
//_futures.insert(_futures.end(), futures.begin(), futures.end());
|
||||
//for (openspace::DownloadManager::FileFuture* f : futures) {
|
||||
// InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1);
|
||||
// _downloadLayout->addWidget(w);
|
||||
|
||||
// _futureInfoWidgetMap[f] = w;
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
|
||||
#include <libtorrent/torrent_handle.hpp>
|
||||
|
||||
#include <atomic>
|
||||
//#include <thread>
|
||||
|
||||
class QBoxLayout;
|
||||
@@ -80,6 +81,8 @@ private:
|
||||
void clear();
|
||||
QStringList selectedScenes() const;
|
||||
|
||||
void handleFileFutureAddition(const std::vector<openspace::DownloadManager::FileFuture*>& futures);
|
||||
|
||||
void handleDirectFiles();
|
||||
void handleFileRequest();
|
||||
void handleTorrentFiles();
|
||||
@@ -98,6 +101,9 @@ private:
|
||||
|
||||
std::vector<openspace::DownloadManager::FileFuture*> _futures;
|
||||
std::map<openspace::DownloadManager::FileFuture*, InfoWidget*> _futureInfoWidgetMap;
|
||||
|
||||
std::vector<openspace::DownloadManager::FileFuture*> _futuresToAdd;
|
||||
std::atomic_flag _mutex;
|
||||
};
|
||||
|
||||
#endif // __SYNCWIDGET_H__
|
||||
|
||||
@@ -58,10 +58,12 @@ public:
|
||||
|
||||
typedef std::function<void(const FileFuture&)> DownloadProgressCallback;
|
||||
typedef std::function<void(const FileFuture&)> DownloadFinishedCallback;
|
||||
typedef std::function<void(const std::vector<FileFuture*>&)> AsyncDownloadFinishedCallback;
|
||||
|
||||
DownloadManager(std::string requestURL, int applicationVersion);
|
||||
|
||||
// callers responsibility to delete
|
||||
// callbacks happen on a different thread
|
||||
FileFuture* downloadFile(
|
||||
const std::string& url,
|
||||
const ghoul::filesystem::File& file,
|
||||
@@ -82,6 +84,14 @@ public:
|
||||
// TODO: Add async version of downloadRequestFiles that returns a filefuture
|
||||
// that can be used to call an additional function
|
||||
|
||||
void downloadRequestFilesAsync(
|
||||
const std::string& identifier,
|
||||
const ghoul::filesystem::Directory& destination,
|
||||
int version,
|
||||
bool overrideFiles,
|
||||
AsyncDownloadFinishedCallback callback
|
||||
);
|
||||
|
||||
private:
|
||||
std::string _requestURL;
|
||||
int _applicationVersion;
|
||||
|
||||
@@ -226,4 +226,24 @@ std::vector<DownloadManager::FileFuture*> DownloadManager::downloadRequestFiles(
|
||||
return futures;
|
||||
}
|
||||
|
||||
void DownloadManager::downloadRequestFilesAsync(
|
||||
const std::string& identifier,
|
||||
const ghoul::filesystem::Directory& destination,
|
||||
int version,
|
||||
bool overrideFiles,
|
||||
AsyncDownloadFinishedCallback callback)
|
||||
{
|
||||
std::thread([this, identifier, destination, version, overrideFiles, callback](){
|
||||
std::vector<FileFuture*> f = downloadRequestFiles(
|
||||
identifier,
|
||||
destination,
|
||||
version,
|
||||
overrideFiles
|
||||
);
|
||||
|
||||
callback(f);
|
||||
}).detach();
|
||||
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
Reference in New Issue
Block a user