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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "DownloadQueueSet.h"
// Local includes
#include "HttpJob.h"
#include "digikam_debug.h"
namespace Marble
{
DownloadQueueSet::DownloadQueueSet(QObject* const parent)
: QObject(parent)
{
}
DownloadQueueSet::DownloadQueueSet(DownloadPolicy const& policy, QObject* const parent)
: QObject(parent),
m_downloadPolicy(policy)
{
}
DownloadQueueSet::~DownloadQueueSet()
{
// todo: delete HttpJobs
}
DownloadPolicy DownloadQueueSet::downloadPolicy() const
{
return m_downloadPolicy;
}
void DownloadQueueSet::setDownloadPolicy(DownloadPolicy const& policy)
{
m_downloadPolicy = policy;
}
bool DownloadQueueSet::canAcceptJob(const QUrl& sourceUrl,
const QString& destinationFileName) const
{
if (jobIsQueued(destinationFileName))
{
qCDebug(DIGIKAM_MARBLE_LOG) << "Download rejected: It's in the queue already:"
<< destinationFileName;
return false;
}
if (jobIsWaitingForRetry(destinationFileName))
{
qCDebug(DIGIKAM_MARBLE_LOG) << "Download rejected: Will try to download again in some time:"
<< destinationFileName;
return false;
}
if (jobIsActive(destinationFileName))
{
qCDebug(DIGIKAM_MARBLE_LOG) << "Download rejected: It's being downloaded already:"
<< destinationFileName;
return false;
}
if (jobIsBlackListed(sourceUrl))
{
qCDebug(DIGIKAM_MARBLE_LOG) << "Download rejected: Blacklisted.";
return false;
}
return true;
}
void DownloadQueueSet::addJob(HttpJob* const job)
{
m_jobs.push(job);
qCDebug(DIGIKAM_MARBLE_LOG) << "addJob: new job queue size:" << m_jobs.count();
Q_EMIT jobAdded();
Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
activateJobs();
}
void DownloadQueueSet::activateJobs()
{
while (!m_jobs.isEmpty()
&& m_activeJobs.count() < m_downloadPolicy.maximumConnections())
{
HttpJob* const job = m_jobs.pop();
activateJob(job);
}
}
void DownloadQueueSet::retryJobs()
{
while (!m_retryQueue.isEmpty())
{
HttpJob* const job = m_retryQueue.dequeue();
qCDebug(DIGIKAM_MARBLE_LOG) << "Requeuing" << job->destinationFileName();
// FIXME: addJob calls activateJobs every time
addJob(job);
}
}
void DownloadQueueSet::purgeJobs()
{
// purge all waiting jobs
while (!m_jobs.isEmpty())
{
HttpJob* const job = m_jobs.pop();
job->deleteLater();
}
// purge all retry jobs
qDeleteAll(m_retryQueue);
m_retryQueue.clear();
// cancel all current jobs
while (!m_activeJobs.isEmpty())
{
deactivateJob(m_activeJobs.first());
}
Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
}
void DownloadQueueSet::finishJob(HttpJob* job, const QByteArray& data)
{
qCDebug(DIGIKAM_MARBLE_LOG) << "finishJob: " << job->sourceUrl() << job->destinationFileName();
deactivateJob(job);
Q_EMIT jobRemoved();
Q_EMIT jobFinished(data, job->destinationFileName(), job->initiatorId());
job->deleteLater();
activateJobs();
}
void DownloadQueueSet::redirectJob(HttpJob* job, const QUrl& newSourceUrl)
{
qCDebug(DIGIKAM_MARBLE_LOG) << "jobRedirected:" << job->sourceUrl() << " -> " << newSourceUrl;
deactivateJob(job);
Q_EMIT jobRemoved();
Q_EMIT jobRedirected(newSourceUrl, job->destinationFileName(), job->initiatorId(),
job->downloadUsage());
job->deleteLater();
}
void DownloadQueueSet::retryOrBlacklistJob(HttpJob* job, const int errorCode)
{
Q_ASSERT(errorCode != 0);
Q_ASSERT(!m_retryQueue.contains(job));
deactivateJob(job);
Q_EMIT jobRemoved();
if (job->tryAgain())
{
qCDebug(DIGIKAM_MARBLE_LOG) << QString::fromUtf8("Download of %1 to %2 failed, but trying again soon")
.arg(job->sourceUrl().toString(), job->destinationFileName());
m_retryQueue.enqueue(job);
Q_EMIT jobRetry();
}
else
{
qCDebug(DIGIKAM_MARBLE_LOG) << "JOB-address: " << job
<< "Blacklist-size:" << m_jobBlackList.size()
<< "err:" << errorCode;
m_jobBlackList.insert(job->sourceUrl().toString());
qCDebug(DIGIKAM_MARBLE_LOG) << QString::fromUtf8("Download of %1 Blacklisted. "
"Number of blacklist items: %2")
.arg(job->destinationFileName())
.arg(m_jobBlackList.size());
job->deleteLater();
}
activateJobs();
}
void DownloadQueueSet::activateJob(HttpJob* const job)
{
m_activeJobs.push_back(job);
Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
connect(job, SIGNAL(jobDone(HttpJob*, int)),
SLOT(retryOrBlacklistJob(HttpJob*, int)));
connect(job, SIGNAL(redirected(HttpJob*, QUrl)),
SLOT(redirectJob(HttpJob*, QUrl)));
connect(job, SIGNAL(dataReceived(HttpJob*, QByteArray)),
SLOT(finishJob(HttpJob*, QByteArray)));
job->execute();
}
/**
pre condition: - job is in m_activeJobs
- job's signal are connected to our slots
post condition: - job is not in m_activeJobs anymore (and btw not
in any other queue)
- job's signals are disconnected from our slots
*/
void DownloadQueueSet::deactivateJob(HttpJob* const job)
{
const bool disconnected = job->disconnect();
Q_ASSERT(disconnected);
Q_UNUSED(disconnected); // for Q_ASSERT in release mode
const bool removed = m_activeJobs.removeOne(job);
Q_ASSERT(removed);
Q_UNUSED(removed); // for Q_ASSERT in release mode
Q_EMIT progressChanged(m_activeJobs.size(), m_jobs.count());
}
bool DownloadQueueSet::jobIsActive(QString const& destinationFileName) const
{
QList<HttpJob*>::const_iterator pos = m_activeJobs.constBegin();
QList<HttpJob*>::const_iterator const end = m_activeJobs.constEnd();
for (; pos != end; ++pos)
{
if ((*pos)->destinationFileName() == destinationFileName)
{
return true;
}
}
return false;
}
inline bool DownloadQueueSet::jobIsQueued(QString const& destinationFileName) const
{
return m_jobs.contains(destinationFileName);
}
bool DownloadQueueSet::jobIsWaitingForRetry(QString const& destinationFileName) const
{
QList<HttpJob*>::const_iterator pos = m_retryQueue.constBegin();
QList<HttpJob*>::const_iterator const end = m_retryQueue.constEnd();
for (; pos != end; ++pos)
{
if ((*pos)->destinationFileName() == destinationFileName)
{
return true;
}
}
return false;
}
bool DownloadQueueSet::jobIsBlackListed(const QUrl& sourceUrl) const
{
QSet<QString>::const_iterator const pos =
m_jobBlackList.constFind(sourceUrl.toString());
return pos != m_jobBlackList.constEnd();
}
inline bool DownloadQueueSet::JobStack::contains(const QString& destinationFileName) const
{
return m_jobsContent.contains(destinationFileName);
}
inline int DownloadQueueSet::JobStack::count() const
{
return m_jobs.count();
}
inline bool DownloadQueueSet::JobStack::isEmpty() const
{
return m_jobs.isEmpty();
}
inline HttpJob* DownloadQueueSet::JobStack::pop()
{
HttpJob* const job = m_jobs.pop();
bool const removed = m_jobsContent.remove(job->destinationFileName());
Q_UNUSED(removed); // for Q_ASSERT in release mode
Q_ASSERT(removed);
return job;
}
inline void DownloadQueueSet::JobStack::push(HttpJob* const job)<--- Parameter 'job' can be declared as pointer to const
{
m_jobs.push(job);
m_jobsContent.insert(job->destinationFileName());
}
} // namespace Marble
#include "moc_DownloadQueueSet.cpp"
|