summaryrefslogtreecommitdiff
path: root/src/PeerChokeCommand.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-05-24 15:18:58 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-05-24 15:18:58 +0000
commit0411de92716bbdefa5dc795f1ecb8a2c2d61004e (patch)
tree374ffa60e707f21a17d3e599459e6614eda98489 /src/PeerChokeCommand.cc
parent896ca193e01f9f629f222fe992e05aebdca23a24 (diff)
2006-05-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To attempt to handle EINTR: * src/SocketCore.cc (isReadable): Added EINTR handling. (isWritable): Added EINTR handling. To improve the performance: * src/AbstractCommand.cc (isTimeoutDetected): Use Util::difftvsec() instead of Util::difftv(). * src/PeerInteractionCommand.h (receiveMessage): Renamed as receiveMessages(). (receiveMessages): New function. * src/PeerInteractionCommand.cc (executeInternal): receiveMessage loop is moved to new receiveMessages () function. detectMessageFlooding() is moved here. (detectMessageFlooding): Use Util::difftvsec() instead of Util::difftv (). The flooding detection for have message is comment out. (checkLongTimePeerChoking): Use Util::difftvsec() instead of Util::difftv(). (receiveMessage): Renamed as receiveMessages(). (receiveMessages): New function. (keepAlive): Use Util::difftvsec() instead of Util::difftv(). (beforeSocketCheck): Commented out checkLongTimePeerChoking(). * src/SleepCommand.cc (execute): Use Util::difftvsec() instead of Util::difftv(). * src/BitfieldMan.h (getNthBitIndex): New function. (hasMissingPiece): New function. (getAllMissingIndexes): New function. * src/BitfieldMan.cc (countSetBit): Rewritten. (getNthBitIndex): New function. (getMissingIndexRandomly): Rewritten. (hasMissingPiece): New function. (getAllMissingIndexes): New function. * src/TorrentMan.cc (hasMissingPiece): New function. (deleteUsedPiece): Rewritten using STL. * src/PeerInteraction.cc (getNewPieceAndSendInterest): Use TorrentMan::hasMissingPiece(), which is a little bit faster then TorrentMan::getMissingPieceIndex(). (addRequests): Updated the number of pending requests. * src/PeerAbstractCommand.cc (isTimeoutDetected): Use Util::difftvsec() instead of Util::difftv(). (execute): Returns true if TorrentMan::isHalt() is true. Corrected the condition of upload limit checking. * src/Util.h (countBit): New function. * src/Util.cc (nbits): New variable. (countBit): New function. * src/DownloadEngine.h (SockCmdMap): New type definition. (waitData): Added an argument. (addSocket): Added an argument. (addSocketForReadCheck): Added an argument. (addSocketForWriteCheck): Added an argument. * src/DownloadEngine.cc (run): Executes only commands whose sockets are ready to read or write. All commands are executed in every 1 second. (waitData): Calls select() again if it returned EINTR. (addSocket): Saves socket and command object pair to the map. (deleteSocket): Deletes socket and command object pair from the map. (addSocketForReadCheck): Added an argument. (addSocketForWriteCheck): Added an argument. * src/DownloadCommand.cc (executeInternal): Use Util::difftvsec() instead of Util::difftv(). To send "event=stopped" to the tracker when the application finishes: * src/PeerChokeCommand.h (checkPoint): New variable. * src/PeerChokeCommand.cc (PeerChokeCommand): Added the initialization of checkPoint. (execute): The interval check is now done by checkPoint, not SleepCommand. Return true if TorrentMan::isHalt() is true. * src/TorrentMan.h (halt): New function. (hasMissingPiece): New function. (isHalt): New function. (setHalt): New function. * src/TorrentMan.cc (TorrentMan): Added the initialization of halt. * src/TorrentAutoSaveCommand.h (checkPoint): New variable. * src/PeerListenCommand.cc (execute): Returns true if TorrentMan::isHalt() is true. * src/main.cc (setSignalHander): Added flags argument. (torrentHandler): Just calls TorrentMan::setHalt(true) and returns. (main): Set SA_ONESHOT flag of the signal hander of SIGINT and SIGTERM. Removed printDownloadAbortMessage() after torrent downloading loop. * src/TorrentAutoSaveCommand.cc (TorrentAutoSaveCommand): Added the initialization of checkPoint. (execute): Returns true if TorrentMan::isHalt() is true. The interval check is now done by checkPoint, not SleepCommand. * src/TrackerWatcherCommand.cc (execute): If TorrentMan::isHalt(), then create a tracker request with event=stopped. * src/TrackerUpdateCommand.cc (prepareForRetry): Do not use SleepCommand here. (execute): Returns true if TorrentMan::isHalt() is true. Others: * src/TorrentMan.cc (getMissingPieceIndex): Updated log message. * src/PeerInteraction.cc (createPeerMessag): Updated log message.
Diffstat (limited to 'src/PeerChokeCommand.cc')
-rw-r--r--src/PeerChokeCommand.cc85
1 files changed, 46 insertions, 39 deletions
diff --git a/src/PeerChokeCommand.cc b/src/PeerChokeCommand.cc
index c941f75c..bf7b363f 100644
--- a/src/PeerChokeCommand.cc
+++ b/src/PeerChokeCommand.cc
@@ -20,10 +20,12 @@
*/
/* copyright --> */
#include "PeerChokeCommand.h"
-#include "SleepCommand.h"
#include "Util.h"
-PeerChokeCommand::PeerChokeCommand(int cuid, int interval, TorrentDownloadEngine* e):Command(cuid), interval(interval), e(e), rotate(0) {}
+PeerChokeCommand::PeerChokeCommand(int cuid, int interval, TorrentDownloadEngine* e):Command(cuid), interval(interval), e(e), rotate(0) {
+ checkPoint.tv_sec = 0;
+ checkPoint.tv_usec = 0;
+}
PeerChokeCommand::~PeerChokeCommand() {}
@@ -91,47 +93,52 @@ void PeerChokeCommand::orderByDownloadRate(Peers& peers) const {
}
bool PeerChokeCommand::execute() {
- Peers peers = e->torrentMan->getActivePeers();
- setAllPeerChoked(peers);
- if(e->torrentMan->downloadComplete()) {
- orderByDownloadRate(peers);
- } else {
- orderByUploadRate(peers);
+ if(e->torrentMan->isHalt()) {
+ return true;
}
- int unchokingCount = peers.size() >= 4 ? 4 : peers.size();
- for(Peers::iterator itr = peers.begin(); unchokingCount > 0 && itr != peers.end(); ) {
- Peer* peer = *itr;
- if(peer->peerInterested) {
- peer->chokingRequired = false;
- peer->optUnchoking = false;
- itr = peers.erase(itr);
- unchokingCount--;
- logger->debug("cat01, unchoking %s, delta=%d", peer->ipaddr.c_str(), peer->getDeltaUpload());
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ if(Util::difftvsec(now, checkPoint) >= interval) {
+ checkPoint = now;
+ Peers peers = e->torrentMan->getActivePeers();
+ setAllPeerChoked(peers);
+ if(e->torrentMan->downloadComplete()) {
+ orderByDownloadRate(peers);
} else {
- itr++;
+ orderByUploadRate(peers);
}
- }
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); ) {
- Peer* peer = *itr;
- if(!peer->peerInterested) {
- peer->chokingRequired = false;
- peer->optUnchoking = false;
- itr = peers.erase(itr);
- logger->debug("cat02, unchoking %s, delta=%d", peer->ipaddr.c_str(), peer->getDeltaUpload());
- break;
- } else {
- itr++;
+ int unchokingCount = peers.size() >= 4 ? 4 : peers.size();
+ for(Peers::iterator itr = peers.begin(); unchokingCount > 0 && itr != peers.end(); ) {
+ Peer* peer = *itr;
+ if(peer->peerInterested) {
+ peer->chokingRequired = false;
+ peer->optUnchoking = false;
+ itr = peers.erase(itr);
+ unchokingCount--;
+ logger->debug("cat01, unchoking %s, delta=%d", peer->ipaddr.c_str(), peer->getDeltaUpload());
+ } else {
+ itr++;
+ }
}
+ for(Peers::iterator itr = peers.begin(); itr != peers.end(); ) {
+ Peer* peer = *itr;
+ if(!peer->peerInterested) {
+ peer->chokingRequired = false;
+ peer->optUnchoking = false;
+ itr = peers.erase(itr);
+ logger->debug("cat02, unchoking %s, delta=%d", peer->ipaddr.c_str(), peer->getDeltaUpload());
+ break;
+ } else {
+ itr++;
+ }
+ }
+ if(rotate%3 == 0) {
+ optUnchokingPeer(peers);
+ rotate = 0;
+ }
+ rotate++;
+ setAllPeerResetDelta(e->torrentMan->getActivePeers());
}
- if(rotate%3 == 0) {
- optUnchokingPeer(peers);
- rotate = 0;
- }
- rotate++;
- setAllPeerResetDelta(e->torrentMan->getActivePeers());
-
- SleepCommand* command = new SleepCommand(cuid, e, this, interval);
- e->commands.push_back(command);
-
+ e->commands.push_back(this);
return false;
}