DownloadEngineFactory.cc 7.5 KB

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