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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* 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-05-20 15:22:50 +00:00
|
|
|
#include "CUIDCounter.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "BtContext.h"
|
|
|
|
#include "PeerStorage.h"
|
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "BtRuntime.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "Logger.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"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-04-03 10:52:31 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
ActivePeerConnectionCommand::ActivePeerConnectionCommand(int cuid,
|
|
|
|
RequestGroup* requestGroup,
|
|
|
|
DownloadEngine* e,
|
|
|
|
const BtContextHandle& btContext,
|
2008-03-09 12:24:01 +00:00
|
|
|
time_t interval)
|
2007-10-11 16:58:24 +00:00
|
|
|
:Command(cuid),
|
|
|
|
BtContextAwareCommand(btContext),
|
|
|
|
RequestGroupAware(requestGroup),
|
|
|
|
interval(interval),
|
|
|
|
e(e),
|
2008-06-04 10:14:48 +00:00
|
|
|
_thresholdSpeed(e->option->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT)),
|
2008-06-10 11:31:35 +00:00
|
|
|
_maxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)),
|
2007-10-11 16:58:24 +00:00
|
|
|
_numNewConnection(5)
|
2008-02-18 17:11:44 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int maxDownloadSpeed = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
|
2008-02-18 17:11:44 +00:00
|
|
|
if(maxDownloadSpeed > 0) {
|
|
|
|
_thresholdSpeed = std::min(maxDownloadSpeed, _thresholdSpeed);
|
|
|
|
}
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
ActivePeerConnectionCommand::~ActivePeerConnectionCommand() {}
|
|
|
|
|
2007-04-03 10:52:31 +00:00
|
|
|
bool ActivePeerConnectionCommand::execute() {
|
|
|
|
if(btRuntime->isHalt()) {
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-10 11:31:35 +00:00
|
|
|
if(checkPoint.elapsed(interval)) {
|
2007-04-03 10:52:31 +00:00
|
|
|
checkPoint.reset();
|
2008-09-28 05:07:52 +00:00
|
|
|
TransferStat tstat = _requestGroup->calculateStat();
|
2008-06-10 11:31:35 +00:00
|
|
|
if(// for seeder state
|
|
|
|
(pieceStorage->downloadFinished() && btRuntime->lessThanMaxPeers() &&
|
2008-08-23 15:17:32 +00:00
|
|
|
(_maxUploadSpeedLimit == 0 ||
|
|
|
|
tstat.getUploadSpeed() < _maxUploadSpeedLimit*0.8)) ||
|
2008-06-10 11:31:35 +00:00
|
|
|
// for leecher state
|
2008-08-23 15:17:32 +00:00
|
|
|
(!pieceStorage->downloadFinished() &&
|
|
|
|
(tstat.getDownloadSpeed() < _thresholdSpeed ||
|
|
|
|
btRuntime->lessThanMinPeers()))) {
|
2008-10-06 14:01:40 +00:00
|
|
|
unsigned int numConnection = pieceStorage->downloadFinished() ?
|
2008-08-23 15:17:32 +00:00
|
|
|
std::min(_numNewConnection,
|
|
|
|
BtRuntime::MAX_PEERS-btRuntime->getConnections()) :
|
|
|
|
_numNewConnection;
|
2008-10-06 14:01:40 +00:00
|
|
|
for(unsigned int numAdd = numConnection;
|
2008-06-05 13:02:49 +00:00
|
|
|
numAdd > 0 && peerStorage->isPeerAvailable(); --numAdd) {
|
2007-04-03 10:52:31 +00:00
|
|
|
PeerHandle peer = peerStorage->getUnusedPeer();
|
|
|
|
connectToPeer(peer);
|
|
|
|
}
|
2008-09-02 13:51:46 +00:00
|
|
|
if(btRuntime->getConnections() == 0 &&
|
|
|
|
!pieceStorage->downloadFinished()) {
|
|
|
|
btAnnounce->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL);
|
|
|
|
}
|
2007-04-03 10:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e->commands.push_back(this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActivePeerConnectionCommand::connectToPeer(const PeerHandle& peer)
|
|
|
|
{
|
|
|
|
if(peer.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
|
2007-04-03 10:52:31 +00:00
|
|
|
PeerInitiateConnectionCommand* command =
|
2008-02-09 17:14:40 +00:00
|
|
|
new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, btContext);
|
2007-04-03 10:52:31 +00:00
|
|
|
e->commands.push_back(command);
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->info(MSG_CONNECTING_TO_PEER,
|
2007-04-03 10:52:31 +00:00
|
|
|
cuid, peer->ipaddr.c_str());
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|