1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 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 "DownloadEngineFactory.h"
36 
37 #include <algorithm>
38 
39 #include "Option.h"
40 #include "RequestGroup.h"
41 #include "DownloadEngine.h"
42 #include "RequestGroupMan.h"
43 #include "FileAllocationMan.h"
44 #include "CheckIntegrityMan.h"
45 #include "CheckIntegrityEntry.h"
46 #include "CheckIntegrityDispatcherCommand.h"
47 #include "prefs.h"
48 #include "FillRequestGroupCommand.h"
49 #include "FileAllocationDispatcherCommand.h"
50 #include "AutoSaveCommand.h"
51 #include "SaveSessionCommand.h"
52 #include "HaveEraseCommand.h"
53 #include "TimedHaltCommand.h"
54 #include "WatchProcessCommand.h"
55 #include "DownloadResult.h"
56 #include "ServerStatMan.h"
57 #include "a2io.h"
58 #include "DownloadContext.h"
59 #include "array_fun.h"
60 #include "EvictSocketPoolCommand.h"
61 #ifdef HAVE_LIBUV
62 #  include "LibuvEventPoll.h"
63 #endif // HAVE_LIBUV
64 #ifdef HAVE_EPOLL
65 #  include "EpollEventPoll.h"
66 #endif // HAVE_EPOLL
67 #ifdef HAVE_PORT_ASSOCIATE
68 #  include "PortEventPoll.h"
69 #endif // HAVE_PORT_ASSOCIATE
70 #ifdef HAVE_KQUEUE
71 #  include "KqueueEventPoll.h"
72 #endif // HAVE_KQUEUE
73 #ifdef HAVE_POLL
74 #  include "PollEventPoll.h"
75 #endif // HAVE_POLL
76 #include "SelectEventPoll.h"
77 #include "DlAbortEx.h"
78 #include "FileAllocationEntry.h"
79 #include "HttpListenCommand.h"
80 #include "LogFactory.h"
81 
82 namespace aria2 {
83 
84 DownloadEngineFactory::DownloadEngineFactory() = default;
85 
86 namespace {
createEventPoll(Option * op)87 std::unique_ptr<EventPoll> createEventPoll(Option* op)
88 {
89   const std::string& pollMethod = op->get(PREF_EVENT_POLL);
90 #ifdef HAVE_LIBUV
91   if (pollMethod == V_LIBUV) {
92     auto ep = make_unique<LibuvEventPoll>();
93     if (!ep->good()) {
94       throw DL_ABORT_EX("Initializing LibuvEventPoll failed."
95                         " Try --event-poll=select");
96     }
97     return std::move(ep);
98   }
99   else
100 #endif // HAVE_LIBUV
101 #ifdef HAVE_EPOLL
102       if (pollMethod == V_EPOLL) {
103     auto ep = make_unique<EpollEventPoll>();
104     if (!ep->good()) {
105       throw DL_ABORT_EX("Initializing EpollEventPoll failed."
106                         " Try --event-poll=select");
107     }
108     return std::move(ep);
109   }
110   else
111 #endif // HAVE_EPLL
112 #ifdef HAVE_KQUEUE
113       if (pollMethod == V_KQUEUE) {
114     auto kp = make_unique<KqueueEventPoll>();
115     if (!kp->good()) {
116       throw DL_ABORT_EX("Initializing KqueueEventPoll failed."
117                         " Try --event-poll=select");
118     }
119     return std::move(kp);
120   }
121   else
122 #endif // HAVE_KQUEUE
123 #ifdef HAVE_PORT_ASSOCIATE
124       if (pollMethod == V_PORT) {
125     auto pp = make_unique<PortEventPoll>();
126     if (!pp->good()) {
127       throw DL_ABORT_EX("Initializing PortEventPoll failed."
128                         " Try --event-poll=select");
129     }
130     return std::move(pp);
131   }
132   else
133 #endif // HAVE_PORT_ASSOCIATE
134 #ifdef HAVE_POLL
135       if (pollMethod == V_POLL) {
136     return make_unique<PollEventPoll>();
137   }
138   else
139 #endif // HAVE_POLL
140       if (pollMethod == V_SELECT) {
141     return make_unique<SelectEventPoll>();
142   }
143   assert(0);
144   return nullptr;
145 }
146 } // namespace
147 
newDownloadEngine(Option * op,std::vector<std::shared_ptr<RequestGroup>> requestGroups)148 std::unique_ptr<DownloadEngine> DownloadEngineFactory::newDownloadEngine(
149     Option* op, std::vector<std::shared_ptr<RequestGroup>> requestGroups)
150 {
151   const size_t MAX_CONCURRENT_DOWNLOADS =
152       op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
153   auto e = make_unique<DownloadEngine>(createEventPoll(op));
154   e->setOption(op);
155   {
156     auto requestGroupMan = make_unique<RequestGroupMan>(
157         std::move(requestGroups), MAX_CONCURRENT_DOWNLOADS, op);
158     requestGroupMan->initWrDiskCache();
159     e->setRequestGroupMan(std::move(requestGroupMan));
160   }
161   e->setFileAllocationMan(make_unique<FileAllocationMan>());
162   e->setCheckIntegrityMan(make_unique<CheckIntegrityMan>());
163   e->addRoutineCommand(
164       make_unique<FillRequestGroupCommand>(e->newCUID(), e.get()));
165   e->addRoutineCommand(make_unique<FileAllocationDispatcherCommand>(
166       e->newCUID(), e->getFileAllocationMan().get(), e.get()));
167   e->addRoutineCommand(make_unique<CheckIntegrityDispatcherCommand>(
168       e->newCUID(), e->getCheckIntegrityMan().get(), e.get()));
169   e->addRoutineCommand(
170       make_unique<EvictSocketPoolCommand>(e->newCUID(), e.get(), 30_s));
171 
172   if (op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
173     e->addRoutineCommand(make_unique<AutoSaveCommand>(
174         e->newCUID(), e.get(),
175         std::chrono::seconds(op->getAsInt(PREF_AUTO_SAVE_INTERVAL))));
176   }
177   if (op->getAsInt(PREF_SAVE_SESSION_INTERVAL) > 0) {
178     e->addRoutineCommand(make_unique<SaveSessionCommand>(
179         e->newCUID(), e.get(),
180         std::chrono::seconds(op->getAsInt(PREF_SAVE_SESSION_INTERVAL))));
181   }
182   e->addRoutineCommand(
183       make_unique<HaveEraseCommand>(e->newCUID(), e.get(), 10_s));
184   {
185     auto stopSec = op->getAsInt(PREF_STOP);
186     if (stopSec > 0) {
187       e->addRoutineCommand(make_unique<TimedHaltCommand>(
188           e->newCUID(), e.get(), std::chrono::seconds(stopSec)));
189     }
190   }
191   if (op->defined(PREF_STOP_WITH_PROCESS)) {
192     unsigned int pid = op->getAsInt(PREF_STOP_WITH_PROCESS);
193     e->addRoutineCommand(
194         make_unique<WatchProcessCommand>(e->newCUID(), e.get(), pid));
195   }
196   if (op->getAsBool(PREF_ENABLE_RPC)) {
197     if (op->get(PREF_RPC_SECRET).empty() && op->get(PREF_RPC_USER).empty()) {
198       A2_LOG_WARN("Neither --rpc-secret nor a combination of --rpc-user and "
199                   "--rpc-passwd is set. This is insecure. It is extremely "
200                   "recommended to specify --rpc-secret with the adequate "
201                   "secrecy or now deprecated --rpc-user and --rpc-passwd.");
202     }
203     bool ok = false;
204     bool secure = op->getAsBool(PREF_RPC_SECURE);
205     if (secure) {
206       A2_LOG_NOTICE("RPC transport will be encrypted.");
207     }
208     static int families[] = {AF_INET, AF_INET6};
209     size_t familiesLength = op->getAsBool(PREF_DISABLE_IPV6) ? 1 : 2;
210     for (size_t i = 0; i < familiesLength; ++i) {
211       auto httpListenCommand = make_unique<HttpListenCommand>(
212           e->newCUID(), e.get(), families[i], secure);
213       if (httpListenCommand->bindPort(op->getAsInt(PREF_RPC_LISTEN_PORT))) {
214         e->addCommand(std::move(httpListenCommand));
215         ok = true;
216       }
217     }
218     if (!ok) {
219       throw DL_ABORT_EX("Failed to setup RPC server.");
220     }
221   }
222   return e;
223 }
224 
225 } // namespace aria2
226