DefaultBtMessageDispatcher.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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(); itr != btMessages.end(); ++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. const struct timeval& _now;
  232. time_t _requestTimeout;
  233. Logger* _logger;
  234. public:
  235. ProcessStaleRequestSlot(int32_t cuid, const SharedHandle<Peer>& peer,
  236. const SharedHandle<PieceStorage>& pieceStorage,
  237. BtMessageDispatcher* dispatcher,
  238. const WeakHandle<BtMessageFactory>& factory,
  239. const struct timeval& now,
  240. time_t requestTimeout):
  241. _cuid(cuid),
  242. _peer(peer),
  243. _pieceStorage(pieceStorage),
  244. _messageDispatcher(dispatcher),
  245. _messageFactory(factory),
  246. _now(now),
  247. _requestTimeout(requestTimeout),
  248. _logger(LogFactory::getInstance()) {}
  249. void operator()(const RequestSlot& slot)
  250. {
  251. if(slot.isTimeout(_now, _requestTimeout)) {
  252. if(_logger->debug()) {
  253. _logger->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
  254. _cuid,
  255. slot.getBlockIndex());
  256. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  257. }
  258. slot.getPiece()->cancelBlock(slot.getBlockIndex());
  259. _peer->snubbing(true);
  260. } else if(slot.getPiece()->hasBlock(slot.getBlockIndex())) {
  261. if(_logger->debug()) {
  262. _logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
  263. _cuid,
  264. slot.getBlockIndex());
  265. _logger->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
  266. }
  267. _messageDispatcher->addMessageToQueue
  268. (_messageFactory->createCancelMessage(slot.getIndex(),
  269. slot.getBegin(),
  270. slot.getLength()));
  271. }
  272. }
  273. };
  274. class FindStaleRequestSlot {
  275. private:
  276. SharedHandle<PieceStorage> _pieceStorage;
  277. const struct timeval& _now;
  278. time_t _requestTimeout;
  279. public:
  280. FindStaleRequestSlot(const SharedHandle<PieceStorage>& pieceStorage,
  281. const struct timeval& now,
  282. time_t requestTimeout):
  283. _pieceStorage(pieceStorage),
  284. _now(now),
  285. _requestTimeout(requestTimeout) {}
  286. bool operator()(const RequestSlot& slot)
  287. {
  288. if(slot.isTimeout(_now, _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. void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
  300. {
  301. struct timeval now;
  302. gettimeofday(&now, 0);
  303. std::for_each(requestSlots.begin(), requestSlots.end(),
  304. ProcessStaleRequestSlot(cuid,
  305. peer,
  306. _pieceStorage,
  307. this,
  308. messageFactory,
  309. now,
  310. requestTimeout));
  311. requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(),
  312. FindStaleRequestSlot(_pieceStorage,
  313. now,
  314. requestTimeout)),
  315. requestSlots.end());
  316. }
  317. bool DefaultBtMessageDispatcher::isSendingInProgress()
  318. {
  319. if(messageQueue.size() > 0) {
  320. return messageQueue.front()->isSendingInProgress();
  321. } else {
  322. return false;
  323. }
  324. }
  325. class BlockIndexLess {
  326. public:
  327. bool operator()(const RequestSlot& lhs, const RequestSlot& rhs) const
  328. {
  329. if(lhs.getIndex() == rhs.getIndex()) {
  330. return lhs.getBlockIndex() < rhs.getBlockIndex();
  331. } else {
  332. return lhs.getIndex() < rhs.getIndex();
  333. }
  334. }
  335. };
  336. bool DefaultBtMessageDispatcher::isOutstandingRequest(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(), rs, BlockIndexLess());
  340. return i != requestSlots.end() &&
  341. (*i).getIndex() == index && (*i).getBlockIndex() == blockIndex;
  342. }
  343. RequestSlot
  344. DefaultBtMessageDispatcher::getOutstandingRequest(size_t index, uint32_t begin, size_t length)
  345. {
  346. RequestSlot ret;
  347. RequestSlot rs(index, begin, length, 0);
  348. std::deque<RequestSlot>::iterator i =
  349. std::lower_bound(requestSlots.begin(), requestSlots.end(), rs);
  350. if(i != requestSlots.end() && (*i) == rs) {
  351. ret = *i;
  352. } else {
  353. ret = RequestSlot::nullSlot;
  354. }
  355. return ret;
  356. }
  357. void DefaultBtMessageDispatcher::removeOutstandingRequest(const RequestSlot& slot)
  358. {
  359. std::deque<RequestSlot>::iterator i =
  360. std::lower_bound(requestSlots.begin(), requestSlots.end(), slot);
  361. if(i != requestSlots.end() && (*i) == slot) {
  362. AbortOutstandingRequest(slot.getPiece(), cuid)(*i);
  363. requestSlots.erase(i);
  364. }
  365. }
  366. void DefaultBtMessageDispatcher::addOutstandingRequest(const RequestSlot& slot)
  367. {
  368. std::deque<RequestSlot>::iterator i =
  369. std::lower_bound(requestSlots.begin(), requestSlots.end(), slot);
  370. if(i == requestSlots.end() || (*i) != slot) {
  371. requestSlots.insert(i, slot);
  372. }
  373. }
  374. size_t DefaultBtMessageDispatcher::countOutstandingUpload()
  375. {
  376. return std::count_if(messageQueue.begin(), messageQueue.end(),
  377. mem_fun_sh(&BtMessage::isUploading));
  378. }
  379. void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
  380. {
  381. this->peer = peer;
  382. }
  383. void DefaultBtMessageDispatcher::setDownloadContext
  384. (const SharedHandle<DownloadContext>& downloadContext)
  385. {
  386. _downloadContext = downloadContext;
  387. }
  388. void DefaultBtMessageDispatcher::setPieceStorage
  389. (const SharedHandle<PieceStorage>& pieceStorage)
  390. {
  391. _pieceStorage = pieceStorage;
  392. }
  393. void DefaultBtMessageDispatcher::setPeerStorage
  394. (const SharedHandle<PeerStorage>& peerStorage)
  395. {
  396. _peerStorage = peerStorage;
  397. }
  398. void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
  399. {
  400. this->messageFactory = factory;
  401. }
  402. void DefaultBtMessageDispatcher::setRequestGroupMan
  403. (const WeakHandle<RequestGroupMan>& rgman)
  404. {
  405. _requestGroupMan = rgman;
  406. }
  407. } // namespace aria2