PeerInitiateConnectionCommand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace aria2 {
  52. PeerInitiateConnectionCommand::PeerInitiateConnectionCommand
  53. (int cuid,
  54. RequestGroup* requestGroup,
  55. const SharedHandle<Peer>& peer,
  56. DownloadEngine* e,
  57. const SharedHandle<BtRuntime>& btRuntime,
  58. bool mseHandshakeEnabled)
  59. :
  60. PeerAbstractCommand(cuid, peer, e),
  61. _requestGroup(requestGroup),
  62. _btRuntime(btRuntime),
  63. _mseHandshakeEnabled(mseHandshakeEnabled)
  64. {
  65. _btRuntime->increaseConnections();
  66. _requestGroup->increaseNumCommand();
  67. }
  68. PeerInitiateConnectionCommand::~PeerInitiateConnectionCommand()
  69. {
  70. _requestGroup->decreaseNumCommand();
  71. _btRuntime->decreaseConnections();
  72. }
  73. bool PeerInitiateConnectionCommand::executeInternal() {
  74. logger->info(MSG_CONNECTING_TO_SERVER, cuid, peer->ipaddr.c_str(),
  75. peer->port);
  76. socket.reset(new SocketCore());
  77. socket->establishConnection(peer->ipaddr, peer->port);
  78. if(_mseHandshakeEnabled) {
  79. InitiatorMSEHandshakeCommand* c =
  80. new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e,
  81. _btRuntime, socket);
  82. c->setPeerStorage(_peerStorage);
  83. c->setPieceStorage(_pieceStorage);
  84. e->commands.push_back(c);
  85. } else {
  86. PeerInteractionCommand* command =
  87. new PeerInteractionCommand
  88. (cuid, _requestGroup, peer, e, _btRuntime, _pieceStorage, _peerStorage,
  89. socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE);
  90. e->commands.push_back(command);
  91. }
  92. return true;
  93. }
  94. // TODO this method removed when PeerBalancerCommand is implemented
  95. bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) {
  96. if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
  97. SharedHandle<Peer> peer = _peerStorage->getUnusedPeer();
  98. peer->usedBy(e->newCUID());
  99. PeerInitiateConnectionCommand* command =
  100. new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e,
  101. _btRuntime);
  102. command->setPeerStorage(_peerStorage);
  103. command->setPieceStorage(_pieceStorage);
  104. e->commands.push_back(command);
  105. }
  106. return true;
  107. }
  108. void PeerInitiateConnectionCommand::onAbort() {
  109. _peerStorage->returnPeer(peer);
  110. }
  111. bool PeerInitiateConnectionCommand::exitBeforeExecute()
  112. {
  113. return _btRuntime->isHalt();
  114. }
  115. void PeerInitiateConnectionCommand::setPeerStorage
  116. (const SharedHandle<PeerStorage>& peerStorage)
  117. {
  118. _peerStorage = peerStorage;
  119. }
  120. void PeerInitiateConnectionCommand::setPieceStorage
  121. (const SharedHandle<PieceStorage>& pieceStorage)
  122. {
  123. _pieceStorage = pieceStorage;
  124. }
  125. } // namespace aria2