PeerInteractionCommand.cc 11 KB

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