PeerInteractionCommand.cc 9.3 KB

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