DefaultBtRequestFactory.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "DefaultBtRequestFactory.h"
  36. #include <algorithm>
  37. #include "LogFactory.h"
  38. #include "Logger.h"
  39. #include "Piece.h"
  40. #include "Peer.h"
  41. #include "PieceStorage.h"
  42. #include "BtMessageDispatcher.h"
  43. #include "BtMessageFactory.h"
  44. #include "BtMessage.h"
  45. #include "a2functional.h"
  46. #include "SimpleRandomizer.h"
  47. #include "array_fun.h"
  48. #include "fmt.h"
  49. namespace aria2 {
  50. DefaultBtRequestFactory::DefaultBtRequestFactory()
  51. : pieceStorage_(0),
  52. dispatcher_(0),
  53. messageFactory_(0),
  54. cuid_(0)
  55. {}
  56. DefaultBtRequestFactory::~DefaultBtRequestFactory() {}
  57. void DefaultBtRequestFactory::addTargetPiece(const std::shared_ptr<Piece>& piece)
  58. {
  59. pieces_.push_back(piece);
  60. }
  61. namespace {
  62. class AbortCompletedPieceRequest
  63. {
  64. private:
  65. BtMessageDispatcher* dispatcher_;
  66. public:
  67. AbortCompletedPieceRequest(BtMessageDispatcher* dispatcher):
  68. dispatcher_(dispatcher) {}
  69. void operator()(const std::shared_ptr<Piece>& piece)
  70. {
  71. if(piece->pieceComplete()) {
  72. dispatcher_->doAbortOutstandingRequestAction(piece);
  73. }
  74. }
  75. };
  76. } // namespace
  77. void DefaultBtRequestFactory::removeCompletedPiece() {
  78. std::for_each(pieces_.begin(), pieces_.end(),
  79. AbortCompletedPieceRequest(dispatcher_));
  80. pieces_.erase(std::remove_if(pieces_.begin(), pieces_.end(),
  81. std::mem_fn(&Piece::pieceComplete)),
  82. pieces_.end());
  83. }
  84. void DefaultBtRequestFactory::removeTargetPiece
  85. (const std::shared_ptr<Piece>& piece)
  86. {
  87. pieces_.erase(std::remove_if(pieces_.begin(), pieces_.end(),
  88. derefEqual(piece)),
  89. pieces_.end());
  90. dispatcher_->doAbortOutstandingRequestAction(piece);
  91. pieceStorage_->cancelPiece(piece, cuid_);
  92. }
  93. namespace {
  94. class ProcessChokedPiece {
  95. private:
  96. std::shared_ptr<Peer> peer_;
  97. PieceStorage* pieceStorage_;
  98. cuid_t cuid_;
  99. public:
  100. ProcessChokedPiece(const std::shared_ptr<Peer>& peer,
  101. PieceStorage* pieceStorage,
  102. cuid_t cuid):
  103. peer_(peer),
  104. pieceStorage_(pieceStorage),
  105. cuid_(cuid)
  106. {}
  107. void operator()(const std::shared_ptr<Piece>& piece)
  108. {
  109. if(!peer_->isInPeerAllowedIndexSet(piece->getIndex())) {
  110. pieceStorage_->cancelPiece(piece, cuid_);
  111. }
  112. }
  113. };
  114. } // namespace
  115. namespace {
  116. class FindChokedPiece {
  117. private:
  118. std::shared_ptr<Peer> peer_;
  119. public:
  120. FindChokedPiece(const std::shared_ptr<Peer>& peer):peer_(peer) {}
  121. bool operator()(const std::shared_ptr<Piece>& piece)
  122. {
  123. return !peer_->isInPeerAllowedIndexSet(piece->getIndex());
  124. }
  125. };
  126. } // namespace
  127. void DefaultBtRequestFactory::doChokedAction()
  128. {
  129. std::for_each(pieces_.begin(), pieces_.end(),
  130. ProcessChokedPiece(peer_, pieceStorage_, cuid_));
  131. pieces_.erase(std::remove_if(pieces_.begin(), pieces_.end(),
  132. FindChokedPiece(peer_)),
  133. pieces_.end());
  134. }
  135. void DefaultBtRequestFactory::removeAllTargetPiece() {
  136. for(std::deque<std::shared_ptr<Piece> >::iterator itr = pieces_.begin(),
  137. eoi = pieces_.end(); itr != eoi; ++itr) {
  138. dispatcher_->doAbortOutstandingRequestAction(*itr);
  139. pieceStorage_->cancelPiece(*itr, cuid_);
  140. }
  141. pieces_.clear();
  142. }
  143. void DefaultBtRequestFactory::createRequestMessages
  144. (std::vector<std::shared_ptr<BtMessage> >& requests, size_t max)
  145. {
  146. if(requests.size() >= max) {
  147. return;
  148. }
  149. size_t getnum = max-requests.size();
  150. std::vector<size_t> blockIndexes;
  151. blockIndexes.reserve(getnum);
  152. for(std::deque<std::shared_ptr<Piece> >::iterator itr = pieces_.begin(),
  153. eoi = pieces_.end(); itr != eoi && getnum; ++itr) {
  154. std::shared_ptr<Piece>& piece = *itr;
  155. if(piece->getMissingUnusedBlockIndex(blockIndexes, getnum)) {
  156. getnum -= blockIndexes.size();
  157. for(std::vector<size_t>::const_iterator i = blockIndexes.begin(),
  158. eoi2 = blockIndexes.end(); i != eoi2; ++i) {
  159. A2_LOG_DEBUG
  160. (fmt("Creating RequestMessage index=%lu, begin=%u,"
  161. " blockIndex=%lu",
  162. static_cast<unsigned long>(piece->getIndex()),
  163. static_cast<unsigned int>((*i)*piece->getBlockLength()),
  164. static_cast<unsigned long>(*i)));
  165. requests.push_back
  166. (messageFactory_->createRequestMessage(piece, *i));
  167. }
  168. blockIndexes.clear();
  169. }
  170. }
  171. }
  172. void DefaultBtRequestFactory::createRequestMessagesOnEndGame
  173. (std::vector<std::shared_ptr<BtMessage> >& requests, size_t max)
  174. {
  175. for(std::deque<std::shared_ptr<Piece> >::iterator itr = pieces_.begin(),
  176. eoi = pieces_.end(); itr != eoi && requests.size() < max; ++itr) {
  177. std::shared_ptr<Piece>& piece = *itr;
  178. const size_t mislen = piece->getBitfieldLength();
  179. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  180. piece->getAllMissingBlockIndexes(misbitfield, mislen);
  181. std::vector<size_t> missingBlockIndexes;
  182. size_t blockIndex = 0;
  183. for(size_t i = 0; i < mislen; ++i) {
  184. unsigned char bits = misbitfield[i];
  185. unsigned char mask = 128;
  186. for(size_t bi = 0; bi < 8; ++bi, mask >>= 1, ++blockIndex) {
  187. if(bits & mask) {
  188. missingBlockIndexes.push_back(blockIndex);
  189. }
  190. }
  191. }
  192. std::random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end(),
  193. *SimpleRandomizer::getInstance());
  194. for(std::vector<size_t>::const_iterator bitr = missingBlockIndexes.begin(),
  195. eoi2 = missingBlockIndexes.end();
  196. bitr != eoi2 && requests.size() < max; ++bitr) {
  197. const size_t& blockIndex = *bitr;
  198. if(!dispatcher_->isOutstandingRequest(piece->getIndex(),
  199. blockIndex)) {
  200. A2_LOG_DEBUG
  201. (fmt("Creating RequestMessage index=%lu, begin=%u,"
  202. " blockIndex=%lu",
  203. static_cast<unsigned long>(piece->getIndex()),
  204. static_cast<unsigned int>(blockIndex*piece->getBlockLength()),
  205. static_cast<unsigned long>(blockIndex)));
  206. requests.push_back(messageFactory_->createRequestMessage
  207. (piece, blockIndex));
  208. }
  209. }
  210. }
  211. }
  212. namespace {
  213. class CountMissingBlock
  214. {
  215. private:
  216. size_t numMissingBlock_;
  217. public:
  218. CountMissingBlock():numMissingBlock_(0) {}
  219. size_t getNumMissingBlock()
  220. {
  221. return numMissingBlock_;
  222. }
  223. void operator()(const std::shared_ptr<Piece>& piece)
  224. {
  225. numMissingBlock_ += piece->countMissingBlock();
  226. }
  227. };
  228. } // namespace
  229. size_t DefaultBtRequestFactory::countMissingBlock()
  230. {
  231. return std::for_each(pieces_.begin(), pieces_.end(),
  232. CountMissingBlock()).getNumMissingBlock();
  233. }
  234. void DefaultBtRequestFactory::getTargetPieceIndexes
  235. (std::vector<size_t>& indexes) const
  236. {
  237. std::transform(pieces_.begin(), pieces_.end(), std::back_inserter(indexes),
  238. std::mem_fn(&Piece::getIndex));
  239. }
  240. void DefaultBtRequestFactory::setPieceStorage(PieceStorage* pieceStorage)
  241. {
  242. pieceStorage_ = pieceStorage;
  243. }
  244. void DefaultBtRequestFactory::setPeer(const std::shared_ptr<Peer>& peer)
  245. {
  246. peer_ = peer;
  247. }
  248. void DefaultBtRequestFactory::setBtMessageDispatcher
  249. (BtMessageDispatcher* dispatcher)
  250. {
  251. dispatcher_ = dispatcher;
  252. }
  253. void DefaultBtRequestFactory::setBtMessageFactory(BtMessageFactory* factory)
  254. {
  255. messageFactory_ = factory;
  256. }
  257. } // namespace aria2