Files
aria2/src/PeerAbstractCommand.cc
Tatsuhiro Tsujikawa ca5e332df9 2006-05-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/PeerAbstractCommand.h
	(beforeSocketCheck): Removed.
	* src/PeerAbstractCommand.cc
	Modified in order to call executeInternal() even if socket check
	and upload limit check fail.
	* src/PeerInteractionCommand.h
	(keepAlive): Removed.
	(sendKeepAlive): New function.
	(checkHave): New function.
	(beforeSocketCheck): Removed.
	* src/PeerInteractionCommand.cc
	(executeInternal): Use peerInteraction->checkRequestSlot(). 
Added
	calls to checkHave() and sendKeepAlive().
	(keepAlive): Renamed to sendKeepAlive().
	(sendKeepAlive): New function.
	(beforeSocketCheck): Removed.
	(checkHave): New function.
	* src/BitfieldMan.cc
	(getMissingIndex): Don't call getMissingIndexRandomly() if max 
is 0.
	(getMissingUnusedIndex): Don't call getMissingIndexRandomly() if 
max is
	0.
	(getMissingIndex): Dont't call getMissingIndexRandomly() if max 
is 0.
	* src/TorrentMan.h
	(UsedPieces): Removed.
	(Pieces): New type definition.
	* src/Piece.h: Updated doc.
	* src/Peer.h
	(totalLength): Removed.
	* src/Peer.cc
	(updateLatency): Fixed the latency calculation.
	* src/TorrentMan.cc
	(addPeer): Call deleteOldErrorPeers() only if peers.size() is 
higher
	than or equal to MAX_PEER_LIST_SIZE.
	If duplicate is false and peer.size() >= MAX_PEER_LIST_SIZE,
	then return false.
	(deleteOldErrorPeers): Rewritten.
	(deleteUsedPiece): Fixed the miss use of STL remove.
	* src/PeerInteraction.h
	(Pieces): Removed.
	(deleteTimeoutRequestSlot): Removed.
	(deleteCompletedRequestSlot): Removed.
	(checkRequestSlot): New function.
	* src/PeerInteraction.cc
	(deleteTimeoutRequestSlot): Merged to checkRequestSlot().
	(deleteCompletedRequestSlot): Merged to checkRequestSlot().
	(checkRequestSlot): New function.
	* src/DownloadEngine.cc
	(run): Clear activeSockets before calling waitData().
	(waitData): Removed a call to activeSockets.clear().
2006-05-26 15:28:19 +00:00

172 lines
4.6 KiB
C++

/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "PeerAbstractCommand.h"
#include "DlAbortEx.h"
#include "DlRetryEx.h"
#include "Util.h"
#include "message.h"
#include "prefs.h"
#include <sys/time.h>
PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s):
Command(cuid), e(e), peer(peer),
checkSocketIsReadable(false), checkSocketIsWritable(false),
uploadLimitCheck(false), uploadLimit(0) {
if(s != NULL) {
socket = new Socket(*s);
setReadCheckSocket(socket);
} else {
socket = NULL;
}
this->checkPoint.tv_sec = 0;
this->checkPoint.tv_usec = 0;
timeout = e->option->getAsInt(PREF_TIMEOUT);
e->torrentMan->connections++;
}
PeerAbstractCommand::~PeerAbstractCommand() {
setReadCheckSocket(NULL);
setWriteCheckSocket(NULL);
if(socket != NULL) {
delete(socket);
}
e->torrentMan->connections--;
}
void PeerAbstractCommand::updateCheckPoint() {
gettimeofday(&checkPoint, NULL);
}
bool PeerAbstractCommand::isTimeoutDetected() {
struct timeval now;
gettimeofday(&now, NULL);
if(checkPoint.tv_sec == 0 && checkPoint.tv_usec == 0) {
checkPoint = now;
return false;
} else {
int elapsed = Util::difftvsec(now, checkPoint);
if(elapsed >= timeout) {
return true;
} else {
return false;
}
}
}
bool PeerAbstractCommand::execute() {
if(e->torrentMan->isHalt()) {
return true;
}
try {
if(uploadLimitCheck && (uploadLimit == 0 ||
e->getUploadSpeed() <= uploadLimit*1024) ||
checkSocketIsReadable && readCheckTarget->isReadable(0) ||
checkSocketIsWritable && writeCheckTarget->isWritable(0)) {
updateCheckPoint();
}
if(isTimeoutDetected()) {
// TODO following 2 lines will be deleted.
checkPoint.tv_sec = 0;
checkPoint.tv_usec = 0;
throw new DlRetryEx(EX_TIME_OUT);
}
return executeInternal();
} catch(Exception* err) {
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
onAbort(err);
delete(err);
return prepareForNextPeer(0);
}
}
// TODO this method removed when PeerBalancerCommand is implemented
bool PeerAbstractCommand::prepareForNextPeer(int wait) {
return true;
}
bool PeerAbstractCommand::prepareForRetry(int wait) {
return true;
}
void PeerAbstractCommand::onAbort(Exception* ex) {
if(peer->isSeeder()) {
peer->error++;
} else {
peer->error += MAX_PEER_ERROR;
}
peer->resetStatus();
logger->debug("CUID#%d - Peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
}
void PeerAbstractCommand::setReadCheckSocket(Socket* socket) {
if(socket == NULL) {
if(checkSocketIsReadable) {
e->deleteSocketForReadCheck(readCheckTarget);
checkSocketIsReadable = false;
readCheckTarget = NULL;
}
} else {
if(checkSocketIsReadable) {
if(readCheckTarget != socket) {
e->deleteSocketForReadCheck(readCheckTarget);
e->addSocketForReadCheck(socket, this);
readCheckTarget = socket;
}
} else {
e->addSocketForReadCheck(socket, this);
checkSocketIsReadable = true;
readCheckTarget = socket;
}
}
}
void PeerAbstractCommand::setWriteCheckSocket(Socket* socket) {
if(socket == NULL) {
if(checkSocketIsWritable) {
e->deleteSocketForWriteCheck(writeCheckTarget);
checkSocketIsWritable = false;
writeCheckTarget = NULL;
}
} else {
if(checkSocketIsWritable) {
if(writeCheckTarget != socket) {
e->deleteSocketForWriteCheck(writeCheckTarget);
e->addSocketForWriteCheck(socket, this);
writeCheckTarget = socket;
}
} else {
e->addSocketForWriteCheck(socket, this);
checkSocketIsWritable = true;
writeCheckTarget = socket;
}
}
}
void PeerAbstractCommand::setUploadLimit(int uploadLimit) {
this->uploadLimit = uploadLimit;
}
void PeerAbstractCommand::setUploadLimitCheck(bool check) {
this->uploadLimitCheck = check;
}