RequestSlotMan.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "RequestSlotMan.h"
  23. void RequestSlotMan::addRequestSlot(const RequestSlot& requestSlot) {
  24. requestSlots.push_back(requestSlot);
  25. }
  26. void RequestSlotMan::deleteRequestSlot(const RequestSlot& requestSlot) {
  27. for(RequestSlots::iterator itr = requestSlots.begin();
  28. itr != requestSlots.end(); itr++) {
  29. if(*itr == requestSlot) {
  30. requestSlots.erase(itr);
  31. break;
  32. }
  33. }
  34. }
  35. void RequestSlotMan::deleteAllRequestSlot(Piece& piece) {
  36. if(!Piece::isNull(piece)) {
  37. for(RequestSlots::const_iterator itr = requestSlots.begin();
  38. itr != requestSlots.end(); itr++) {
  39. if(itr->getIndex() == piece.getIndex()) {
  40. piece.cancelBlock(itr->getBlockIndex());
  41. }
  42. }
  43. torrentMan->updatePiece(piece);
  44. }
  45. requestSlots.clear();
  46. }
  47. void RequestSlotMan::deleteTimeoutRequestSlot(Piece& piece) {
  48. for(RequestSlots::iterator itr = requestSlots.begin();
  49. itr != requestSlots.end();) {
  50. if(itr->isTimeout(timeout)) {
  51. logger->debug("CUID#%d - deleting requestslot blockIndex %d because of time out", cuid,
  52. itr->getBlockIndex());
  53. if(!Piece::isNull(piece)) {
  54. piece.cancelBlock(itr->getBlockIndex());
  55. }
  56. itr = requestSlots.erase(itr);
  57. } else {
  58. itr++;
  59. }
  60. }
  61. torrentMan->updatePiece(piece);
  62. }
  63. void RequestSlotMan::deleteCompletedRequestSlot(const Piece& piece) {
  64. for(RequestSlots::iterator itr = requestSlots.begin();
  65. itr != requestSlots.end();) {
  66. if(Piece::isNull(piece) || piece.hasBlock(itr->getBlockIndex()) ||
  67. torrentMan->hasPiece(piece.getIndex())) {
  68. logger->debug("CUID#%d - deleting requestslot blockIndex %d because the block is already acquired.", cuid,
  69. itr->getBlockIndex());
  70. PendingMessage pendingMessage =
  71. PendingMessage::createCancelMessage(itr->getIndex(),
  72. itr->getBegin(),
  73. itr->getLength(),
  74. peerConnection);
  75. pendingMessages->push_back(pendingMessage);
  76. itr = requestSlots.erase(itr);
  77. } else {
  78. itr++;
  79. }
  80. }
  81. }
  82. RequestSlot RequestSlotMan::getCorrespoindingRequestSlot(const PeerMessage* pieceMessage) const {
  83. for(RequestSlots::const_iterator itr = requestSlots.begin();
  84. itr != requestSlots.end(); itr++) {
  85. const RequestSlot& slot = *itr;
  86. if(slot.getIndex() == pieceMessage->getIndex() &&
  87. slot.getBegin() == pieceMessage->getBegin() &&
  88. slot.getLength() == pieceMessage->getBlockLength()) {
  89. return slot;
  90. }
  91. }
  92. return RequestSlot::nullSlot;
  93. }