RpcMethodImpl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_RPC_METHOD_IMPL_H
  36. #define D_RPC_METHOD_IMPL_H
  37. #include "RpcMethod.h"
  38. #include <cassert>
  39. #include <deque>
  40. #include <algorithm>
  41. #include "RpcRequest.h"
  42. #include "ValueBase.h"
  43. #include "TorrentAttribute.h"
  44. #include "DlAbortEx.h"
  45. namespace aria2 {
  46. struct DownloadResult;
  47. class RequestGroup;
  48. namespace rpc {
  49. template<typename OutputIterator>
  50. void toStringList(OutputIterator out, const List* src)
  51. {
  52. if(!src) {
  53. return;
  54. }
  55. for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
  56. i != eoi; ++i) {
  57. const String* s = asString(*i);
  58. if(s) {
  59. *out++ = s->s();
  60. }
  61. }
  62. }
  63. class AddUriRpcMethod:public RpcMethod {
  64. protected:
  65. virtual SharedHandle<ValueBase> process
  66. (const RpcRequest& req, DownloadEngine* e);
  67. public:
  68. static const std::string& getMethodName()
  69. {
  70. static std::string methodName = "aria2.addUri";
  71. return methodName;
  72. }
  73. };
  74. class RemoveRpcMethod:public RpcMethod {
  75. protected:
  76. virtual SharedHandle<ValueBase> process
  77. (const RpcRequest& req, DownloadEngine* e);
  78. public:
  79. static const std::string& getMethodName()
  80. {
  81. static std::string methodName = "aria2.remove";
  82. return methodName;
  83. }
  84. };
  85. class ForceRemoveRpcMethod:public RpcMethod {
  86. protected:
  87. virtual SharedHandle<ValueBase> process
  88. (const RpcRequest& req, DownloadEngine* e);
  89. public:
  90. static const std::string& getMethodName()
  91. {
  92. static std::string methodName = "aria2.forceRemove";
  93. return methodName;
  94. }
  95. };
  96. class PauseRpcMethod:public RpcMethod {
  97. protected:
  98. virtual SharedHandle<ValueBase> process
  99. (const RpcRequest& req, DownloadEngine* e);
  100. public:
  101. static const std::string& getMethodName()
  102. {
  103. static std::string methodName = "aria2.pause";
  104. return methodName;
  105. }
  106. };
  107. class ForcePauseRpcMethod:public RpcMethod {
  108. protected:
  109. virtual SharedHandle<ValueBase> process
  110. (const RpcRequest& req, DownloadEngine* e);
  111. public:
  112. static const std::string& getMethodName()
  113. {
  114. static std::string methodName = "aria2.forcePause";
  115. return methodName;
  116. }
  117. };
  118. class PauseAllRpcMethod:public RpcMethod {
  119. protected:
  120. virtual SharedHandle<ValueBase> process
  121. (const RpcRequest& req, DownloadEngine* e);
  122. public:
  123. static const std::string& getMethodName()
  124. {
  125. static std::string methodName = "aria2.pauseAll";
  126. return methodName;
  127. }
  128. };
  129. class ForcePauseAllRpcMethod:public RpcMethod {
  130. protected:
  131. virtual SharedHandle<ValueBase> process
  132. (const RpcRequest& req, DownloadEngine* e);
  133. public:
  134. static const std::string& getMethodName()
  135. {
  136. static std::string methodName = "aria2.forcePauseAll";
  137. return methodName;
  138. }
  139. };
  140. class UnpauseRpcMethod:public RpcMethod {
  141. protected:
  142. virtual SharedHandle<ValueBase> process
  143. (const RpcRequest& req, DownloadEngine* e);
  144. public:
  145. static const std::string& getMethodName()
  146. {
  147. static std::string methodName = "aria2.unpause";
  148. return methodName;
  149. }
  150. };
  151. class UnpauseAllRpcMethod:public RpcMethod {
  152. protected:
  153. virtual SharedHandle<ValueBase> process
  154. (const RpcRequest& req, DownloadEngine* e);
  155. public:
  156. static const std::string& getMethodName()
  157. {
  158. static std::string methodName = "aria2.unpauseAll";
  159. return methodName;
  160. }
  161. };
  162. #ifdef ENABLE_BITTORRENT
  163. class AddTorrentRpcMethod:public RpcMethod {
  164. protected:
  165. virtual SharedHandle<ValueBase> process
  166. (const RpcRequest& req, DownloadEngine* e);
  167. public:
  168. static const std::string& getMethodName()
  169. {
  170. static std::string methodName = "aria2.addTorrent";
  171. return methodName;
  172. }
  173. };
  174. #endif // ENABLE_BITTORRENT
  175. #ifdef ENABLE_METALINK
  176. class AddMetalinkRpcMethod:public RpcMethod {
  177. protected:
  178. virtual SharedHandle<ValueBase> process
  179. (const RpcRequest& req, DownloadEngine* e);
  180. public:
  181. static const std::string& getMethodName()
  182. {
  183. static std::string methodName = "aria2.addMetalink";
  184. return methodName;
  185. }
  186. };
  187. #endif // ENABLE_METALINK
  188. class PurgeDownloadResultRpcMethod:public RpcMethod {
  189. protected:
  190. virtual SharedHandle<ValueBase> process
  191. (const RpcRequest& req, DownloadEngine* e);
  192. public:
  193. static const std::string& getMethodName()
  194. {
  195. static std::string methodName = "aria2.purgeDownloadResult";
  196. return methodName;
  197. }
  198. };
  199. class RemoveDownloadResultRpcMethod:public RpcMethod {
  200. protected:
  201. virtual SharedHandle<ValueBase> process
  202. (const RpcRequest& req, DownloadEngine* e);
  203. public:
  204. static const std::string& getMethodName()
  205. {
  206. static std::string methodName = "aria2.removeDownloadResult";
  207. return methodName;
  208. }
  209. };
  210. class GetUrisRpcMethod:public RpcMethod {
  211. protected:
  212. virtual SharedHandle<ValueBase> process
  213. (const RpcRequest& req, DownloadEngine* e);
  214. public:
  215. static const std::string& getMethodName()
  216. {
  217. static std::string methodName = "aria2.getUris";
  218. return methodName;
  219. }
  220. };
  221. class GetFilesRpcMethod:public RpcMethod {
  222. protected:
  223. virtual SharedHandle<ValueBase> process
  224. (const RpcRequest& req, DownloadEngine* e);
  225. public:
  226. static const std::string& getMethodName()
  227. {
  228. static std::string methodName = "aria2.getFiles";
  229. return methodName;
  230. }
  231. };
  232. #ifdef ENABLE_BITTORRENT
  233. class GetPeersRpcMethod:public RpcMethod {
  234. protected:
  235. virtual SharedHandle<ValueBase> process
  236. (const RpcRequest& req, DownloadEngine* e);
  237. public:
  238. static const std::string& getMethodName()
  239. {
  240. static std::string methodName = "aria2.getPeers";
  241. return methodName;
  242. }
  243. };
  244. #endif // ENABLE_BITTORRENT
  245. class GetServersRpcMethod:public RpcMethod {
  246. protected:
  247. virtual SharedHandle<ValueBase> process
  248. (const RpcRequest& req, DownloadEngine* e);
  249. public:
  250. static const std::string& getMethodName()
  251. {
  252. static std::string methodName = "aria2.getServers";
  253. return methodName;
  254. }
  255. };
  256. class TellStatusRpcMethod:public RpcMethod {
  257. protected:
  258. virtual SharedHandle<ValueBase> process
  259. (const RpcRequest& req, DownloadEngine* e);
  260. public:
  261. static const std::string& getMethodName()
  262. {
  263. static std::string methodName = "aria2.tellStatus";
  264. return methodName;
  265. }
  266. };
  267. class TellActiveRpcMethod:public RpcMethod {
  268. protected:
  269. virtual SharedHandle<ValueBase> process
  270. (const RpcRequest& req, DownloadEngine* e);
  271. public:
  272. static const std::string& getMethodName()
  273. {
  274. static std::string methodName = "aria2.tellActive";
  275. return methodName;
  276. }
  277. };
  278. template<typename T>
  279. class AbstractPaginationRpcMethod:public RpcMethod {
  280. private:
  281. template<typename InputIterator>
  282. std::pair<InputIterator, InputIterator>
  283. getPaginationRange
  284. (ssize_t offset, size_t num, InputIterator first, InputIterator last)
  285. {
  286. size_t size = std::distance(first, last);
  287. if(offset < 0) {
  288. ssize_t tempoffset = offset+size;
  289. if(tempoffset < 0) {
  290. return std::make_pair(last, last);
  291. }
  292. offset = tempoffset-(num-1);
  293. if(offset < 0) {
  294. offset = 0;
  295. num = tempoffset+1;
  296. }
  297. } else if(size <= (size_t)offset) {
  298. return std::make_pair(last, last);
  299. }
  300. size_t lastDistance;
  301. if(size < offset+num) {
  302. lastDistance = size;
  303. } else {
  304. lastDistance = offset+num;
  305. }
  306. last = first;
  307. std::advance(first, offset);
  308. std::advance(last, lastDistance);
  309. return std::make_pair(first, last);
  310. }
  311. void checkPaginationParams(const SharedHandle<List>& params) const
  312. {
  313. if(params->size() < 2) {
  314. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  315. }
  316. const Integer* p1 = asInteger(params->get(0));
  317. const Integer* p2 = asInteger(params->get(1));
  318. if(!p1 || !p2 || p2->i() < 0) {
  319. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  320. }
  321. }
  322. protected:
  323. virtual SharedHandle<ValueBase> process
  324. (const RpcRequest& req, DownloadEngine* e)
  325. {
  326. const SharedHandle<List>& params = req.params;
  327. checkPaginationParams(params);
  328. ssize_t offset = req.getIntegerParam(0)->i();
  329. size_t num = req.getIntegerParam(1)->i();
  330. const List* keysParam = req.getListParam(2);
  331. std::vector<std::string> keys;
  332. toStringList(std::back_inserter(keys), keysParam);
  333. const std::deque<SharedHandle<T> >& items = getItems(e);
  334. std::pair<typename std::deque<SharedHandle<T> >::const_iterator,
  335. typename std::deque<SharedHandle<T> >::const_iterator> range =
  336. getPaginationRange(offset, num, items.begin(), items.end());
  337. SharedHandle<List> list = List::g();
  338. for(; range.first != range.second; ++range.first) {
  339. SharedHandle<Dict> entryDict = Dict::g();
  340. createEntry(entryDict, *range.first, e, keys);
  341. list->append(entryDict);
  342. }
  343. if(offset < 0) {
  344. std::reverse(list->begin(), list->end());
  345. }
  346. return list;
  347. }
  348. virtual const std::deque<SharedHandle<T> >&
  349. getItems(DownloadEngine* e) const = 0;
  350. virtual void createEntry
  351. (const SharedHandle<Dict>& entryDict,
  352. const SharedHandle<T>& item,
  353. DownloadEngine* e,
  354. const std::vector<std::string>& keys) const = 0;
  355. };
  356. class TellWaitingRpcMethod:
  357. public AbstractPaginationRpcMethod<RequestGroup> {
  358. protected:
  359. virtual const std::deque<SharedHandle<RequestGroup> >&
  360. getItems(DownloadEngine* e) const;
  361. virtual void createEntry
  362. (const SharedHandle<Dict>& entryDict,
  363. const SharedHandle<RequestGroup>& item,
  364. DownloadEngine* e,
  365. const std::vector<std::string>& keys) const;
  366. public:
  367. static const std::string& getMethodName()
  368. {
  369. static std::string methodName = "aria2.tellWaiting";
  370. return methodName;
  371. }
  372. };
  373. class TellStoppedRpcMethod:
  374. public AbstractPaginationRpcMethod<DownloadResult> {
  375. protected:
  376. virtual const std::deque<SharedHandle<DownloadResult> >&
  377. getItems(DownloadEngine* e) const;
  378. virtual void createEntry
  379. (const SharedHandle<Dict>& entryDict,
  380. const SharedHandle<DownloadResult>& item,
  381. DownloadEngine* e,
  382. const std::vector<std::string>& keys) const;
  383. public:
  384. static const std::string& getMethodName()
  385. {
  386. static std::string methodName = "aria2.tellStopped";
  387. return methodName;
  388. }
  389. };
  390. class ChangeOptionRpcMethod:public RpcMethod {
  391. protected:
  392. virtual SharedHandle<ValueBase> process
  393. (const RpcRequest& req, DownloadEngine* e);
  394. public:
  395. static const std::string& getMethodName()
  396. {
  397. static std::string methodName = "aria2.changeOption";
  398. return methodName;
  399. }
  400. };
  401. class ChangeGlobalOptionRpcMethod:public RpcMethod {
  402. protected:
  403. virtual SharedHandle<ValueBase> process
  404. (const RpcRequest& req, DownloadEngine* e);
  405. public:
  406. static const std::string& getMethodName()
  407. {
  408. static std::string methodName = "aria2.changeGlobalOption";
  409. return methodName;
  410. }
  411. };
  412. class GetVersionRpcMethod:public RpcMethod {
  413. protected:
  414. virtual SharedHandle<ValueBase> process
  415. (const RpcRequest& req, DownloadEngine* e);
  416. public:
  417. static const std::string& getMethodName()
  418. {
  419. static std::string methodName = "aria2.getVersion";
  420. return methodName;
  421. }
  422. };
  423. class GetOptionRpcMethod:public RpcMethod {
  424. protected:
  425. virtual SharedHandle<ValueBase> process
  426. (const RpcRequest& req, DownloadEngine* e);
  427. public:
  428. static const std::string& getMethodName()
  429. {
  430. static std::string methodName = "aria2.getOption";
  431. return methodName;
  432. }
  433. };
  434. class GetGlobalOptionRpcMethod:public RpcMethod {
  435. protected:
  436. virtual SharedHandle<ValueBase> process
  437. (const RpcRequest& req, DownloadEngine* e);
  438. public:
  439. static const std::string& getMethodName()
  440. {
  441. static std::string methodName = "aria2.getGlobalOption";
  442. return methodName;
  443. }
  444. };
  445. class ChangePositionRpcMethod:public RpcMethod {
  446. protected:
  447. virtual SharedHandle<ValueBase> process
  448. (const RpcRequest& req, DownloadEngine* e);
  449. public:
  450. static const std::string& getMethodName()
  451. {
  452. static std::string methodName = "aria2.changePosition";
  453. return methodName;
  454. }
  455. };
  456. class ChangeUriRpcMethod:public RpcMethod {
  457. protected:
  458. virtual SharedHandle<ValueBase> process
  459. (const RpcRequest& req, DownloadEngine* e);
  460. public:
  461. static const std::string& getMethodName()
  462. {
  463. static std::string methodName = "aria2.changeUri";
  464. return methodName;
  465. }
  466. };
  467. class GetSessionInfoRpcMethod:public RpcMethod {
  468. protected:
  469. virtual SharedHandle<ValueBase> process
  470. (const RpcRequest& req, DownloadEngine* e);
  471. public:
  472. static const std::string& getMethodName()
  473. {
  474. static std::string methodName = "aria2.getSessionInfo";
  475. return methodName;
  476. }
  477. };
  478. class ShutdownRpcMethod:public RpcMethod {
  479. protected:
  480. virtual SharedHandle<ValueBase> process
  481. (const RpcRequest& req, DownloadEngine* e);
  482. public:
  483. static const std::string& getMethodName()
  484. {
  485. static std::string methodName = "aria2.shutdown";
  486. return methodName;
  487. }
  488. };
  489. class GetGlobalStatRpcMethod:public RpcMethod {
  490. protected:
  491. virtual SharedHandle<ValueBase> process
  492. (const RpcRequest& req, DownloadEngine* e);
  493. public:
  494. static const std::string& getMethodName()
  495. {
  496. static std::string methodName = "aria2.getGlobalStat";
  497. return methodName;
  498. }
  499. };
  500. class ForceShutdownRpcMethod:public RpcMethod {
  501. protected:
  502. virtual SharedHandle<ValueBase> process
  503. (const RpcRequest& req, DownloadEngine* e);
  504. public:
  505. static const std::string& getMethodName()
  506. {
  507. static std::string methodName = "aria2.forceShutdown";
  508. return methodName;
  509. }
  510. };
  511. class SystemMulticallRpcMethod:public RpcMethod {
  512. protected:
  513. virtual SharedHandle<ValueBase> process
  514. (const RpcRequest& req, DownloadEngine* e);
  515. public:
  516. static const std::string& getMethodName()
  517. {
  518. static std::string methodName = "system.multicall";
  519. return methodName;
  520. }
  521. };
  522. class NoSuchMethodRpcMethod:public RpcMethod {
  523. protected:
  524. virtual SharedHandle<ValueBase> process
  525. (const RpcRequest& req, DownloadEngine* e);
  526. };
  527. // Helper function to store data to entryDict from ds. This function
  528. // is used by tellStatus method.
  529. void gatherStoppedDownload
  530. (const SharedHandle<Dict>& entryDict, const SharedHandle<DownloadResult>& ds,
  531. const std::vector<std::string>& keys);
  532. // Helper function to store data to entryDict from group. This
  533. // function is used by tellStatus/tellActive/tellWaiting method
  534. void gatherProgressCommon
  535. (const SharedHandle<Dict>& entryDict, const SharedHandle<RequestGroup>& group,
  536. const std::vector<std::string>& keys);
  537. #ifdef ENABLE_BITTORRENT
  538. // Helper function to store BitTorrent metadata from torrentAttrs.
  539. void gatherBitTorrentMetadata
  540. (const SharedHandle<Dict>& btDict,
  541. const SharedHandle<TorrentAttribute>& torrentAttrs);
  542. #endif // ENABLE_BITTORRENT
  543. } // namespace rpc
  544. } // namespace aria2
  545. #endif // D_RPC_METHOD_IMPL_H