RpcMethodFactory.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. SharedHandle<RpcMethod> getNoSuchMethod()
  43. {
  44. static SharedHandle<RpcMethod> m(new NoSuchMethodRpcMethod());
  45. return m;
  46. }
  47. } // namespace
  48. namespace {
  49. SharedHandle<RpcMethod>
  50. createMethod(const std::string& methodName)
  51. {
  52. if(methodName == AddUriRpcMethod::getMethodName()) {
  53. return SharedHandle<RpcMethod>(new AddUriRpcMethod());
  54. #ifdef ENABLE_BITTORRENT
  55. } else if(methodName == AddTorrentRpcMethod::getMethodName()) {
  56. return SharedHandle<RpcMethod>(new AddTorrentRpcMethod());
  57. #endif // ENABLE_BITTORRENT
  58. #ifdef ENABLE_METALINK
  59. }
  60. else if(methodName == AddMetalinkRpcMethod::getMethodName()) {
  61. return SharedHandle<RpcMethod>(new AddMetalinkRpcMethod());
  62. #endif // ENABLE_METALINK
  63. }
  64. else if(methodName == RemoveRpcMethod::getMethodName()) {
  65. return SharedHandle<RpcMethod>(new RemoveRpcMethod());
  66. } else if(methodName == PauseRpcMethod::getMethodName()) {
  67. return SharedHandle<RpcMethod>(new PauseRpcMethod());
  68. } else if(methodName == ForcePauseRpcMethod::getMethodName()) {
  69. return SharedHandle<RpcMethod>(new ForcePauseRpcMethod());
  70. } else if(methodName == PauseAllRpcMethod::getMethodName()) {
  71. return SharedHandle<RpcMethod>(new PauseAllRpcMethod());
  72. } else if(methodName == ForcePauseAllRpcMethod::getMethodName()) {
  73. return SharedHandle<RpcMethod>(new ForcePauseAllRpcMethod());
  74. } else if(methodName == UnpauseRpcMethod::getMethodName()) {
  75. return SharedHandle<RpcMethod>(new UnpauseRpcMethod());
  76. } else if(methodName == UnpauseAllRpcMethod::getMethodName()) {
  77. return SharedHandle<RpcMethod>(new UnpauseAllRpcMethod());
  78. } else if(methodName == ForceRemoveRpcMethod::getMethodName()) {
  79. return SharedHandle<RpcMethod>(new ForceRemoveRpcMethod());
  80. } else if(methodName == ChangePositionRpcMethod::getMethodName()) {
  81. return SharedHandle<RpcMethod>(new ChangePositionRpcMethod());
  82. } else if(methodName == TellStatusRpcMethod::getMethodName()) {
  83. return SharedHandle<RpcMethod>(new TellStatusRpcMethod());
  84. } else if(methodName == GetUrisRpcMethod::getMethodName()) {
  85. return SharedHandle<RpcMethod>(new GetUrisRpcMethod());
  86. } else if(methodName == GetFilesRpcMethod::getMethodName()) {
  87. return SharedHandle<RpcMethod>(new GetFilesRpcMethod());
  88. #ifdef ENABLE_BITTORRENT
  89. }
  90. else if(methodName == GetPeersRpcMethod::getMethodName()) {
  91. return SharedHandle<RpcMethod>(new GetPeersRpcMethod());
  92. #endif // ENABLE_BITTORRENT
  93. } else if(methodName == GetServersRpcMethod::getMethodName()) {
  94. return SharedHandle<RpcMethod>(new GetServersRpcMethod());
  95. } else if(methodName == TellActiveRpcMethod::getMethodName()) {
  96. return SharedHandle<RpcMethod>(new TellActiveRpcMethod());
  97. } else if(methodName == TellWaitingRpcMethod::getMethodName()) {
  98. return SharedHandle<RpcMethod>(new TellWaitingRpcMethod());
  99. } else if(methodName == TellStoppedRpcMethod::getMethodName()) {
  100. return SharedHandle<RpcMethod>(new TellStoppedRpcMethod());
  101. } else if(methodName == GetOptionRpcMethod::getMethodName()) {
  102. return SharedHandle<RpcMethod>(new GetOptionRpcMethod());
  103. } else if(methodName == ChangeUriRpcMethod::getMethodName()) {
  104. return SharedHandle<RpcMethod>(new ChangeUriRpcMethod());
  105. } else if(methodName == ChangeOptionRpcMethod::getMethodName()) {
  106. return SharedHandle<RpcMethod>(new ChangeOptionRpcMethod());
  107. } else if(methodName == GetGlobalOptionRpcMethod::getMethodName()) {
  108. return SharedHandle<RpcMethod>(new GetGlobalOptionRpcMethod());
  109. } else if(methodName == ChangeGlobalOptionRpcMethod::getMethodName()) {
  110. return SharedHandle<RpcMethod>(new ChangeGlobalOptionRpcMethod());
  111. } else if(methodName == PurgeDownloadResultRpcMethod::getMethodName()) {
  112. return SharedHandle<RpcMethod>(new PurgeDownloadResultRpcMethod());
  113. } else if(methodName == RemoveDownloadResultRpcMethod::getMethodName()) {
  114. return SharedHandle<RpcMethod>(new RemoveDownloadResultRpcMethod());
  115. } else if(methodName == GetVersionRpcMethod::getMethodName()) {
  116. return SharedHandle<RpcMethod>(new GetVersionRpcMethod());
  117. } else if(methodName == GetSessionInfoRpcMethod::getMethodName()) {
  118. return SharedHandle<RpcMethod>(new GetSessionInfoRpcMethod());
  119. } else if(methodName == ShutdownRpcMethod::getMethodName()) {
  120. return SharedHandle<RpcMethod>(new ShutdownRpcMethod());
  121. } else if(methodName == ForceShutdownRpcMethod::getMethodName()) {
  122. return SharedHandle<RpcMethod>(new ForceShutdownRpcMethod());
  123. } else if(methodName == GetGlobalStatRpcMethod::getMethodName()) {
  124. return SharedHandle<RpcMethod>(new GetGlobalStatRpcMethod());
  125. } else if(methodName == SystemMulticallRpcMethod::getMethodName()) {
  126. return SharedHandle<RpcMethod>(new SystemMulticallRpcMethod());
  127. } else {
  128. return SharedHandle<RpcMethod>();
  129. }
  130. }
  131. } // namespace
  132. std::map<std::string, SharedHandle<RpcMethod> > RpcMethodFactory::cache_;
  133. SharedHandle<RpcMethod>
  134. RpcMethodFactory::create(const std::string& methodName)
  135. {
  136. std::map<std::string, SharedHandle<RpcMethod> >::const_iterator itr =
  137. cache_.find(methodName);
  138. if(itr == cache_.end()) {
  139. SharedHandle<RpcMethod> m = createMethod(methodName);
  140. if(m) {
  141. cache_.insert(std::make_pair(methodName, m));
  142. return m;
  143. } else {
  144. return getNoSuchMethod();
  145. }
  146. } else {
  147. return (*itr).second;
  148. }
  149. }
  150. } // namespace rpc
  151. } // namespace aria2