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 
40 namespace aria2 {
41 
42 namespace rpc {
43 
44 namespace {
45 std::map<std::string, std::unique_ptr<RpcMethod>> cache;
46 } // namespace
47 
48 namespace {
49 std::unique_ptr<RpcMethod> noSuchRpcMethod;
50 } // namespace
51 
52 namespace {
53 std::vector<std::string> rpcMethodNames = {
54     "aria2.addUri",
55 #ifdef ENABLE_BITTORRENT
56     "aria2.addTorrent",
57     "aria2.getPeers",
58 #endif // ENABLE_BITTORRENT
59 #ifdef ENABLE_METALINK
60     "aria2.addMetalink",
61 #endif // ENABLE_METALINK
62     "aria2.remove",
63     "aria2.pause",
64     "aria2.forcePause",
65     "aria2.pauseAll",
66     "aria2.forcePauseAll",
67     "aria2.unpause",
68     "aria2.unpauseAll",
69     "aria2.forceRemove",
70     "aria2.changePosition",
71     "aria2.tellStatus",
72     "aria2.getUris",
73     "aria2.getFiles",
74     "aria2.getServers",
75     "aria2.tellActive",
76     "aria2.tellWaiting",
77     "aria2.tellStopped",
78     "aria2.getOption",
79     "aria2.changeUri",
80     "aria2.changeOption",
81     "aria2.getGlobalOption",
82     "aria2.changeGlobalOption",
83     "aria2.purgeDownloadResult",
84     "aria2.removeDownloadResult",
85     "aria2.getVersion",
86     "aria2.getSessionInfo",
87     "aria2.shutdown",
88     "aria2.forceShutdown",
89     "aria2.getGlobalStat",
90     "aria2.saveSession",
91     "system.multicall",
92     "system.listMethods",
93     "system.listNotifications",
94 };
95 } // namespace
96 
allMethodNames()97 const std::vector<std::string>& allMethodNames() { return rpcMethodNames; }
98 
99 namespace {
100 std::vector<std::string> rpcNotificationsNames = {
101     "aria2.onDownloadStart",      "aria2.onDownloadPause",
102     "aria2.onDownloadStop",       "aria2.onDownloadComplete",
103     "aria2.onDownloadError",
104 #ifdef ENABLE_BITTORRENT
105     "aria2.onBtDownloadComplete",
106 #endif // ENABLE_BITTORRENT
107 };
108 } // namespace
109 
allNotificationsNames()110 const std::vector<std::string>& allNotificationsNames()
111 {
112   return rpcNotificationsNames;
113 }
114 
115 namespace {
createMethod(const std::string & methodName)116 std::unique_ptr<RpcMethod> createMethod(const std::string& methodName)
117 {
118   if (methodName == AddUriRpcMethod::getMethodName()) {
119     return make_unique<AddUriRpcMethod>();
120   }
121 
122 #ifdef ENABLE_BITTORRENT
123   if (methodName == AddTorrentRpcMethod::getMethodName()) {
124     return make_unique<AddTorrentRpcMethod>();
125   }
126 
127   if (methodName == GetPeersRpcMethod::getMethodName()) {
128     return make_unique<GetPeersRpcMethod>();
129   }
130 #endif // ENABLE_BITTORRENT
131 
132 #ifdef ENABLE_METALINK
133   if (methodName == AddMetalinkRpcMethod::getMethodName()) {
134     return make_unique<AddMetalinkRpcMethod>();
135   }
136 #endif // ENABLE_METALINK
137 
138   if (methodName == RemoveRpcMethod::getMethodName()) {
139     return make_unique<RemoveRpcMethod>();
140   }
141 
142   if (methodName == PauseRpcMethod::getMethodName()) {
143     return make_unique<PauseRpcMethod>();
144   }
145 
146   if (methodName == ForcePauseRpcMethod::getMethodName()) {
147     return make_unique<ForcePauseRpcMethod>();
148   }
149 
150   if (methodName == PauseAllRpcMethod::getMethodName()) {
151     return make_unique<PauseAllRpcMethod>();
152   }
153 
154   if (methodName == ForcePauseAllRpcMethod::getMethodName()) {
155     return make_unique<ForcePauseAllRpcMethod>();
156   }
157 
158   if (methodName == UnpauseRpcMethod::getMethodName()) {
159     return make_unique<UnpauseRpcMethod>();
160   }
161 
162   if (methodName == UnpauseAllRpcMethod::getMethodName()) {
163     return make_unique<UnpauseAllRpcMethod>();
164   }
165 
166   if (methodName == ForceRemoveRpcMethod::getMethodName()) {
167     return make_unique<ForceRemoveRpcMethod>();
168   }
169 
170   if (methodName == ChangePositionRpcMethod::getMethodName()) {
171     return make_unique<ChangePositionRpcMethod>();
172   }
173 
174   if (methodName == TellStatusRpcMethod::getMethodName()) {
175     return make_unique<TellStatusRpcMethod>();
176   }
177 
178   if (methodName == GetUrisRpcMethod::getMethodName()) {
179     return make_unique<GetUrisRpcMethod>();
180   }
181 
182   if (methodName == GetFilesRpcMethod::getMethodName()) {
183     return make_unique<GetFilesRpcMethod>();
184   }
185 
186   if (methodName == GetServersRpcMethod::getMethodName()) {
187     return make_unique<GetServersRpcMethod>();
188   }
189 
190   if (methodName == TellActiveRpcMethod::getMethodName()) {
191     return make_unique<TellActiveRpcMethod>();
192   }
193 
194   if (methodName == TellWaitingRpcMethod::getMethodName()) {
195     return make_unique<TellWaitingRpcMethod>();
196   }
197 
198   if (methodName == TellStoppedRpcMethod::getMethodName()) {
199     return make_unique<TellStoppedRpcMethod>();
200   }
201 
202   if (methodName == GetOptionRpcMethod::getMethodName()) {
203     return make_unique<GetOptionRpcMethod>();
204   }
205 
206   if (methodName == ChangeUriRpcMethod::getMethodName()) {
207     return make_unique<ChangeUriRpcMethod>();
208   }
209 
210   if (methodName == ChangeOptionRpcMethod::getMethodName()) {
211     return make_unique<ChangeOptionRpcMethod>();
212   }
213 
214   if (methodName == GetGlobalOptionRpcMethod::getMethodName()) {
215     return make_unique<GetGlobalOptionRpcMethod>();
216   }
217 
218   if (methodName == ChangeGlobalOptionRpcMethod::getMethodName()) {
219     return make_unique<ChangeGlobalOptionRpcMethod>();
220   }
221 
222   if (methodName == PurgeDownloadResultRpcMethod::getMethodName()) {
223     return make_unique<PurgeDownloadResultRpcMethod>();
224   }
225 
226   if (methodName == RemoveDownloadResultRpcMethod::getMethodName()) {
227     return make_unique<RemoveDownloadResultRpcMethod>();
228   }
229 
230   if (methodName == GetVersionRpcMethod::getMethodName()) {
231     return make_unique<GetVersionRpcMethod>();
232   }
233 
234   if (methodName == GetSessionInfoRpcMethod::getMethodName()) {
235     return make_unique<GetSessionInfoRpcMethod>();
236   }
237 
238   if (methodName == ShutdownRpcMethod::getMethodName()) {
239     return make_unique<ShutdownRpcMethod>();
240   }
241 
242   if (methodName == ForceShutdownRpcMethod::getMethodName()) {
243     return make_unique<ForceShutdownRpcMethod>();
244   }
245 
246   if (methodName == GetGlobalStatRpcMethod::getMethodName()) {
247     return make_unique<GetGlobalStatRpcMethod>();
248   }
249 
250   if (methodName == SaveSessionRpcMethod::getMethodName()) {
251     return make_unique<SaveSessionRpcMethod>();
252   }
253 
254   if (methodName == SystemMulticallRpcMethod::getMethodName()) {
255     return make_unique<SystemMulticallRpcMethod>();
256   }
257 
258   if (methodName == SystemListMethodsRpcMethod::getMethodName()) {
259     return make_unique<SystemListMethodsRpcMethod>();
260   }
261 
262   if (methodName == SystemListNotificationsRpcMethod::getMethodName()) {
263     return make_unique<SystemListNotificationsRpcMethod>();
264   }
265 
266   return nullptr;
267 }
268 } // namespace
269 
getMethod(const std::string & methodName)270 RpcMethod* getMethod(const std::string& methodName)
271 {
272   auto itr = cache.find(methodName);
273   if (itr == std::end(cache)) {
274     auto m = createMethod(methodName);
275     if (m) {
276       auto rv = cache.insert(std::make_pair(methodName, std::move(m)));
277       return (*rv.first).second.get();
278     }
279 
280     if (!noSuchRpcMethod) {
281       noSuchRpcMethod = make_unique<NoSuchMethodRpcMethod>();
282     }
283 
284     return noSuchRpcMethod.get();
285   }
286 
287   return (*itr).second.get();
288 }
289 
290 } // namespace rpc
291 
292 } // namespace aria2
293