/* */ #include "TrackerWatcherCommand.h" #include "InitiateConnectionCommandFactory.h" #include "Util.h" #include "SleepCommand.h" #include "prefs.h" #include "RequestFactory.h" #include "TrackerSegmentManFactory.h" TrackerWatcherCommand::TrackerWatcherCommand(int cuid, TorrentDownloadEngine* e, const BtContextHandle& btContext): BtContextAwareCommand(cuid, btContext), e(e) { } TrackerWatcherCommand::~TrackerWatcherCommand() {} bool TrackerWatcherCommand::execute() { if(btAnnounce->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(btAnnounce->isAnnounceReady()) { command = createRequestCommand(btAnnounce->getAnnounceUrl()); btAnnounce->announceStart(); // inside it, trackers++. } else if(e->_requestGroupMan->getErrors() > 0) { btAnnounce->announceFailure(); // inside it, trackers = 0. e->_requestGroupMan->removeStoppedGroup(); if(btAnnounce->isAllAnnounceFailed()) { btAnnounce->resetAnnounce(); // sleep a few seconds. command = new SleepCommand(cuid, e, createRequestCommand(btAnnounce->getAnnounceUrl()), e->option->getAsInt(PREF_RETRY_WAIT)); } } return command; } Command* TrackerWatcherCommand::createRequestCommand(const string& url) { RequestGroupHandle rg = new RequestGroup(url, e->option); rg->isTorrent = true; rg->setSegmentManFactory(new TrackerSegmentManFactory(e->option)); e->_requestGroupMan->addRequestGroup(rg); Commands commands = e->_requestGroupMan->getInitialCommands(e); if(commands.empty()) { logger->error("CUID#%d - Cannot create tracker request."); return 0; } logger->info("CUID#%d - Creating new tracker request command #%d", cuid, commands.front()->getCuid()); return commands.front(); }