DefaultBtMessageDispatcher.cc 14 KB

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