RpcMethodFactory.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "RpcMethodFactory.h"
  36. #include "RpcMethodImpl.h"
  37. #include "OptionParser.h"
  38. #include "OptionHandler.h"
  39. namespace aria2 {
  40. namespace rpc {
  41. namespace {
  42. std::map<std::string, std::unique_ptr<RpcMethod>> cache;
  43. } // namespace
  44. namespace {
  45. std::unique_ptr<RpcMethod> noSuchRpcMethod;
  46. } // namespace
  47. namespace {
  48. std::vector<std::string> rpcMethodNames = {
  49. "aria2.addUri",
  50. #ifdef ENABLE_BITTORRENT
  51. "aria2.addTorrent",
  52. "aria2.getPeers",
  53. #endif // ENABLE_BITTORRENT
  54. #ifdef ENABLE_METALINK
  55. "aria2.addMetalink",
  56. #endif // ENABLE_METALINK
  57. "aria2.remove",
  58. "aria2.pause",
  59. "aria2.forcePause",
  60. "aria2.pauseAll",
  61. "aria2.forcePauseAll",
  62. "aria2.unpause",
  63. "aria2.unpauseAll",
  64. "aria2.forceRemove",
  65. "aria2.changePosition",
  66. "aria2.tellStatus",
  67. "aria2.getUris",
  68. "aria2.getFiles",
  69. "aria2.getServers",
  70. "aria2.tellActive",
  71. "aria2.tellWaiting",
  72. "aria2.tellStopped",
  73. "aria2.getOption",
  74. "aria2.changeUri",
  75. "aria2.changeOption",
  76. "aria2.getGlobalOption",
  77. "aria2.changeGlobalOption",
  78. "aria2.purgeDownloadResult",
  79. "aria2.removeDownloadResult",
  80. "aria2.getVersion",
  81. "aria2.getSessionInfo",
  82. "aria2.shutdown",
  83. "aria2.forceShutdown",
  84. "aria2.getGlobalStat",
  85. "aria2.saveSession",
  86. "system.multicall",
  87. "system.listMethods",
  88. "system.listNotifications",
  89. };
  90. } // namespace
  91. const std::vector<std::string>& allMethodNames() { return rpcMethodNames; }
  92. namespace {
  93. std::vector<std::string> rpcNotificationsNames = {
  94. "aria2.onDownloadStart", "aria2.onDownloadPause",
  95. "aria2.onDownloadStop", "aria2.onDownloadComplete",
  96. "aria2.onDownloadError",
  97. #ifdef ENABLE_BITTORRENT
  98. "aria2.onBtDownloadComplete",
  99. #endif // ENABLE_BITTORRENT
  100. };
  101. } // namespace
  102. const std::vector<std::string>& allNotificationsNames()
  103. {
  104. return rpcNotificationsNames;
  105. }
  106. namespace {
  107. std::unique_ptr<RpcMethod> createMethod(const std::string& methodName)
  108. {
  109. if (methodName == AddUriRpcMethod::getMethodName()) {
  110. return make_unique<AddUriRpcMethod>();
  111. }
  112. #ifdef ENABLE_BITTORRENT
  113. if (methodName == AddTorrentRpcMethod::getMethodName()) {
  114. return make_unique<AddTorrentRpcMethod>();
  115. }
  116. if (methodName == GetPeersRpcMethod::getMethodName()) {
  117. return make_unique<GetPeersRpcMethod>();
  118. }
  119. #endif // ENABLE_BITTORRENT
  120. #ifdef ENABLE_METALINK
  121. if (methodName == AddMetalinkRpcMethod::getMethodName()) {
  122. return make_unique<AddMetalinkRpcMethod>();
  123. }
  124. #endif // ENABLE_METALINK
  125. if (methodName == RemoveRpcMethod::getMethodName()) {
  126. return make_unique<RemoveRpcMethod>();
  127. }
  128. if (methodName == PauseRpcMethod::getMethodName()) {
  129. return make_unique<PauseRpcMethod>();
  130. }
  131. if (methodName == ForcePauseRpcMethod::getMethodName()) {
  132. return make_unique<ForcePauseRpcMethod>();
  133. }
  134. if (methodName == PauseAllRpcMethod::getMethodName()) {
  135. return make_unique<PauseAllRpcMethod>();
  136. }
  137. if (methodName == ForcePauseAllRpcMethod::getMethodName()) {
  138. return make_unique<ForcePauseAllRpcMethod>();
  139. }
  140. if (methodName == UnpauseRpcMethod::getMethodName()) {
  141. return make_unique<UnpauseRpcMethod>();
  142. }
  143. if (methodName == UnpauseAllRpcMethod::getMethodName()) {
  144. return make_unique<UnpauseAllRpcMethod>();
  145. }
  146. if (methodName == ForceRemoveRpcMethod::getMethodName()) {
  147. return make_unique<ForceRemoveRpcMethod>();
  148. }
  149. if (methodName == ChangePositionRpcMethod::getMethodName()) {
  150. return make_unique<ChangePositionRpcMethod>();
  151. }
  152. if (methodName == TellStatusRpcMethod::getMethodName()) {
  153. return make_unique<TellStatusRpcMethod>();
  154. }
  155. if (methodName == GetUrisRpcMethod::getMethodName()) {
  156. return make_unique<GetUrisRpcMethod>();
  157. }
  158. if (methodName == GetFilesRpcMethod::getMethodName()) {
  159. return make_unique<GetFilesRpcMethod>();
  160. }
  161. if (methodName == GetServersRpcMethod::getMethodName()) {
  162. return make_unique<GetServersRpcMethod>();
  163. }
  164. if (methodName == TellActiveRpcMethod::getMethodName()) {
  165. return make_unique<TellActiveRpcMethod>();
  166. }
  167. if (methodName == TellWaitingRpcMethod::getMethodName()) {
  168. return make_unique<TellWaitingRpcMethod>();
  169. }
  170. if (methodName == TellStoppedRpcMethod::getMethodName()) {
  171. return make_unique<TellStoppedRpcMethod>();
  172. }
  173. if (methodName == GetOptionRpcMethod::getMethodName()) {
  174. return make_unique<GetOptionRpcMethod>();
  175. }
  176. if (methodName == ChangeUriRpcMethod::getMethodName()) {
  177. return make_unique<ChangeUriRpcMethod>();
  178. }
  179. if (methodName == ChangeOptionRpcMethod::getMethodName()) {
  180. return make_unique<ChangeOptionRpcMethod>();
  181. }
  182. if (methodName == GetGlobalOptionRpcMethod::getMethodName()) {
  183. return make_unique<GetGlobalOptionRpcMethod>();
  184. }
  185. if (methodName == ChangeGlobalOptionRpcMethod::getMethodName()) {
  186. return make_unique<ChangeGlobalOptionRpcMethod>();
  187. }
  188. if (methodName == PurgeDownloadResultRpcMethod::getMethodName()) {
  189. return make_unique<PurgeDownloadResultRpcMethod>();
  190. }
  191. if (methodName == RemoveDownloadResultRpcMethod::getMethodName()) {
  192. return make_unique<RemoveDownloadResultRpcMethod>();
  193. }
  194. if (methodName == GetVersionRpcMethod::getMethodName()) {
  195. return make_unique<GetVersionRpcMethod>();
  196. }
  197. if (methodName == GetSessionInfoRpcMethod::getMethodName()) {
  198. return make_unique<GetSessionInfoRpcMethod>();
  199. }
  200. if (methodName == ShutdownRpcMethod::getMethodName()) {
  201. return make_unique<ShutdownRpcMethod>();
  202. }
  203. if (methodName == ForceShutdownRpcMethod::getMethodName()) {
  204. return make_unique<ForceShutdownRpcMethod>();
  205. }
  206. if (methodName == GetGlobalStatRpcMethod::getMethodName()) {
  207. return make_unique<GetGlobalStatRpcMethod>();
  208. }
  209. if (methodName == SaveSessionRpcMethod::getMethodName()) {
  210. return make_unique<SaveSessionRpcMethod>();
  211. }
  212. if (methodName == SystemMulticallRpcMethod::getMethodName()) {
  213. return make_unique<SystemMulticallRpcMethod>();
  214. }
  215. if (methodName == SystemListMethodsRpcMethod::getMethodName()) {
  216. return make_unique<SystemListMethodsRpcMethod>();
  217. }
  218. if (methodName == SystemListNotificationsRpcMethod::getMethodName()) {
  219. return make_unique<SystemListNotificationsRpcMethod>();
  220. }
  221. return nullptr;
  222. }
  223. } // namespace
  224. RpcMethod* getMethod(const std::string& methodName)
  225. {
  226. auto itr = cache.find(methodName);
  227. if (itr == std::end(cache)) {
  228. auto m = createMethod(methodName);
  229. if (m) {
  230. auto rv = cache.insert(std::make_pair(methodName, std::move(m)));
  231. return (*rv.first).second.get();
  232. }
  233. if (!noSuchRpcMethod) {
  234. noSuchRpcMethod = make_unique<NoSuchMethodRpcMethod>();
  235. }
  236. return noSuchRpcMethod.get();
  237. }
  238. return (*itr).second.get();
  239. }
  240. } // namespace rpc
  241. } // namespace aria2