/* */ #include "PeerInitiateConnectionCommand.h" #include "InitiatorMSEHandshakeCommand.h" #include "PeerInteractionCommand.h" #include "DownloadEngine.h" #include "DlAbortEx.h" #include "message.h" #include "prefs.h" #include "SocketCore.h" #include "Logger.h" #include "LogFactory.h" #include "Peer.h" #include "BtRuntime.h" #include "PeerStorage.h" #include "PieceStorage.h" #include "PeerConnection.h" #include "RequestGroup.h" #include "util.h" #include "fmt.h" namespace aria2 { PeerInitiateConnectionCommand::PeerInitiateConnectionCommand (cuid_t cuid, RequestGroup* requestGroup, const std::shared_ptr& peer, DownloadEngine* e, const std::shared_ptr& btRuntime, bool mseHandshakeEnabled) : PeerAbstractCommand(cuid, peer, e), requestGroup_(requestGroup), btRuntime_(btRuntime), mseHandshakeEnabled_(mseHandshakeEnabled) { btRuntime_->increaseConnections(); requestGroup_->increaseNumCommand(); } PeerInitiateConnectionCommand::~PeerInitiateConnectionCommand() { requestGroup_->decreaseNumCommand(); btRuntime_->decreaseConnections(); } bool PeerInitiateConnectionCommand::executeInternal() { A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), getPeer()->getIPAddress().c_str(), getPeer()->getPort())); createSocket(); getSocket()->establishConnection(getPeer()->getIPAddress(), getPeer()->getPort(), false); if(mseHandshakeEnabled_) { auto c = make_unique (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, getSocket()); c->setPeerStorage(peerStorage_); c->setPieceStorage(pieceStorage_); getDownloadEngine()->addCommand(std::move(c)); } else { getDownloadEngine()->addCommand (make_unique (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, pieceStorage_, peerStorage_, getSocket(), PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE)); } return true; } // TODO this method removed when PeerBalancerCommand is implemented bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) { if(peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) { cuid_t ncuid = getDownloadEngine()->newCUID(); std::shared_ptr peer = peerStorage_->checkoutPeer(ncuid); // sanity check if(peer) { auto command = make_unique (ncuid, requestGroup_, peer, getDownloadEngine(), btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); getDownloadEngine()->addCommand(std::move(command)); } } return true; } void PeerInitiateConnectionCommand::onAbort() { peerStorage_->returnPeer(getPeer()); } bool PeerInitiateConnectionCommand::exitBeforeExecute() { return btRuntime_->isHalt(); } void PeerInitiateConnectionCommand::setPeerStorage (const std::shared_ptr& peerStorage) { peerStorage_ = peerStorage; } void PeerInitiateConnectionCommand::setPieceStorage (const std::shared_ptr& pieceStorage) { pieceStorage_ = pieceStorage; } } // namespace aria2