DefaultBtMessageDispatcher.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "prefs.h"
  37. #include "BtAbortOutstandingRequestEvent.h"
  38. #include "BtCancelSendingPieceEvent.h"
  39. #include "BtChokedEvent.h"
  40. #include "BtChokingEvent.h"
  41. #include "BtMessageFactory.h"
  42. #include "message.h"
  43. #include "BtContext.h"
  44. #include "PeerStorage.h"
  45. #include "PieceStorage.h"
  46. #include "BtMessage.h"
  47. #include "BtRegistry.h"
  48. #include "Peer.h"
  49. #include "Piece.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include <algorithm>
  53. namespace aria2 {
  54. DefaultBtMessageDispatcher::DefaultBtMessageDispatcher():
  55. cuid(0),
  56. btContext(0),
  57. peerStorage(0),
  58. pieceStorage(0),
  59. peer(0),
  60. maxUploadSpeedLimit(0),
  61. requestTimeout(0),
  62. logger(LogFactory::getInstance()) {}
  63. DefaultBtMessageDispatcher::~DefaultBtMessageDispatcher()
  64. {
  65. logger->debug("DefaultBtMessageDispatcher::deleted");
  66. }
  67. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessageHandle& btMessage)
  68. {
  69. btMessage->onQueued();
  70. messageQueue.push_back(btMessage);
  71. }
  72. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessages& btMessages)
  73. {
  74. for(BtMessages::const_iterator itr = btMessages.begin(); itr != btMessages.end(); itr++) {
  75. addMessageToQueue(*itr);
  76. }
  77. }
  78. void DefaultBtMessageDispatcher::sendMessages() {
  79. BtMessages tempQueue;
  80. while(messageQueue.size() > 0) {
  81. BtMessageHandle msg = messageQueue.front();
  82. messageQueue.pop_front();
  83. if(maxUploadSpeedLimit > 0 &&
  84. msg->isUploading() && !msg->isSendingInProgress()) {
  85. TransferStat stat = peerStorage->calculateStat();
  86. if(maxUploadSpeedLimit < stat.getUploadSpeed()) {
  87. tempQueue.push_back(msg);
  88. continue;
  89. }
  90. }
  91. msg->send();
  92. if(msg->isSendingInProgress()) {
  93. messageQueue.push_front(msg);
  94. break;
  95. }
  96. }
  97. std::copy(tempQueue.begin(), tempQueue.end(), std::back_inserter(messageQueue));
  98. }
  99. // Cancel sending piece message to peer.
  100. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(size_t index, uint32_t begin, size_t length)
  101. {
  102. BtCancelSendingPieceEventHandle event =
  103. new BtCancelSendingPieceEvent(index, begin, length);
  104. BtMessages tempQueue = messageQueue;
  105. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); itr++) {
  106. (*itr)->handleEvent(event);
  107. }
  108. }
  109. // Cancel sending piece message to peer.
  110. // TODO Is this method really necessary?
  111. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(const PieceHandle& piece)
  112. {
  113. }
  114. // localhost cancels outstanding download requests to the peer.
  115. void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(const PieceHandle& piece) {
  116. for(RequestSlots::iterator itr = requestSlots.begin();
  117. itr != requestSlots.end();) {
  118. RequestSlot& slot = *itr;
  119. if(slot.getIndex() == piece->getIndex()) {
  120. logger->debug(MSG_DELETING_REQUEST_SLOT,
  121. cuid,
  122. slot.getIndex(),
  123. slot.getBlockIndex());
  124. piece->cancelBlock(slot.getBlockIndex());
  125. itr = requestSlots.erase(itr);
  126. } else {
  127. itr++;
  128. }
  129. }
  130. BtAbortOutstandingRequestEventHandle event =
  131. new BtAbortOutstandingRequestEvent(piece);
  132. BtMessages tempQueue = messageQueue;
  133. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  134. (*itr)->handleEvent(event);
  135. }
  136. }
  137. // localhost received choke message from the peer.
  138. void DefaultBtMessageDispatcher::doChokedAction()
  139. {
  140. for(RequestSlots::iterator itr = requestSlots.begin();
  141. itr != requestSlots.end();) {
  142. RequestSlot& slot = *itr;
  143. if(peer->isInPeerAllowedIndexSet(slot.getIndex())) {
  144. itr++;
  145. } else {
  146. logger->debug(MSG_DELETING_REQUEST_SLOT_CHOKED,
  147. cuid,
  148. slot.getIndex(),
  149. slot.getBlockIndex());
  150. PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
  151. piece->cancelBlock(slot.getBlockIndex());
  152. itr = requestSlots.erase(itr);
  153. }
  154. }
  155. BtChokedEventHandle event = new BtChokedEvent();
  156. BtMessages tempQueue = messageQueue;
  157. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  158. (*itr)->handleEvent(event);
  159. }
  160. }
  161. // localhost dispatched choke message to the peer.
  162. void DefaultBtMessageDispatcher::doChokingAction()
  163. {
  164. BtChokingEventHandle event = new BtChokingEvent();
  165. BtMessages tempQueue = messageQueue;
  166. for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
  167. (*itr)->handleEvent(event);
  168. }
  169. }
  170. void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
  171. {
  172. for(RequestSlots::iterator itr = requestSlots.begin();
  173. itr != requestSlots.end();) {
  174. RequestSlot& slot = *itr;
  175. PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
  176. if(slot.isTimeout(requestTimeout)) {
  177. logger->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
  178. cuid,
  179. slot.getBlockIndex());
  180. piece->cancelBlock(slot.getBlockIndex());
  181. peer->snubbing(true);
  182. itr = requestSlots.erase(itr);
  183. } else if(piece->hasBlock(slot.getBlockIndex())) {
  184. logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
  185. cuid,
  186. slot.getBlockIndex());
  187. addMessageToQueue(messageFactory->createCancelMessage(slot.getIndex(),
  188. slot.getBegin(),
  189. slot.getLength()));
  190. itr = requestSlots.erase(itr);
  191. } else {
  192. itr++;
  193. }
  194. }
  195. }
  196. bool DefaultBtMessageDispatcher::isSendingInProgress()
  197. {
  198. if(messageQueue.size() > 0) {
  199. return messageQueue.front()->isSendingInProgress();
  200. } else {
  201. return false;
  202. }
  203. }
  204. size_t DefaultBtMessageDispatcher::countOutstandingRequest()
  205. {
  206. return requestSlots.size();
  207. }
  208. bool DefaultBtMessageDispatcher::isOutstandingRequest(size_t index, size_t blockIndex) {
  209. for(RequestSlots::const_iterator itr = requestSlots.begin();
  210. itr != requestSlots.end(); itr++) {
  211. const RequestSlot& slot = *itr;
  212. if(slot.getIndex() == index && slot.getBlockIndex() == blockIndex) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. RequestSlot
  219. DefaultBtMessageDispatcher::getOutstandingRequest(size_t index, uint32_t begin, size_t length)
  220. {
  221. for(RequestSlots::iterator itr = requestSlots.begin();
  222. itr != requestSlots.end(); itr++) {
  223. if(itr->getIndex() == index &&
  224. itr->getBegin() == begin &&
  225. itr->getLength() == length) {
  226. return *itr;
  227. }
  228. }
  229. return RequestSlot::nullSlot;
  230. }
  231. void DefaultBtMessageDispatcher::removeOutstandingRequest(const RequestSlot& slot)
  232. {
  233. RequestSlots temp;
  234. std::remove_copy(requestSlots.begin(), requestSlots.end(), std::back_inserter(temp), slot);
  235. requestSlots = temp;
  236. }
  237. void DefaultBtMessageDispatcher::addOutstandingRequest(const RequestSlot& requestSlot)
  238. {
  239. if(!isOutstandingRequest(requestSlot.getIndex(), requestSlot.getBlockIndex())) {
  240. requestSlots.push_back(requestSlot);
  241. }
  242. }
  243. std::deque<SharedHandle<BtMessage> >&
  244. DefaultBtMessageDispatcher::getMessageQueue()
  245. {
  246. return messageQueue;
  247. }
  248. std::deque<RequestSlot>& DefaultBtMessageDispatcher::getRequestSlots()
  249. {
  250. return requestSlots;
  251. }
  252. void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
  253. {
  254. this->peer = peer;
  255. }
  256. void DefaultBtMessageDispatcher::setBtContext(const BtContextHandle& btContext)
  257. {
  258. this->btContext = btContext;
  259. this->pieceStorage = PIECE_STORAGE(btContext);
  260. this->peerStorage = PEER_STORAGE(btContext);
  261. }
  262. void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
  263. {
  264. this->messageFactory = factory;
  265. }
  266. } // namespace aria2