Multithreading the downloadrequests

This commit is contained in:
Alexander Bock
2015-06-17 01:02:07 +02:00
parent 72c520e1d1
commit 850d9d3789
4 changed files with 84 additions and 12 deletions
+48 -12
View File
@@ -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;
//}
}
+6
View File
@@ -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__