DefaultBtMessageDispatcher.cc 14 KB

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