TrackerWatcherCommand.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "TrackerWatcherCommand.h"
  36. #include "InitiateConnectionCommandFactory.h"
  37. #include "Util.h"
  38. #include "SleepCommand.h"
  39. #include "prefs.h"
  40. TrackerWatcherCommand::TrackerWatcherCommand(int cuid,
  41. TorrentDownloadEngine* e,
  42. int interval):
  43. Command(cuid), e(e), interval(interval) {
  44. // to force requesting to a tracker first time.
  45. checkPoint.setTimeInSec(0);
  46. }
  47. TrackerWatcherCommand::~TrackerWatcherCommand() {}
  48. bool TrackerWatcherCommand::execute() {
  49. if(e->segmentMan->errors > 0) {
  50. if(e->torrentMan->isHalt()) {
  51. return true;
  52. }
  53. // we assume the tracker request has failed.
  54. e->torrentMan->trackers = 0;
  55. e->segmentMan->init();
  56. // sleep a few seconds.
  57. SleepCommand* sleepCommand =
  58. new SleepCommand(cuid, e, this, e->option->getAsInt(PREF_RETRY_WAIT));
  59. e->commands.push_back(sleepCommand);
  60. return false;
  61. }
  62. if(e->torrentMan->trackers == 0 &&
  63. (e->torrentMan->isHalt() ||
  64. checkPoint.elapsed(interval))) {
  65. checkPoint.reset();
  66. e->torrentMan->req->resetTryCount();
  67. int numWant = 50;
  68. if(e->torrentMan->connections >= MIN_PEERS || e->torrentMan->isHalt()) {
  69. numWant = 0;
  70. }
  71. if(e->torrentMan->isHalt()) {
  72. e->torrentMan->req->setTrackerEvent(Request::STOPPED);
  73. } else if(e->torrentMan->downloadComplete()) {
  74. if(e->torrentMan->req->getTrackerEvent() == Request::COMPLETED) {
  75. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  76. } else {
  77. if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) {
  78. // in case where download had completed when aria2c started.
  79. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  80. } else if(e->torrentMan->req->getTrackerEvent() != Request::AFTER_COMPLETED) {
  81. e->torrentMan->req->setTrackerEvent(Request::COMPLETED);
  82. }
  83. }
  84. }
  85. string event;
  86. switch(e->torrentMan->req->getTrackerEvent()) {
  87. case Request::STARTED:
  88. event = "started";
  89. break;
  90. case Request::STOPPED:
  91. event = "stopped";
  92. break;
  93. case Request::COMPLETED:
  94. event = "completed";
  95. break;
  96. }
  97. string url = e->torrentMan->announce+"?"+
  98. "info_hash="+Util::torrentUrlencode(e->torrentMan->getInfoHash(), 20)+"&"+
  99. "peer_id="+e->torrentMan->peerId+"&"+
  100. "port="+Util::itos(e->torrentMan->getPort())+"&"+
  101. "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+
  102. "downloaded="+Util::llitos(e->torrentMan->getSessionDownloadLength())+"&"+
  103. "left="+(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength() <= 0
  104. ? "0" : Util::llitos(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength()))+"&"+
  105. "compact=1"+"&"+
  106. "key="+e->torrentMan->key+"&"+
  107. "numwant="+Util::itos(numWant)+"&"+
  108. "no_peer_id=1";
  109. if(!event.empty()) {
  110. url += string("&")+"event="+event;
  111. }
  112. if(!e->torrentMan->trackerId.empty()) {
  113. url += string("&")+"trackerid="+e->torrentMan->trackerId;
  114. }
  115. e->torrentMan->req->setUrl(url);
  116. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(e->torrentMan->getNewCuid(), e->torrentMan->req, e);
  117. e->commands.push_back(command);
  118. e->torrentMan->trackers++;
  119. logger->info("CUID#%d - Creating new tracker request command #%d", cuid,
  120. command->getCuid());
  121. if(e->torrentMan->isHalt()) {
  122. return true;
  123. }
  124. }
  125. interval = e->torrentMan->minInterval;
  126. e->commands.push_back(this);
  127. return false;
  128. }