PeerInitiateConnectionCommand.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "PeerInitiateConnectionCommand.h"
  36. #include "InitiatorMSEHandshakeCommand.h"
  37. #include "PeerInteractionCommand.h"
  38. #include "DownloadEngine.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "prefs.h"
  42. #include "Socket.h"
  43. #include "Logger.h"
  44. #include "Peer.h"
  45. #include "BtRuntime.h"
  46. #include "PeerStorage.h"
  47. #include "PieceStorage.h"
  48. #include "PeerConnection.h"
  49. #include "RequestGroup.h"
  50. #include "DownloadContext.h"
  51. #include "util.h"
  52. #include "FileAllocationEntry.h"
  53. #include "CheckIntegrityEntry.h"
  54. #include "RequestGroupMan.h"
  55. #include "ServerStatMan.h"
  56. namespace aria2 {
  57. PeerInitiateConnectionCommand::PeerInitiateConnectionCommand
  58. (cuid_t cuid,
  59. RequestGroup* requestGroup,
  60. const SharedHandle<Peer>& peer,
  61. DownloadEngine* e,
  62. const SharedHandle<BtRuntime>& btRuntime,
  63. bool mseHandshakeEnabled)
  64. :
  65. PeerAbstractCommand(cuid, peer, e),
  66. _requestGroup(requestGroup),
  67. _btRuntime(btRuntime),
  68. _mseHandshakeEnabled(mseHandshakeEnabled)
  69. {
  70. _btRuntime->increaseConnections();
  71. _requestGroup->increaseNumCommand();
  72. }
  73. PeerInitiateConnectionCommand::~PeerInitiateConnectionCommand()
  74. {
  75. _requestGroup->decreaseNumCommand();
  76. _btRuntime->decreaseConnections();
  77. }
  78. bool PeerInitiateConnectionCommand::executeInternal() {
  79. if(getLogger()->info()) {
  80. getLogger()->info(MSG_CONNECTING_TO_SERVER,
  81. util::itos(getCuid()).c_str(), getPeer()->ipaddr.c_str(),
  82. getPeer()->port);
  83. }
  84. createSocket();
  85. getSocket()->establishConnection(getPeer()->ipaddr, getPeer()->port);
  86. if(_mseHandshakeEnabled) {
  87. InitiatorMSEHandshakeCommand* c =
  88. new InitiatorMSEHandshakeCommand(getCuid(), _requestGroup, getPeer(),
  89. getDownloadEngine(),
  90. _btRuntime, getSocket());
  91. c->setPeerStorage(_peerStorage);
  92. c->setPieceStorage(_pieceStorage);
  93. getDownloadEngine()->addCommand(c);
  94. } else {
  95. PeerInteractionCommand* command =
  96. new PeerInteractionCommand
  97. (getCuid(), _requestGroup, getPeer(), getDownloadEngine(),
  98. _btRuntime, _pieceStorage, _peerStorage,
  99. getSocket(), PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE);
  100. getDownloadEngine()->addCommand(command);
  101. }
  102. return true;
  103. }
  104. // TODO this method removed when PeerBalancerCommand is implemented
  105. bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) {
  106. if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
  107. SharedHandle<Peer> peer = _peerStorage->getUnusedPeer();
  108. peer->usedBy(getDownloadEngine()->newCUID());
  109. PeerInitiateConnectionCommand* command =
  110. new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer,
  111. getDownloadEngine(), _btRuntime);
  112. command->setPeerStorage(_peerStorage);
  113. command->setPieceStorage(_pieceStorage);
  114. getDownloadEngine()->addCommand(command);
  115. }
  116. return true;
  117. }
  118. void PeerInitiateConnectionCommand::onAbort() {
  119. _peerStorage->returnPeer(getPeer());
  120. }
  121. bool PeerInitiateConnectionCommand::exitBeforeExecute()
  122. {
  123. return _btRuntime->isHalt();
  124. }
  125. void PeerInitiateConnectionCommand::setPeerStorage
  126. (const SharedHandle<PeerStorage>& peerStorage)
  127. {
  128. _peerStorage = peerStorage;
  129. }
  130. void PeerInitiateConnectionCommand::setPieceStorage
  131. (const SharedHandle<PieceStorage>& pieceStorage)
  132. {
  133. _pieceStorage = pieceStorage;
  134. }
  135. } // namespace aria2