DefaultBtMessageDispatcher.cc 14 KB

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