DefaultBtMessageDispatcher.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <algorithm>
  37. #include "prefs.h"
  38. #include "BtAbortOutstandingRequestEvent.h"
  39. #include "BtCancelSendingPieceEvent.h"
  40. #include "BtChokingEvent.h"
  41. #include "BtMessageFactory.h"
  42. #include "message.h"
  43. #include "DownloadContext.h"
  44. #include "PeerStorage.h"
  45. #include "PieceStorage.h"
  46. #include "BtMessage.h"
  47. #include "Peer.h"
  48. #include "Piece.h"
  49. #include "LogFactory.h"
  50. #include "Logger.h"
  51. #include "a2functional.h"
  52. #include "a2algo.h"
  53. #include "RequestGroupMan.h"
  54. #include "RequestGroup.h"
  55. namespace aria2 {
  56. DefaultBtMessageDispatcher::DefaultBtMessageDispatcher():
  57. cuid(0),
  58. requestTimeout(0),
  59. logger(LogFactory::getInstance()) {}
  60. DefaultBtMessageDispatcher::~DefaultBtMessageDispatcher()
  61. {
  62. if(logger->debug()) {
  63. logger->debug("DefaultBtMessageDispatcher::deleted");
  64. }
  65. }
  66. void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessageHandle& btMessage)
  67. {
  68. btMessage->onQueued();
  69. messageQueue.push_back(btMessage);
  70. }
  71. void DefaultBtMessageDispatcher::addMessageToQueue
  72. (const std::vector<SharedHandle<BtMessage> >& btMessages)
  73. {
  74. for(std::vector<SharedHandle<BtMessage> >::const_iterator itr =
  75. btMessages.begin(), eoi = btMessages.end(); itr != eoi; ++itr) {
  76. addMessageToQueue(*itr);
  77. }
  78. }
  79. void DefaultBtMessageDispatcher::sendMessages() {
  80. std::vector<SharedHandle<BtMessage> > tempQueue;
  81. while(!messageQueue.empty()) {
  82. BtMessageHandle msg = messageQueue.front();
  83. messageQueue.pop_front();
  84. if(msg->isUploading() && !msg->isSendingInProgress()) {
  85. if(_requestGroupMan->doesOverallUploadSpeedExceed() ||
  86. _downloadContext->getOwnerRequestGroup()->doesUploadSpeedExceed()) {
  87. tempQueue.push_back(msg);
  88. continue;
  89. }
  90. }
  91. msg->send();
  92. if(msg->isUploading()) {
  93. _peerStorage->updateTransferStatFor(peer);
  94. }
  95. if(msg->isSendingInProgress()) {
  96. messageQueue.push_front(msg);
  97. break;
  98. }
  99. }
  100. if(!tempQueue.empty()) {
  101. // Insert pending message to the front, so that message is likely sent in
  102. // the same order as it is queued.
  103. if(!messageQueue.empty() && messageQueue.front()->isSendingInProgress()) {
  104. messageQueue.insert(messageQueue.begin()+1,
  105. tempQueue.begin(), tempQueue.end());
  106. } else {
  107. messageQueue.insert(messageQueue.begin(),
  108. tempQueue.begin(), tempQueue.end());
  109. }
  110. }
  111. }
  112. // Cancel sending piece message to peer.
  113. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(size_t index, uint32_t begin, size_t length)
  114. {
  115. BtCancelSendingPieceEvent event(index, begin, length);
  116. std::vector<SharedHandle<BtMessage> > tempQueue
  117. (messageQueue.begin(), messageQueue.end());
  118. forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
  119. &BtMessage::onCancelSendingPieceEvent, event);
  120. }
  121. // Cancel sending piece message to peer.
  122. // TODO Is this method really necessary?
  123. void DefaultBtMessageDispatcher::doCancelSendingPieceAction
  124. (const SharedHandle<Piece>& piece)
  125. {
  126. }
  127. class AbortOutstandingRequest {
  128. private:
  129. SharedHandle<Piece> _piece;
  130. int32_t _cuid;
  131. Logger* _logger;
  132. public:
  133. AbortOutstandingRequest(const SharedHandle<Piece>& piece, int32_t cuid):
  134. _piece(piece),
  135. _cuid(cuid),
  136. _logger(LogFactory::getInstance()) {}
  137. void operator()(const RequestSlot& slot) const
  138. {
  139. if(_logger->debug()) {
  140. _logger->debug(MSG_DELETING_REQUEST_SLOT,
  141. _cuid,
  142. slot.getIndex(),
  143. slot.getBlockIndex());
  144. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  145. }
  146. _piece->cancelBlock(slot.getBlockIndex());
  147. }
  148. };
  149. // localhost cancels outstanding download requests to the peer.
  150. void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction
  151. (const SharedHandle<Piece>& piece) {
  152. RequestSlot rs(piece->getIndex(), 0, 0, 0);
  153. std::deque<RequestSlot>::iterator first =
  154. std::lower_bound(requestSlots.begin(), requestSlots.end(), rs);
  155. rs.setIndex(piece->getIndex()+1);
  156. std::deque<RequestSlot>::iterator last =
  157. std::lower_bound(requestSlots.begin(), requestSlots.end(), rs);
  158. std::for_each(first, last, AbortOutstandingRequest(piece, cuid));
  159. requestSlots.erase(first, last);
  160. BtAbortOutstandingRequestEvent event(piece);
  161. std::vector<SharedHandle<BtMessage> > tempQueue
  162. (messageQueue.begin(), messageQueue.end());
  163. forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
  164. &BtMessage::onAbortOutstandingRequestEvent, event);
  165. }
  166. class ProcessChokedRequestSlot {
  167. private:
  168. int32_t _cuid;
  169. SharedHandle<Peer> _peer;
  170. SharedHandle<PieceStorage> _pieceStorage;
  171. Logger* _logger;
  172. public:
  173. ProcessChokedRequestSlot(int32_t cuid,
  174. const SharedHandle<Peer>& peer,
  175. const SharedHandle<PieceStorage>& pieceStorage):
  176. _cuid(cuid),
  177. _peer(peer),
  178. _pieceStorage(pieceStorage),
  179. _logger(LogFactory::getInstance()) {}
  180. void operator()(const RequestSlot& slot) const
  181. {
  182. if(!_peer->isInPeerAllowedIndexSet(slot.getIndex())) {
  183. if(_logger->debug()) {
  184. _logger->debug(MSG_DELETING_REQUEST_SLOT_CHOKED,
  185. _cuid,
  186. slot.getIndex(),
  187. slot.getBlockIndex());
  188. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  189. }
  190. SharedHandle<Piece> piece = _pieceStorage->getPiece(slot.getIndex());
  191. piece->cancelBlock(slot.getBlockIndex());
  192. }
  193. }
  194. };
  195. class FindChokedRequestSlot {
  196. private:
  197. SharedHandle<Peer> _peer;
  198. public:
  199. FindChokedRequestSlot(const SharedHandle<Peer>& peer):
  200. _peer(peer) {}
  201. bool operator()(const RequestSlot& slot) const
  202. {
  203. return !_peer->isInPeerAllowedIndexSet(slot.getIndex());
  204. }
  205. };
  206. // localhost received choke message from the peer.
  207. void DefaultBtMessageDispatcher::doChokedAction()
  208. {
  209. std::for_each(requestSlots.begin(), requestSlots.end(),
  210. ProcessChokedRequestSlot(cuid, peer, _pieceStorage));
  211. requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(),
  212. FindChokedRequestSlot(peer)),
  213. requestSlots.end());
  214. }
  215. // localhost dispatched choke message to the peer.
  216. void DefaultBtMessageDispatcher::doChokingAction()
  217. {
  218. BtChokingEvent event;
  219. std::vector<SharedHandle<BtMessage> > tempQueue
  220. (messageQueue.begin(), messageQueue.end());
  221. forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
  222. &BtMessage::onChokingEvent, event);
  223. }
  224. class ProcessStaleRequestSlot {
  225. private:
  226. int32_t _cuid;
  227. SharedHandle<Peer> _peer;
  228. SharedHandle<PieceStorage> _pieceStorage;
  229. BtMessageDispatcher* _messageDispatcher;
  230. WeakHandle<BtMessageFactory> _messageFactory;
  231. time_t _requestTimeout;
  232. Logger* _logger;
  233. public:
  234. ProcessStaleRequestSlot(int32_t cuid, const SharedHandle<Peer>& peer,
  235. const SharedHandle<PieceStorage>& pieceStorage,
  236. BtMessageDispatcher* dispatcher,
  237. const WeakHandle<BtMessageFactory>& factory,
  238. time_t requestTimeout):
  239. _cuid(cuid),
  240. _peer(peer),
  241. _pieceStorage(pieceStorage),
  242. _messageDispatcher(dispatcher),
  243. _messageFactory(factory),
  244. _requestTimeout(requestTimeout),
  245. _logger(LogFactory::getInstance()) {}
  246. void operator()(const RequestSlot& slot)
  247. {
  248. if(slot.isTimeout(_requestTimeout)) {
  249. if(_logger->debug()) {
  250. _logger->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
  251. _cuid,
  252. slot.getBlockIndex());
  253. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  254. }
  255. slot.getPiece()->cancelBlock(slot.getBlockIndex());
  256. _peer->snubbing(true);
  257. } else if(slot.getPiece()->hasBlock(slot.getBlockIndex())) {
  258. if(_logger->debug()) {
  259. _logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
  260. _cuid,
  261. slot.getBlockIndex());
  262. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  263. }
  264. _messageDispatcher->addMessageToQueue
  265. (_messageFactory->createCancelMessage(slot.getIndex(),
  266. slot.getBegin(),
  267. slot.getLength()));
  268. }
  269. }
  270. };
  271. class FindStaleRequestSlot {
  272. private:
  273. SharedHandle<PieceStorage> _pieceStorage;
  274. time_t _requestTimeout;
  275. public:
  276. FindStaleRequestSlot(const SharedHandle<PieceStorage>& pieceStorage,
  277. time_t requestTimeout):
  278. _pieceStorage(pieceStorage),
  279. _requestTimeout(requestTimeout) {}
  280. bool operator()(const RequestSlot& slot)
  281. {
  282. if(slot.isTimeout(_requestTimeout)) {
  283. return true;
  284. } else {
  285. if(slot.getPiece()->hasBlock(slot.getBlockIndex())) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. }
  292. };
  293. void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
  294. {
  295. std::for_each(requestSlots.begin(), requestSlots.end(),
  296. ProcessStaleRequestSlot(cuid,
  297. peer,
  298. _pieceStorage,
  299. this,
  300. messageFactory,
  301. requestTimeout));
  302. requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(),
  303. FindStaleRequestSlot(_pieceStorage,
  304. requestTimeout)),
  305. requestSlots.end());
  306. }
  307. bool DefaultBtMessageDispatcher::isSendingInProgress()
  308. {
  309. if(messageQueue.size() > 0) {
  310. return messageQueue.front()->isSendingInProgress();
  311. } else {
  312. return false;
  313. }
  314. }
  315. class BlockIndexLess {
  316. public:
  317. bool operator()(const RequestSlot& lhs, const RequestSlot& rhs) const
  318. {
  319. if(lhs.getIndex() == rhs.getIndex()) {
  320. return lhs.getBlockIndex() < rhs.getBlockIndex();
  321. } else {
  322. return lhs.getIndex() < rhs.getIndex();
  323. }
  324. }
  325. };
  326. bool DefaultBtMessageDispatcher::isOutstandingRequest(size_t index, size_t blockIndex) {
  327. RequestSlot rs(index, 0, 0, blockIndex);
  328. std::deque<RequestSlot>::iterator i =
  329. std::lower_bound(requestSlots.begin(), requestSlots.end(), rs, BlockIndexLess());
  330. return i != requestSlots.end() &&
  331. (*i).getIndex() == index && (*i).getBlockIndex() == blockIndex;
  332. }
  333. RequestSlot
  334. DefaultBtMessageDispatcher::getOutstandingRequest(size_t index, uint32_t begin, size_t length)
  335. {
  336. RequestSlot ret;
  337. RequestSlot rs(index, begin, length, 0);
  338. std::deque<RequestSlot>::iterator i =
  339. std::lower_bound(requestSlots.begin(), requestSlots.end(), rs);
  340. if(i != requestSlots.end() && (*i) == rs) {
  341. ret = *i;
  342. } else {
  343. ret = RequestSlot::nullSlot;
  344. }
  345. return ret;
  346. }
  347. void DefaultBtMessageDispatcher::removeOutstandingRequest(const RequestSlot& slot)
  348. {
  349. std::deque<RequestSlot>::iterator i =
  350. std::lower_bound(requestSlots.begin(), requestSlots.end(), slot);
  351. if(i != requestSlots.end() && (*i) == slot) {
  352. AbortOutstandingRequest(slot.getPiece(), cuid)(*i);
  353. requestSlots.erase(i);
  354. }
  355. }
  356. void DefaultBtMessageDispatcher::addOutstandingRequest(const RequestSlot& slot)
  357. {
  358. std::deque<RequestSlot>::iterator i =
  359. std::lower_bound(requestSlots.begin(), requestSlots.end(), slot);
  360. if(i == requestSlots.end() || (*i) != slot) {
  361. requestSlots.insert(i, slot);
  362. }
  363. }
  364. size_t DefaultBtMessageDispatcher::countOutstandingUpload()
  365. {
  366. return std::count_if(messageQueue.begin(), messageQueue.end(),
  367. mem_fun_sh(&BtMessage::isUploading));
  368. }
  369. void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
  370. {
  371. this->peer = peer;
  372. }
  373. void DefaultBtMessageDispatcher::setDownloadContext
  374. (const SharedHandle<DownloadContext>& downloadContext)
  375. {
  376. _downloadContext = downloadContext;
  377. }
  378. void DefaultBtMessageDispatcher::setPieceStorage
  379. (const SharedHandle<PieceStorage>& pieceStorage)
  380. {
  381. _pieceStorage = pieceStorage;
  382. }
  383. void DefaultBtMessageDispatcher::setPeerStorage
  384. (const SharedHandle<PeerStorage>& peerStorage)
  385. {
  386. _peerStorage = peerStorage;
  387. }
  388. void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
  389. {
  390. this->messageFactory = factory;
  391. }
  392. void DefaultBtMessageDispatcher::setRequestGroupMan
  393. (const WeakHandle<RequestGroupMan>& rgman)
  394. {
  395. _requestGroupMan = rgman;
  396. }
  397. } // namespace aria2