2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- 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"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include <sys/time.h>
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-03-31 14:46:48 +00:00
|
|
|
PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s):
|
2006-05-18 17:08:29 +00:00
|
|
|
Command(cuid), e(e), peer(peer),
|
|
|
|
checkSocketIsReadable(false), checkSocketIsWritable(false),
|
|
|
|
uploadLimitCheck(false), uploadLimit(0) {
|
2006-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
if(s != NULL) {
|
|
|
|
socket = new Socket(*s);
|
|
|
|
setReadCheckSocket(socket);
|
|
|
|
} else {
|
|
|
|
socket = NULL;
|
|
|
|
}
|
|
|
|
this->checkPoint.tv_sec = 0;
|
|
|
|
this->checkPoint.tv_usec = 0;
|
Added new class SendMessageQueue that includes PendingMessages
and
RequestSlotMan.
* src/SendMessageQueue.h: New class.
* src/SendMessageQueue.cc: New class.
* src/PendingMessage.h: Added new member variable blockIndex and
its
accessors.
(createRequestMessage): Updated.
* src/PendingMessage.cc (createRequestMessage): Updated.
* src/PeerInteractionCommand.cc (executeInternal): Updated with
SendMessageQueue.
(checkLongTimePeerChoking): Updated with SendMessageQueue.
(receiveMessage): Updated with SendMessageQueue.
(deletePendingPieceMessage): Removed.
(getNewPieceAndSendInterest): Updated with SendMessageQueue.
(sendInterest): Updated with SendMessageQueue.
(createRequestPendingMessage): Updated with SendMessageQueue.
(sendMessages): Updated with SendMessageQueue.
(onAbort): Updated with SendMessageQueue.
(keepAlive): Updated with SendMessageQueue.
(beforeSocketCheck): Updated SendMessageQueue.
* src/PeerInteractionCommand (sendMessages): Shuffle
missingBLockIndexes before using it.
Added its own timeout for peer connection.
* src/PeerAbstractCommand.h: Added member variable timeout and
its
setter.
* src/prefs.h: Added PREF_PEER_CONNECTION_TIMEOUT.
* src/PeerInteractionCommand.cc (PeerInteractionCommand):
Added setTimeout() call.
(executeInternal): Added setTimeout() call.
* src/PeerAbstractCommand.cc (PeerAbstractCommand):
Added timeout.
(isTimeoutDetected): Updated.
* src/main.cc (main): Added PREF_PEER_CONNECTION_TIMEOUT entry
to
option.
Added *simple* message flooding checker.
* src/PeerInteractionCommand.cc (executeInternal):
Added detectMessageFlooding() call.
(detectMessageFlooding): New function.
(receiveMessage): Count up CHOKE, UNCHOKE, HAVE message.
(beforeSocketCheck): Added detectMessageFlooding() call.
* src/PeerInteractionCommand.h: Added sendMessageQueue,
chokeUnchokeCount, haveCount, detectMessageFlooding().
Removed deletePendingPieceMessage(), getRequestSlot(),
deleteRequestSlot(), deleteAllRequestSlot().
* src/PeerInteractionCommand.cc (beforeSocketCheck):
Added checkLongTimePeerChoking() call.
* src/RequestSlotMan.h: Renamed deleteTimeoutRequestSlot().
* src/TorrentMan.cc (addPeer): Delete at most MAX_PEER_LIST_SIZE
peers
if duplicate == false.
The parameter "uploaded" and "downloaded" in the tracker request
are
the size since the client sent the "started" event to the
tracker.
* src/TorrentMan.cc (setup): Assigned saved downloaded Size and
uploaded size to preDownloadedSize, preUploadedSize
respectively.
* src/TorrentMan.h: Added preDownloadedSize, preUploadedSize,
getSessionDownloadedSize(), getSessionUploadedSize().
* src/TrackerInitCommand.cc (execute): Use
getSessionDownloadedSize(),
getSessionUploadedSize() instead of getDownloadedSize(),
getUploadedSize().
* src/PendingMessage.cc (processMessage): Do not send request
message
if the peer is choking the client.
* src/TrackerUpdateCommand.cc (execute): Check wtheher
minInterval is
less than interval.
2006-03-28 15:23:51 +00:00
|
|
|
timeout = e->option->getAsInt(PREF_TIMEOUT);
|
2006-03-21 14:12:51 +00:00
|
|
|
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 {
|
2006-05-24 15:18:58 +00:00
|
|
|
int elapsed = Util::difftvsec(now, checkPoint);
|
|
|
|
if(elapsed >= timeout) {
|
2006-03-21 14:12:51 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerAbstractCommand::execute() {
|
2006-05-24 15:18:58 +00:00
|
|
|
if(e->torrentMan->isHalt()) {
|
|
|
|
return true;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
try {
|
|
|
|
beforeSocketCheck();
|
2006-05-24 15:18:58 +00:00
|
|
|
if(uploadLimitCheck && (uploadLimit == 0 ||
|
|
|
|
e->getUploadSpeed() <= uploadLimit*1024) ||
|
2006-05-18 17:08:29 +00:00
|
|
|
checkSocketIsReadable && readCheckTarget->isReadable(0) ||
|
|
|
|
checkSocketIsWritable && writeCheckTarget->isWritable(0) ||
|
|
|
|
!checkSocketIsReadable && !checkSocketIsWritable) {
|
|
|
|
updateCheckPoint();
|
|
|
|
return executeInternal();
|
|
|
|
} else {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(isTimeoutDetected()) {
|
|
|
|
// TODO
|
|
|
|
checkPoint.tv_sec = 0;
|
|
|
|
checkPoint.tv_usec = 0;
|
|
|
|
throw new DlRetryEx(EX_TIME_OUT);
|
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-03-21 14:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch(Exception* err) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
|
2006-03-21 14:12:51 +00:00
|
|
|
onAbort(err);
|
|
|
|
delete(err);
|
|
|
|
return prepareForNextPeer(0);
|
2006-03-21 15:21:11 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2006-03-31 13:58:22 +00:00
|
|
|
if(peer->isSeeder()) {
|
|
|
|
peer->error++;
|
|
|
|
} else {
|
|
|
|
peer->error += MAX_PEER_ERROR;
|
|
|
|
}
|
2006-04-28 15:55:11 +00:00
|
|
|
peer->resetStatus();
|
2006-05-18 17:08:29 +00:00
|
|
|
logger->debug("CUID#%d - Peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2006-05-24 15:18:58 +00:00
|
|
|
e->addSocketForReadCheck(socket, this);
|
2006-03-21 14:12:51 +00:00
|
|
|
readCheckTarget = socket;
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-24 15:18:58 +00:00
|
|
|
e->addSocketForReadCheck(socket, this);
|
2006-03-21 14:12:51 +00:00
|
|
|
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);
|
2006-05-24 15:18:58 +00:00
|
|
|
e->addSocketForWriteCheck(socket, this);
|
2006-03-21 14:12:51 +00:00
|
|
|
writeCheckTarget = socket;
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-24 15:18:58 +00:00
|
|
|
e->addSocketForWriteCheck(socket, this);
|
2006-03-21 14:12:51 +00:00
|
|
|
checkSocketIsWritable = true;
|
|
|
|
writeCheckTarget = socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-18 17:08:29 +00:00
|
|
|
|
|
|
|
void PeerAbstractCommand::setUploadLimit(int uploadLimit) {
|
|
|
|
this->uploadLimit = uploadLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerAbstractCommand::setUploadLimitCheck(bool check) {
|
|
|
|
this->uploadLimitCheck = check;
|
|
|
|
}
|