DownloadEngineFactory.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifdef ENABLE_MESSAGE_DIGEST
  45. # include "CheckIntegrityMan.h"
  46. # include "CheckIntegrityEntry.h"
  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. #ifdef HAVE_EPOLL
  59. # include "EpollEventPoll.h"
  60. #endif // HAVE_EPOLL
  61. #include "SelectEventPoll.h"
  62. #include "DlAbortEx.h"
  63. #include "FileAllocationEntry.h"
  64. #ifdef ENABLE_XML_RPC
  65. # include "HttpListenCommand.h"
  66. #endif // ENABLE_XML_RPC
  67. namespace aria2 {
  68. DownloadEngineFactory::DownloadEngineFactory():
  69. _logger(LogFactory::getInstance()) {}
  70. DownloadEngineHandle
  71. DownloadEngineFactory::newDownloadEngine(Option* op,
  72. const RequestGroups& requestGroups)
  73. {
  74. RequestGroups workingSet;
  75. RequestGroups reservedSet;
  76. const size_t MAX_CONCURRENT_DOWNLOADS = op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
  77. if(MAX_CONCURRENT_DOWNLOADS < requestGroups.size()) {
  78. workingSet.insert(workingSet.end(),
  79. requestGroups.begin(),
  80. requestGroups.begin()+MAX_CONCURRENT_DOWNLOADS);
  81. reservedSet.insert(reservedSet.end(),
  82. requestGroups.begin()+MAX_CONCURRENT_DOWNLOADS,
  83. requestGroups.end());
  84. } else {
  85. workingSet = requestGroups;
  86. }
  87. SharedHandle<EventPoll> eventPoll;
  88. #ifdef HAVE_EPOLL
  89. if(op->get(PREF_EVENT_POLL) == 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. if(op->get(PREF_EVENT_POLL) == V_SELECT) {
  100. eventPoll.reset(new SelectEventPoll());
  101. } else {
  102. abort();
  103. }
  104. DownloadEngineHandle e(new DownloadEngine(eventPoll));
  105. e->option = op;
  106. RequestGroupManHandle
  107. requestGroupMan(new RequestGroupMan(workingSet, MAX_CONCURRENT_DOWNLOADS,
  108. op));
  109. requestGroupMan->addReservedGroup(reservedSet);
  110. e->_requestGroupMan = requestGroupMan;
  111. e->_fileAllocationMan.reset(new FileAllocationMan());
  112. #ifdef ENABLE_MESSAGE_DIGEST
  113. e->_checkIntegrityMan.reset(new CheckIntegrityMan());
  114. #endif // ENABLE_MESSAGE_DIGEST
  115. e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get(), 1));
  116. e->addRoutineCommand(new FileAllocationDispatcherCommand
  117. (e->newCUID(), e->_fileAllocationMan, e.get()));
  118. #ifdef ENABLE_MESSAGE_DIGEST
  119. e->addRoutineCommand(new CheckIntegrityDispatcherCommand
  120. (e->newCUID(), e->_checkIntegrityMan, e.get()));
  121. #endif // ENABLE_MESSAGE_DIGEST
  122. if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
  123. e->addRoutineCommand
  124. (new AutoSaveCommand(e->newCUID(), e.get(),
  125. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  126. }
  127. e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10));
  128. {
  129. time_t stopSec = op->getAsInt(PREF_STOP);
  130. if(stopSec > 0) {
  131. e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e.get(),
  132. stopSec));
  133. }
  134. }
  135. #ifdef ENABLE_XML_RPC
  136. if(op->getAsBool(PREF_ENABLE_XML_RPC)) {
  137. HttpListenCommand* httpListenCommand =
  138. new HttpListenCommand(e->newCUID(), e.get());
  139. if(httpListenCommand->bindPort(op->getAsInt(PREF_XML_RPC_LISTEN_PORT))){
  140. e->addRoutineCommand(httpListenCommand);
  141. } else {
  142. delete httpListenCommand;
  143. }
  144. }
  145. #endif // ENABLE_XML_RPC
  146. return e;
  147. }
  148. } // namespace aria2