XmlRpcMethodImpl.h 10 KB

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