TrackerInitCommand.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "TrackerInitCommand.h"
  23. #include "Request.h"
  24. #include "InitiateConnectionCommandFactory.h"
  25. #include "TorrentMan.h"
  26. #include "Util.h"
  27. TrackerInitCommand::TrackerInitCommand(int cuid, Request* req, TorrentDownloadEngine* e):Command(cuid), req(req), e(e) {}
  28. TrackerInitCommand::~TrackerInitCommand() {}
  29. bool TrackerInitCommand::execute() {
  30. if(e->torrentMan->downloadComplete()) {
  31. if(req->getTrackerEvent() == Request::COMPLETED) {
  32. req->setTrackerEvent(Request::AFTER_COMPLETED);
  33. } else {
  34. if(req->getTrackerEvent() == Request::STARTED) {
  35. req->setTrackerEvent(Request::AFTER_COMPLETED);
  36. } else if(req->getTrackerEvent() != Request::AFTER_COMPLETED) {
  37. req->setTrackerEvent(Request::COMPLETED);
  38. }
  39. }
  40. }
  41. string event;
  42. switch(req->getTrackerEvent()) {
  43. case Request::STARTED:
  44. event = "started";
  45. break;
  46. case Request::STOPPED:
  47. event = "stopped";
  48. break;
  49. case Request::COMPLETED:
  50. event = "completed";
  51. break;
  52. }
  53. string url = e->torrentMan->announce+"?"+
  54. "info_hash="+Util::urlencode(e->torrentMan->getInfoHash(), 20)+"&"+
  55. "peer_id="+e->torrentMan->peerId+"&"+
  56. "port="+Util::itos(e->torrentMan->getPort())+"&"+
  57. "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+
  58. "downloaded="+Util::llitos(e->torrentMan->getSessionDownloadLength())+"&"+
  59. "left="+(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength() <= 0
  60. ? "0" : Util::llitos(e->torrentMan->getTotalLength()-e->torrentMan->getDownloadLength()))+"&"+
  61. "compact=1";
  62. if(!event.empty()) {
  63. url += string("&")+"event="+event;
  64. }
  65. if(!e->torrentMan->trackerId.empty()) {
  66. url += string("&")+"trackerid="+e->torrentMan->trackerId;
  67. }
  68. req->setUrl(url);
  69. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  70. e->commands.push(command);
  71. return true;
  72. }