DefaultBtRequestFactory.cc 8.6 KB

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