commit a9158e0a4c942bb1a825e8eb7656227883c1ef34
parent cb30aa7f107ba0aa0b141dc42be58ab3bbab57d9
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 6 Dec 2015 16:22:55 -0500
download up to three files concurrently when using the queue
Diffstat:
2 files changed, 12 insertions(+), 17 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -10,6 +10,7 @@ Download::Queue Download::s_finished;
WDL_Mutex Download::s_mutex;
static const int DOWNLOAD_TIMEOUT = 5;
+static const int CONCURRENT_DOWNLOADS = 3;
Download::Download(const string &name, const string &url)
: m_name(name), m_url(url), m_threadHandle(0)
@@ -199,11 +200,6 @@ void Download::abort()
m_aborted = true;
}
-DownloadQueue::DownloadQueue()
- : m_current(0)
-{
-}
-
DownloadQueue::~DownloadQueue()
{
abort();
@@ -214,7 +210,7 @@ void DownloadQueue::push(Download *dl)
m_onPush(dl);
dl->onFinish([=]() {
- m_current = 0;
+ m_running.erase(remove(m_running.begin(), m_running.end(), dl));
delete dl;
start();
@@ -227,21 +223,19 @@ void DownloadQueue::push(Download *dl)
void DownloadQueue::start()
{
- if(m_queue.empty())
- return;
-
- if(!m_current) {
- m_current = m_queue.front();
+ while(m_running.size() < CONCURRENT_DOWNLOADS && !m_queue.empty()) {
+ Download *dl = m_queue.front();
m_queue.pop();
- m_current->start();
+ m_running.push_back(dl);
+ dl->start();
}
}
void DownloadQueue::abort()
{
- if(m_current)
- m_current->abort();
+ for(Download *dl : m_running)
+ dl->abort();
clear();
}
diff --git a/src/download.hpp b/src/download.hpp
@@ -3,6 +3,7 @@
#include <queue>
#include <string>
+#include <vector>
#include <boost/signals2.hpp>
#include <WDL/mutex.h>
@@ -67,7 +68,7 @@ public:
typedef boost::signals2::signal<void (Download *)> Signal;
typedef Signal::slot_type Callback;
- DownloadQueue();
+ DownloadQueue() {}
DownloadQueue(const DownloadQueue &) = delete;
~DownloadQueue();
@@ -75,7 +76,7 @@ public:
void start();
void abort();
- bool idle() const { return m_queue.empty() && !m_current; }
+ bool idle() const { return m_queue.empty() && m_running.empty(); }
void onPush(const Callback &callback) { m_onPush.connect(callback); }
@@ -83,7 +84,7 @@ private:
void clear();
Download::Queue m_queue;
- Download *m_current;
+ std::vector<Download *> m_running;
Signal m_onPush;
};