DefaultBtMessageDispatcher.cc 8.9 KB

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