DefaultBtMessageDispatcher.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "DefaultBtMessageDispatcher.h"
  36. #include "BtRegistry.h"
  37. #include "prefs.h"
  38. #include "BtAbortOutstandingRequestEvent.h"
  39. #include "BtCancelSendingPieceEvent.h"
  40. #include "BtChokedEvent.h"
  41. #include "BtChokingEvent.h"
  42. #include "BtRegistry.h"
  43. #include "BtMessageFactory.h"
  44. #include "message.h"
  45. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessageHandle& btMessage)
  46. {
  47. btMessage->onQueued();
  48. messageQueue.push_back(btMessage);
  49. }
  50. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessages& btMessages)
  51. {
  52. for(BtMessages::const_iterator itr = btMessages.begin(); itr != btMessages.end(); itr++) {
  53. addMessageToQueue(*itr);
  54. }
  55. }
  56. void DefaultBtMessageDispatcher::sendMessages() {
  57. BtMessages tempQueue;
  58. while(messageQueue.size() > 0) {
  59. BtMessageHandle msg = messageQueue.front();
  60. messageQueue.pop_front();
  61. if(maxUploadSpeedLimit > 0 &&
  62. msg->isUploading() && !msg->isSendingInProgress()) {
  63. TransferStat stat = peerStorage->calculateStat();
  64. if(maxUploadSpeedLimit < stat.getUploadSpeed()) {
  65. tempQueue.push_back(msg);
  66. continue;
  67. }
  68. }
  69. msg->send();
  70. if(msg->isSendingInProgress()) {
  71. messageQueue.push_front(msg);
  72. break;
  73. }
  74. }
  75. copy(tempQueue.begin(), tempQueue.end(), back_inserter(messageQueue));
  76. }
  77. // Cancel sending piece message to peer.
  78. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length)
  79. {
  80. BtCancelSendingPieceEventHandle event =
  81. new BtCancelSendingPieceEvent(index, begin, length);
  82. BtMessages tempQueue = messageQueue;
  83. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); itr++) {
  84. (*itr)->handleEvent(event);
  85. }
  86. }
  87. // Cancel sending piece message to peer.
  88. // TODO Is this method really necessary?
  89. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(const PieceHandle& piece)
  90. {
  91. }
  92. // localhost cancels outstanding download requests to the peer.
  93. void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(const PieceHandle& piece) {
  94. for(RequestSlots::iterator itr = requestSlots.begin();
  95. itr != requestSlots.end();) {
  96. RequestSlot& slot = *itr;
  97. if(slot.getIndex() == piece->getIndex()) {
  98. logger->debug(MSG_DELETING_REQUEST_SLOT,
  99. cuid,
  100. slot.getIndex(),
  101. slot.getBlockIndex());
  102. piece->cancelBlock(slot.getBlockIndex());
  103. itr = requestSlots.erase(itr);
  104. } else {
  105. itr++;
  106. }
  107. }
  108. BtAbortOutstandingRequestEventHandle event =
  109. new BtAbortOutstandingRequestEvent(piece);
  110. BtMessages tempQueue = messageQueue;
  111. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  112. (*itr)->handleEvent(event);
  113. }
  114. }
  115. // localhost received choke message from the peer.
  116. void DefaultBtMessageDispatcher::doChokedAction()
  117. {
  118. for(RequestSlots::iterator itr = requestSlots.begin();
  119. itr != requestSlots.end();) {
  120. RequestSlot& slot = *itr;
  121. if(peer->isInPeerAllowedIndexSet(slot.getIndex())) {
  122. itr++;
  123. } else {
  124. logger->debug(MSG_DELETING_REQUEST_SLOT_CHOKED,
  125. cuid,
  126. slot.getIndex(),
  127. slot.getBlockIndex());
  128. PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
  129. piece->cancelBlock(slot.getBlockIndex());
  130. itr = requestSlots.erase(itr);
  131. }
  132. }
  133. BtChokedEventHandle event = new BtChokedEvent();
  134. BtMessages tempQueue = messageQueue;
  135. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  136. (*itr)->handleEvent(event);
  137. }
  138. }
  139. // localhost dispatched choke message to the peer.
  140. void DefaultBtMessageDispatcher::doChokingAction()
  141. {
  142. BtChokingEventHandle event = new BtChokingEvent();
  143. BtMessages tempQueue = messageQueue;
  144. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  145. (*itr)->handleEvent(event);
  146. }
  147. }
  148. void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
  149. {
  150. for(RequestSlots::iterator itr = requestSlots.begin();
  151. itr != requestSlots.end();) {
  152. RequestSlot& slot = *itr;
  153. PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
  154. if(slot.isTimeout(requestTimeout)) {
  155. logger->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
  156. cuid,
  157. slot.getBlockIndex());
  158. piece->cancelBlock(slot.getBlockIndex());
  159. peer->snubbing = true;
  160. itr = requestSlots.erase(itr);
  161. } else if(piece->hasBlock(slot.getBlockIndex())) {
  162. logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
  163. cuid,
  164. slot.getBlockIndex());
  165. addMessageToQueue(messageFactory->createCancelMessage(slot.getIndex(),
  166. slot.getBegin(),
  167. slot.getLength()));
  168. itr = requestSlots.erase(itr);
  169. } else {
  170. itr++;
  171. }
  172. }
  173. }
  174. bool DefaultBtMessageDispatcher::isSendingInProgress()
  175. {
  176. if(messageQueue.size() > 0) {
  177. return messageQueue.front()->isSendingInProgress();
  178. } else {
  179. return false;
  180. }
  181. }
  182. int32_t DefaultBtMessageDispatcher::countOutstandingRequest()
  183. {
  184. return requestSlots.size();
  185. }
  186. bool DefaultBtMessageDispatcher::isOutstandingRequest(int32_t index, int32_t blockIndex) {
  187. for(RequestSlots::const_iterator itr = requestSlots.begin();
  188. itr != requestSlots.end(); itr++) {
  189. const RequestSlot& slot = *itr;
  190. if(slot.getIndex() == index && slot.getBlockIndex() == blockIndex) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }