PeerInitiateConnectionCommand.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "CUIDCounter.h"
  43. #include "Socket.h"
  44. #include "Logger.h"
  45. #include "Peer.h"
  46. #include "PeerConnection.h"
  47. #include "BtContext.h"
  48. #include "BtRuntime.h"
  49. #include "PieceStorage.h"
  50. #include "PeerStorage.h"
  51. #include "BtAnnounce.h"
  52. #include "BtProgressInfoFile.h"
  53. namespace aria2 {
  54. PeerInitiateConnectionCommand::PeerInitiateConnectionCommand(int cuid,
  55. RequestGroup* requestGroup,
  56. const PeerHandle& peer,
  57. DownloadEngine* e,
  58. const BtContextHandle& btContext,
  59. bool mseHandshakeEnabled)
  60. :PeerAbstractCommand(cuid, peer, e),
  61. BtContextAwareCommand(btContext),
  62. RequestGroupAware(requestGroup),
  63. _mseHandshakeEnabled(mseHandshakeEnabled)
  64. {
  65. btRuntime->increaseConnections();
  66. }
  67. PeerInitiateConnectionCommand::~PeerInitiateConnectionCommand()
  68. {
  69. btRuntime->decreaseConnections();
  70. }
  71. bool PeerInitiateConnectionCommand::executeInternal() {
  72. logger->info(MSG_CONNECTING_TO_SERVER, cuid, peer->ipaddr.c_str(),
  73. peer->port);
  74. socket->establishConnection(peer->ipaddr, peer->port);
  75. Command* command;
  76. if(_mseHandshakeEnabled) {
  77. command =
  78. new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e, btContext,
  79. socket);
  80. } else {
  81. command =
  82. new PeerInteractionCommand(cuid, _requestGroup, peer, e, btContext, socket,
  83. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE);
  84. }
  85. e->commands.push_back(command);
  86. return true;
  87. }
  88. // TODO this method removed when PeerBalancerCommand is implemented
  89. bool PeerInitiateConnectionCommand::prepareForNextPeer(int wait) {
  90. if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeer()) {
  91. PeerHandle peer = peerStorage->getUnusedPeer();
  92. peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
  93. Command* command =
  94. new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e,
  95. btContext);
  96. e->commands.push_back(command);
  97. }
  98. return true;
  99. }
  100. void PeerInitiateConnectionCommand::onAbort(Exception* ex) {
  101. peerStorage->returnPeer(peer);
  102. }
  103. bool PeerInitiateConnectionCommand::exitBeforeExecute()
  104. {
  105. return btRuntime->isHalt();
  106. }
  107. } // namespace aria2