DefaultBtRequestFactory.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. namespace aria2 {
  49. DefaultBtRequestFactory::DefaultBtRequestFactory():
  50. cuid(0),
  51. _logger(LogFactory::getInstance())
  52. {
  53. if(_logger->debug()) {
  54. _logger->debug("DefaultBtRequestFactory::instantiated");
  55. }
  56. }
  57. DefaultBtRequestFactory::~DefaultBtRequestFactory()
  58. {
  59. if(_logger->debug()) {
  60. _logger->debug("DefaultBtRequestFactory::deleted");
  61. }
  62. }
  63. void DefaultBtRequestFactory::addTargetPiece(const PieceHandle& piece)
  64. {
  65. pieces.push_back(piece);
  66. }
  67. class AbortCompletedPieceRequest
  68. {
  69. private:
  70. WeakHandle<BtMessageDispatcher> _dispatcher;
  71. public:
  72. AbortCompletedPieceRequest(const WeakHandle<BtMessageDispatcher>& dispatcher):
  73. _dispatcher(dispatcher) {}
  74. void operator()(const SharedHandle<Piece>& piece)
  75. {
  76. if(piece->pieceComplete()) {
  77. _dispatcher->doAbortOutstandingRequestAction(piece);
  78. }
  79. }
  80. };
  81. void DefaultBtRequestFactory::removeCompletedPiece() {
  82. std::for_each(pieces.begin(), pieces.end(),
  83. AbortCompletedPieceRequest(dispatcher));
  84. pieces.erase(std::remove_if(pieces.begin(), pieces.end(),
  85. mem_fun_sh(&Piece::pieceComplete)),
  86. pieces.end());
  87. }
  88. void DefaultBtRequestFactory::removeTargetPiece(const PieceHandle& piece) {
  89. pieces.erase(std::remove(pieces.begin(), pieces.end(), piece),
  90. pieces.end());
  91. dispatcher->doAbortOutstandingRequestAction(piece);
  92. _pieceStorage->cancelPiece(piece);
  93. }
  94. class ProcessChokedPiece {
  95. private:
  96. SharedHandle<Peer> _peer;
  97. WeakHandle<PieceStorage> _pieceStorage;
  98. public:
  99. ProcessChokedPiece(const SharedHandle<Peer>& peer,
  100. const WeakHandle<PieceStorage>& pieceStorage):
  101. _peer(peer),
  102. _pieceStorage(pieceStorage) {}
  103. void operator()(const SharedHandle<Piece>& piece)
  104. {
  105. if(!_peer->isInPeerAllowedIndexSet(piece->getIndex())) {
  106. _pieceStorage->cancelPiece(piece);
  107. }
  108. }
  109. };
  110. class FindChokedPiece {
  111. private:
  112. SharedHandle<Peer> _peer;
  113. public:
  114. FindChokedPiece(const SharedHandle<Peer>& peer):_peer(peer) {}
  115. bool operator()(const SharedHandle<Piece>& piece)
  116. {
  117. return !_peer->isInPeerAllowedIndexSet(piece->getIndex());
  118. }
  119. };
  120. void DefaultBtRequestFactory::doChokedAction()
  121. {
  122. std::for_each(pieces.begin(), pieces.end(),
  123. ProcessChokedPiece(peer, _pieceStorage));
  124. pieces.erase(std::remove_if(pieces.begin(), pieces.end(),
  125. FindChokedPiece(peer)),
  126. pieces.end());
  127. }
  128. void DefaultBtRequestFactory::removeAllTargetPiece() {
  129. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); ++itr) {
  130. dispatcher->doAbortOutstandingRequestAction(*itr);
  131. _pieceStorage->cancelPiece(*itr);
  132. }
  133. pieces.clear();
  134. }
  135. void DefaultBtRequestFactory::createRequestMessages
  136. (std::deque<SharedHandle<BtMessage> >& requests, size_t max)
  137. {
  138. for(Pieces::iterator itr = pieces.begin();
  139. itr != pieces.end() && requests.size() < max; ++itr) {
  140. PieceHandle& piece = *itr;
  141. size_t blockIndex;
  142. while(requests.size() < max &&
  143. piece->getMissingUnusedBlockIndex(blockIndex)) {
  144. if(_logger->debug()) {
  145. _logger->debug("Creating RequestMessage index=%u, begin=%u,"
  146. " blockIndex=%u",
  147. piece->getIndex(),
  148. blockIndex*piece->getBlockLength(),
  149. blockIndex);
  150. }
  151. requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
  152. }
  153. }
  154. }
  155. void DefaultBtRequestFactory::createRequestMessagesOnEndGame
  156. (std::deque<SharedHandle<BtMessage> >& requests, size_t max)
  157. {
  158. for(Pieces::iterator itr = pieces.begin();
  159. itr != pieces.end() && requests.size() < max; ++itr) {
  160. PieceHandle& piece = *itr;
  161. const size_t mislen = piece->getBitfieldLength();
  162. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  163. piece->getAllMissingBlockIndexes(misbitfield, mislen);
  164. std::deque<size_t> missingBlockIndexes;
  165. size_t blockIndex = 0;
  166. for(size_t i = 0; i < mislen; ++i) {
  167. unsigned char bits = misbitfield[i];
  168. unsigned char mask = 128;
  169. for(size_t bi = 0; bi < 8; ++bi, mask >>= 1, ++blockIndex) {
  170. if(bits & mask) {
  171. missingBlockIndexes.push_back(blockIndex);
  172. }
  173. }
  174. }
  175. std::random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end(),
  176. *(SimpleRandomizer::getInstance().get()));
  177. for(std::deque<size_t>::const_iterator bitr = missingBlockIndexes.begin();
  178. bitr != missingBlockIndexes.end() && requests.size() < max; bitr++) {
  179. const size_t& blockIndex = *bitr;
  180. if(!dispatcher->isOutstandingRequest(piece->getIndex(),
  181. blockIndex)) {
  182. if(_logger->debug()) {
  183. _logger->debug("Creating RequestMessage index=%u, begin=%u,"
  184. " blockIndex=%u",
  185. piece->getIndex(),
  186. blockIndex*piece->getBlockLength(),
  187. blockIndex);
  188. }
  189. requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
  190. }
  191. }
  192. }
  193. }
  194. class CountMissingBlock
  195. {
  196. private:
  197. size_t _numMissingBlock;
  198. public:
  199. CountMissingBlock():_numMissingBlock(0) {}
  200. size_t getNumMissingBlock()
  201. {
  202. return _numMissingBlock;
  203. }
  204. void operator()(const SharedHandle<Piece>& piece)
  205. {
  206. _numMissingBlock += piece->countMissingBlock();
  207. }
  208. };
  209. size_t DefaultBtRequestFactory::countMissingBlock()
  210. {
  211. return std::for_each(pieces.begin(), pieces.end(),
  212. CountMissingBlock()).getNumMissingBlock();
  213. }
  214. void DefaultBtRequestFactory::getTargetPieceIndexes
  215. (std::deque<size_t>& indexes) const
  216. {
  217. std::transform(pieces.begin(), pieces.end(), std::back_inserter(indexes),
  218. mem_fun_sh(&Piece::getIndex));
  219. }
  220. void DefaultBtRequestFactory::setPieceStorage
  221. (const SharedHandle<PieceStorage>& pieceStorage)
  222. {
  223. _pieceStorage = pieceStorage;
  224. }
  225. void DefaultBtRequestFactory::setPeer(const SharedHandle<Peer>& peer)
  226. {
  227. this->peer = peer;
  228. }
  229. void DefaultBtRequestFactory::setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher)
  230. {
  231. this->dispatcher = dispatcher;
  232. }
  233. void DefaultBtRequestFactory::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
  234. {
  235. this->messageFactory = factory;
  236. }
  237. } // namespace aria2