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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "aria2api.h"
#include <functional>
#include "Platform.h"
#include "Context.h"
#include "DownloadEngine.h"
#include "OptionParser.h"
#include "Option.h"
#include "DlAbortEx.h"
#include "fmt.h"
#include "OptionHandler.h"
#include "RequestGroupMan.h"
#include "RequestGroup.h"
#include "MultiUrlRequestInfo.h"
#include "prefs.h"
#include "download_helper.h"
#include "LogFactory.h"
namespace aria2 {
Session::Session(const KeyVals& options)
: context(new Context(false, 0, 0, options))
{}
Session::~Session()
{}
namespace {
Platform* platform = 0;
} // namespace
int libraryInit()
{
platform = new Platform();
return 0;
}
int libraryDeinit()
{
delete platform;
return 0;
}
Session* sessionNew(const KeyVals& options)
{
int rv;
Session* session = new Session(options);
if(session->context->reqinfo) {
rv = session->context->reqinfo->prepare();
if(rv != 0) {
delete session;
session = 0;
}
} else {
delete session;
session = 0;
}
return session;
}
int sessionFinal(Session* session)
{
error_code::Value rv = session->context->reqinfo->getResult();
delete session;
return rv;
}
int run(Session* session, RUN_MODE mode)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
return e->run(mode == RUN_ONCE);
}
std::string gidToString(const A2Gid& gid)
{
return GroupId::toHex(gid);
}
namespace {
template<typename InputIterator, typename Pred>
void apiGatherOption
(InputIterator first, InputIterator last,
Pred pred,
Option* option,
const SharedHandle<OptionParser>& optionParser)
{
for(; first != last; ++first) {
const std::string& optionName = (*first).first;
const Pref* pref = option::k2p(optionName);
if(!pref) {
throw DL_ABORT_EX
(fmt("We don't know how to deal with %s option", optionName.c_str()));
}
const OptionHandler* handler = optionParser->find(pref);
if(!handler || !pred(handler)) {
// Just ignore the unacceptable options in this context.
continue;
}
handler->parse(*option, (*first).second);
}
}
} // namespace
namespace {
void apiGatherRequestOption(Option* option, const KeyVals& options,
const SharedHandle<OptionParser>& optionParser)
{
apiGatherOption(options.begin(), options.end(),
std::mem_fun(&OptionHandler::getInitialOption),
option, optionParser);
}
} // namespace
namespace {
void addRequestGroup(const SharedHandle<RequestGroup>& group,
const SharedHandle<DownloadEngine>& e,
int position)
{
if(position >= 0) {
e->getRequestGroupMan()->insertReservedGroup(position, group);
} else {
e->getRequestGroupMan()->addReservedGroup(group);
}
}
} // namespace
int addUri(Session* session,
A2Gid& gid,
const std::vector<std::string>& uris,
const KeyVals& options,
int position)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<Option> requestOption(new Option(*e->getOption()));
try {
apiGatherRequestOption(requestOption.get(), options,
OptionParser::getInstance());
} catch(RecoverableException& e) {
A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e);
return -1;
}
std::vector<SharedHandle<RequestGroup> > result;
createRequestGroupForUri(result, requestOption, uris,
/* ignoreForceSeq = */ true,
/* ignoreLocalPath = */ true);
if(!result.empty()) {
gid = result.front()->getGID();
addRequestGroup(result.front(), e, position);
}
return 0;
}
std::vector<A2Gid> getActiveDownload(Session* session)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
const RequestGroupList& groups = e->getRequestGroupMan()->getRequestGroups();
std::vector<A2Gid> res;
for(RequestGroupList::const_iterator i = groups.begin(),
eoi = groups.end(); i != eoi; ++i) {
res.push_back((*i)->getGID());
}
return res;
}
namespace {
struct RequestGroupDH : public DownloadHandle {
RequestGroupDH(const SharedHandle<RequestGroup>& group)
: group(group),
ts(group->calculateStat())
{}
virtual ~RequestGroupDH() {}
virtual DOWNLOAD_STATUS getStatus()
{
if(group->getState() == RequestGroup::STATE_ACTIVE) {
return DOWNLOAD_ACTIVE;
} else {
if(group->isPauseRequested()) {
return DOWNLOAD_PAUSED;
} else {
return DOWNLOAD_WAITING;
}
}
}
virtual int64_t getTotalLength()
{
return group->getTotalLength();
}
virtual int64_t getCompletedLength()
{
return group->getCompletedLength();
}
virtual int64_t getUploadLength()
{
return ts.allTimeUploadLength;
}
virtual int getDownloadSpeed()
{
return ts.downloadSpeed;
}
virtual int getUploadSpeed()
{
return ts.uploadSpeed;
}
SharedHandle<RequestGroup> group;
TransferStat ts;
};
} // namespace
namespace {
struct DownloadResultDH : public DownloadHandle {
DownloadResultDH(const SharedHandle<DownloadResult>& dr)
: dr(dr)
{}
virtual ~DownloadResultDH() {}
virtual DOWNLOAD_STATUS getStatus()
{
switch(dr->result) {
case error_code::FINISHED:
return DOWNLOAD_COMPLETE;
case error_code::REMOVED:
return DOWNLOAD_REMOVED;
default:
return DOWNLOAD_ERROR;
}
}
virtual int64_t getTotalLength()
{
return dr->totalLength;
}
virtual int64_t getCompletedLength()
{
return dr->completedLength;
}
virtual int64_t getUploadLength()
{
return dr->uploadLength;
}
virtual int getDownloadSpeed()
{
return 0;
}
virtual int getUploadSpeed()
{
return 0;
}
SharedHandle<DownloadResult> dr;
};
} // namespace
DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
const SharedHandle<RequestGroupMan>& rgman = e->getRequestGroupMan();
SharedHandle<RequestGroup> group = rgman->findGroup(gid);
if(group) {
return new RequestGroupDH(group);
} else {
SharedHandle<DownloadResult> ds = rgman->findDownloadResult(gid);
if(ds) {
return new DownloadResultDH(ds);
}
}
return 0;
}
void deleteDownloadHandle(DownloadHandle* dh)
{
delete dh;
}
DOWNLOAD_STATUS downloadGetStatus(DownloadHandle* dh)
{
return dh->getStatus();
}
int64_t downloadGetTotalLength(DownloadHandle* dh)
{
return dh->getTotalLength();
}
int64_t downloadGetCompletedLength(DownloadHandle* dh)
{
return dh->getCompletedLength();
}
int64_t downloadGetUploadLength(DownloadHandle* dh)
{
return dh->getUploadLength();
}
int downloadGetDownloadSpeed(DownloadHandle* dh)
{
return dh->getDownloadSpeed();
}
int downloadGetUploadSpeed(DownloadHandle* dh)
{
return dh->getUploadSpeed();
}
} // namespace aria2
|