/* */ #include "PeerInitiateConnectionCommand.h" #include "InitiatorMSEHandshakeCommand.h" #include "PeerInteractionCommand.h" #include "DownloadEngine.h" #include "DlAbortEx.h" #include "message.h" #include "prefs.h" #include "Socket.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 SharedHandle& peer, DownloadEngine* e, const SharedHandle& 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, util::itos(getCuid()).c_str(), getPeer()->getIPAddress().c_str(), getPeer()->getPort())); createSocket(); getSocket()->establishConnection(getPeer()->getIPAddress(), getPeer()->getPort()); if(mseHandshakeEnabled_) { InitiatorMSEHandshakeCommand* c = new InitiatorMSEHandshakeCommand(getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, getSocket()); c->setPeerStorage(peerStorage_); c->setPieceStorage(pieceStorage_); getDownloadEngine()->addCommand(c); } else { PeerInteractionCommand* command = new PeerInteractionCommand (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_, pieceStorage_, peerStorage_, getSocket(), PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); getDownloadEngine()->addCommand(command); } return true; } // TODO this method removed when PeerBalancerCommand is implemented bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) { if(peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) { SharedHandle peer = peerStorage_->getUnusedPeer(); peer->usedBy(getDownloadEngine()->newCUID()); PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(peer->usedBy(), requestGroup_, peer, getDownloadEngine(), btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); getDownloadEngine()->addCommand(command); } return true; } void PeerInitiateConnectionCommand::onAbort() { peerStorage_->returnPeer(getPeer()); } bool PeerInitiateConnectionCommand::exitBeforeExecute() { return btRuntime_->isHalt(); } void PeerInitiateConnectionCommand::setPeerStorage (const SharedHandle& peerStorage) { peerStorage_ = peerStorage; } void PeerInitiateConnectionCommand::setPieceStorage (const SharedHandle& pieceStorage) { pieceStorage_ = pieceStorage; } } // namespace aria2