From f0cfbb21c1c0e68aef0de8e2380ac744be832d13 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 14 Mar 2011 16:38:54 +0900 Subject: Renamed classes in abstract layer of RPC service from XmlRpc* to Rpc*. Now JSON-RPC is available by default regardless of XML library. XML-RPC becomes available when XML library is available. --- src/RpcMethodImpl.h | 586 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 586 insertions(+) create mode 100644 src/RpcMethodImpl.h (limited to 'src/RpcMethodImpl.h') diff --git a/src/RpcMethodImpl.h b/src/RpcMethodImpl.h new file mode 100644 index 00000000..b83c4dfe --- /dev/null +++ b/src/RpcMethodImpl.h @@ -0,0 +1,586 @@ +/* */ +#ifndef D_RPC_METHOD_IMPL_H +#define D_RPC_METHOD_IMPL_H + +#include "RpcMethod.h" + +#include +#include +#include + +#include "RpcRequest.h" +#include "ValueBase.h" +#include "TorrentAttribute.h" +#include "DlAbortEx.h" + +namespace aria2 { + +struct DownloadResult; +class RequestGroup; + +namespace rpc { + +template +void toStringList(OutputIterator out, const List* src) +{ + if(!src) { + return; + } + for(List::ValueType::const_iterator i = src->begin(), eoi = src->end(); + i != eoi; ++i) { + const String* s = asString(*i); + if(s) { + *out++ = s->s(); + } + } +} + +class AddUriRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.addUri"; + return methodName; + } +}; + +class RemoveRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.remove"; + return methodName; + } +}; + +class ForceRemoveRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.forceRemove"; + return methodName; + } +}; + +class PauseRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.pause"; + return methodName; + } +}; + +class ForcePauseRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.forcePause"; + return methodName; + } +}; + +class PauseAllRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.pauseAll"; + return methodName; + } +}; + +class ForcePauseAllRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.forcePauseAll"; + return methodName; + } +}; + +class UnpauseRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.unpause"; + return methodName; + } +}; + +class UnpauseAllRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.unpauseAll"; + return methodName; + } +}; + +#ifdef ENABLE_BITTORRENT +class AddTorrentRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.addTorrent"; + return methodName; + } +}; +#endif // ENABLE_BITTORRENT + +#ifdef ENABLE_METALINK +class AddMetalinkRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.addMetalink"; + return methodName; + } +}; +#endif // ENABLE_METALINK + +class PurgeDownloadResultRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.purgeDownloadResult"; + return methodName; + } +}; + +class RemoveDownloadResultRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.removeDownloadResult"; + return methodName; + } +}; + +class GetUrisRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getUris"; + return methodName; + } +}; + +class GetFilesRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getFiles"; + return methodName; + } +}; + +#ifdef ENABLE_BITTORRENT +class GetPeersRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getPeers"; + return methodName; + } +}; +#endif // ENABLE_BITTORRENT + +class GetServersRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getServers"; + return methodName; + } +}; + +class TellStatusRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.tellStatus"; + return methodName; + } +}; + +class TellActiveRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.tellActive"; + return methodName; + } +}; + +template +class AbstractPaginationRpcMethod:public RpcMethod { +private: + template + std::pair + getPaginationRange + (ssize_t offset, size_t num, InputIterator first, InputIterator last) + { + size_t size = std::distance(first, last); + if(offset < 0) { + ssize_t tempoffset = offset+size; + if(tempoffset < 0) { + return std::make_pair(last, last); + } + offset = tempoffset-(num-1); + if(offset < 0) { + offset = 0; + num = tempoffset+1; + } + } else if(size <= (size_t)offset) { + return std::make_pair(last, last); + } + size_t lastDistance; + if(size < offset+num) { + lastDistance = size; + } else { + lastDistance = offset+num; + } + last = first; + std::advance(first, offset); + std::advance(last, lastDistance); + return std::make_pair(first, last); + } + + void checkPaginationParams(const SharedHandle& params) const + { + if(params->size() < 2) { + throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer."); + } + const Integer* p1 = asInteger(params->get(0)); + const Integer* p2 = asInteger(params->get(1)); + if(!p1 || !p2 || p2->i() < 0) { + throw DL_ABORT_EX("Invalid argument. Specify offset and num in integer."); + } + } +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e) + { + const SharedHandle& params = req.params; + checkPaginationParams(params); + ssize_t offset = req.getIntegerParam(0)->i(); + size_t num = req.getIntegerParam(1)->i(); + const List* keysParam = req.getListParam(2); + std::vector keys; + toStringList(std::back_inserter(keys), keysParam); + const std::deque >& items = getItems(e); + std::pair >::const_iterator, + typename std::deque >::const_iterator> range = + getPaginationRange(offset, num, items.begin(), items.end()); + SharedHandle list = List::g(); + for(; range.first != range.second; ++range.first) { + SharedHandle entryDict = Dict::g(); + createEntry(entryDict, *range.first, e, keys); + list->append(entryDict); + } + if(offset < 0) { + std::reverse(list->begin(), list->end()); + } + return list; + } + + virtual const std::deque >& + getItems(DownloadEngine* e) const = 0; + + virtual void createEntry + (const SharedHandle& entryDict, + const SharedHandle& item, + DownloadEngine* e, + const std::vector& keys) const = 0; +}; + +class TellWaitingRpcMethod: + public AbstractPaginationRpcMethod { +protected: + virtual const std::deque >& + getItems(DownloadEngine* e) const; + + virtual void createEntry + (const SharedHandle& entryDict, + const SharedHandle& item, + DownloadEngine* e, + const std::vector& keys) const; +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.tellWaiting"; + return methodName; + } +}; + +class TellStoppedRpcMethod: + public AbstractPaginationRpcMethod { +protected: + virtual const std::deque >& + getItems(DownloadEngine* e) const; + + virtual void createEntry + (const SharedHandle& entryDict, + const SharedHandle& item, + DownloadEngine* e, + const std::vector& keys) const; +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.tellStopped"; + return methodName; + } +}; + +class ChangeOptionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.changeOption"; + return methodName; + } +}; + +class ChangeGlobalOptionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.changeGlobalOption"; + return methodName; + } +}; + +class GetVersionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getVersion"; + return methodName; + } +}; + +class GetOptionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getOption"; + return methodName; + } +}; + +class GetGlobalOptionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getGlobalOption"; + return methodName; + } +}; + +class ChangePositionRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.changePosition"; + return methodName; + } +}; + +class ChangeUriRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.changeUri"; + return methodName; + } +}; + +class GetSessionInfoRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.getSessionInfo"; + return methodName; + } +}; + +class ShutdownRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.shutdown"; + return methodName; + } +}; + +class ForceShutdownRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "aria2.forceShutdown"; + return methodName; + } +}; + +class SystemMulticallRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +public: + static const std::string& getMethodName() + { + static std::string methodName = "system.multicall"; + return methodName; + } +}; + +class NoSuchMethodRpcMethod:public RpcMethod { +protected: + virtual SharedHandle process + (const RpcRequest& req, DownloadEngine* e); +}; + +// Helper function to store data to entryDict from ds. This function +// is used by tellStatus method. +void gatherStoppedDownload +(const SharedHandle& entryDict, const SharedHandle& ds, + const std::vector& keys); + +// Helper function to store data to entryDict from group. This +// function is used by tellStatus/tellActive/tellWaiting method +void gatherProgressCommon +(const SharedHandle& entryDict, const SharedHandle& group, + const std::vector& keys); + +#ifdef ENABLE_BITTORRENT +// Helper function to store BitTorrent metadata from torrentAttrs. +void gatherBitTorrentMetadata +(const SharedHandle& btDict, + const SharedHandle& torrentAttrs); +#endif // ENABLE_BITTORRENT + +} // namespace rpc + +} // namespace aria2 + +#endif // D_RPC_METHOD_IMPL_H -- cgit