DownloadEngineFactory.cc 6.2 KB

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