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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
/* <!-- 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"
#include "IndexedList.h"
#include "GroupId.h"
#include "RequestGroupMan.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);
}
struct IntegerGE {
IntegerGE(int32_t min):min(min) {}
bool operator()(const Integer* param, std::string* error) const
{
if(min <= param->i()) {
return true;
} else {
if(error) {
*error = fmt("the value must be greater than or equal to %d.", min);
}
return false;
}
}
int32_t min;
};
template<typename Validator>
const Integer* checkRequiredInteger(const RpcRequest& req, size_t index,
Validator validator)
{
const Integer* param = checkRequiredParam<Integer>(req, index);
std::string error;
if(!validator(param, &error)) {
throw DL_ABORT_EX(fmt("The integer parameter at %lu has invalid value: %s",
static_cast<unsigned long>(index), error.c_str()));
}
return param;
}
template<typename OutputIterator>
void toStringList(OutputIterator out, const List* src)
{
if(!src) {
return;
}
for(auto & elem : *src) {
const String* s = downcast<String>(elem);
if(s) {
*out++ = s->s();
}
}
}
class AddUriRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.addUri";
}
};
class RemoveRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.remove";
}
};
class ForceRemoveRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.forceRemove";
}
};
class PauseRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.pause";
}
};
class ForcePauseRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.forcePause";
}
};
class PauseAllRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.pauseAll";
}
};
class ForcePauseAllRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.forcePauseAll";
}
};
class UnpauseRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.unpause";
}
};
class UnpauseAllRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.unpauseAll";
}
};
#ifdef ENABLE_BITTORRENT
class AddTorrentRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.addTorrent";
}
};
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
class AddMetalinkRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.addMetalink";
}
};
#endif // ENABLE_METALINK
class PurgeDownloadResultRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.purgeDownloadResult";
}
};
class RemoveDownloadResultRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.removeDownloadResult";
}
};
class GetUrisRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getUris";
}
};
class GetFilesRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getFiles";
}
};
#ifdef ENABLE_BITTORRENT
class GetPeersRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getPeers";
}
};
#endif // ENABLE_BITTORRENT
class GetServersRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getServers";
}
};
class TellStatusRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.tellStatus";
}
};
class TellActiveRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.tellActive";
}
};
template<typename T>
class AbstractPaginationRpcMethod:public RpcMethod {
private:
template<typename InputIterator>
std::pair<InputIterator, InputIterator>
getPaginationRange
(int64_t offset, int64_t num, InputIterator first, InputIterator last)
{
if(num <= 0) {
return std::make_pair(last, last);
}
int64_t size = std::distance(first, last);
if(offset < 0) {
int64_t 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);
}
int64_t 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:
typedef IndexedList<a2_gid_t, std::shared_ptr<T> > ItemListType;
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE
{
const Integer* offsetParam = checkRequiredParam<Integer>(req, 0);
const Integer* numParam = checkRequiredInteger(req, 1, IntegerGE(0));
const List* keysParam = checkParam<List>(req, 2);
int64_t offset = offsetParam->i();
int64_t num = numParam->i();
std::vector<std::string> keys;
toStringList(std::back_inserter(keys), keysParam);
const ItemListType& items = getItems(e);
auto range = getPaginationRange(offset, num,
std::begin(items), std::end(items));
auto list = List::g();
for(; range.first != range.second; ++range.first) {
auto entryDict = Dict::g();
createEntry(entryDict.get(), *range.first, e, keys);
list->append(std::move(entryDict));
}
if(offset < 0) {
std::reverse(list->begin(), list->end());
}
return std::move(list);
}
virtual const ItemListType& getItems(DownloadEngine* e) const = 0;
virtual void createEntry
(Dict* entryDict,
const std::shared_ptr<T>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const = 0;
};
class TellWaitingRpcMethod:
public AbstractPaginationRpcMethod<RequestGroup> {
protected:
virtual const RequestGroupList& getItems(DownloadEngine* e) const
CXX11_OVERRIDE;
virtual void createEntry
(Dict* entryDict,
const std::shared_ptr<RequestGroup>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.tellWaiting";
}
};
class TellStoppedRpcMethod:
public AbstractPaginationRpcMethod<DownloadResult> {
protected:
virtual const DownloadResultList& getItems(DownloadEngine* e) const
CXX11_OVERRIDE;
virtual void createEntry
(Dict* entryDict,
const std::shared_ptr<DownloadResult>& item,
DownloadEngine* e,
const std::vector<std::string>& keys) const CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.tellStopped";
}
};
class ChangeOptionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.changeOption";
}
};
class ChangeGlobalOptionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.changeGlobalOption";
}
};
class GetVersionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getVersion";
}
};
class GetOptionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getOption";
}
};
class GetGlobalOptionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getGlobalOption";
}
};
class ChangePositionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.changePosition";
}
};
class ChangeUriRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.changeUri";
}
};
class GetSessionInfoRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getSessionInfo";
}
};
class ShutdownRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.shutdown";
}
};
class GetGlobalStatRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.getGlobalStat";
}
};
class ForceShutdownRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.forceShutdown";
}
};
class SaveSessionRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
static const char* getMethodName()
{
return "aria2.saveSession";
}
};
class SystemMulticallRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
public:
virtual RpcResponse execute(RpcRequest req, DownloadEngine* e) CXX11_OVERRIDE;
static const char* getMethodName()
{
return "system.multicall";
}
};
class NoSuchMethodRpcMethod:public RpcMethod {
protected:
virtual std::unique_ptr<ValueBase> process
(const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
};
// Helper function to store data to entryDict from ds. This function
// is used by tellStatus method.
void gatherStoppedDownload
(Dict* entryDict, const std::shared_ptr<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
(Dict* entryDict, const std::shared_ptr<RequestGroup>& group,
const std::vector<std::string>& keys);
#ifdef ENABLE_BITTORRENT
// Helper function to store BitTorrent metadata from torrentAttrs.
void gatherBitTorrentMetadata(Dict* btDict, TorrentAttribute* torrentAttrs);
#endif // ENABLE_BITTORRENT
} // namespace rpc
bool pauseRequestGroup
(const std::shared_ptr<RequestGroup>& group, bool reserved, bool forcePause);
void changeOption
(const std::shared_ptr<RequestGroup>& group,
const Option& option,
DownloadEngine* e);
void changeGlobalOption(const Option& option, DownloadEngine* e);
} // namespace aria2
#endif // D_RPC_METHOD_IMPL_H
|