2007-04-03 10:52:31 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-04-03 10:52:31 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "ActivePeerConnectionCommand.h"
|
|
|
|
#include "PeerInitiateConnectionCommand.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "PeerStorage.h"
|
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "BtRuntime.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "Logger.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-18 17:11:44 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "BtConstants.h"
|
2008-05-17 09:15:14 +00:00
|
|
|
#include "SocketCore.h"
|
2008-09-02 13:51:46 +00:00
|
|
|
#include "BtAnnounce.h"
|
2008-09-28 05:07:52 +00:00
|
|
|
#include "RequestGroup.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2009-11-22 15:16:12 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2010-03-06 08:29:53 +00:00
|
|
|
#include "wallclock.h"
|
2010-03-21 07:05:49 +00:00
|
|
|
#include "util.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-04-03 10:52:31 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
ActivePeerConnectionCommand::ActivePeerConnectionCommand
|
2010-03-20 14:30:36 +00:00
|
|
|
(cuid_t cuid,
|
2008-11-03 06:49:02 +00:00
|
|
|
RequestGroup* requestGroup,
|
|
|
|
DownloadEngine* e,
|
|
|
|
time_t interval)
|
2010-11-20 08:21:36 +00:00
|
|
|
: Command(cuid),
|
|
|
|
requestGroup_(requestGroup),
|
|
|
|
interval_(interval),
|
|
|
|
e_(e),
|
|
|
|
numNewConnection_(5)
|
2008-02-18 17:11:44 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_->increaseNumCommand();
|
2008-02-18 17:11:44 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2009-04-23 15:00:09 +00:00
|
|
|
ActivePeerConnectionCommand::~ActivePeerConnectionCommand()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_->decreaseNumCommand();
|
2009-04-23 15:00:09 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2007-04-03 10:52:31 +00:00
|
|
|
bool ActivePeerConnectionCommand::execute() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(btRuntime_->isHalt()) {
|
2007-04-03 10:52:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-09 16:17:28 +00:00
|
|
|
if(checkPoint_.difference(global::wallclock()) >= interval_) {
|
|
|
|
checkPoint_ = global::wallclock();
|
2012-10-25 15:16:20 +00:00
|
|
|
NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
|
2011-12-08 12:38:00 +00:00
|
|
|
const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit();
|
|
|
|
const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit();
|
|
|
|
int thresholdSpeed;
|
2010-06-18 14:47:09 +00:00
|
|
|
if(!bittorrent::getTorrentAttrs
|
2010-06-21 13:51:56 +00:00
|
|
|
(requestGroup_->getDownloadContext())->metadata.empty()) {
|
2009-11-22 15:16:12 +00:00
|
|
|
thresholdSpeed =
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_->getOption()->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT);
|
2009-11-22 15:16:12 +00:00
|
|
|
} else {
|
|
|
|
thresholdSpeed = 0;
|
|
|
|
}
|
2009-05-07 08:40:45 +00:00
|
|
|
if(maxDownloadLimit > 0) {
|
2009-05-08 16:41:21 +00:00
|
|
|
thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed);
|
2009-05-07 08:40:45 +00:00
|
|
|
}
|
2008-06-10 11:31:35 +00:00
|
|
|
if(// for seeder state
|
2010-06-21 13:51:56 +00:00
|
|
|
(pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() &&
|
2012-10-25 15:16:20 +00:00
|
|
|
(maxUploadLimit == 0 ||
|
|
|
|
stat.calculateUploadSpeed() < maxUploadLimit*0.8)) ||
|
2008-06-10 11:31:35 +00:00
|
|
|
// for leecher state
|
2010-06-21 13:51:56 +00:00
|
|
|
(!pieceStorage_->downloadFinished() &&
|
2012-10-25 15:16:20 +00:00
|
|
|
(stat.calculateDownloadSpeed() < thresholdSpeed ||
|
2010-06-21 13:51:56 +00:00
|
|
|
btRuntime_->lessThanMinPeers()))) {
|
2009-01-18 10:11:08 +00:00
|
|
|
|
2011-12-08 12:38:00 +00:00
|
|
|
int numConnection = 0;
|
2010-06-21 13:51:56 +00:00
|
|
|
if(pieceStorage_->downloadFinished()) {
|
|
|
|
if(btRuntime_->getMaxPeers() > btRuntime_->getConnections()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
numConnection =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::min(numNewConnection_,
|
|
|
|
btRuntime_->getMaxPeers()-btRuntime_->getConnections());
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
2009-01-18 10:11:08 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
numConnection = numNewConnection_;
|
2009-01-18 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 15:18:31 +00:00
|
|
|
for(; numConnection > 0; --numConnection) {
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<Peer> peer = peerStorage_->getUnusedPeer();
|
2013-01-23 15:18:31 +00:00
|
|
|
if(!peer) {
|
|
|
|
break;
|
|
|
|
}
|
2010-01-05 16:01:46 +00:00
|
|
|
connectToPeer(peer);
|
2007-04-03 10:52:31 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
if(btRuntime_->getConnections() == 0 &&
|
|
|
|
!pieceStorage_->downloadFinished()) {
|
|
|
|
btAnnounce_->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL);
|
2008-09-02 13:51:46 +00:00
|
|
|
}
|
2007-04-03 10:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->addCommand(this);
|
2007-04-03 10:52:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void ActivePeerConnectionCommand::connectToPeer(const SharedHandle<Peer>& peer)
|
2007-04-03 10:52:31 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peer->usedBy(e_->newCUID());
|
2007-04-03 10:52:31 +00:00
|
|
|
PeerInitiateConnectionCommand* command =
|
2010-06-21 13:51:56 +00:00
|
|
|
new PeerInitiateConnectionCommand(peer->usedBy(), requestGroup_, peer, e_,
|
|
|
|
btRuntime_);
|
|
|
|
command->setPeerStorage(peerStorage_);
|
|
|
|
command->setPieceStorage(pieceStorage_);
|
|
|
|
e_->addCommand(command);
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_CONNECTING_TO_PEER,
|
2010-11-20 12:12:06 +00:00
|
|
|
getCuid(),
|
2010-11-20 08:21:36 +00:00
|
|
|
peer->getIPAddress().c_str()));
|
2007-04-03 10:52:31 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
void ActivePeerConnectionCommand::setBtRuntime
|
|
|
|
(const SharedHandle<BtRuntime>& btRuntime)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
btRuntime_ = btRuntime;
|
2008-11-03 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActivePeerConnectionCommand::setPieceStorage
|
|
|
|
(const SharedHandle<PieceStorage>& pieceStorage)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceStorage_ = pieceStorage;
|
2008-11-03 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActivePeerConnectionCommand::setPeerStorage
|
|
|
|
(const SharedHandle<PeerStorage>& peerStorage)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerStorage_ = peerStorage;
|
2008-11-03 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActivePeerConnectionCommand::setBtAnnounce
|
|
|
|
(const SharedHandle<BtAnnounce>& btAnnounce)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
btAnnounce_ = btAnnounce;
|
2008-11-03 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|