From 59e63d956ee63d99202b67a32e8a277f3b04650d Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 11 Jul 2013 21:50:47 +0900 Subject: [PATCH] Rewrite RPC method factory function --- src/HttpServerBodyCommand.cc | 4 +- src/RpcMethodFactory.cc | 101 +++++++++++++++++------------------ src/RpcMethodFactory.h | 8 +-- src/RpcMethodImpl.cc | 3 +- src/rpc_helper.cc | 12 +---- 5 files changed, 56 insertions(+), 72 deletions(-) diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index 67d660db..3922ff0a 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -226,9 +226,9 @@ bool HttpServerBodyCommand::execute() addHttpServerResponseCommand(); return true; } - auto method = rpc::RpcMethodFactory::create(req.methodName); A2_LOG_INFO(fmt("Executing RPC method %s", req.methodName.c_str())); - rpc::RpcResponse res = method->execute(std::move(req), e_); + auto res = rpc::getMethod(req.methodName) + ->execute(std::move(req), e_); bool gzip = httpServer_->supportsGZip(); std::string responseData = rpc::toXml(res, gzip); httpServer_->feedResponse(std::move(responseData), "text/xml"); diff --git a/src/RpcMethodFactory.cc b/src/RpcMethodFactory.cc index 845a7856..8813a17b 100644 --- a/src/RpcMethodFactory.cc +++ b/src/RpcMethodFactory.cc @@ -42,115 +42,114 @@ namespace aria2 { namespace rpc { namespace { -std::shared_ptr getNoSuchMethod() -{ - static std::shared_ptr m(new NoSuchMethodRpcMethod()); - return m; -} +std::map> cache; } // namespace namespace { -std::shared_ptr +std::unique_ptr noSuchRpcMethod; +} // namespace + +namespace { +std::unique_ptr createMethod(const std::string& methodName) { if(methodName == AddUriRpcMethod::getMethodName()) { - return std::shared_ptr(new AddUriRpcMethod()); + return make_unique(); #ifdef ENABLE_BITTORRENT } else if(methodName == AddTorrentRpcMethod::getMethodName()) { - return std::shared_ptr(new AddTorrentRpcMethod()); + return make_unique(); #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK } else if(methodName == AddMetalinkRpcMethod::getMethodName()) { - return std::shared_ptr(new AddMetalinkRpcMethod()); + return make_unique(); #endif // ENABLE_METALINK } else if(methodName == RemoveRpcMethod::getMethodName()) { - return std::shared_ptr(new RemoveRpcMethod()); + return make_unique(); } else if(methodName == PauseRpcMethod::getMethodName()) { - return std::shared_ptr(new PauseRpcMethod()); + return make_unique(); } else if(methodName == ForcePauseRpcMethod::getMethodName()) { - return std::shared_ptr(new ForcePauseRpcMethod()); + return make_unique(); } else if(methodName == PauseAllRpcMethod::getMethodName()) { - return std::shared_ptr(new PauseAllRpcMethod()); + return make_unique(); } else if(methodName == ForcePauseAllRpcMethod::getMethodName()) { - return std::shared_ptr(new ForcePauseAllRpcMethod()); + return make_unique(); } else if(methodName == UnpauseRpcMethod::getMethodName()) { - return std::shared_ptr(new UnpauseRpcMethod()); + return make_unique(); } else if(methodName == UnpauseAllRpcMethod::getMethodName()) { - return std::shared_ptr(new UnpauseAllRpcMethod()); + return make_unique(); } else if(methodName == ForceRemoveRpcMethod::getMethodName()) { - return std::shared_ptr(new ForceRemoveRpcMethod()); + return make_unique(); } else if(methodName == ChangePositionRpcMethod::getMethodName()) { - return std::shared_ptr(new ChangePositionRpcMethod()); + return make_unique(); } else if(methodName == TellStatusRpcMethod::getMethodName()) { - return std::shared_ptr(new TellStatusRpcMethod()); + return make_unique(); } else if(methodName == GetUrisRpcMethod::getMethodName()) { - return std::shared_ptr(new GetUrisRpcMethod()); + return make_unique(); } else if(methodName == GetFilesRpcMethod::getMethodName()) { - return std::shared_ptr(new GetFilesRpcMethod()); + return make_unique(); #ifdef ENABLE_BITTORRENT } else if(methodName == GetPeersRpcMethod::getMethodName()) { - return std::shared_ptr(new GetPeersRpcMethod()); + return make_unique(); #endif // ENABLE_BITTORRENT } else if(methodName == GetServersRpcMethod::getMethodName()) { - return std::shared_ptr(new GetServersRpcMethod()); + return make_unique(); } else if(methodName == TellActiveRpcMethod::getMethodName()) { - return std::shared_ptr(new TellActiveRpcMethod()); + return make_unique(); } else if(methodName == TellWaitingRpcMethod::getMethodName()) { - return std::shared_ptr(new TellWaitingRpcMethod()); + return make_unique(); } else if(methodName == TellStoppedRpcMethod::getMethodName()) { - return std::shared_ptr(new TellStoppedRpcMethod()); + return make_unique(); } else if(methodName == GetOptionRpcMethod::getMethodName()) { - return std::shared_ptr(new GetOptionRpcMethod()); + return make_unique(); } else if(methodName == ChangeUriRpcMethod::getMethodName()) { - return std::shared_ptr(new ChangeUriRpcMethod()); + return make_unique(); } else if(methodName == ChangeOptionRpcMethod::getMethodName()) { - return std::shared_ptr(new ChangeOptionRpcMethod()); + return make_unique(); } else if(methodName == GetGlobalOptionRpcMethod::getMethodName()) { - return std::shared_ptr(new GetGlobalOptionRpcMethod()); + return make_unique(); } else if(methodName == ChangeGlobalOptionRpcMethod::getMethodName()) { - return std::shared_ptr(new ChangeGlobalOptionRpcMethod()); + return make_unique(); } else if(methodName == PurgeDownloadResultRpcMethod::getMethodName()) { - return std::shared_ptr(new PurgeDownloadResultRpcMethod()); + return make_unique(); } else if(methodName == RemoveDownloadResultRpcMethod::getMethodName()) { - return std::shared_ptr(new RemoveDownloadResultRpcMethod()); + return make_unique(); } else if(methodName == GetVersionRpcMethod::getMethodName()) { - return std::shared_ptr(new GetVersionRpcMethod()); + return make_unique(); } else if(methodName == GetSessionInfoRpcMethod::getMethodName()) { - return std::shared_ptr(new GetSessionInfoRpcMethod()); + return make_unique(); } else if(methodName == ShutdownRpcMethod::getMethodName()) { - return std::shared_ptr(new ShutdownRpcMethod()); + return make_unique(); } else if(methodName == ForceShutdownRpcMethod::getMethodName()) { - return std::shared_ptr(new ForceShutdownRpcMethod()); + return make_unique(); } else if(methodName == GetGlobalStatRpcMethod::getMethodName()) { - return std::shared_ptr(new GetGlobalStatRpcMethod()); + return make_unique(); } else if(methodName == SystemMulticallRpcMethod::getMethodName()) { - return std::shared_ptr(new SystemMulticallRpcMethod()); + return make_unique(); } else { return nullptr; } } } // namespace -std::map > RpcMethodFactory::cache_; - -std::shared_ptr -RpcMethodFactory::create(const std::string& methodName) +RpcMethod* getMethod(const std::string& methodName) { - std::map >::const_iterator itr = - cache_.find(methodName); - if(itr == cache_.end()) { - std::shared_ptr m = createMethod(methodName); + auto itr = cache.find(methodName); + if(itr == cache.end()) { + auto m = createMethod(methodName); if(m) { - cache_.insert(std::make_pair(methodName, m)); - return m; + auto rv = cache.insert(std::make_pair(methodName, std::move(m))); + return (*rv.first).second.get(); } else { - return getNoSuchMethod(); + if(!noSuchRpcMethod) { + noSuchRpcMethod = make_unique(); + } + return noSuchRpcMethod.get(); } } else { - return (*itr).second; + return (*itr).second.get(); } } diff --git a/src/RpcMethodFactory.h b/src/RpcMethodFactory.h index 5d479d56..07f83c58 100644 --- a/src/RpcMethodFactory.h +++ b/src/RpcMethodFactory.h @@ -38,7 +38,6 @@ #include "common.h" #include -#include #include namespace aria2 { @@ -47,12 +46,7 @@ namespace rpc { class RpcMethod; -class RpcMethodFactory { -public: - static std::shared_ptr create(const std::string& methodName); -private: - static std::map > cache_; -}; +RpcMethod* getMethod(const std::string& methodName); } // namespace rpc diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 1edccd63..c7ea16e6 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -1381,8 +1381,7 @@ std::unique_ptr SystemMulticallRpcMethod::process } else { paramsList = List::g(); } - auto method = RpcMethodFactory::create(methodName->s()); - RpcResponse res = method->execute + RpcResponse res = getMethod(methodName->s())->execute ({methodName->s(), std::move(paramsList), nullptr, req.jsonRpc}, e); if(res.code == 0) { auto l = List::g(); diff --git a/src/rpc_helper.cc b/src/rpc_helper.cc index 3b953e15..d88ae529 100644 --- a/src/rpc_helper.cc +++ b/src/rpc_helper.cc @@ -98,17 +98,9 @@ RpcResponse processJsonRpcRequest(Dict* jsondict, DownloadEngine* e) return createJsonRpcErrorResponse(-32602, "Invalid params.", std::move(id)); } - std::shared_ptr method; - try { - method = rpc::RpcMethodFactory::create(methodName->s()); - } catch(RecoverableException& e) { - A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e); - return createJsonRpcErrorResponse(-32601, "Method not found.", - std::move(id)); - } A2_LOG_INFO(fmt("Executing RPC method %s", methodName->s().c_str())); - return method->execute({methodName->s(), std::move(params), - std::move(id), true}, e); + return getMethod(methodName->s())->execute + ({methodName->s(), std::move(params), std::move(id), true}, e); } } // namespace rpc