diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2006-10-20 13:20:50 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2006-10-20 13:20:50 +0000 |
| commit | b8737b0e7c92fb4a5b1c8b6173663381aacca40c (patch) | |
| tree | 0d5b0f79eb0bfbe5cda58cce30f3523c803f2d5c /src/TrackerWatcherCommand.cc | |
| parent | c0fd1fff2a6d78d3157790bbef6f98d54f31d0fc (diff) | |
2006-10-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To simplify TrackerWatherCommand, TrackerUpdateCommand and
make the process of announce request testable.
* src/TrackerWatcherCommand.h
(TimeA2.h): Removed.
(interval): Removed.
(checkPoint): Removed.
(createRequestCommand): Added an argument 'url'.
(TrackerWatherCommand): Removed interval.
(createCommand): New function.
* src/DownloadEngineFactory.cc
(newTorrentConsoleEngine): Updated according to the changes in
TrackerWatherCommand.
* src/TorrentMan.cc
(DelegatingPeerListProcessor.h): New includes.
(TorrentMan): Added the initialization of announceInterval.
(isStoppedAnnounceReady): New function.
(isCompletedAnnounceReady): New function.
(isDefaultAnnounceReady): New function.
(isAnnounceReady): New function.
(getAnnounceUrl): New function.
(announceStart): New function.
(announceFailure): New function.
(announceSuccess): New function.
(isAllAnnounceFailed): New function.
(resetAnnounce): New function.
(processAnnounceResponse): New function.
(needMorePeerConnection): New function.
(noMoreAnnounce): New function.
* src/TrackerUpdateCommand.h
(getTrackerResponse): int->size_t
* src/TorrentMan.h
(isStoppedAnnounceReady): New function.
(isCompletedAnnounceReady): New function.
(isDefaultAnnounceReady): New function.
(announceInterval): New variable.
(isAnnounceReady): New function.
(getAnnounceUrl): New function.
(announceStart): New function.
(announceFailure): New function.
(announceSuccess): New function.
(isAllAnnounceFailed): New function.
(resetAnnounce): New function.
(processAnnounceResponse): New function.
(needMorePeerConnection): New function.
(noMoreAnnounce): New function.
* src/TrackerWatcherCommand.cc
(TrackerWatherCommand): Removed interval and checkPoint.
(execute): Rewritten.
(createCommand): New function.
(createRequestCommand): Rewritten.
* src/TrackerUpdateCommand.cc
(MetaFileUtil.h): Removed.
(DelegatingPeerListProcessor.h): Removed.
(getTrackerResponse): int->size_t. Use torrentMan's new
functions.
Diffstat (limited to 'src/TrackerWatcherCommand.cc')
| -rw-r--r-- | src/TrackerWatcherCommand.cc | 107 |
1 files changed, 26 insertions, 81 deletions
diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index be7ed234..cea9a914 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -39,103 +39,48 @@ #include "prefs.h" TrackerWatcherCommand::TrackerWatcherCommand(int cuid, - TorrentDownloadEngine* e, - int interval): - Command(cuid), e(e), interval(interval) { - // to force requesting to a tracker first time. - checkPoint.setTimeInSec(0); + TorrentDownloadEngine* e): + Command(cuid), e(e) { } TrackerWatcherCommand::~TrackerWatcherCommand() {} + bool TrackerWatcherCommand::execute() { + if(e->torrentMan->isHalt() && + e->segmentMan->errors > 0 && e->torrentMan->isAllAnnounceFailed() || + e->torrentMan->noMoreAnnounce()) { + return true; + } + Command* command = createCommand(); + if(command) { + e->commands.push_back(command); + } + e->commands.push_back(this); + return false; +} + +Command* TrackerWatcherCommand::createCommand() { Command* command = 0; - if(e->torrentMan->trackers == 0 && e->torrentMan->isHalt()) { - // Download is going to halt. - // Check whether there are at least one tracker which can receive - // "stopped" event. - if(e->torrentMan->announceList.countStoppedAllowedTier()) { - e->torrentMan->announceList.moveToStoppedAllowedTier(); - e->torrentMan->announceList.setEvent(AnnounceTier::STOPPED); - command = createRequestCommand(); - } else { - // We don't send "stopped" event since no tracker cares about it. - return true; - } - } else if(e->torrentMan->trackers == 0 && - e->torrentMan->downloadComplete() && - e->torrentMan->announceList.countCompletedAllowedTier()) { - // Send "completed" event to all trackers which can accept it. - e->torrentMan->announceList.moveToCompletedAllowedTier(); - e->torrentMan->announceList.setEvent(AnnounceTier::COMPLETED); - command = createRequestCommand(); - } else if(e->torrentMan->trackers == 0 && - checkPoint.elapsed(interval)) { - checkPoint.reset(); - // If download completed before "started" event is sent to a tracker, - // we change the event to something else to prevent us from - // sending "completed" event. - if(e->torrentMan->downloadComplete() && - e->torrentMan->announceList.getEvent() == AnnounceTier::STARTED) { - e->torrentMan->announceList.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION); - } - command = createRequestCommand(); + if(e->torrentMan->isAnnounceReady()) { + command = createRequestCommand(e->torrentMan->getAnnounceUrl()); + e->torrentMan->announceStart(); // inside it, trackers++. } else if(e->segmentMan->errors > 0) { - e->torrentMan->trackerNumTry++; - checkPoint.reset(); - // we assume the tracker request has failed. - e->torrentMan->announceList.announceFailure(); - e->torrentMan->trackers = 0; + e->torrentMan->announceFailure(); // inside it, trackers = 0. e->segmentMan->init(); - if(e->torrentMan->trackerNumTry >= e->option->getAsInt(PREF_TRACKER_MAX_TRIES)) { - // abort tracker request - e->torrentMan->trackerNumTry = 0; - if(e->torrentMan->isHalt()) { - return true; - } - } else { + if(e->torrentMan->isAllAnnounceFailed()) { + e->torrentMan->resetAnnounce(); // sleep a few seconds. command = new SleepCommand(cuid, e, - createRequestCommand(), + createRequestCommand(e->torrentMan->getAnnounceUrl()), e->option->getAsInt(PREF_RETRY_WAIT)); } } - - if(command) { - e->commands.push_back(command); - e->torrentMan->trackers++; - } - // updates interval with newest minInterval - interval = e->torrentMan->minInterval; - e->commands.push_back(this); - return false; + return command; } -Command* TrackerWatcherCommand::createRequestCommand() { - int numWant = 50; - if(e->torrentMan->connections >= MIN_PEERS || e->torrentMan->isHalt()) { - numWant = 0; - } - string url = e->torrentMan->announceList.getAnnounce()+"?"+ - "info_hash="+Util::torrentUrlencode(e->torrentMan->getInfoHash(), 20)+"&"+ - "peer_id="+e->torrentMan->peerId+"&"+ - "port="+Util::itos(e->torrentMan->getPort())+"&"+ - "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+ - "downloaded="+Util::llitos(e->torrentMan->getSessionDownloadLength())+"&"+ - "left="+(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength() <= 0 - ? "0" : Util::llitos(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength()))+"&"+ - "compact=1"+"&"+ - "key="+e->torrentMan->key+"&"+ - "numwant="+Util::itos(numWant)+"&"+ - "no_peer_id=1"; - string event = e->torrentMan->announceList.getEventString(); - if(!event.empty()) { - url += string("&")+"event="+event; - } - if(!e->torrentMan->trackerId.empty()) { - url += string("&")+"trackerid="+e->torrentMan->trackerId; - } +Command* TrackerWatcherCommand::createRequestCommand(const string& url) { RequestHandle req; req->setUrl(url); req->isTorrent = true; |
