DefaultBtRequestFactory.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. if(requests.size() >= max) {
  139. return;
  140. }
  141. size_t getnum = max-requests.size();
  142. std::vector<size_t> blockIndexes;
  143. blockIndexes.reserve(getnum);
  144. for(Pieces::iterator itr = pieces.begin();
  145. itr != pieces.end() && getnum; ++itr) {
  146. SharedHandle<Piece>& piece = *itr;
  147. if(piece->getMissingUnusedBlockIndex(blockIndexes, getnum)) {
  148. getnum -= blockIndexes.size();
  149. for(std::vector<size_t>::const_iterator i = blockIndexes.begin();
  150. i != blockIndexes.end(); ++i) {
  151. if(_logger->debug()) {
  152. _logger->debug("Creating RequestMessage index=%u, begin=%u,"
  153. " blockIndex=%u",
  154. piece->getIndex(),
  155. (*i)*piece->getBlockLength(),
  156. (*i));
  157. }
  158. requests.push_back
  159. (messageFactory->createRequestMessage(piece, *i));
  160. }
  161. blockIndexes.clear();
  162. }
  163. }
  164. }
  165. void DefaultBtRequestFactory::createRequestMessagesOnEndGame
  166. (std::deque<SharedHandle<BtMessage> >& requests, size_t max)
  167. {
  168. for(Pieces::iterator itr = pieces.begin();
  169. itr != pieces.end() && requests.size() < max; ++itr) {
  170. PieceHandle& piece = *itr;
  171. const size_t mislen = piece->getBitfieldLength();
  172. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  173. piece->getAllMissingBlockIndexes(misbitfield, mislen);
  174. std::deque<size_t> missingBlockIndexes;
  175. size_t blockIndex = 0;
  176. for(size_t i = 0; i < mislen; ++i) {
  177. unsigned char bits = misbitfield[i];
  178. unsigned char mask = 128;
  179. for(size_t bi = 0; bi < 8; ++bi, mask >>= 1, ++blockIndex) {
  180. if(bits & mask) {
  181. missingBlockIndexes.push_back(blockIndex);
  182. }
  183. }
  184. }
  185. std::random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end(),
  186. *(SimpleRandomizer::getInstance().get()));
  187. for(std::deque<size_t>::const_iterator bitr = missingBlockIndexes.begin();
  188. bitr != missingBlockIndexes.end() && requests.size() < max; bitr++) {
  189. const size_t& blockIndex = *bitr;
  190. if(!dispatcher->isOutstandingRequest(piece->getIndex(),
  191. blockIndex)) {
  192. if(_logger->debug()) {
  193. _logger->debug("Creating RequestMessage index=%u, begin=%u,"
  194. " blockIndex=%u",
  195. piece->getIndex(),
  196. blockIndex*piece->getBlockLength(),
  197. blockIndex);
  198. }
  199. requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
  200. }
  201. }
  202. }
  203. }
  204. class CountMissingBlock
  205. {
  206. private:
  207. size_t _numMissingBlock;
  208. public:
  209. CountMissingBlock():_numMissingBlock(0) {}
  210. size_t getNumMissingBlock()
  211. {
  212. return _numMissingBlock;
  213. }
  214. void operator()(const SharedHandle<Piece>& piece)
  215. {
  216. _numMissingBlock += piece->countMissingBlock();
  217. }
  218. };
  219. size_t DefaultBtRequestFactory::countMissingBlock()
  220. {
  221. return std::for_each(pieces.begin(), pieces.end(),
  222. CountMissingBlock()).getNumMissingBlock();
  223. }
  224. void DefaultBtRequestFactory::getTargetPieceIndexes
  225. (std::deque<size_t>& indexes) const
  226. {
  227. std::transform(pieces.begin(), pieces.end(), std::back_inserter(indexes),
  228. mem_fun_sh(&Piece::getIndex));
  229. }
  230. void DefaultBtRequestFactory::setPieceStorage
  231. (const SharedHandle<PieceStorage>& pieceStorage)
  232. {
  233. _pieceStorage = pieceStorage;
  234. }
  235. void DefaultBtRequestFactory::setPeer(const SharedHandle<Peer>& peer)
  236. {
  237. this->peer = peer;
  238. }
  239. void DefaultBtRequestFactory::setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher)
  240. {
  241. this->dispatcher = dispatcher;
  242. }
  243. void DefaultBtRequestFactory::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
  244. {
  245. this->messageFactory = factory;
  246. }
  247. } // namespace aria2