DefaultBtInteractive.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 "DefaultBtInteractive.h"
  36. #include <cstring>
  37. #include <vector>
  38. #include "prefs.h"
  39. #include "message.h"
  40. #include "BtHandshakeMessage.h"
  41. #include "util.h"
  42. #include "BtKeepAliveMessage.h"
  43. #include "BtChokeMessage.h"
  44. #include "BtUnchokeMessage.h"
  45. #include "BtRequestMessage.h"
  46. #include "BtPieceMessage.h"
  47. #include "DlAbortEx.h"
  48. #include "BtExtendedMessage.h"
  49. #include "HandshakeExtensionMessage.h"
  50. #include "UTPexExtensionMessage.h"
  51. #include "DefaultExtensionMessageFactory.h"
  52. #include "ExtensionMessageRegistry.h"
  53. #include "DHTNode.h"
  54. #include "Peer.h"
  55. #include "Piece.h"
  56. #include "DownloadContext.h"
  57. #include "PieceStorage.h"
  58. #include "PeerStorage.h"
  59. #include "BtRuntime.h"
  60. #include "BtMessageReceiver.h"
  61. #include "BtMessageDispatcher.h"
  62. #include "BtMessageFactory.h"
  63. #include "BtRequestFactory.h"
  64. #include "PeerConnection.h"
  65. #include "Logger.h"
  66. #include "LogFactory.h"
  67. #include "fmt.h"
  68. #include "RequestGroup.h"
  69. #include "RequestGroupMan.h"
  70. #include "bittorrent_helper.h"
  71. #include "UTMetadataRequestFactory.h"
  72. #include "UTMetadataRequestTracker.h"
  73. #include "wallclock.h"
  74. namespace aria2 {
  75. DefaultBtInteractive::DefaultBtInteractive
  76. (const SharedHandle<DownloadContext>& downloadContext,
  77. const SharedHandle<Peer>& peer)
  78. : downloadContext_(downloadContext),
  79. peer_(peer),
  80. metadataGetMode_(false),
  81. localNode_(0),
  82. allowedFastSetSize_(10),
  83. haveTimer_(global::wallclock()),
  84. keepAliveTimer_(global::wallclock()),
  85. floodingTimer_(global::wallclock()),
  86. inactiveTimer_(global::wallclock()),
  87. pexTimer_(global::wallclock()),
  88. perSecTimer_(global::wallclock()),
  89. keepAliveInterval_(120),
  90. utPexEnabled_(false),
  91. dhtEnabled_(false),
  92. numReceivedMessage_(0),
  93. maxOutstandingRequest_(DEFAULT_MAX_OUTSTANDING_REQUEST),
  94. requestGroupMan_(0)
  95. {}
  96. DefaultBtInteractive::~DefaultBtInteractive() {}
  97. void DefaultBtInteractive::initiateHandshake() {
  98. SharedHandle<BtMessage> message =
  99. messageFactory_->createHandshakeMessage
  100. (bittorrent::getInfoHash(downloadContext_), bittorrent::getStaticPeerId());
  101. dispatcher_->addMessageToQueue(message);
  102. dispatcher_->sendMessages();
  103. }
  104. BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) {
  105. SharedHandle<BtHandshakeMessage> message =
  106. btMessageReceiver_->receiveHandshake(quickReply);
  107. if(!message) {
  108. return SharedHandle<BtMessage>();
  109. }
  110. if(memcmp(message->getPeerId(), bittorrent::getStaticPeerId(),
  111. PEER_ID_LENGTH) == 0) {
  112. throw DL_ABORT_EX
  113. (fmt("CUID#%" PRId64 " - Drop connection from the same Peer ID",
  114. cuid_));
  115. }
  116. std::vector<SharedHandle<Peer> > activePeers;
  117. peerStorage_->getActivePeers(activePeers);
  118. for(std::vector<SharedHandle<Peer> >::const_iterator i = activePeers.begin(),
  119. eoi = activePeers.end(); i != eoi; ++i) {
  120. if(memcmp((*i)->getPeerId(), message->getPeerId(), PEER_ID_LENGTH) == 0) {
  121. throw DL_ABORT_EX
  122. (fmt("CUID#%" PRId64 " - Same Peer ID has been already seen.",
  123. cuid_));
  124. }
  125. }
  126. peer_->setPeerId(message->getPeerId());
  127. if(message->isFastExtensionSupported()) {
  128. peer_->setFastExtensionEnabled(true);
  129. A2_LOG_INFO(fmt(MSG_FAST_EXTENSION_ENABLED, cuid_));
  130. }
  131. if(message->isExtendedMessagingEnabled()) {
  132. peer_->setExtendedMessagingEnabled(true);
  133. if(!utPexEnabled_) {
  134. extensionMessageRegistry_->removeExtension("ut_pex");
  135. }
  136. A2_LOG_INFO(fmt(MSG_EXTENDED_MESSAGING_ENABLED, cuid_));
  137. }
  138. if(message->isDHTEnabled()) {
  139. peer_->setDHTEnabled(true);
  140. A2_LOG_INFO(fmt(MSG_DHT_ENABLED_PEER, cuid_));
  141. }
  142. A2_LOG_INFO(fmt(MSG_RECEIVE_PEER_MESSAGE, cuid_,
  143. peer_->getIPAddress().c_str(), peer_->getPort(),
  144. message->toString().c_str()));
  145. return message;
  146. }
  147. BtMessageHandle DefaultBtInteractive::receiveAndSendHandshake() {
  148. return receiveHandshake(true);
  149. }
  150. void DefaultBtInteractive::doPostHandshakeProcessing() {
  151. // Set time 0 to haveTimer to cache http/ftp download piece completion
  152. haveTimer_.reset(0);
  153. keepAliveTimer_ = global::wallclock();
  154. floodingTimer_ = global::wallclock();
  155. pexTimer_.reset(0);
  156. if(peer_->isExtendedMessagingEnabled()) {
  157. addHandshakeExtendedMessageToQueue();
  158. }
  159. if(!metadataGetMode_) {
  160. addBitfieldMessageToQueue();
  161. }
  162. if(peer_->isDHTEnabled() && dhtEnabled_) {
  163. addPortMessageToQueue();
  164. }
  165. if(!metadataGetMode_) {
  166. addAllowedFastMessageToQueue();
  167. }
  168. sendPendingMessage();
  169. }
  170. void DefaultBtInteractive::addPortMessageToQueue()
  171. {
  172. dispatcher_->addMessageToQueue
  173. (messageFactory_->createPortMessage(localNode_->getPort()));
  174. }
  175. void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
  176. {
  177. static const std::string CLIENT_ARIA2("aria2/"PACKAGE_VERSION);
  178. HandshakeExtensionMessageHandle m(new HandshakeExtensionMessage());
  179. m->setClientVersion(CLIENT_ARIA2);
  180. m->setTCPPort(tcpPort_);
  181. m->setExtensions(extensionMessageRegistry_->getExtensions());
  182. SharedHandle<TorrentAttribute> attrs =
  183. bittorrent::getTorrentAttrs(downloadContext_);
  184. if(!attrs->metadata.empty()) {
  185. m->setMetadataSize(attrs->metadataSize);
  186. }
  187. SharedHandle<BtMessage> msg = messageFactory_->createBtExtendedMessage(m);
  188. dispatcher_->addMessageToQueue(msg);
  189. }
  190. void DefaultBtInteractive::addBitfieldMessageToQueue() {
  191. if(peer_->isFastExtensionEnabled()) {
  192. if(pieceStorage_->allDownloadFinished()) {
  193. dispatcher_->addMessageToQueue(messageFactory_->createHaveAllMessage());
  194. } else if(pieceStorage_->getCompletedLength() > 0) {
  195. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  196. } else {
  197. dispatcher_->addMessageToQueue(messageFactory_->createHaveNoneMessage());
  198. }
  199. } else {
  200. if(pieceStorage_->getCompletedLength() > 0) {
  201. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  202. }
  203. }
  204. }
  205. void DefaultBtInteractive::addAllowedFastMessageToQueue() {
  206. if(peer_->isFastExtensionEnabled()) {
  207. std::vector<size_t> fastSet;
  208. bittorrent::computeFastSet(fastSet, peer_->getIPAddress(),
  209. downloadContext_->getNumPieces(),
  210. bittorrent::getInfoHash(downloadContext_),
  211. allowedFastSetSize_);
  212. for(std::vector<size_t>::const_iterator itr = fastSet.begin(),
  213. eoi = fastSet.end(); itr != eoi; ++itr) {
  214. dispatcher_->addMessageToQueue
  215. (messageFactory_->createAllowedFastMessage(*itr));
  216. }
  217. }
  218. }
  219. void DefaultBtInteractive::decideChoking() {
  220. if(peer_->shouldBeChoking()) {
  221. if(!peer_->amChoking()) {
  222. dispatcher_->addMessageToQueue(messageFactory_->createChokeMessage());
  223. }
  224. } else {
  225. if(peer_->amChoking()) {
  226. dispatcher_->addMessageToQueue(messageFactory_->createUnchokeMessage());
  227. }
  228. }
  229. }
  230. void DefaultBtInteractive::checkHave() {
  231. std::vector<size_t> indexes;
  232. pieceStorage_->getAdvertisedPieceIndexes(indexes, cuid_, haveTimer_);
  233. haveTimer_ = global::wallclock();
  234. if(indexes.size() >= 20) {
  235. if(peer_->isFastExtensionEnabled() &&
  236. pieceStorage_->allDownloadFinished()) {
  237. dispatcher_->addMessageToQueue(messageFactory_->createHaveAllMessage());
  238. } else {
  239. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  240. }
  241. } else {
  242. for(std::vector<size_t>::const_iterator itr = indexes.begin(),
  243. eoi = indexes.end(); itr != eoi; ++itr) {
  244. dispatcher_->addMessageToQueue(messageFactory_->createHaveMessage(*itr));
  245. }
  246. }
  247. }
  248. void DefaultBtInteractive::sendKeepAlive() {
  249. if(keepAliveTimer_.difference(global::wallclock()) >= keepAliveInterval_) {
  250. dispatcher_->addMessageToQueue(messageFactory_->createKeepAliveMessage());
  251. dispatcher_->sendMessages();
  252. keepAliveTimer_ = global::wallclock();
  253. }
  254. }
  255. size_t DefaultBtInteractive::receiveMessages() {
  256. size_t countOldOutstandingRequest = dispatcher_->countOutstandingRequest();
  257. size_t msgcount = 0;
  258. while(1) {
  259. if(requestGroupMan_->doesOverallDownloadSpeedExceed() ||
  260. downloadContext_->getOwnerRequestGroup()->doesDownloadSpeedExceed()) {
  261. break;
  262. }
  263. BtMessageHandle message = btMessageReceiver_->receiveMessage();
  264. if(!message) {
  265. break;
  266. }
  267. ++msgcount;
  268. A2_LOG_INFO(fmt(MSG_RECEIVE_PEER_MESSAGE,
  269. cuid_,
  270. peer_->getIPAddress().c_str(), peer_->getPort(),
  271. message->toString().c_str()));
  272. message->doReceivedAction();
  273. switch(message->getId()) {
  274. case BtKeepAliveMessage::ID:
  275. floodingStat_.incKeepAliveCount();
  276. break;
  277. case BtChokeMessage::ID:
  278. if(!peer_->peerChoking()) {
  279. floodingStat_.incChokeUnchokeCount();
  280. }
  281. break;
  282. case BtUnchokeMessage::ID:
  283. if(peer_->peerChoking()) {
  284. floodingStat_.incChokeUnchokeCount();
  285. }
  286. break;
  287. case BtPieceMessage::ID:
  288. peerStorage_->updateTransferStatFor(peer_);
  289. // pass through
  290. case BtRequestMessage::ID:
  291. inactiveTimer_ = global::wallclock();
  292. break;
  293. }
  294. }
  295. if(!pieceStorage_->isEndGame() &&
  296. countOldOutstandingRequest >= maxOutstandingRequest_ &&
  297. dispatcher_->countOutstandingRequest()*2 <= maxOutstandingRequest_){
  298. maxOutstandingRequest_ =
  299. std::min((size_t)UB_MAX_OUTSTANDING_REQUEST,
  300. maxOutstandingRequest_+OUTSTANDING_REQUEST_STEP);
  301. }
  302. return msgcount;
  303. }
  304. void DefaultBtInteractive::decideInterest() {
  305. if(pieceStorage_->hasMissingPiece(peer_)) {
  306. if(!peer_->amInterested()) {
  307. A2_LOG_DEBUG(fmt(MSG_PEER_INTERESTED, cuid_));
  308. dispatcher_->
  309. addMessageToQueue(messageFactory_->createInterestedMessage());
  310. }
  311. } else {
  312. if(peer_->amInterested()) {
  313. A2_LOG_DEBUG(fmt(MSG_PEER_NOT_INTERESTED, cuid_));
  314. dispatcher_->
  315. addMessageToQueue(messageFactory_->createNotInterestedMessage());
  316. }
  317. }
  318. }
  319. void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) {
  320. if(pieceStorage_->hasMissingPiece(peer_)) {
  321. size_t numMissingBlock = btRequestFactory_->countMissingBlock();
  322. if(numMissingBlock >= maxMissingBlock) {
  323. return;
  324. }
  325. size_t diffMissingBlock = maxMissingBlock-numMissingBlock;
  326. std::vector<SharedHandle<Piece> > pieces;
  327. if(peer_->peerChoking()) {
  328. if(peer_->isFastExtensionEnabled()) {
  329. if(pieceStorage_->isEndGame()) {
  330. std::vector<size_t> excludedIndexes;
  331. excludedIndexes.reserve(btRequestFactory_->countTargetPiece());
  332. btRequestFactory_->getTargetPieceIndexes(excludedIndexes);
  333. pieceStorage_->getMissingFastPiece
  334. (pieces, diffMissingBlock, peer_, excludedIndexes, cuid_);
  335. } else {
  336. pieces.reserve(diffMissingBlock);
  337. pieceStorage_->getMissingFastPiece
  338. (pieces, diffMissingBlock, peer_, cuid_);
  339. }
  340. }
  341. } else {
  342. if(pieceStorage_->isEndGame()) {
  343. std::vector<size_t> excludedIndexes;
  344. excludedIndexes.reserve(btRequestFactory_->countTargetPiece());
  345. btRequestFactory_->getTargetPieceIndexes(excludedIndexes);
  346. pieceStorage_->getMissingPiece
  347. (pieces, diffMissingBlock, peer_, excludedIndexes, cuid_);
  348. } else {
  349. pieces.reserve(diffMissingBlock);
  350. pieceStorage_->getMissingPiece(pieces, diffMissingBlock, peer_, cuid_);
  351. }
  352. }
  353. for(std::vector<SharedHandle<Piece> >::const_iterator i =
  354. pieces.begin(), eoi = pieces.end(); i != eoi; ++i) {
  355. btRequestFactory_->addTargetPiece(*i);
  356. }
  357. }
  358. }
  359. void DefaultBtInteractive::addRequests() {
  360. if(!pieceStorage_->isEndGame() && !pieceStorage_->hasMissingUnusedPiece()) {
  361. pieceStorage_->enterEndGame();
  362. }
  363. if(pieceStorage_->isEndGame()) {
  364. maxOutstandingRequest_ = 2;
  365. }
  366. fillPiece(maxOutstandingRequest_);
  367. size_t reqNumToCreate =
  368. maxOutstandingRequest_ <= dispatcher_->countOutstandingRequest() ?
  369. 0 : maxOutstandingRequest_-dispatcher_->countOutstandingRequest();
  370. if(reqNumToCreate > 0) {
  371. std::vector<SharedHandle<BtMessage> > requests;
  372. requests.reserve(reqNumToCreate);
  373. if(pieceStorage_->isEndGame()) {
  374. btRequestFactory_->createRequestMessagesOnEndGame(requests,reqNumToCreate);
  375. } else {
  376. btRequestFactory_->createRequestMessages(requests, reqNumToCreate);
  377. }
  378. dispatcher_->addMessageToQueue(requests);
  379. }
  380. }
  381. void DefaultBtInteractive::cancelAllPiece() {
  382. btRequestFactory_->removeAllTargetPiece();
  383. if(metadataGetMode_ && downloadContext_->getTotalLength() > 0) {
  384. std::vector<size_t> metadataRequests =
  385. utMetadataRequestTracker_->getAllTrackedIndex();
  386. for(std::vector<size_t>::const_iterator i = metadataRequests.begin(),
  387. eoi = metadataRequests.end(); i != eoi; ++i) {
  388. A2_LOG_DEBUG(fmt("Cancel metadata: piece=%lu",
  389. static_cast<unsigned long>(*i)));
  390. pieceStorage_->cancelPiece(pieceStorage_->getPiece(*i), cuid_);
  391. }
  392. }
  393. }
  394. void DefaultBtInteractive::sendPendingMessage() {
  395. dispatcher_->sendMessages();
  396. }
  397. void DefaultBtInteractive::detectMessageFlooding() {
  398. if(floodingTimer_.
  399. difference(global::wallclock()) >= FLOODING_CHECK_INTERVAL) {
  400. if(floodingStat_.getChokeUnchokeCount() >= 2 ||
  401. floodingStat_.getKeepAliveCount() >= 2) {
  402. throw DL_ABORT_EX(EX_FLOODING_DETECTED);
  403. } else {
  404. floodingStat_.reset();
  405. }
  406. floodingTimer_ = global::wallclock();
  407. }
  408. }
  409. void DefaultBtInteractive::checkActiveInteraction()
  410. {
  411. time_t inactiveTime = inactiveTimer_.difference(global::wallclock());
  412. // To allow aria2 to accept mutially interested peer, disconnect unintersted
  413. // peer.
  414. {
  415. const time_t interval = 30;
  416. if(!peer_->amInterested() && !peer_->peerInterested() &&
  417. inactiveTime >= interval) {
  418. peer_->setDisconnectedGracefully(true);
  419. // TODO change the message
  420. throw DL_ABORT_EX
  421. (fmt("Disconnect peer because we are not interested each other"
  422. " after %ld second(s).",
  423. static_cast<long int>(interval)));
  424. }
  425. }
  426. // Since the peers which are *just* connected and do nothing to improve
  427. // mutual download progress are completely waste of resources, those peers
  428. // are disconnected in a certain time period.
  429. {
  430. const time_t interval = 60;
  431. if(inactiveTime >= interval) {
  432. peer_->setDisconnectedGracefully(true);
  433. throw DL_ABORT_EX
  434. (fmt(EX_DROP_INACTIVE_CONNECTION,
  435. static_cast<long int>(interval)));
  436. }
  437. }
  438. // If both of us are seeders, drop connection.
  439. if(peer_->isSeeder() && pieceStorage_->downloadFinished()) {
  440. throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
  441. }
  442. }
  443. void DefaultBtInteractive::addPeerExchangeMessage()
  444. {
  445. if(pexTimer_.
  446. difference(global::wallclock()) >= UTPexExtensionMessage::DEFAULT_INTERVAL) {
  447. UTPexExtensionMessageHandle m
  448. (new UTPexExtensionMessage(peer_->getExtensionMessageID("ut_pex")));
  449. std::vector<SharedHandle<Peer> > activePeers;
  450. peerStorage_->getActivePeers(activePeers);
  451. for(std::vector<SharedHandle<Peer> >::const_iterator i =
  452. activePeers.begin(), eoi = activePeers.end();
  453. i != eoi && !m->freshPeersAreFull(); ++i) {
  454. if(peer_->getIPAddress() != (*i)->getIPAddress()) {
  455. m->addFreshPeer(*i);
  456. }
  457. }
  458. const std::deque<SharedHandle<Peer> >& droppedPeers =
  459. peerStorage_->getDroppedPeers();
  460. for(std::deque<SharedHandle<Peer> >::const_iterator i =
  461. droppedPeers.begin(), eoi = droppedPeers.end();
  462. i != eoi && !m->droppedPeersAreFull();
  463. ++i) {
  464. if(peer_->getIPAddress() != (*i)->getIPAddress()) {
  465. m->addDroppedPeer(*i);
  466. }
  467. }
  468. BtMessageHandle msg = messageFactory_->createBtExtendedMessage(m);
  469. dispatcher_->addMessageToQueue(msg);
  470. pexTimer_ = global::wallclock();
  471. }
  472. }
  473. void DefaultBtInteractive::doInteractionProcessing() {
  474. if(metadataGetMode_) {
  475. sendKeepAlive();
  476. numReceivedMessage_ = receiveMessages();
  477. // PieceStorage is re-initialized with metadata_size in
  478. // HandshakeExtensionMessage::doReceivedAction().
  479. pieceStorage_ =
  480. downloadContext_->getOwnerRequestGroup()->getPieceStorage();
  481. if(peer_->getExtensionMessageID("ut_metadata") &&
  482. downloadContext_->getTotalLength() > 0) {
  483. size_t num = utMetadataRequestTracker_->avail();
  484. if(num > 0) {
  485. std::vector<SharedHandle<BtMessage> > requests;
  486. utMetadataRequestFactory_->create(requests, num, pieceStorage_);
  487. dispatcher_->addMessageToQueue(requests);
  488. }
  489. if(perSecTimer_.difference(global::wallclock()) >= 1) {
  490. perSecTimer_ = global::wallclock();
  491. // Drop timeout request after queuing message to give a chance
  492. // to other connection to request piece.
  493. std::vector<size_t> indexes =
  494. utMetadataRequestTracker_->removeTimeoutEntry();
  495. for(std::vector<size_t>::const_iterator i = indexes.begin(),
  496. eoi = indexes.end(); i != eoi; ++i) {
  497. pieceStorage_->cancelPiece(pieceStorage_->getPiece(*i), cuid_);
  498. }
  499. }
  500. if(pieceStorage_->downloadFinished()) {
  501. downloadContext_->getOwnerRequestGroup()->setForceHaltRequested
  502. (true, RequestGroup::NONE);
  503. }
  504. }
  505. } else {
  506. checkActiveInteraction();
  507. decideChoking();
  508. detectMessageFlooding();
  509. if(perSecTimer_.difference(global::wallclock()) >= 1) {
  510. perSecTimer_ = global::wallclock();
  511. dispatcher_->checkRequestSlotAndDoNecessaryThing();
  512. }
  513. checkHave();
  514. sendKeepAlive();
  515. numReceivedMessage_ = receiveMessages();
  516. btRequestFactory_->removeCompletedPiece();
  517. decideInterest();
  518. if(!pieceStorage_->downloadFinished()) {
  519. addRequests();
  520. }
  521. }
  522. if(peer_->getExtensionMessageID("ut_pex") && utPexEnabled_) {
  523. addPeerExchangeMessage();
  524. }
  525. sendPendingMessage();
  526. }
  527. void DefaultBtInteractive::setLocalNode(DHTNode* node)
  528. {
  529. localNode_ = node;
  530. }
  531. size_t DefaultBtInteractive::countPendingMessage()
  532. {
  533. return dispatcher_->countMessageInQueue();
  534. }
  535. bool DefaultBtInteractive::isSendingMessageInProgress()
  536. {
  537. return dispatcher_->isSendingInProgress();
  538. }
  539. size_t DefaultBtInteractive::countReceivedMessageInIteration() const
  540. {
  541. return numReceivedMessage_;
  542. }
  543. size_t DefaultBtInteractive::countOutstandingRequest()
  544. {
  545. if(metadataGetMode_) {
  546. return utMetadataRequestTracker_->count();
  547. } else {
  548. return dispatcher_->countOutstandingRequest();
  549. }
  550. }
  551. void DefaultBtInteractive::setBtRuntime
  552. (const SharedHandle<BtRuntime>& btRuntime)
  553. {
  554. btRuntime_ = btRuntime;
  555. }
  556. void DefaultBtInteractive::setPieceStorage
  557. (const SharedHandle<PieceStorage>& pieceStorage)
  558. {
  559. pieceStorage_ = pieceStorage;
  560. }
  561. void DefaultBtInteractive::setPeerStorage
  562. (const SharedHandle<PeerStorage>& peerStorage)
  563. {
  564. peerStorage_ = peerStorage;
  565. }
  566. void DefaultBtInteractive::setPeer(const SharedHandle<Peer>& peer)
  567. {
  568. peer_ = peer;
  569. }
  570. void DefaultBtInteractive::setBtMessageReceiver
  571. (const SharedHandle<BtMessageReceiver>& receiver)
  572. {
  573. btMessageReceiver_ = receiver;
  574. }
  575. void DefaultBtInteractive::setDispatcher
  576. (const SharedHandle<BtMessageDispatcher>& dispatcher)
  577. {
  578. dispatcher_ = dispatcher;
  579. }
  580. void DefaultBtInteractive::setBtRequestFactory
  581. (const SharedHandle<BtRequestFactory>& factory)
  582. {
  583. btRequestFactory_ = factory;
  584. }
  585. void DefaultBtInteractive::setPeerConnection
  586. (const SharedHandle<PeerConnection>& peerConnection)
  587. {
  588. peerConnection_ = peerConnection;
  589. }
  590. void DefaultBtInteractive::setExtensionMessageFactory
  591. (const SharedHandle<ExtensionMessageFactory>& factory)
  592. {
  593. extensionMessageFactory_ = factory;
  594. }
  595. void DefaultBtInteractive::setBtMessageFactory
  596. (const SharedHandle<BtMessageFactory>& factory)
  597. {
  598. messageFactory_ = factory;
  599. }
  600. void DefaultBtInteractive::setRequestGroupMan(RequestGroupMan* rgman)
  601. {
  602. requestGroupMan_ = rgman;
  603. }
  604. } // namespace aria2