PeerInteractionCommand.cc 11 KB

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