DefaultBtMessageDispatcher.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessageHandle& btMessage)
  45. {
  46. btMessage->onQueued();
  47. messageQueue.push_back(btMessage);
  48. }
  49. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessages& btMessages)
  50. {
  51. for(BtMessages::const_iterator itr = btMessages.begin(); itr != btMessages.end(); itr++) {
  52. addMessageToQueue(*itr);
  53. }
  54. }
  55. void DefaultBtMessageDispatcher::sendMessages() {
  56. BtMessages tempQueue;
  57. while(messageQueue.size() > 0) {
  58. BtMessageHandle msg = messageQueue.front();
  59. messageQueue.pop_front();
  60. if(maxUploadSpeedLimit > 0 &&
  61. msg->isUploading() && !msg->isSendingInProgress()) {
  62. TransferStat stat = peerStorage->calculateStat();
  63. if(maxUploadSpeedLimit < stat.getUploadSpeed()) {
  64. tempQueue.push_back(msg);
  65. continue;
  66. }
  67. }
  68. msg->send();
  69. if(msg->isSendingInProgress()) {
  70. messageQueue.push_front(msg);
  71. break;
  72. }
  73. }
  74. copy(tempQueue.begin(), tempQueue.end(), back_inserter(messageQueue));
  75. }
  76. // Cancel sending piece message to peer.
  77. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length)
  78. {
  79. BtCancelSendingPieceEventHandle event =
  80. new BtCancelSendingPieceEvent(index, begin, length);
  81. BtMessages tempQueue = messageQueue;
  82. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); itr++) {
  83. (*itr)->handleEvent(event);
  84. }
  85. }
  86. // Cancel sending piece message to peer.
  87. // TODO Is this method really necessary?
  88. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(const PieceHandle& piece)
  89. {
  90. }
  91. // localhost cancels outstanding download requests to the peer.
  92. void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(const PieceHandle& piece) {
  93. for(RequestSlots::iterator itr = requestSlots.begin();
  94. itr != requestSlots.end();) {
  95. RequestSlot& slot = *itr;
  96. if(slot.getIndex() == piece->getIndex()) {
  97. logger->debug("CUID#%d - Deleting request slot index=%d, blockIndex=%d",
  98. cuid,
  99. slot.getIndex(),
  100. slot.getBlockIndex());
  101. piece->cancelBlock(slot.getBlockIndex());
  102. itr = requestSlots.erase(itr);
  103. } else {
  104. itr++;
  105. }
  106. }
  107. BtAbortOutstandingRequestEventHandle event =
  108. new BtAbortOutstandingRequestEvent(piece);
  109. BtMessages tempQueue = messageQueue;
  110. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  111. (*itr)->handleEvent(event);
  112. }
  113. }
  114. // localhost received choke message from the peer.
  115. void DefaultBtMessageDispatcher::doChokedAction()
  116. {
  117. for(RequestSlots::iterator itr = requestSlots.begin();
  118. itr != requestSlots.end();) {
  119. RequestSlot& slot = *itr;
  120. if(peer->isInPeerAllowedIndexSet(slot.getIndex())) {
  121. itr++;
  122. } else {
  123. logger->debug("CUID#%d - Deleting request slot index=%d, blockIndex=%d"
  124. " because localhost got 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("CUID#%d - Deleting request slot blockIndex=%d"
  156. " because of time out",
  157. cuid,
  158. slot.getBlockIndex());
  159. piece->cancelBlock(slot.getBlockIndex());
  160. peer->snubbing = true;
  161. itr = requestSlots.erase(itr);
  162. } else if(piece->hasBlock(slot.getBlockIndex())) {
  163. logger->debug("CUID#%d - Deleting request slot blockIndex=%d because"
  164. " the block has been acquired.",
  165. cuid,
  166. slot.getBlockIndex());
  167. addMessageToQueue(messageFactory->createCancelMessage(slot.getIndex(),
  168. slot.getBegin(),
  169. slot.getLength()));
  170. itr = requestSlots.erase(itr);
  171. } else {
  172. itr++;
  173. }
  174. }
  175. }
  176. bool DefaultBtMessageDispatcher::isSendingInProgress()
  177. {
  178. if(messageQueue.size() > 0) {
  179. return messageQueue.front()->isSendingInProgress();
  180. } else {
  181. return false;
  182. }
  183. }
  184. int32_t DefaultBtMessageDispatcher::countOutstandingRequest()
  185. {
  186. return requestSlots.size();
  187. }
  188. bool DefaultBtMessageDispatcher::isOutstandingRequest(int32_t index, int32_t blockIndex) {
  189. for(RequestSlots::const_iterator itr = requestSlots.begin();
  190. itr != requestSlots.end(); itr++) {
  191. const RequestSlot& slot = *itr;
  192. if(slot.getIndex() == index && slot.getBlockIndex() == blockIndex) {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }