DownloadEngineFactory.cc 6.1 KB

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