aria2/src/RpcMethodFactory.cc

293 lines
8.1 KiB
C++
Raw Normal View History

/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2009 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "RpcMethodFactory.h"
#include "RpcMethodImpl.h"
#include "OptionParser.h"
#include "OptionHandler.h"
namespace aria2 {
namespace rpc {
2011-06-15 11:07:10 +00:00
namespace {
2013-07-11 12:50:47 +00:00
std::map<std::string, std::unique_ptr<RpcMethod>> cache;
} // namespace
namespace {
std::unique_ptr<RpcMethod> noSuchRpcMethod;
2011-06-15 11:07:10 +00:00
} // namespace
namespace {
std::vector<std::string> rpcMethodNames = {
"aria2.addUri",
#ifdef ENABLE_BITTORRENT
2016-10-15 10:02:54 +00:00
"aria2.addTorrent",
"aria2.getPeers",
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
"aria2.addMetalink",
#endif // ENABLE_METALINK
2016-10-15 10:02:54 +00:00
"aria2.remove",
"aria2.pause",
"aria2.forcePause",
"aria2.pauseAll",
"aria2.forcePauseAll",
"aria2.unpause",
"aria2.unpauseAll",
"aria2.forceRemove",
"aria2.changePosition",
"aria2.tellStatus",
"aria2.getUris",
"aria2.getFiles",
"aria2.getServers",
"aria2.tellActive",
"aria2.tellWaiting",
"aria2.tellStopped",
"aria2.getOption",
"aria2.changeUri",
"aria2.changeOption",
"aria2.getGlobalOption",
"aria2.changeGlobalOption",
"aria2.purgeDownloadResult",
"aria2.removeDownloadResult",
"aria2.getVersion",
"aria2.getSessionInfo",
"aria2.shutdown",
"aria2.forceShutdown",
"aria2.getGlobalStat",
"aria2.saveSession",
"system.multicall",
"system.listMethods",
2016-04-08 11:06:32 +00:00
"system.listNotifications",
};
} // namespace
const std::vector<std::string>& allMethodNames() { return rpcMethodNames; }
2016-04-08 11:06:32 +00:00
namespace {
std::vector<std::string> rpcNotificationsNames = {
2016-04-15 14:31:00 +00:00
"aria2.onDownloadStart", "aria2.onDownloadPause",
"aria2.onDownloadStop", "aria2.onDownloadComplete",
"aria2.onDownloadError",
#ifdef ENABLE_BITTORRENT
2016-04-08 11:06:32 +00:00
"aria2.onBtDownloadComplete",
2016-04-15 14:31:00 +00:00
#endif // ENABLE_BITTORRENT
2016-04-08 11:06:32 +00:00
};
} // namespace
2016-04-15 14:31:00 +00:00
const std::vector<std::string>& allNotificationsNames()
{
return rpcNotificationsNames;
}
2016-04-08 11:06:32 +00:00
2011-06-15 11:07:10 +00:00
namespace {
2015-12-26 10:03:50 +00:00
std::unique_ptr<RpcMethod> createMethod(const std::string& methodName)
{
2015-12-26 10:03:50 +00:00
if (methodName == AddUriRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<AddUriRpcMethod>();
2015-12-26 10:03:50 +00:00
}
#ifdef ENABLE_BITTORRENT
2015-12-26 10:03:50 +00:00
if (methodName == AddTorrentRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<AddTorrentRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetPeersRpcMethod::getMethodName()) {
return make_unique<GetPeersRpcMethod>();
}
#endif // ENABLE_BITTORRENT
2015-12-26 10:03:50 +00:00
#ifdef ENABLE_METALINK
2015-12-26 10:03:50 +00:00
if (methodName == AddMetalinkRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<AddMetalinkRpcMethod>();
}
2015-12-26 10:03:50 +00:00
#endif // ENABLE_METALINK
if (methodName == RemoveRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<RemoveRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == PauseRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<PauseRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ForcePauseRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ForcePauseRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == PauseAllRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<PauseAllRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ForcePauseAllRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ForcePauseAllRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == UnpauseRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<UnpauseRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == UnpauseAllRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<UnpauseAllRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ForceRemoveRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ForceRemoveRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ChangePositionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ChangePositionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == TellStatusRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<TellStatusRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetUrisRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetUrisRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetFilesRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetFilesRpcMethod>();
}
2015-12-26 10:03:50 +00:00
if (methodName == GetServersRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetServersRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == TellActiveRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<TellActiveRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == TellWaitingRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<TellWaitingRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == TellStoppedRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<TellStoppedRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetOptionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetOptionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ChangeUriRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ChangeUriRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ChangeOptionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ChangeOptionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetGlobalOptionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetGlobalOptionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ChangeGlobalOptionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ChangeGlobalOptionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == PurgeDownloadResultRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<PurgeDownloadResultRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == RemoveDownloadResultRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<RemoveDownloadResultRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetVersionRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetVersionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetSessionInfoRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetSessionInfoRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ShutdownRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ShutdownRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == ForceShutdownRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<ForceShutdownRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == GetGlobalStatRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<GetGlobalStatRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == SaveSessionRpcMethod::getMethodName()) {
return make_unique<SaveSessionRpcMethod>();
2015-12-26 10:03:50 +00:00
}
if (methodName == SystemMulticallRpcMethod::getMethodName()) {
2013-07-11 12:50:47 +00:00
return make_unique<SystemMulticallRpcMethod>();
2011-06-15 11:07:10 +00:00
}
2015-12-26 10:03:50 +00:00
if (methodName == SystemListMethodsRpcMethod::getMethodName()) {
return make_unique<SystemListMethodsRpcMethod>();
}
2016-04-08 11:06:32 +00:00
if (methodName == SystemListNotificationsRpcMethod::getMethodName()) {
return make_unique<SystemListNotificationsRpcMethod>();
}
2015-12-26 10:03:50 +00:00
return nullptr;
2011-06-15 11:07:10 +00:00
}
} // namespace
2013-07-11 12:50:47 +00:00
RpcMethod* getMethod(const std::string& methodName)
2011-06-15 11:07:10 +00:00
{
2013-07-11 12:50:47 +00:00
auto itr = cache.find(methodName);
2015-12-26 10:03:50 +00:00
if (itr == std::end(cache)) {
2013-07-11 12:50:47 +00:00
auto m = createMethod(methodName);
2015-12-26 10:03:50 +00:00
if (m) {
2013-07-11 12:50:47 +00:00
auto rv = cache.insert(std::make_pair(methodName, std::move(m)));
return (*rv.first).second.get();
2011-06-15 11:07:10 +00:00
}
2015-12-26 10:03:50 +00:00
if (!noSuchRpcMethod) {
noSuchRpcMethod = make_unique<NoSuchMethodRpcMethod>();
}
return noSuchRpcMethod.get();
}
2015-12-26 10:03:50 +00:00
return (*itr).second.get();
}
} // namespace rpc
} // namespace aria2