DownloadEngineFactory.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "LogFactory.h"
  38. #include "Logger.h"
  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. #ifdef ENABLE_MESSAGE_DIGEST
  47. # include "CheckIntegrityDispatcherCommand.h"
  48. #endif // ENABLE_MESSAGE_DIGEST
  49. #include "prefs.h"
  50. #include "FillRequestGroupCommand.h"
  51. #include "FileAllocationDispatcherCommand.h"
  52. #include "AutoSaveCommand.h"
  53. #include "HaveEraseCommand.h"
  54. #include "TimedHaltCommand.h"
  55. #include "DownloadResult.h"
  56. #include "ServerStatMan.h"
  57. #include "a2io.h"
  58. #include "DownloadContext.h"
  59. #include "array_fun.h"
  60. #ifdef HAVE_EPOLL
  61. # include "EpollEventPoll.h"
  62. #endif // HAVE_EPOLL
  63. #ifdef HAVE_PORT_ASSOCIATE
  64. # include "PortEventPoll.h"
  65. #endif // HAVE_PORT_ASSOCIATE
  66. #ifdef HAVE_KQUEUE
  67. # include "KqueueEventPoll.h"
  68. #endif // HAVE_KQUEUE
  69. #ifdef HAVE_POLL
  70. # include "PollEventPoll.h"
  71. #endif // HAVE_POLL
  72. #include "SelectEventPoll.h"
  73. #include "DlAbortEx.h"
  74. #include "FileAllocationEntry.h"
  75. #ifdef ENABLE_XML_RPC
  76. # include "HttpListenCommand.h"
  77. #endif // ENABLE_XML_RPC
  78. namespace aria2 {
  79. DownloadEngineFactory::DownloadEngineFactory():
  80. logger_(LogFactory::getInstance()) {}
  81. DownloadEngineHandle
  82. DownloadEngineFactory::newDownloadEngine
  83. (Option* op, const std::vector<SharedHandle<RequestGroup> >& requestGroups)
  84. {
  85. const size_t MAX_CONCURRENT_DOWNLOADS =
  86. op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
  87. SharedHandle<EventPoll> eventPoll;
  88. const std::string& pollMethod = op->get(PREF_EVENT_POLL);
  89. #ifdef HAVE_EPOLL
  90. if(pollMethod == V_EPOLL) {
  91. SharedHandle<EpollEventPoll> ep(new EpollEventPoll());
  92. if(ep->good()) {
  93. eventPoll = ep;
  94. } else {
  95. throw DL_ABORT_EX("Initializing EpollEventPoll failed."
  96. " Try --event-poll=select");
  97. }
  98. } else
  99. #endif // HAVE_EPLL
  100. #ifdef HAVE_KQUEUE
  101. if(pollMethod == V_KQUEUE) {
  102. SharedHandle<KqueueEventPoll> kp(new KqueueEventPoll());
  103. if(kp->good()) {
  104. eventPoll = kp;
  105. } else {
  106. throw DL_ABORT_EX("Initializing KqueueEventPoll failed."
  107. " Try --event-poll=select");
  108. }
  109. } else
  110. #endif // HAVE_KQUEUE
  111. #ifdef HAVE_PORT_ASSOCIATE
  112. if(pollMethod == V_PORT) {
  113. SharedHandle<PortEventPoll> pp(new PortEventPoll());
  114. if(pp->good()) {
  115. eventPoll = pp;
  116. } else {
  117. throw DL_ABORT_EX("Initializing PortEventPoll failed."
  118. " Try --event-poll=select");
  119. }
  120. } else
  121. #endif // HAVE_PORT_ASSOCIATE
  122. #ifdef HAVE_POLL
  123. if(pollMethod == V_POLL) {
  124. eventPoll.reset(new PollEventPoll());
  125. } else
  126. #endif // HAVE_POLL
  127. if(pollMethod == V_SELECT) {
  128. eventPoll.reset(new SelectEventPoll());
  129. } else {
  130. abort();
  131. }
  132. DownloadEngineHandle e(new DownloadEngine(eventPoll));
  133. e->setOption(op);
  134. RequestGroupManHandle
  135. requestGroupMan(new RequestGroupMan(requestGroups, MAX_CONCURRENT_DOWNLOADS,
  136. op));
  137. e->setRequestGroupMan(requestGroupMan);
  138. e->setFileAllocationMan
  139. (SharedHandle<FileAllocationMan>(new FileAllocationMan()));
  140. #ifdef ENABLE_MESSAGE_DIGEST
  141. e->setCheckIntegrityMan
  142. (SharedHandle<CheckIntegrityMan>(new CheckIntegrityMan()));
  143. #endif // ENABLE_MESSAGE_DIGEST
  144. e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get()));
  145. e->addRoutineCommand(new FileAllocationDispatcherCommand
  146. (e->newCUID(), e->getFileAllocationMan(), e.get()));
  147. #ifdef ENABLE_MESSAGE_DIGEST
  148. e->addRoutineCommand(new CheckIntegrityDispatcherCommand
  149. (e->newCUID(), e->getCheckIntegrityMan(), e.get()));
  150. #endif // ENABLE_MESSAGE_DIGEST
  151. if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
  152. e->addRoutineCommand
  153. (new AutoSaveCommand(e->newCUID(), e.get(),
  154. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  155. }
  156. e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10));
  157. {
  158. time_t stopSec = op->getAsInt(PREF_STOP);
  159. if(stopSec > 0) {
  160. e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e.get(),
  161. stopSec));
  162. }
  163. }
  164. #ifdef ENABLE_XML_RPC
  165. if(op->getAsBool(PREF_ENABLE_XML_RPC)) {
  166. static int families[] = { AF_INET, AF_INET6 };
  167. size_t familiesLength = op->getAsBool(PREF_DISABLE_IPV6)?1:2;
  168. for(size_t i = 0; i < familiesLength; ++i) {
  169. HttpListenCommand* httpListenCommand =
  170. new HttpListenCommand(e->newCUID(), e.get(), families[i]);
  171. if(httpListenCommand->bindPort(op->getAsInt(PREF_XML_RPC_LISTEN_PORT))){
  172. e->addRoutineCommand(httpListenCommand);
  173. } else {
  174. delete httpListenCommand;
  175. }
  176. }
  177. }
  178. #endif // ENABLE_XML_RPC
  179. return e;
  180. }
  181. } // namespace aria2