BtSetup.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "BtSetup.h"
  36. #include "RequestGroup.h"
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "BtRegistry.h"
  40. #include "PeerListenCommand.h"
  41. #include "TrackerWatcherCommand.h"
  42. #include "SeedCheckCommand.h"
  43. #include "PeerChokeCommand.h"
  44. #include "ActivePeerConnectionCommand.h"
  45. #include "PeerListenCommand.h"
  46. #include "UnionSeedCriteria.h"
  47. #include "TimeSeedCriteria.h"
  48. #include "ShareRatioSeedCriteria.h"
  49. #include "CUIDCounter.h"
  50. #include "prefs.h"
  51. #include "LogFactory.h"
  52. #include "Logger.h"
  53. #include "Util.h"
  54. #include "IntSequence.h"
  55. #include "DHTGetPeersCommand.h"
  56. #include "DHTPeerAnnounceStorage.h"
  57. #include "DHTSetup.h"
  58. #include "DHTRegistry.h"
  59. namespace aria2 {
  60. BtSetup::BtSetup():_logger(LogFactory::getInstance()) {}
  61. void BtSetup::setup(std::deque<Command*>& commands,
  62. RequestGroup* requestGroup,
  63. DownloadEngine* e,
  64. const Option* option)
  65. {
  66. BtContextHandle btContext(dynamic_pointer_cast<BtContext>(requestGroup->getDownloadContext()));
  67. if(btContext.isNull()) {
  68. return;
  69. }
  70. // commands
  71. commands.push_back(new TrackerWatcherCommand(CUIDCounterSingletonHolder::instance()->newID(),
  72. requestGroup,
  73. e,
  74. btContext));
  75. commands.push_back(new PeerChokeCommand(CUIDCounterSingletonHolder::instance()->newID(),
  76. e,
  77. btContext));
  78. commands.push_back(new ActivePeerConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(),
  79. requestGroup, e, btContext, 10));
  80. if(!btContext->isPrivate() && DHTSetup::initialized()) {
  81. DHTRegistry::_peerAnnounceStorage->addPeerAnnounce(btContext);
  82. DHTGetPeersCommand* command = new DHTGetPeersCommand(CUIDCounterSingletonHolder::instance()->newID(),
  83. requestGroup,
  84. e,
  85. btContext);
  86. command->setTaskQueue(DHTRegistry::_taskQueue);
  87. command->setTaskFactory(DHTRegistry::_taskFactory);
  88. commands.push_back(command);
  89. }
  90. SharedHandle<UnionSeedCriteria> unionCri(new UnionSeedCriteria());
  91. if(option->defined(PREF_SEED_TIME)) {
  92. SharedHandle<SeedCriteria> cri(new TimeSeedCriteria(option->getAsInt(PREF_SEED_TIME)*60));
  93. unionCri->addSeedCriteria(cri);
  94. }
  95. {
  96. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  97. if(ratio > 0.0) {
  98. SharedHandle<SeedCriteria> cri(new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO), btContext));
  99. unionCri->addSeedCriteria(cri);
  100. }
  101. }
  102. if(unionCri->getSeedCriterion().size() > 0) {
  103. commands.push_back(new SeedCheckCommand(CUIDCounterSingletonHolder::instance()->newID(),
  104. requestGroup,
  105. e,
  106. btContext,
  107. unionCri));
  108. }
  109. if(PeerListenCommand::getNumInstance() == 0) {
  110. PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e);
  111. IntSequence seq = Util::parseIntRange(option->get(PREF_LISTEN_PORT));
  112. uint16_t port;
  113. if(listenCommand->bindPort(port, seq)) {
  114. BT_RUNTIME(btContext)->setListenPort(port);
  115. commands.push_back(listenCommand);
  116. } else {
  117. _logger->error(_("Errors occurred while binding port.\n"));
  118. delete listenCommand;
  119. }
  120. }
  121. BT_RUNTIME(btContext)->setReady(true);
  122. }
  123. } // namespace aria2