PeerInteractionCommand.cc 9.2 KB

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