RpcMethodFactory.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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", "aria2.getPeers",
  52. #endif // ENABLE_BITTORRENT
  53. #ifdef ENABLE_METALINK
  54. "aria2.addMetalink",
  55. #endif // ENABLE_METALINK
  56. "aria2.remove", "aria2.pause", "aria2.forcePause", "aria2.pauseAll",
  57. "aria2.forcePauseAll", "aria2.unpause", "aria2.unpauseAll",
  58. "aria2.forceRemove", "aria2.changePosition", "aria2.tellStatus",
  59. "aria2.getUris", "aria2.getFiles", "aria2.getServers", "aria2.tellActive",
  60. "aria2.tellWaiting", "aria2.tellStopped", "aria2.getOption",
  61. "aria2.changeUri", "aria2.changeOption", "aria2.getGlobalOption",
  62. "aria2.changeGlobalOption", "aria2.purgeDownloadResult",
  63. "aria2.removeDownloadResult", "aria2.getVersion", "aria2.getSessionInfo",
  64. "aria2.shutdown", "aria2.forceShutdown", "aria2.getGlobalStat",
  65. "aria2.saveSession", "system.multicall", "system.listMethods",
  66. };
  67. } // namespace
  68. const std::vector<std::string>& allMethodNames() { return rpcMethodNames; }
  69. namespace {
  70. std::unique_ptr<RpcMethod> createMethod(const std::string& methodName)
  71. {
  72. if (methodName == AddUriRpcMethod::getMethodName()) {
  73. return make_unique<AddUriRpcMethod>();
  74. }
  75. #ifdef ENABLE_BITTORRENT
  76. if (methodName == AddTorrentRpcMethod::getMethodName()) {
  77. return make_unique<AddTorrentRpcMethod>();
  78. }
  79. if (methodName == GetPeersRpcMethod::getMethodName()) {
  80. return make_unique<GetPeersRpcMethod>();
  81. }
  82. #endif // ENABLE_BITTORRENT
  83. #ifdef ENABLE_METALINK
  84. if (methodName == AddMetalinkRpcMethod::getMethodName()) {
  85. return make_unique<AddMetalinkRpcMethod>();
  86. }
  87. #endif // ENABLE_METALINK
  88. if (methodName == RemoveRpcMethod::getMethodName()) {
  89. return make_unique<RemoveRpcMethod>();
  90. }
  91. if (methodName == PauseRpcMethod::getMethodName()) {
  92. return make_unique<PauseRpcMethod>();
  93. }
  94. if (methodName == ForcePauseRpcMethod::getMethodName()) {
  95. return make_unique<ForcePauseRpcMethod>();
  96. }
  97. if (methodName == PauseAllRpcMethod::getMethodName()) {
  98. return make_unique<PauseAllRpcMethod>();
  99. }
  100. if (methodName == ForcePauseAllRpcMethod::getMethodName()) {
  101. return make_unique<ForcePauseAllRpcMethod>();
  102. }
  103. if (methodName == UnpauseRpcMethod::getMethodName()) {
  104. return make_unique<UnpauseRpcMethod>();
  105. }
  106. if (methodName == UnpauseAllRpcMethod::getMethodName()) {
  107. return make_unique<UnpauseAllRpcMethod>();
  108. }
  109. if (methodName == ForceRemoveRpcMethod::getMethodName()) {
  110. return make_unique<ForceRemoveRpcMethod>();
  111. }
  112. if (methodName == ChangePositionRpcMethod::getMethodName()) {
  113. return make_unique<ChangePositionRpcMethod>();
  114. }
  115. if (methodName == TellStatusRpcMethod::getMethodName()) {
  116. return make_unique<TellStatusRpcMethod>();
  117. }
  118. if (methodName == GetUrisRpcMethod::getMethodName()) {
  119. return make_unique<GetUrisRpcMethod>();
  120. }
  121. if (methodName == GetFilesRpcMethod::getMethodName()) {
  122. return make_unique<GetFilesRpcMethod>();
  123. }
  124. if (methodName == GetServersRpcMethod::getMethodName()) {
  125. return make_unique<GetServersRpcMethod>();
  126. }
  127. if (methodName == TellActiveRpcMethod::getMethodName()) {
  128. return make_unique<TellActiveRpcMethod>();
  129. }
  130. if (methodName == TellWaitingRpcMethod::getMethodName()) {
  131. return make_unique<TellWaitingRpcMethod>();
  132. }
  133. if (methodName == TellStoppedRpcMethod::getMethodName()) {
  134. return make_unique<TellStoppedRpcMethod>();
  135. }
  136. if (methodName == GetOptionRpcMethod::getMethodName()) {
  137. return make_unique<GetOptionRpcMethod>();
  138. }
  139. if (methodName == ChangeUriRpcMethod::getMethodName()) {
  140. return make_unique<ChangeUriRpcMethod>();
  141. }
  142. if (methodName == ChangeOptionRpcMethod::getMethodName()) {
  143. return make_unique<ChangeOptionRpcMethod>();
  144. }
  145. if (methodName == GetGlobalOptionRpcMethod::getMethodName()) {
  146. return make_unique<GetGlobalOptionRpcMethod>();
  147. }
  148. if (methodName == ChangeGlobalOptionRpcMethod::getMethodName()) {
  149. return make_unique<ChangeGlobalOptionRpcMethod>();
  150. }
  151. if (methodName == PurgeDownloadResultRpcMethod::getMethodName()) {
  152. return make_unique<PurgeDownloadResultRpcMethod>();
  153. }
  154. if (methodName == RemoveDownloadResultRpcMethod::getMethodName()) {
  155. return make_unique<RemoveDownloadResultRpcMethod>();
  156. }
  157. if (methodName == GetVersionRpcMethod::getMethodName()) {
  158. return make_unique<GetVersionRpcMethod>();
  159. }
  160. if (methodName == GetSessionInfoRpcMethod::getMethodName()) {
  161. return make_unique<GetSessionInfoRpcMethod>();
  162. }
  163. if (methodName == ShutdownRpcMethod::getMethodName()) {
  164. return make_unique<ShutdownRpcMethod>();
  165. }
  166. if (methodName == ForceShutdownRpcMethod::getMethodName()) {
  167. return make_unique<ForceShutdownRpcMethod>();
  168. }
  169. if (methodName == GetGlobalStatRpcMethod::getMethodName()) {
  170. return make_unique<GetGlobalStatRpcMethod>();
  171. }
  172. if (methodName == SaveSessionRpcMethod::getMethodName()) {
  173. return make_unique<SaveSessionRpcMethod>();
  174. }
  175. if (methodName == SystemMulticallRpcMethod::getMethodName()) {
  176. return make_unique<SystemMulticallRpcMethod>();
  177. }
  178. if (methodName == SystemListMethodsRpcMethod::getMethodName()) {
  179. return make_unique<SystemListMethodsRpcMethod>();
  180. }
  181. return nullptr;
  182. }
  183. } // namespace
  184. RpcMethod* getMethod(const std::string& methodName)
  185. {
  186. auto itr = cache.find(methodName);
  187. if (itr == std::end(cache)) {
  188. auto m = createMethod(methodName);
  189. if (m) {
  190. auto rv = cache.insert(std::make_pair(methodName, std::move(m)));
  191. return (*rv.first).second.get();
  192. }
  193. if (!noSuchRpcMethod) {
  194. noSuchRpcMethod = make_unique<NoSuchMethodRpcMethod>();
  195. }
  196. return noSuchRpcMethod.get();
  197. }
  198. return (*itr).second.get();
  199. }
  200. } // namespace rpc
  201. } // namespace aria2