DefaultBtMessageDispatcher.cc 14 KB

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