PeerInteractionCommand.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "PeerMessageUtil.h"
  39. #include "DefaultBtInteractive.h"
  40. #include "DlAbortEx.h"
  41. #include "Util.h"
  42. #include "message.h"
  43. #include "prefs.h"
  44. #include "DefaultBtMessageDispatcher.h"
  45. #include "DefaultBtMessageReceiver.h"
  46. #include "DefaultBtRequestFactory.h"
  47. #include "DefaultBtMessageFactory.h"
  48. #include "DefaultBtInteractive.h"
  49. #include "CUIDCounter.h"
  50. #include <algorithm>
  51. PeerInteractionCommand::PeerInteractionCommand(int32_t cuid,
  52. RequestGroup* requestGroup,
  53. const PeerHandle& p,
  54. DownloadEngine* e,
  55. const BtContextHandle& btContext,
  56. const SocketHandle& s,
  57. Seq sequence,
  58. const PeerConnectionHandle& passedPeerConnection)
  59. :PeerAbstractCommand(cuid, p, e, s),
  60. BtContextAwareCommand(btContext),
  61. RequestGroupAware(requestGroup),
  62. sequence(sequence),
  63. btInteractive(0),
  64. maxDownloadSpeedLimit(0)
  65. {
  66. // TODO move following bunch of processing to separate method, like init()
  67. if(sequence == INITIATOR_SEND_HANDSHAKE) {
  68. disableReadCheckSocket();
  69. setWriteCheckSocket(socket);
  70. setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  71. }
  72. DefaultBtMessageFactoryHandle factory = new DefaultBtMessageFactory();
  73. factory->setCuid(cuid);
  74. factory->setBtContext(btContext);
  75. factory->setPeer(peer);
  76. PeerConnectionHandle peerConnection = passedPeerConnection.isNull() ?
  77. new PeerConnection(cuid, socket, e->option) : passedPeerConnection;
  78. DefaultBtMessageDispatcherHandle dispatcher = new DefaultBtMessageDispatcher();
  79. dispatcher->setCuid(cuid);
  80. dispatcher->setPeer(peer);
  81. dispatcher->setBtContext(btContext);
  82. dispatcher->setMaxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
  83. dispatcher->setRequestTimeout(e->option->getAsInt(PREF_BT_REQUEST_TIMEOUT));
  84. dispatcher->setBtMessageFactory(factory);
  85. DefaultBtMessageReceiverHandle receiver = new DefaultBtMessageReceiver();
  86. receiver->setCuid(cuid);
  87. receiver->setPeer(peer);
  88. receiver->setBtContext(btContext);
  89. receiver->setPeerConnection(peerConnection);
  90. receiver->setDispatcher(dispatcher);
  91. receiver->setBtMessageFactory(factory);
  92. DefaultBtRequestFactoryHandle reqFactory = new DefaultBtRequestFactory();
  93. reqFactory->setCuid(cuid);
  94. reqFactory->setPeer(peer);
  95. reqFactory->setBtContext(btContext);
  96. reqFactory->setBtMessageDispatcher(dispatcher);
  97. reqFactory->setBtMessageFactory(factory);
  98. DefaultBtInteractiveHandle btInteractive = new DefaultBtInteractive();
  99. btInteractive->setCuid(cuid);
  100. btInteractive->setPeer(peer);
  101. btInteractive->setBtContext(btContext);
  102. btInteractive->setBtMessageReceiver(receiver);
  103. btInteractive->setDispatcher(dispatcher);
  104. btInteractive->setBtRequestFactory(reqFactory);
  105. btInteractive->setPeerConnection(peerConnection);
  106. btInteractive->setKeepAliveInterval(e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
  107. btInteractive->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  108. btInteractive->setBtMessageFactory(factory);
  109. this->btInteractive = btInteractive;
  110. // reverse depends
  111. factory->setBtMessageDispatcher(dispatcher);
  112. factory->setBtRequestFactory(reqFactory);
  113. factory->setPeerConnection(peerConnection);
  114. PeerObjectHandle peerObject = new PeerObject();
  115. peerObject->btMessageDispatcher = dispatcher;
  116. peerObject->btMessageReceiver = receiver;
  117. peerObject->btMessageFactory = factory;
  118. peerObject->btRequestFactory = reqFactory;
  119. peerObject->peerConnection = peerConnection;
  120. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), peerObject);
  121. setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
  122. peer->activate();
  123. maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
  124. btRuntime->increaseConnections();
  125. }
  126. PeerInteractionCommand::~PeerInteractionCommand() {
  127. peer->deactivate();
  128. PEER_OBJECT_CLUSTER(btContext)->unregisterHandle(peer->getId());
  129. btRuntime->decreaseConnections();
  130. //logger->debug("CUID#%d - unregistered message factory using ID:%s",
  131. //cuid, peer->getId().c_str());
  132. }
  133. bool PeerInteractionCommand::executeInternal() {
  134. setReadCheckSocket(socket);
  135. disableWriteCheckSocket();
  136. setUploadLimitCheck(false);
  137. setNoCheck(false);
  138. switch(sequence) {
  139. case INITIATOR_SEND_HANDSHAKE:
  140. if(!socket->isWritable(0)) {
  141. disableReadCheckSocket();
  142. setWriteCheckSocket(socket);
  143. break;
  144. }
  145. socket->setBlockingMode();
  146. setTimeout(e->option->getAsInt(PREF_BT_TIMEOUT));
  147. btInteractive->initiateHandshake();
  148. sequence = INITIATOR_WAIT_HANDSHAKE;
  149. break;
  150. case INITIATOR_WAIT_HANDSHAKE: {
  151. if(btInteractive->countPendingMessage() > 0) {
  152. btInteractive->sendPendingMessage();
  153. if(btInteractive->countPendingMessage() > 0) {
  154. break;
  155. }
  156. }
  157. BtMessageHandle handshakeMessage = btInteractive->receiveHandshake();
  158. if(handshakeMessage.isNull()) {
  159. break;
  160. }
  161. btInteractive->doPostHandshakeProcessing();
  162. sequence = WIRED;
  163. break;
  164. }
  165. case RECEIVER_WAIT_HANDSHAKE: {
  166. BtMessageHandle handshakeMessage = btInteractive->receiveAndSendHandshake();
  167. if(handshakeMessage.isNull()) {
  168. break;
  169. }
  170. btInteractive->doPostHandshakeProcessing();
  171. sequence = WIRED;
  172. break;
  173. }
  174. case WIRED:
  175. btInteractive->doInteractionProcessing();
  176. break;
  177. }
  178. if(btInteractive->countPendingMessage() > 0) {
  179. setNoCheck(true);
  180. }
  181. if(maxDownloadSpeedLimit > 0) {
  182. TransferStat stat = peerStorage->calculateStat();
  183. if(maxDownloadSpeedLimit < stat.downloadSpeed) {
  184. disableReadCheckSocket();
  185. setNoCheck(true);
  186. }
  187. }
  188. e->commands.push_back(this);
  189. return false;
  190. }
  191. // TODO this method removed when PeerBalancerCommand is implemented
  192. bool PeerInteractionCommand::prepareForNextPeer(int32_t wait) {
  193. if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeer()) {
  194. PeerHandle peer = peerStorage->getUnusedPeer();
  195. peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
  196. PeerInitiateConnectionCommand* command =
  197. new PeerInitiateConnectionCommand(peer->cuid,
  198. _requestGroup,
  199. peer,
  200. e,
  201. btContext);
  202. e->commands.push_back(command);
  203. }
  204. return true;
  205. }
  206. bool PeerInteractionCommand::prepareForRetry(int32_t wait) {
  207. e->commands.push_back(this);
  208. return false;
  209. }
  210. void PeerInteractionCommand::onAbort(Exception* ex) {
  211. btInteractive->cancelAllPiece();
  212. peerStorage->returnPeer(peer);
  213. //PeerAbstractCommand::onAbort(ex);
  214. }
  215. bool PeerInteractionCommand::exitBeforeExecute()
  216. {
  217. return btRuntime->isHalt();
  218. }