TrackerWatcherCommand.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "SleepCommand.h"
  24. #include "InitiateConnectionCommandFactory.h"
  25. #include "Util.h"
  26. TrackerWatcherCommand::TrackerWatcherCommand(int cuid,
  27. TorrentDownloadEngine* e):
  28. Command(cuid), e(e) {
  29. }
  30. TrackerWatcherCommand::~TrackerWatcherCommand() {}
  31. bool TrackerWatcherCommand::execute() {
  32. if(e->torrentMan->trackers == 0 && e->torrentMan->connections < 30) {
  33. e->torrentMan->req->resetTryCount();
  34. if(e->torrentMan->downloadComplete()) {
  35. if(e->torrentMan->req->getTrackerEvent() == Request::COMPLETED) {
  36. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  37. } else {
  38. if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) {
  39. e->torrentMan->req->setTrackerEvent(Request::AFTER_COMPLETED);
  40. } else if(e->torrentMan->req->getTrackerEvent() != Request::AFTER_COMPLETED) {
  41. e->torrentMan->req->setTrackerEvent(Request::COMPLETED);
  42. }
  43. }
  44. }
  45. string event;
  46. switch(e->torrentMan->req->getTrackerEvent()) {
  47. case Request::STARTED:
  48. event = "started";
  49. break;
  50. case Request::STOPPED:
  51. event = "stopped";
  52. break;
  53. case Request::COMPLETED:
  54. event = "completed";
  55. break;
  56. }
  57. string url = e->torrentMan->announce+"?"+
  58. "info_hash="+Util::urlencode(e->torrentMan->getInfoHash(), 20)+"&"+
  59. "peer_id="+e->torrentMan->peerId+"&"+
  60. "port="+Util::itos(e->torrentMan->getPort())+"&"+
  61. "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+
  62. "downloaded="+Util::llitos(e->torrentMan->getSessionDownloadLength())+"&"+
  63. "left="+(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength() <= 0
  64. ? "0" : Util::llitos(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength()))+"&"+
  65. "compact=1"+"&"+
  66. "key="+e->torrentMan->peerId;
  67. if(!event.empty()) {
  68. url += string("&")+"event="+event;
  69. }
  70. if(!e->torrentMan->trackerId.empty()) {
  71. url += string("&")+"trackerid="+e->torrentMan->trackerId;
  72. }
  73. e->torrentMan->req->setUrl(url);
  74. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(e->torrentMan->getNewCuid(), e->torrentMan->req, e);
  75. e->commands.push(command);
  76. e->torrentMan->trackers++;
  77. logger->info("CUID#%d - creating new tracker request command #%d", cuid,
  78. command->getCuid());
  79. }
  80. SleepCommand* slpCommand = new SleepCommand(cuid, e, this,
  81. e->torrentMan->minInterval);
  82. e->commands.push(slpCommand);
  83. return false;
  84. }