DefaultBtRequestFactory.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. void DefaultBtRequestFactory::removeCompletedPiece() {
  37. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end();) {
  38. PieceHandle& piece = *itr;
  39. if(piece->pieceComplete()) {
  40. dispatcher->doAbortOutstandingRequestAction(piece);
  41. itr = pieces.erase(itr);
  42. } else {
  43. itr++;
  44. }
  45. }
  46. }
  47. void DefaultBtRequestFactory::removeTargetPiece(const PieceHandle& piece) {
  48. Pieces temp;
  49. remove_copy(pieces.begin(), pieces.end(), back_inserter(temp), piece);
  50. pieces = temp;
  51. dispatcher->doAbortOutstandingRequestAction(piece);
  52. pieceStorage->cancelPiece(piece);
  53. }
  54. void DefaultBtRequestFactory::doChokedAction()
  55. {
  56. Pieces temp;
  57. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  58. PieceHandle& piece = *itr;
  59. if(peer->isInPeerAllowedIndexSet(piece->getIndex())) {
  60. temp.push_back(piece);
  61. } else {
  62. pieceStorage->cancelPiece(*itr);
  63. }
  64. }
  65. pieces = temp;
  66. }
  67. void DefaultBtRequestFactory::removeAllTargetPiece() {
  68. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  69. dispatcher->doAbortOutstandingRequestAction(*itr);
  70. pieceStorage->cancelPiece(*itr);
  71. }
  72. pieces.clear();
  73. }
  74. BtMessages DefaultBtRequestFactory::createRequestMessages(int32_t max) {
  75. BtMessages requests;
  76. for(Pieces::iterator itr = pieces.begin();
  77. itr != pieces.end() && requests.size() < (size_t)max; itr++) {
  78. PieceHandle& piece = *itr;
  79. int32_t blockIndex;
  80. while(requests.size() < (size_t)max &&
  81. (blockIndex = piece->getMissingUnusedBlockIndex()) != -1) {
  82. requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
  83. }
  84. }
  85. return requests;
  86. }
  87. BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(int32_t max) {
  88. BtMessages requests;
  89. for(Pieces::iterator itr = pieces.begin();
  90. itr != pieces.end() && requests.size() < (size_t)max; itr++) {
  91. PieceHandle& piece = *itr;
  92. BlockIndexes missingBlockIndexes = piece->getAllMissingBlockIndexes();
  93. random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end());
  94. for(BlockIndexes::const_iterator bitr = missingBlockIndexes.begin();
  95. bitr != missingBlockIndexes.end() && requests.size() < (size_t)max;
  96. bitr++) {
  97. int32_t blockIndex = *bitr;
  98. if(!dispatcher->isOutstandingRequest(piece->getIndex(),
  99. blockIndex)) {
  100. requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
  101. }
  102. }
  103. }
  104. return requests;
  105. }