XmlRpcMethodImpl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_XML_RPC_METHOD_IMPL_H_
  36. #define _D_XML_RPC_METHOD_IMPL_H_
  37. #include "XmlRpcMethod.h"
  38. #include <cassert>
  39. #include <deque>
  40. #include <algorithm>
  41. #include "BDE.h"
  42. #include "XmlRpcRequest.h"
  43. namespace aria2 {
  44. class DownloadResult;
  45. class RequestGroup;
  46. namespace xmlrpc {
  47. class AddUriXmlRpcMethod:public XmlRpcMethod {
  48. protected:
  49. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  50. public:
  51. static const std::string& getMethodName()
  52. {
  53. static std::string methodName = "aria2.addUri";
  54. return methodName;
  55. }
  56. };
  57. class RemoveXmlRpcMethod:public XmlRpcMethod {
  58. protected:
  59. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  60. public:
  61. static const std::string& getMethodName()
  62. {
  63. static std::string methodName = "aria2.remove";
  64. return methodName;
  65. }
  66. };
  67. #ifdef ENABLE_BITTORRENT
  68. class AddTorrentXmlRpcMethod:public XmlRpcMethod {
  69. protected:
  70. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  71. public:
  72. static const std::string& getMethodName()
  73. {
  74. static std::string methodName = "aria2.addTorrent";
  75. return methodName;
  76. }
  77. };
  78. #endif // ENABLE_BITTORRENT
  79. #ifdef ENABLE_METALINK
  80. class AddMetalinkXmlRpcMethod:public XmlRpcMethod {
  81. protected:
  82. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  83. public:
  84. static const std::string& getMethodName()
  85. {
  86. static std::string methodName = "aria2.addMetalink";
  87. return methodName;
  88. }
  89. };
  90. #endif // ENABLE_METALINK
  91. class PurgeDownloadResultXmlRpcMethod:public XmlRpcMethod {
  92. protected:
  93. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  94. public:
  95. static const std::string& getMethodName()
  96. {
  97. static std::string methodName = "aria2.purgeDownloadResult";
  98. return methodName;
  99. }
  100. };
  101. class GetUrisXmlRpcMethod:public XmlRpcMethod {
  102. protected:
  103. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  104. public:
  105. static const std::string& getMethodName()
  106. {
  107. static std::string methodName = "aria2.getUris";
  108. return methodName;
  109. }
  110. };
  111. class GetFilesXmlRpcMethod:public XmlRpcMethod {
  112. protected:
  113. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  114. public:
  115. static const std::string& getMethodName()
  116. {
  117. static std::string methodName = "aria2.getFiles";
  118. return methodName;
  119. }
  120. };
  121. #ifdef ENABLE_BITTORRENT
  122. class GetPeersXmlRpcMethod:public XmlRpcMethod {
  123. protected:
  124. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  125. public:
  126. static const std::string& getMethodName()
  127. {
  128. static std::string methodName = "aria2.getPeers";
  129. return methodName;
  130. }
  131. };
  132. #endif // ENABLE_BITTORRENT
  133. class TellStatusXmlRpcMethod:public XmlRpcMethod {
  134. protected:
  135. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  136. public:
  137. static const std::string& getMethodName()
  138. {
  139. static std::string methodName = "aria2.tellStatus";
  140. return methodName;
  141. }
  142. };
  143. class TellActiveXmlRpcMethod:public XmlRpcMethod {
  144. protected:
  145. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  146. public:
  147. static const std::string& getMethodName()
  148. {
  149. static std::string methodName = "aria2.tellActive";
  150. return methodName;
  151. }
  152. };
  153. template<typename T>
  154. class AbstractPaginationXmlRpcMethod:public XmlRpcMethod {
  155. private:
  156. template<typename InputIterator>
  157. std::pair<InputIterator, InputIterator>
  158. getPaginationRange
  159. (ssize_t offset, size_t num, InputIterator first, InputIterator last)
  160. {
  161. size_t size = std::distance(first, last);
  162. if(offset < 0) {
  163. ssize_t tempoffset = offset+size;
  164. if(tempoffset < 0) {
  165. return std::make_pair(last, last);
  166. }
  167. offset = tempoffset-(num-1);
  168. if(offset < 0) {
  169. offset = 0;
  170. num = tempoffset+1;
  171. }
  172. } else if(size <= (size_t)offset) {
  173. return std::make_pair(last, last);
  174. }
  175. BDE list = BDE::list();
  176. size_t lastDistance;
  177. if(size < offset+num) {
  178. lastDistance = size;
  179. } else {
  180. lastDistance = offset+num;
  181. }
  182. last = first;
  183. std::advance(first, offset);
  184. std::advance(last, lastDistance);
  185. return std::make_pair(first, last);
  186. }
  187. void checkPaginationParams(const BDE& params) const
  188. {
  189. assert(params.isList());
  190. if(params.size() != 2 ||
  191. !params[0].isInteger() || !params[1].isInteger() ||
  192. params[1].i() < 0) {
  193. throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer.");
  194. }
  195. }
  196. protected:
  197. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e)
  198. {
  199. const BDE& params = req._params;
  200. checkPaginationParams(params);
  201. ssize_t offset = params[0].i();
  202. size_t num = params[1].i();
  203. const std::deque<SharedHandle<T> >& items = getItems(e);
  204. std::pair<typename std::deque<SharedHandle<T> >::const_iterator,
  205. typename std::deque<SharedHandle<T> >::const_iterator> range =
  206. getPaginationRange(offset, num, items.begin(), items.end());
  207. BDE list = BDE::list();
  208. for(; range.first != range.second; ++range.first) {
  209. BDE entryDict = BDE::dict();
  210. createEntry(entryDict, *range.first, e);
  211. list << entryDict;
  212. }
  213. if(offset < 0) {
  214. std::reverse(list.listBegin(), list.listEnd());
  215. }
  216. return list;
  217. }
  218. virtual const std::deque<SharedHandle<T> >&
  219. getItems(DownloadEngine* e) const = 0;
  220. virtual void createEntry
  221. (BDE& entryDict, const SharedHandle<T>& item, DownloadEngine* e) const = 0;
  222. };
  223. class TellWaitingXmlRpcMethod:
  224. public AbstractPaginationXmlRpcMethod<RequestGroup> {
  225. protected:
  226. virtual const std::deque<SharedHandle<RequestGroup> >&
  227. getItems(DownloadEngine* e) const;
  228. virtual void createEntry
  229. (BDE& entryDict, const SharedHandle<RequestGroup>& item,
  230. DownloadEngine* e) const;
  231. public:
  232. static const std::string& getMethodName()
  233. {
  234. static std::string methodName = "aria2.tellWaiting";
  235. return methodName;
  236. }
  237. };
  238. class TellStoppedXmlRpcMethod:
  239. public AbstractPaginationXmlRpcMethod<DownloadResult> {
  240. protected:
  241. virtual const std::deque<SharedHandle<DownloadResult> >&
  242. getItems(DownloadEngine* e) const;
  243. virtual void createEntry
  244. (BDE& entryDict, const SharedHandle<DownloadResult>& item,
  245. DownloadEngine* e) const;
  246. public:
  247. static const std::string& getMethodName()
  248. {
  249. static std::string methodName = "aria2.tellStopped";
  250. return methodName;
  251. }
  252. };
  253. class ChangeOptionXmlRpcMethod:public XmlRpcMethod {
  254. protected:
  255. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  256. public:
  257. static const std::string& getMethodName()
  258. {
  259. static std::string methodName = "aria2.changeOption";
  260. return methodName;
  261. }
  262. };
  263. class ChangeGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  264. protected:
  265. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  266. public:
  267. static const std::string& getMethodName()
  268. {
  269. static std::string methodName = "aria2.changeGlobalOption";
  270. return methodName;
  271. }
  272. };
  273. class GetVersionXmlRpcMethod:public XmlRpcMethod {
  274. protected:
  275. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  276. public:
  277. static const std::string& getMethodName()
  278. {
  279. static std::string methodName = "aria2.getVersion";
  280. return methodName;
  281. }
  282. };
  283. class GetOptionXmlRpcMethod:public XmlRpcMethod {
  284. protected:
  285. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  286. public:
  287. static const std::string& getMethodName()
  288. {
  289. static std::string methodName = "aria2.getOption";
  290. return methodName;
  291. }
  292. };
  293. class GetGlobalOptionXmlRpcMethod:public XmlRpcMethod {
  294. protected:
  295. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  296. public:
  297. static const std::string& getMethodName()
  298. {
  299. static std::string methodName = "aria2.getGlobalOption";
  300. return methodName;
  301. }
  302. };
  303. class ChangePositionXmlRpcMethod:public XmlRpcMethod {
  304. protected:
  305. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  306. public:
  307. static const std::string& getMethodName()
  308. {
  309. static std::string methodName = "aria2.changePosition";
  310. return methodName;
  311. }
  312. };
  313. class GetSessionInfoXmlRpcMethod:public XmlRpcMethod {
  314. protected:
  315. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  316. public:
  317. static const std::string& getMethodName()
  318. {
  319. static std::string methodName = "aria2.getSessionInfo";
  320. return methodName;
  321. }
  322. };
  323. class SystemMulticallXmlRpcMethod:public XmlRpcMethod {
  324. protected:
  325. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  326. public:
  327. static const std::string& getMethodName()
  328. {
  329. static std::string methodName = "system.multicall";
  330. return methodName;
  331. }
  332. };
  333. class NoSuchMethodXmlRpcMethod:public XmlRpcMethod {
  334. protected:
  335. virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
  336. };
  337. // Helper function to store data to entryDict from ds. This function
  338. // is used by tellStatus method.
  339. void gatherStoppedDownload
  340. (BDE& entryDict, const SharedHandle<DownloadResult>& ds);
  341. // Helper function to store data to entryDict from group. This
  342. // function is used by tellStatus/tellActive/tellWaiting method
  343. void gatherProgressCommon
  344. (BDE& entryDict, const SharedHandle<RequestGroup>& group);
  345. } // namespace xmlrpc
  346. } // namespace aria2
  347. #endif // _D_XML_RPC_METHOD_IMPL_H_