TrackerWatcherCommand.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "TrackerWatcherCommand.h"
  23. #include "InitiateConnectionCommandFactory.h"
  24. #include "Util.h"
  25. TrackerWatcherCommand::TrackerWatcherCommand(int cuid,
  26. TorrentDownloadEngine* e,
  27. int interval):
  28. Command(cuid), e(e), interval(interval) {
  29. checkPoint.tv_sec = 0;
  30. checkPoint.tv_usec = 0;
  31. }
  32. TrackerWatcherCommand::~TrackerWatcherCommand() {}
  33. bool TrackerWatcherCommand::execute() {
  34. struct timeval now;
  35. gettimeofday(&now, NULL);
  36. if(e->torrentMan->trackers == 0 &&
  37. (Util::difftvsec(now, checkPoint) >= interval || e->torrentMan->isHalt())) {
  38. checkPoint = now;
  39. e->torrentMan->req->resetTryCount();
  40. int numWant = 50;
  41. if(e->torrentMan->connections >= MIN_PEERS || e->torrentMan->isHalt()) {
  42. numWant = 0;
  43. }
  44. if(e->torrentMan->isHalt()) {
  45. e->torrentMan->req->setTrackerEvent(Request::STOPPED);
  46. } else if(e->torrentMan->downloadComplete()) {
  47. if(e->torrentMan->req->getTrackerEvent() == Request::COMPLETED) {
  48. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  49. } else {
  50. if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) {
  51. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  52. } else if(e->torrentMan->req->getTrackerEvent() != Request::AFTER_COMPLETED) {
  53. e->torrentMan->req->setTrackerEvent(Request::COMPLETED);
  54. }
  55. }
  56. }
  57. string event;
  58. switch(e->torrentMan->req->getTrackerEvent()) {
  59. case Request::STARTED:
  60. event = "started";
  61. break;
  62. case Request::STOPPED:
  63. event = "stopped";
  64. break;
  65. case Request::COMPLETED:
  66. event = "completed";
  67. break;
  68. }
  69. string url = e->torrentMan->announce+"?"+
  70. "info_hash="+Util::urlencode(e->torrentMan->getInfoHash(), 20)+"&"+
  71. "peer_id="+e->torrentMan->peerId+"&"+
  72. "port="+Util::itos(e->torrentMan->getPort())+"&"+
  73. "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+
  74. "downloaded="+Util::llitos(e->torrentMan->getSessionDownloadLength())+"&"+
  75. "left="+(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength() <= 0
  76. ? "0" : Util::llitos(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength()))+"&"+
  77. "compact=1"+"&"+
  78. "key="+e->torrentMan->peerId+"&"+
  79. "numwant="+Util::itos(numWant);
  80. if(!event.empty()) {
  81. url += string("&")+"event="+event;
  82. }
  83. if(!e->torrentMan->trackerId.empty()) {
  84. url += string("&")+"trackerid="+e->torrentMan->trackerId;
  85. }
  86. e->torrentMan->req->setUrl(url);
  87. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(e->torrentMan->getNewCuid(), e->torrentMan->req, e);
  88. e->commands.push_back(command);
  89. e->torrentMan->trackers++;
  90. logger->info("CUID#%d - creating new tracker request command #%d", cuid,
  91. command->getCuid());
  92. if(e->torrentMan->isHalt()) {
  93. return true;
  94. }
  95. }
  96. interval = e->torrentMan->minInterval;
  97. e->commands.push_back(this);
  98. return false;
  99. }