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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2009 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 --> */
#ifndef D_RPC_METHOD_IMPL_H
#define D_RPC_METHOD_IMPL_H
#include "RpcMethod.h"
#include <cassert>
#include <deque>
#include <algorithm>
#include "RpcRequest.h"
#include "ValueBase.h"
#include "TorrentAttribute.h"
#include "DlAbortEx.h"
#include "fmt.h"
namespace aria2 {
struct DownloadResult;
class RequestGroup;
namespace rpc {
template<typename T>
const T* checkParam(const RpcRequest& req, size_t index, bool required = false)
{
const T* p = 0;
if(req.params->size() > index) {
if((p = downcast<T>(req.params->get(index))) == 0) {
throw DL_ABORT_EX(fmt("The parameter at %lu has wrong type.",
static_cast<unsigned long>(index)));
}
} else if(required) {
throw DL_ABORT_EX(fmt("The parameter at %lu is required but missing.",
static_cast<unsigned long>(index)));
}
return p;
}
template<typename T>
const T* checkRequiredParam(const RpcRequest& req, size_t index)
{
return checkParam<T>(req, index, true);
}
template<typename OutputIterator>
void toStringList(OutputIterator out, const List* src)
{
if(!src) {
return;
}
for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
i != eoi; ++i) {
const String* s = downcast<String>(*i);
if(s) {
*out++ = s->s();
}
}
}
class AddUriRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.addUri";
return methodName;
}
};
class RemoveRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.remove";
return methodName;
}
};
class ForceRemoveRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.forceRemove";
return methodName;
}
};
class PauseRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.pause";
return methodName;
}
};
class ForcePauseRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.forcePause";
return methodName;
}
};
class PauseAllRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.pauseAll";
return methodName;
}
};
class ForcePauseAllRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.forcePauseAll";
return methodName;
}
};
class UnpauseRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.unpause";
return methodName;
}
};
class UnpauseAllRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.unpauseAll";
return methodName;
}
};
#ifdef ENABLE_BITTORRENT
class AddTorrentRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.addTorrent";
return methodName;
}
};
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
class AddMetalinkRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.addMetalink";
return methodName;
}
};
#endif // ENABLE_METALINK
class PurgeDownloadResultRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.purgeDownloadResult";
return methodName;
}
};
class RemoveDownloadResultRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.removeDownloadResult";
return methodName;
}
};
class GetUrisRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getUris";
return methodName;
}
};
class GetFilesRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getFiles";
return methodName;
}
};
#ifdef ENABLE_BITTORRENT
class GetPeersRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getPeers";
return methodName;
}
};
#endif // ENABLE_BITTORRENT
class GetServersRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getServers";
return methodName;
}
};
class TellStatusRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.tellStatus";
return methodName;
}
};
class TellActiveRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.tellActive";
return methodName;
}
};
template<typename T>
class AbstractPaginationRpcMethod:public RpcMethod {
private:
template<typename InputIterator>
std::pair<InputIterator, InputIterator>
getPaginationRange
(int offset, int num, InputIterator first, InputIterator last)
{
int size = std::distance(first, last);
if(offset < 0) {
int tempoffset = offset+size;
if(tempoffset < 0) {
return std::make_pair(last, last);
}
offset = tempoffset-(num-1);
if(offset < 0) {
offset = 0;
num = tempoffset+1;
}
} else if(size <= offset) {
return std::make_pair(last, last);
}
int lastDistance;
if(size < offset+num) {
lastDistance = size;
} else {
lastDistance = offset+num;
}
last = first;
std::advance(first, offset);
std::advance(last, lastDistance);
return std::make_pair(first, last);
}
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e)
{
const Integer* offsetParam = checkRequiredParam<Integer>(req, 0);
const Integer* numParam = checkRequiredParam<Integer>(req, 1);
const List* keysParam = checkParam<List>(req, 2);
if(numParam->i() < 0) {
throw DL_ABORT_EX("The parameter num must be zero or positive integer.");
}
int offset = offsetParam->i();
int num = numParam->i();
std::vector<std::string> keys;
toStringList(std::back_inserter(keys), keysParam);
const std::deque<SharedHandle<T> >& items = getItems(e);
std::pair<typename std::deque<SharedHandle<T> >::const_iterator,
typename std::deque<SharedHandle<T> >::const_iterator> range =
getPaginationRange(offset, num, items.begin(), items.end());
SharedHandle<List> list = List::g();
for(; range.first != range.second; ++range.first) {
SharedHandle<Dict> entryDict = Dict::g();
createEntry(entryDict, *range.first, e, keys);
list->append(entryDict);
}
if(offset < 0) {
std::reverse(list->begin(), list->end());
}
return list;
}
virtual const std::deque<SharedHandle<T> >&
getItems(DownloadEngine* e) const = 0;
virtual void createEntry
(const SharedHandle<Dict>& entryDict,
const SharedHandle<T>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const = 0;
};
class TellWaitingRpcMethod:
public AbstractPaginationRpcMethod<RequestGroup> {
protected:
virtual const std::deque<SharedHandle<RequestGroup> >&
getItems(DownloadEngine* e) const;
virtual void createEntry
(const SharedHandle<Dict>& entryDict,
const SharedHandle<RequestGroup>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const;
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.tellWaiting";
return methodName;
}
};
class TellStoppedRpcMethod:
public AbstractPaginationRpcMethod<DownloadResult> {
protected:
virtual const std::deque<SharedHandle<DownloadResult> >&
getItems(DownloadEngine* e) const;
virtual void createEntry
(const SharedHandle<Dict>& entryDict,
const SharedHandle<DownloadResult>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const;
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.tellStopped";
return methodName;
}
};
class ChangeOptionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.changeOption";
return methodName;
}
};
class ChangeGlobalOptionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.changeGlobalOption";
return methodName;
}
};
class GetVersionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getVersion";
return methodName;
}
};
class GetOptionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getOption";
return methodName;
}
};
class GetGlobalOptionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getGlobalOption";
return methodName;
}
};
class ChangePositionRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.changePosition";
return methodName;
}
};
class ChangeUriRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.changeUri";
return methodName;
}
};
class GetSessionInfoRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getSessionInfo";
return methodName;
}
};
class ShutdownRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.shutdown";
return methodName;
}
};
class GetGlobalStatRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.getGlobalStat";
return methodName;
}
};
class ForceShutdownRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "aria2.forceShutdown";
return methodName;
}
};
class SystemMulticallRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
public:
static const std::string& getMethodName()
{
static std::string methodName = "system.multicall";
return methodName;
}
};
class NoSuchMethodRpcMethod:public RpcMethod {
protected:
virtual SharedHandle<ValueBase> process
(const RpcRequest& req, DownloadEngine* e);
};
// Helper function to store data to entryDict from ds. This function
// is used by tellStatus method.
void gatherStoppedDownload
(const SharedHandle<Dict>& entryDict, const SharedHandle<DownloadResult>& ds,
const std::vector<std::string>& keys);
// Helper function to store data to entryDict from group. This
// function is used by tellStatus/tellActive/tellWaiting method
void gatherProgressCommon
(const SharedHandle<Dict>& entryDict, const SharedHandle<RequestGroup>& group,
const std::vector<std::string>& keys);
#ifdef ENABLE_BITTORRENT
// Helper function to store BitTorrent metadata from torrentAttrs.
void gatherBitTorrentMetadata
(const SharedHandle<Dict>& btDict,
const SharedHandle<TorrentAttribute>& torrentAttrs);
#endif // ENABLE_BITTORRENT
} // namespace rpc
} // namespace aria2
#endif // D_RPC_METHOD_IMPL_H
|