PeerInteractionCommand.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "PeerInteractionCommand.h"
  36. #include <algorithm>
  37. #include "DownloadEngine.h"
  38. #include "PeerInitiateConnectionCommand.h"
  39. #include "DefaultBtInteractive.h"
  40. #include "DlAbortEx.h"
  41. #include "message.h"
  42. #include "prefs.h"
  43. #include "Socket.h"
  44. #include "Option.h"
  45. #include "DownloadContext.h"
  46. #include "Peer.h"
  47. #include "BtMessage.h"
  48. #include "BtRuntime.h"
  49. #include "PeerStorage.h"
  50. #include "DefaultBtMessageDispatcher.h"
  51. #include "DefaultBtMessageReceiver.h"
  52. #include "DefaultBtRequestFactory.h"
  53. #include "DefaultBtMessageFactory.h"
  54. #include "DefaultBtInteractive.h"
  55. #include "PeerConnection.h"
  56. #include "ExtensionMessageFactory.h"
  57. #include "DHTRoutingTable.h"
  58. #include "DHTTaskQueue.h"
  59. #include "DHTTaskFactory.h"
  60. #include "DHTNode.h"
  61. #include "DHTSetup.h"
  62. #include "DHTRegistry.h"
  63. #include "PieceStorage.h"
  64. #include "RequestGroup.h"
  65. #include "BtAnnounce.h"
  66. #include "BtProgressInfoFile.h"
  67. #include "DefaultExtensionMessageFactory.h"
  68. #include "RequestGroupMan.h"
  69. #include "ExtensionMessageRegistry.h"
  70. #include "bittorrent_helper.h"
  71. #include "UTMetadataRequestFactory.h"
  72. #include "UTMetadataRequestTracker.h"
  73. namespace aria2 {
  74. PeerInteractionCommand::PeerInteractionCommand
  75. (int32_t cuid,
  76. RequestGroup* requestGroup,
  77. const SharedHandle<Peer>& p,
  78. DownloadEngine* e,
  79. const SharedHandle<BtRuntime>& btRuntime,
  80. const SharedHandle<PieceStorage>& pieceStorage,
  81. const SharedHandle<PeerStorage>& peerStorage,
  82. const SocketHandle& s,
  83. Seq sequence,
  84. const PeerConnectionHandle& passedPeerConnection)
  85. :PeerAbstractCommand(cuid, p, e, s),
  86. _requestGroup(requestGroup),
  87. _btRuntime(btRuntime),
  88. _pieceStorage(pieceStorage),
  89. _peerStorage(peerStorage),
  90. sequence(sequence)
  91. {
  92. // TODO move following bunch of processing to separate method, like init()
  93. if(sequence == INITIATOR_SEND_HANDSHAKE) {
  94. disableReadCheckSocket();
  95. setWriteCheckSocket(socket);
  96. setTimeout(getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  97. }
  98. const BDE& torrentAttrs =
  99. _requestGroup->getDownloadContext()->getAttribute(bittorrent::BITTORRENT);
  100. bool metadataGetMode = !torrentAttrs.containsKey(bittorrent::METADATA);
  101. SharedHandle<ExtensionMessageRegistry> exMsgRegistry
  102. (new ExtensionMessageRegistry());
  103. SharedHandle<UTMetadataRequestFactory> utMetadataRequestFactory;
  104. SharedHandle<UTMetadataRequestTracker> utMetadataRequestTracker;
  105. if(metadataGetMode) {
  106. utMetadataRequestFactory.reset(new UTMetadataRequestFactory());
  107. utMetadataRequestTracker.reset(new UTMetadataRequestTracker());
  108. }
  109. SharedHandle<DefaultExtensionMessageFactory> extensionMessageFactory
  110. (new DefaultExtensionMessageFactory(peer, exMsgRegistry));
  111. extensionMessageFactory->setPeerStorage(peerStorage);
  112. extensionMessageFactory->setDownloadContext
  113. (_requestGroup->getDownloadContext());
  114. extensionMessageFactory->setUTMetadataRequestTracker
  115. (utMetadataRequestTracker);
  116. // PieceStorage will be set later.
  117. SharedHandle<DefaultBtMessageFactory> factory(new DefaultBtMessageFactory());
  118. factory->setCuid(cuid);
  119. factory->setDownloadContext(_requestGroup->getDownloadContext());
  120. factory->setPieceStorage(pieceStorage);
  121. factory->setPeerStorage(peerStorage);
  122. factory->setExtensionMessageFactory(extensionMessageFactory);
  123. factory->setPeer(peer);
  124. factory->setLocalNode(DHTRegistry::_localNode);
  125. factory->setRoutingTable(DHTRegistry::_routingTable);
  126. factory->setTaskQueue(DHTRegistry::_taskQueue);
  127. factory->setTaskFactory(DHTRegistry::_taskFactory);
  128. if(metadataGetMode) {
  129. factory->enableMetadataGetMode();
  130. }
  131. PeerConnectionHandle peerConnection;
  132. if(passedPeerConnection.isNull()) {
  133. peerConnection.reset(new PeerConnection(cuid, socket));
  134. } else {
  135. peerConnection = passedPeerConnection;
  136. }
  137. SharedHandle<DefaultBtMessageDispatcher> dispatcher
  138. (new DefaultBtMessageDispatcher());
  139. dispatcher->setCuid(cuid);
  140. dispatcher->setPeer(peer);
  141. dispatcher->setDownloadContext(_requestGroup->getDownloadContext());
  142. dispatcher->setPieceStorage(pieceStorage);
  143. dispatcher->setPeerStorage(peerStorage);
  144. dispatcher->setRequestTimeout(getOption()->getAsInt(PREF_BT_REQUEST_TIMEOUT));
  145. dispatcher->setBtMessageFactory(factory);
  146. dispatcher->setRequestGroupMan(e->_requestGroupMan);
  147. DefaultBtMessageReceiverHandle receiver(new DefaultBtMessageReceiver());
  148. receiver->setCuid(cuid);
  149. receiver->setPeer(peer);
  150. receiver->setDownloadContext(_requestGroup->getDownloadContext());
  151. receiver->setPeerConnection(peerConnection);
  152. receiver->setDispatcher(dispatcher);
  153. receiver->setBtMessageFactory(factory);
  154. SharedHandle<DefaultBtRequestFactory> reqFactory
  155. (new DefaultBtRequestFactory());
  156. reqFactory->setCuid(cuid);
  157. reqFactory->setPeer(peer);
  158. reqFactory->setPieceStorage(pieceStorage);
  159. reqFactory->setBtMessageDispatcher(dispatcher);
  160. reqFactory->setBtMessageFactory(factory);
  161. DefaultBtInteractiveHandle btInteractive
  162. (new DefaultBtInteractive(_requestGroup->getDownloadContext(), peer));
  163. btInteractive->setBtRuntime(_btRuntime);
  164. btInteractive->setPieceStorage(_pieceStorage);
  165. btInteractive->setPeerStorage(peerStorage); // Note: Not a member variable.
  166. btInteractive->setCuid(cuid);
  167. btInteractive->setBtMessageReceiver(receiver);
  168. btInteractive->setDispatcher(dispatcher);
  169. btInteractive->setBtRequestFactory(reqFactory);
  170. btInteractive->setPeerConnection(peerConnection);
  171. btInteractive->setExtensionMessageFactory(extensionMessageFactory);
  172. btInteractive->setExtensionMessageRegistry(exMsgRegistry);
  173. btInteractive->setKeepAliveInterval
  174. (getOption()->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
  175. btInteractive->setRequestGroupMan(e->_requestGroupMan);
  176. btInteractive->setBtMessageFactory(factory);
  177. if((metadataGetMode || torrentAttrs[bittorrent::PRIVATE].i() == 0) &&
  178. !peer->isLocalPeer()) {
  179. if(getOption()->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) {
  180. btInteractive->setUTPexEnabled(true);
  181. }
  182. if(DHTSetup::initialized()) {
  183. btInteractive->setDHTEnabled(true);
  184. btInteractive->setLocalNode(DHTRegistry::_localNode);
  185. factory->setDHTEnabled(true);
  186. }
  187. }
  188. btInteractive->setUTMetadataRequestFactory(utMetadataRequestFactory);
  189. btInteractive->setUTMetadataRequestTracker(utMetadataRequestTracker);
  190. if(metadataGetMode) {
  191. btInteractive->enableMetadataGetMode();
  192. }
  193. this->btInteractive = btInteractive;
  194. // reverse depends
  195. factory->setBtMessageDispatcher(dispatcher);
  196. factory->setBtRequestFactory(reqFactory);
  197. factory->setPeerConnection(peerConnection);
  198. extensionMessageFactory->setBtMessageDispatcher(dispatcher);
  199. extensionMessageFactory->setBtMessageFactory(factory);
  200. if(metadataGetMode) {
  201. utMetadataRequestFactory->setDownloadContext
  202. (_requestGroup->getDownloadContext());
  203. utMetadataRequestFactory->setBtMessageDispatcher(dispatcher);
  204. utMetadataRequestFactory->setBtMessageFactory(factory);
  205. utMetadataRequestFactory->setPeer(peer);
  206. utMetadataRequestFactory->setUTMetadataRequestTracker
  207. (utMetadataRequestTracker);
  208. }
  209. peer->allocateSessionResource
  210. (_requestGroup->getDownloadContext()->getPieceLength(),
  211. _requestGroup->getDownloadContext()->getTotalLength());
  212. peer->setBtMessageDispatcher(dispatcher);
  213. _btRuntime->increaseConnections();
  214. _requestGroup->increaseNumCommand();
  215. }
  216. PeerInteractionCommand::~PeerInteractionCommand() {
  217. if(peer->getCompletedLength() > 0) {
  218. _pieceStorage->subtractPieceStats(peer->getBitfield(),
  219. peer->getBitfieldLength());
  220. }
  221. peer->releaseSessionResource();
  222. _requestGroup->decreaseNumCommand();
  223. _btRuntime->decreaseConnections();
  224. //logger->debug("CUID#%d - unregistered message factory using ID:%s",
  225. //cuid, peer->getId().c_str());
  226. }
  227. bool PeerInteractionCommand::executeInternal() {
  228. setNoCheck(false);
  229. switch(sequence) {
  230. case INITIATOR_SEND_HANDSHAKE:
  231. if(!socket->isWritable(0)) {
  232. break;
  233. }
  234. disableWriteCheckSocket();
  235. setReadCheckSocket(socket);
  236. //socket->setBlockingMode();
  237. setTimeout(getOption()->getAsInt(PREF_BT_TIMEOUT));
  238. btInteractive->initiateHandshake();
  239. sequence = INITIATOR_WAIT_HANDSHAKE;
  240. break;
  241. case INITIATOR_WAIT_HANDSHAKE: {
  242. if(btInteractive->countPendingMessage() > 0) {
  243. btInteractive->sendPendingMessage();
  244. if(btInteractive->countPendingMessage() > 0) {
  245. break;
  246. }
  247. }
  248. BtMessageHandle handshakeMessage = btInteractive->receiveHandshake();
  249. if(handshakeMessage.isNull()) {
  250. break;
  251. }
  252. btInteractive->doPostHandshakeProcessing();
  253. sequence = WIRED;
  254. break;
  255. }
  256. case RECEIVER_WAIT_HANDSHAKE: {
  257. BtMessageHandle handshakeMessage = btInteractive->receiveAndSendHandshake();
  258. if(handshakeMessage.isNull()) {
  259. break;
  260. }
  261. btInteractive->doPostHandshakeProcessing();
  262. sequence = WIRED;
  263. break;
  264. }
  265. case WIRED:
  266. // See the comment for writable check below.
  267. disableWriteCheckSocket();
  268. btInteractive->doInteractionProcessing();
  269. if(btInteractive->countReceivedMessageInIteration() > 0) {
  270. updateKeepAlive();
  271. }
  272. if((peer->amInterested() && !peer->peerChoking()) ||
  273. btInteractive->countOutstandingRequest() ||
  274. (peer->peerInterested() && !peer->amChoking())) {
  275. // Writable check to avoid slow seeding
  276. if(btInteractive->isSendingMessageInProgress()) {
  277. setWriteCheckSocket(socket);
  278. }
  279. if(e->_requestGroupMan->doesOverallDownloadSpeedExceed() ||
  280. _requestGroup->doesDownloadSpeedExceed()) {
  281. disableReadCheckSocket();
  282. setNoCheck(true);
  283. } else {
  284. setReadCheckSocket(socket);
  285. }
  286. } else {
  287. disableReadCheckSocket();
  288. }
  289. break;
  290. }
  291. if(btInteractive->countPendingMessage() > 0) {
  292. setNoCheck(true);
  293. }
  294. e->commands.push_back(this);
  295. return false;
  296. }
  297. // TODO this method removed when PeerBalancerCommand is implemented
  298. bool PeerInteractionCommand::prepareForNextPeer(time_t wait) {
  299. if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
  300. SharedHandle<Peer> peer = _peerStorage->getUnusedPeer();
  301. peer->usedBy(e->newCUID());
  302. PeerInitiateConnectionCommand* command =
  303. new PeerInitiateConnectionCommand
  304. (peer->usedBy(), _requestGroup, peer, e, _btRuntime);
  305. command->setPeerStorage(_peerStorage);
  306. command->setPieceStorage(_pieceStorage);
  307. e->commands.push_back(command);
  308. }
  309. return true;
  310. }
  311. void PeerInteractionCommand::onAbort() {
  312. btInteractive->cancelAllPiece();
  313. _peerStorage->returnPeer(peer);
  314. }
  315. void PeerInteractionCommand::onFailure()
  316. {
  317. _requestGroup->setHaltRequested(true);
  318. }
  319. bool PeerInteractionCommand::exitBeforeExecute()
  320. {
  321. return _btRuntime->isHalt();
  322. }
  323. const SharedHandle<Option>& PeerInteractionCommand::getOption() const
  324. {
  325. return _requestGroup->getOption();
  326. }
  327. } // namespace aria2