PeerInteractionCommand.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "CUIDCounter.h"
  59. #include "DHTRoutingTable.h"
  60. #include "DHTTaskQueue.h"
  61. #include "DHTTaskFactory.h"
  62. #include "DHTNode.h"
  63. #include "DHTSetup.h"
  64. #include "DHTRegistry.h"
  65. #include "PieceStorage.h"
  66. #include "RequestGroup.h"
  67. #include "BtAnnounce.h"
  68. #include "BtProgressInfoFile.h"
  69. #include "DefaultExtensionMessageFactory.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->setRequestTimeout(e->option->getAsInt(PREF_BT_REQUEST_TIMEOUT));
  129. dispatcher->setBtMessageFactory(factory);
  130. DefaultBtMessageReceiverHandle receiver(new DefaultBtMessageReceiver());
  131. receiver->setCuid(cuid);
  132. receiver->setPeer(peer);
  133. receiver->setBtContext(_btContext);
  134. receiver->setPeerConnection(peerConnection);
  135. receiver->setDispatcher(dispatcher);
  136. receiver->setBtMessageFactory(factory);
  137. SharedHandle<DefaultBtRequestFactory> reqFactory
  138. (new DefaultBtRequestFactory());
  139. reqFactory->setCuid(cuid);
  140. reqFactory->setPeer(peer);
  141. reqFactory->setBtContext(_btContext);
  142. reqFactory->setPieceStorage(pieceStorage);
  143. reqFactory->setBtMessageDispatcher(dispatcher);
  144. reqFactory->setBtMessageFactory(factory);
  145. DefaultBtInteractiveHandle btInteractive
  146. (new DefaultBtInteractive(_btContext, peer));
  147. btInteractive->setBtRuntime(_btRuntime);
  148. btInteractive->setPieceStorage(_pieceStorage);
  149. btInteractive->setPeerStorage(peerStorage); // Note: Not a member variable.
  150. btInteractive->setCuid(cuid);
  151. btInteractive->setBtMessageReceiver(receiver);
  152. btInteractive->setDispatcher(dispatcher);
  153. btInteractive->setBtRequestFactory(reqFactory);
  154. btInteractive->setPeerConnection(peerConnection);
  155. btInteractive->setExtensionMessageFactory(extensionMessageFactory);
  156. btInteractive->setKeepAliveInterval
  157. (e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
  158. btInteractive->setMaxDownloadSpeedLimit
  159. (e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  160. btInteractive->setBtMessageFactory(factory);
  161. if(!_btContext->isPrivate()) {
  162. if(e->option->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) {
  163. btInteractive->setUTPexEnabled(true);
  164. }
  165. if(DHTSetup::initialized()) {
  166. btInteractive->setDHTEnabled(true);
  167. btInteractive->setLocalNode(DHTRegistry::_localNode);
  168. factory->setDHTEnabled(true);
  169. }
  170. }
  171. this->btInteractive = btInteractive;
  172. // reverse depends
  173. factory->setBtMessageDispatcher(dispatcher);
  174. factory->setBtRequestFactory(reqFactory);
  175. factory->setPeerConnection(peerConnection);
  176. setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
  177. peer->allocateSessionResource(_btContext->getPieceLength(),
  178. _btContext->getTotalLength());
  179. peer->setBtMessageDispatcher(dispatcher);
  180. maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
  181. _btRuntime->increaseConnections();
  182. }
  183. PeerInteractionCommand::~PeerInteractionCommand() {
  184. if(peer->getCompletedLength() > 0) {
  185. _pieceStorage->subtractPieceStats(peer->getBitfield(),
  186. peer->getBitfieldLength());
  187. }
  188. peer->releaseSessionResource();
  189. _btRuntime->decreaseConnections();
  190. //logger->debug("CUID#%d - unregistered message factory using ID:%s",
  191. //cuid, peer->getId().c_str());
  192. }
  193. bool PeerInteractionCommand::executeInternal() {
  194. setUploadLimitCheck(false);
  195. setNoCheck(false);
  196. switch(sequence) {
  197. case INITIATOR_SEND_HANDSHAKE:
  198. if(!socket->isWritable(0)) {
  199. break;
  200. }
  201. disableWriteCheckSocket();
  202. setReadCheckSocket(socket);
  203. //socket->setBlockingMode();
  204. setTimeout(e->option->getAsInt(PREF_BT_TIMEOUT));
  205. btInteractive->initiateHandshake();
  206. sequence = INITIATOR_WAIT_HANDSHAKE;
  207. break;
  208. case INITIATOR_WAIT_HANDSHAKE: {
  209. if(btInteractive->countPendingMessage() > 0) {
  210. btInteractive->sendPendingMessage();
  211. if(btInteractive->countPendingMessage() > 0) {
  212. break;
  213. }
  214. }
  215. BtMessageHandle handshakeMessage = btInteractive->receiveHandshake();
  216. if(handshakeMessage.isNull()) {
  217. break;
  218. }
  219. btInteractive->doPostHandshakeProcessing();
  220. sequence = WIRED;
  221. break;
  222. }
  223. case RECEIVER_WAIT_HANDSHAKE: {
  224. BtMessageHandle handshakeMessage = btInteractive->receiveAndSendHandshake();
  225. if(handshakeMessage.isNull()) {
  226. break;
  227. }
  228. btInteractive->doPostHandshakeProcessing();
  229. sequence = WIRED;
  230. break;
  231. }
  232. case WIRED:
  233. // See the comment for writable check below.
  234. // disableWriteCheckSocket();
  235. btInteractive->doInteractionProcessing();
  236. if(btInteractive->countReceivedMessageInIteration() > 0) {
  237. updateKeepAlive();
  238. }
  239. if((peer->amInterested() && !peer->peerChoking() &&
  240. (peer->getLatency() < 1500)) ||
  241. (peer->peerInterested() && !peer->amChoking())) {
  242. // Writable check causes CPU usage high because socket becomes writable
  243. // instantly. So don't do it.
  244. // if(btInteractive->isSendingMessageInProgress()) {
  245. // setWriteCheckSocket(socket);
  246. // }
  247. if(maxDownloadSpeedLimit > 0) {
  248. TransferStat stat = _requestGroup->calculateStat();
  249. if(maxDownloadSpeedLimit < stat.downloadSpeed) {
  250. disableReadCheckSocket();
  251. setNoCheck(true);
  252. } else {
  253. setReadCheckSocket(socket);
  254. }
  255. } else {
  256. setReadCheckSocket(socket);
  257. }
  258. } else {
  259. disableReadCheckSocket();
  260. }
  261. break;
  262. }
  263. if(btInteractive->countPendingMessage() > 0) {
  264. setNoCheck(true);
  265. }
  266. e->commands.push_back(this);
  267. return false;
  268. }
  269. // TODO this method removed when PeerBalancerCommand is implemented
  270. bool PeerInteractionCommand::prepareForNextPeer(time_t wait) {
  271. if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
  272. PeerHandle peer = _peerStorage->getUnusedPeer();
  273. peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
  274. PeerInitiateConnectionCommand* command =
  275. new PeerInitiateConnectionCommand(peer->usedBy(),
  276. _requestGroup,
  277. peer,
  278. e,
  279. _btContext,
  280. _btRuntime);
  281. command->setPeerStorage(_peerStorage);
  282. command->setPieceStorage(_pieceStorage);
  283. e->commands.push_back(command);
  284. }
  285. return true;
  286. }
  287. void PeerInteractionCommand::onAbort() {
  288. btInteractive->cancelAllPiece();
  289. _peerStorage->returnPeer(peer);
  290. }
  291. void PeerInteractionCommand::onFailure()
  292. {
  293. _requestGroup->setHaltRequested(true);
  294. }
  295. bool PeerInteractionCommand::exitBeforeExecute()
  296. {
  297. return _btRuntime->isHalt();
  298. }
  299. void PeerInteractionCommand::setPeerStorage
  300. (const SharedHandle<PeerStorage>& peerStorage)
  301. {
  302. _peerStorage = peerStorage;
  303. }
  304. } // namespace aria2