DownloadEngineFactory.cc 7.5 KB

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