PeerInteractionCommand.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "DownloadEngine.h"
  37. #include "PeerInitiateConnectionCommand.h"
  38. #include "DefaultBtInteractive.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "prefs.h"
  42. #include "Socket.h"
  43. #include "Option.h"
  44. #include "BtContext.h"
  45. #include "BtRegistry.h"
  46. #include "PeerObject.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 <algorithm>
  68. namespace aria2 {
  69. PeerInteractionCommand::PeerInteractionCommand(int32_t cuid,
  70. RequestGroup* requestGroup,
  71. const PeerHandle& p,
  72. DownloadEngine* e,
  73. const BtContextHandle& btContext,
  74. const SocketHandle& s,
  75. Seq sequence,
  76. const PeerConnectionHandle& passedPeerConnection)
  77. :PeerAbstractCommand(cuid, p, e, s),
  78. BtContextAwareCommand(btContext),
  79. RequestGroupAware(requestGroup),
  80. sequence(sequence),
  81. maxDownloadSpeedLimit(0)
  82. {
  83. // TODO move following bunch of processing to separate method, like init()
  84. if(sequence == INITIATOR_SEND_HANDSHAKE) {
  85. disableReadCheckSocket();
  86. setWriteCheckSocket(socket);
  87. setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  88. }
  89. DefaultBtMessageFactoryHandle factory(new DefaultBtMessageFactory());
  90. factory->setCuid(cuid);
  91. factory->setBtContext(btContext);
  92. factory->setPeer(peer);
  93. factory->setLocalNode(DHTRegistry::_localNode);
  94. factory->setRoutingTable(DHTRegistry::_routingTable);
  95. factory->setTaskQueue(DHTRegistry::_taskQueue);
  96. factory->setTaskFactory(DHTRegistry::_taskFactory);
  97. PeerConnectionHandle peerConnection;
  98. if(passedPeerConnection.isNull()) {
  99. peerConnection.reset(new PeerConnection(cuid, socket, e->option));
  100. } else {
  101. peerConnection = passedPeerConnection;
  102. }
  103. DefaultBtMessageDispatcherHandle dispatcher(new DefaultBtMessageDispatcher());
  104. dispatcher->setCuid(cuid);
  105. dispatcher->setPeer(peer);
  106. dispatcher->setBtContext(btContext);
  107. dispatcher->setMaxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
  108. dispatcher->setRequestTimeout(e->option->getAsInt(PREF_BT_REQUEST_TIMEOUT));
  109. dispatcher->setBtMessageFactory(factory);
  110. DefaultBtMessageReceiverHandle receiver(new DefaultBtMessageReceiver());
  111. receiver->setCuid(cuid);
  112. receiver->setPeer(peer);
  113. receiver->setBtContext(btContext);
  114. receiver->setPeerConnection(peerConnection);
  115. receiver->setDispatcher(dispatcher);
  116. receiver->setBtMessageFactory(factory);
  117. DefaultBtRequestFactoryHandle reqFactory(new DefaultBtRequestFactory());
  118. reqFactory->setCuid(cuid);
  119. reqFactory->setPeer(peer);
  120. reqFactory->setBtContext(btContext);
  121. reqFactory->setBtMessageDispatcher(dispatcher);
  122. reqFactory->setBtMessageFactory(factory);
  123. DefaultBtInteractiveHandle btInteractive(new DefaultBtInteractive(btContext, peer));
  124. btInteractive->setCuid(cuid);
  125. btInteractive->setBtMessageReceiver(receiver);
  126. btInteractive->setDispatcher(dispatcher);
  127. btInteractive->setBtRequestFactory(reqFactory);
  128. btInteractive->setPeerConnection(peerConnection);
  129. btInteractive->setKeepAliveInterval(e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
  130. btInteractive->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  131. btInteractive->setBtMessageFactory(factory);
  132. if(!btContext->isPrivate()) {
  133. if(e->option->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) {
  134. btInteractive->setUTPexEnabled(true);
  135. }
  136. if(DHTSetup::initialized()) {
  137. btInteractive->setDHTEnabled(true);
  138. btInteractive->setLocalNode(DHTRegistry::_localNode);
  139. factory->setDHTEnabled(true);
  140. }
  141. }
  142. this->btInteractive = btInteractive;
  143. // reverse depends
  144. factory->setBtMessageDispatcher(dispatcher);
  145. factory->setBtRequestFactory(reqFactory);
  146. factory->setPeerConnection(peerConnection);
  147. PeerObjectHandle peerObject(new PeerObject());
  148. peerObject->btMessageDispatcher = dispatcher;
  149. peerObject->btMessageReceiver = receiver;
  150. peerObject->btMessageFactory = factory;
  151. peerObject->btRequestFactory = reqFactory;
  152. peerObject->peerConnection = peerConnection;
  153. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), peerObject);
  154. setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
  155. peer->allocateSessionResource(btContext->getPieceLength(), btContext->getTotalLength());
  156. maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
  157. btRuntime->increaseConnections();
  158. }
  159. PeerInteractionCommand::~PeerInteractionCommand() {
  160. if(peer->getCompletedLength() > 0) {
  161. pieceStorage->subtractPieceStats(peer->getBitfield(),
  162. peer->getBitfieldLength());
  163. }
  164. peer->releaseSessionResource();
  165. PEER_OBJECT_CLUSTER(btContext)->unregisterHandle(peer->getID());
  166. btRuntime->decreaseConnections();
  167. //logger->debug("CUID#%d - unregistered message factory using ID:%s",
  168. //cuid, peer->getId().c_str());
  169. }
  170. bool PeerInteractionCommand::executeInternal() {
  171. setUploadLimitCheck(false);
  172. setNoCheck(false);
  173. switch(sequence) {
  174. case INITIATOR_SEND_HANDSHAKE:
  175. if(!socket->isWritable(0)) {
  176. break;
  177. }
  178. disableWriteCheckSocket();
  179. setReadCheckSocket(socket);
  180. socket->setBlockingMode();
  181. setTimeout(e->option->getAsInt(PREF_BT_TIMEOUT));
  182. btInteractive->initiateHandshake();
  183. sequence = INITIATOR_WAIT_HANDSHAKE;
  184. break;
  185. case INITIATOR_WAIT_HANDSHAKE: {
  186. if(btInteractive->countPendingMessage() > 0) {
  187. btInteractive->sendPendingMessage();
  188. if(btInteractive->countPendingMessage() > 0) {
  189. break;
  190. }
  191. }
  192. BtMessageHandle handshakeMessage = btInteractive->receiveHandshake();
  193. if(handshakeMessage.isNull()) {
  194. break;
  195. }
  196. btInteractive->doPostHandshakeProcessing();
  197. sequence = WIRED;
  198. break;
  199. }
  200. case RECEIVER_WAIT_HANDSHAKE: {
  201. BtMessageHandle handshakeMessage = btInteractive->receiveAndSendHandshake();
  202. if(handshakeMessage.isNull()) {
  203. break;
  204. }
  205. btInteractive->doPostHandshakeProcessing();
  206. sequence = WIRED;
  207. break;
  208. }
  209. case WIRED:
  210. // See the comment for writable check below.
  211. // disableWriteCheckSocket();
  212. btInteractive->doInteractionProcessing();
  213. if(btInteractive->countReceivedMessageInIteration() > 0) {
  214. updateKeepAlive();
  215. }
  216. if((peer->amInterested() && !peer->peerChoking() && (peer->getLatency() < 1500)) ||
  217. (peer->peerInterested() && !peer->amChoking())) {
  218. // Writable check causes CPU usage high because socket becomes writable
  219. // instantly. So don't do it.
  220. // if(btInteractive->isSendingMessageInProgress()) {
  221. // setWriteCheckSocket(socket);
  222. // }
  223. if(maxDownloadSpeedLimit > 0) {
  224. TransferStat stat = peerStorage->calculateStat();
  225. if(maxDownloadSpeedLimit < stat.downloadSpeed) {
  226. disableReadCheckSocket();
  227. setNoCheck(true);
  228. } else {
  229. setReadCheckSocket(socket);
  230. }
  231. } else {
  232. setReadCheckSocket(socket);
  233. }
  234. } else {
  235. disableReadCheckSocket();
  236. }
  237. break;
  238. }
  239. if(btInteractive->countPendingMessage() > 0) {
  240. setNoCheck(true);
  241. }
  242. e->commands.push_back(this);
  243. return false;
  244. }
  245. // TODO this method removed when PeerBalancerCommand is implemented
  246. bool PeerInteractionCommand::prepareForNextPeer(time_t wait) {
  247. if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeers()) {
  248. PeerHandle peer = peerStorage->getUnusedPeer();
  249. peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
  250. PeerInitiateConnectionCommand* command =
  251. new PeerInitiateConnectionCommand(peer->usedBy(),
  252. _requestGroup,
  253. peer,
  254. e,
  255. btContext);
  256. e->commands.push_back(command);
  257. }
  258. return true;
  259. }
  260. void PeerInteractionCommand::onAbort() {
  261. btInteractive->cancelAllPiece();
  262. peerStorage->returnPeer(peer);
  263. }
  264. void PeerInteractionCommand::onFailure()
  265. {
  266. _requestGroup->setHaltRequested(true);
  267. }
  268. bool PeerInteractionCommand::exitBeforeExecute()
  269. {
  270. return btRuntime->isHalt();
  271. }
  272. } // namespace aria2