summaryrefslogtreecommitdiffstats
path: root/api/logic/net
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2017-06-26 01:14:32 +0200
committerPetr Mrázek <peterix@gmail.com>2017-06-26 01:14:32 +0200
commit89d3a66658ebdb16582a4d7a2cab57cfd6906393 (patch)
treef3b4daf14a18db76de1b9998da16b505b3872d10 /api/logic/net
parent2973b11d3edf47fe437af4dd1dc1a5f250c49ba3 (diff)
downloadMultiMC-89d3a66658ebdb16582a4d7a2cab57cfd6906393.tar
MultiMC-89d3a66658ebdb16582a4d7a2cab57cfd6906393.tar.gz
MultiMC-89d3a66658ebdb16582a4d7a2cab57cfd6906393.tar.lz
MultiMC-89d3a66658ebdb16582a4d7a2cab57cfd6906393.tar.xz
MultiMC-89d3a66658ebdb16582a4d7a2cab57cfd6906393.zip
NOISSUE some safe refactors and changes of the task subsystem
Possibly also some bug fixes.
Diffstat (limited to 'api/logic/net')
-rw-r--r--api/logic/net/NetAction.h67
-rw-r--r--api/logic/net/NetJob.cpp45
-rw-r--r--api/logic/net/NetJob.h31
3 files changed, 76 insertions, 67 deletions
diff --git a/api/logic/net/NetAction.h b/api/logic/net/NetAction.h
index a533c317..f164dda3 100644
--- a/api/logic/net/NetAction.h
+++ b/api/logic/net/NetAction.h
@@ -43,18 +43,26 @@ protected:
public:
virtual ~NetAction() {};
-public:
- virtual qint64 totalProgress() const
+ bool isRunning() const
{
- return m_total_progress;
+ return m_status == Job_InProgress;
}
- virtual qint64 currentProgress() const
+ bool isFinished() const
{
- return m_progress;
+ return m_status >= Job_Finished;
}
- virtual qint64 numberOfFailures() const
+ bool wasSuccessful() const
{
- return m_failures;
+ return m_status == Job_Finished || m_status == Job_Failed_Proceed;
+ }
+
+ qint64 totalProgress() const
+ {
+ return m_total_progress;
+ }
+ qint64 currentProgress() const
+ {
+ return m_progress;
}
virtual bool abort()
{
@@ -64,25 +72,10 @@ public:
{
return false;
}
-
-public:
- /// the network reply
- unique_qobject_ptr<QNetworkReply> m_reply;
-
- /// source URL
- QUrl m_url;
-
- /// The file's status
- JobStatus m_status = Job_NotStarted;
-
- /// index within the parent job
- int m_index_within_job = 0;
-
- qint64 m_progress = 0;
- qint64 m_total_progress = 1;
-
- /// number of failures up to this point
- int m_failures = 0;
+ QUrl url()
+ {
+ return m_url;
+ }
signals:
void started(int index);
@@ -91,14 +84,28 @@ signals:
void failed(int index);
void aborted(int index);
-protected
-slots:
+protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
virtual void downloadError(QNetworkReply::NetworkError error) = 0;
virtual void downloadFinished() = 0;
virtual void downloadReadyRead() = 0;
-public
-slots:
+public slots:
virtual void start() = 0;
+
+public:
+ /// index within the parent job, FIXME: nuke
+ int m_index_within_job = 0;
+
+ /// the network reply
+ unique_qobject_ptr<QNetworkReply> m_reply;
+
+ /// source URL
+ QUrl m_url;
+
+ qint64 m_progress = 0;
+ qint64 m_total_progress = 1;
+
+protected:
+ JobStatus m_status = Job_NotStarted;
};
diff --git a/api/logic/net/NetJob.cpp b/api/logic/net/NetJob.cpp
index 275da749..693a0934 100644
--- a/api/logic/net/NetJob.cpp
+++ b/api/logic/net/NetJob.cpp
@@ -73,18 +73,19 @@ void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal)
void NetJob::executeTask()
{
qDebug() << m_job_name.toLocal8Bit() << " started.";
- m_running = true;
- for (int i = 0; i < downloads.size(); i++)
- {
- m_todo.enqueue(i);
- }
// hack that delays early failures so they can be caught easier
QMetaObject::invokeMethod(this, "startMoreParts", Qt::QueuedConnection);
}
void NetJob::startMoreParts()
{
- // check for final conditions if there's nothing in the queue
+ if(!isRunning())
+ {
+ // this actually makes sense. You can put running downloads into a NetJob and then not start it until much later.
+ return;
+ }
+ // OK. We are actively processing tasks, proceed.
+ // Check for final conditions if there's nothing in the queue.
if(!m_todo.size())
{
if(!m_doing.size())
@@ -107,7 +108,7 @@ void NetJob::startMoreParts()
}
return;
}
- // otherwise try to start more parts
+ // There's work to do, try to start more parts.
while (m_doing.size() < 6)
{
if(!m_todo.size())
@@ -131,7 +132,7 @@ QStringList NetJob::getFailedFiles()
QStringList failed;
for (auto index: m_failed)
{
- failed.push_back(downloads[index]->m_url.toString());
+ failed.push_back(downloads[index]->url().toString());
}
failed.sort();
return failed;
@@ -170,3 +171,31 @@ bool NetJob::abort()
}
return fullyAborted;
}
+
+bool NetJob::addNetAction(NetActionPtr action)
+{
+ action->m_index_within_job = downloads.size();
+ downloads.append(action);
+ part_info pi;
+ {
+ pi.current_progress = action->currentProgress();
+ pi.total_progress = action->totalProgress();
+ pi.failures = 0;
+ }
+ parts_progress.append(pi);
+ total_progress += pi.total_progress;
+
+ // FIXME: detect if the action is already running, put it in m_doing if it is!
+ setProgress(current_progress, total_progress);
+ if(action->isRunning())
+ {
+ connect(action.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
+ connect(action.get(), SIGNAL(failed(int)), SLOT(partFailed(int)));
+ connect(action.get(), SIGNAL(netActionProgress(int, qint64, qint64)), SLOT(partProgress(int, qint64, qint64)));
+ }
+ else
+ {
+ m_todo.append(parts_progress.size() - 1);
+ }
+ return true;
+}
diff --git a/api/logic/net/NetJob.h b/api/logic/net/NetJob.h
index ca4f5df1..cd576664 100644
--- a/api/logic/net/NetJob.h
+++ b/api/logic/net/NetJob.h
@@ -32,30 +32,8 @@ class MULTIMC_LOGIC_EXPORT NetJob : public Task
public:
explicit NetJob(QString job_name) : Task(), m_job_name(job_name) {}
virtual ~NetJob() {}
- bool addNetAction(NetActionPtr action)
- {
- action->m_index_within_job = downloads.size();
- downloads.append(action);
- part_info pi;
- {
- pi.current_progress = action->currentProgress();
- pi.total_progress = action->totalProgress();
- pi.failures = action->numberOfFailures();
- }
- parts_progress.append(pi);
- total_progress += pi.total_progress;
- // if this is already running, the action needs to be started right away!
- if (isRunning())
- {
- setProgress(current_progress, total_progress);
- connect(action.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
- connect(action.get(), SIGNAL(failed(int)), SLOT(partFailed(int)));
- connect(action.get(), SIGNAL(netActionProgress(int, qint64, qint64)),
- SLOT(partProgress(int, qint64, qint64)));
- action->start();
- }
- return true;
- }
+
+ bool addNetAction(NetActionPtr action);
NetActionPtr operator[](int index)
{
@@ -75,10 +53,6 @@ public:
{
return downloads.size();
}
- virtual bool isRunning() const override
- {
- return m_running;
- }
QStringList getFailedFiles();
bool canAbort() const override;
@@ -113,6 +87,5 @@ private:
QSet<int> m_failed;
qint64 current_progress = 0;
qint64 total_progress = 0;
- bool m_running = false;
bool m_aborted = false;
};