DownloadEngineFactory.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "DownloadContext.h"
  59. #ifdef HAVE_EPOLL
  60. # include "EpollEventPoll.h"
  61. #endif // HAVE_EPOLL
  62. #include "SelectEventPoll.h"
  63. #include "DlAbortEx.h"
  64. #include "FileAllocationEntry.h"
  65. #ifdef ENABLE_XML_RPC
  66. # include "HttpListenCommand.h"
  67. #endif // ENABLE_XML_RPC
  68. namespace aria2 {
  69. DownloadEngineFactory::DownloadEngineFactory():
  70. _logger(LogFactory::getInstance()) {}
  71. DownloadEngineHandle
  72. DownloadEngineFactory::newDownloadEngine
  73. (Option* op, const RequestGroups& requestGroups)
  74. {
  75. const size_t MAX_CONCURRENT_DOWNLOADS =
  76. op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
  77. SharedHandle<EventPoll> eventPoll;
  78. #ifdef HAVE_EPOLL
  79. if(op->get(PREF_EVENT_POLL) == V_EPOLL) {
  80. SharedHandle<EpollEventPoll> ep(new EpollEventPoll());
  81. if(ep->good()) {
  82. eventPoll = ep;
  83. } else {
  84. throw DL_ABORT_EX("Initializing EpollEventPoll failed."
  85. " Try --event-poll=select");
  86. }
  87. } else
  88. #endif // HAVE_EPLL
  89. if(op->get(PREF_EVENT_POLL) == V_SELECT) {
  90. eventPoll.reset(new SelectEventPoll());
  91. } else {
  92. abort();
  93. }
  94. DownloadEngineHandle e(new DownloadEngine(eventPoll));
  95. e->option = op;
  96. RequestGroupManHandle
  97. requestGroupMan(new RequestGroupMan(requestGroups, MAX_CONCURRENT_DOWNLOADS,
  98. op));
  99. e->_requestGroupMan = requestGroupMan;
  100. e->_fileAllocationMan.reset(new FileAllocationMan());
  101. #ifdef ENABLE_MESSAGE_DIGEST
  102. e->_checkIntegrityMan.reset(new CheckIntegrityMan());
  103. #endif // ENABLE_MESSAGE_DIGEST
  104. e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get(), 1));
  105. e->addRoutineCommand(new FileAllocationDispatcherCommand
  106. (e->newCUID(), e->_fileAllocationMan, e.get()));
  107. #ifdef ENABLE_MESSAGE_DIGEST
  108. e->addRoutineCommand(new CheckIntegrityDispatcherCommand
  109. (e->newCUID(), e->_checkIntegrityMan, e.get()));
  110. #endif // ENABLE_MESSAGE_DIGEST
  111. if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
  112. e->addRoutineCommand
  113. (new AutoSaveCommand(e->newCUID(), e.get(),
  114. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  115. }
  116. e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10));
  117. {
  118. time_t stopSec = op->getAsInt(PREF_STOP);
  119. if(stopSec > 0) {
  120. e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e.get(),
  121. stopSec));
  122. }
  123. }
  124. #ifdef ENABLE_XML_RPC
  125. if(op->getAsBool(PREF_ENABLE_XML_RPC)) {
  126. HttpListenCommand* httpListenCommand =
  127. new HttpListenCommand(e->newCUID(), e.get());
  128. if(httpListenCommand->bindPort(op->getAsInt(PREF_XML_RPC_LISTEN_PORT))){
  129. e->addRoutineCommand(httpListenCommand);
  130. } else {
  131. delete httpListenCommand;
  132. }
  133. }
  134. #endif // ENABLE_XML_RPC
  135. return e;
  136. }
  137. } // namespace aria2