TrackerUpdateCommand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "TrackerUpdateCommand.h"
  23. #include "PeerInitiateConnectionCommand.h"
  24. #include "PeerListenCommand.h"
  25. #include "Dictionary.h"
  26. #include "Data.h"
  27. #include "DlAbortEx.h"
  28. #include "message.h"
  29. #include <netinet/in.h>
  30. TrackerUpdateCommand::TrackerUpdateCommand(int cuid, Request* req,
  31. TorrentDownloadEngine* e,
  32. MetaEntry* trackerResponse)
  33. :Command(cuid), req(req), e(e), trackerResponse(trackerResponse) {}
  34. TrackerUpdateCommand::~TrackerUpdateCommand() {
  35. delete trackerResponse;
  36. }
  37. bool TrackerUpdateCommand::execute() {
  38. Dictionary* response = (Dictionary*)trackerResponse;
  39. Data* failureReason = (Data*)response->get("failure reason");
  40. if(failureReason != NULL) {
  41. throw new DlAbortEx("Tracker returned failure reason: %s", failureReason->toString().c_str());
  42. }
  43. Data* warningMessage = (Data*)response->get("warning message");
  44. if(warningMessage != NULL) {
  45. e->logger->warn(MSG_TRACKER_WARNING_MESSAGE, cuid, warningMessage->toString().c_str());
  46. }
  47. Data* trackerId = (Data*)response->get("tracker id");
  48. if(trackerId != NULL) {
  49. e->torrentMan->trackerId = trackerId->toString();
  50. e->logger->debug("CUID#%d - Tracker ID:%s", cuid, e->torrentMan->trackerId.c_str());
  51. }
  52. Data* interval = (Data*)response->get("interval");
  53. if(interval != NULL) {
  54. e->torrentMan->interval = interval->toInt();
  55. e->logger->debug("CUID#%d - interval:%d", cuid, e->torrentMan->interval);
  56. }
  57. Data* minInterval = (Data*)response->get("min interval");
  58. if(minInterval != NULL) {
  59. e->torrentMan->minInterval = minInterval->toInt();
  60. e->logger->debug("CUID#%d - min interval:%d", cuid, e->torrentMan->minInterval);
  61. }
  62. if(e->torrentMan->minInterval > e->torrentMan->interval) {
  63. e->torrentMan->minInterval = e->torrentMan->interval;
  64. }
  65. Data* complete = (Data*)response->get("complete");
  66. if(complete != NULL) {
  67. e->torrentMan->complete = complete->toInt();
  68. e->logger->debug("CUID#%d - complete:%d", cuid, e->torrentMan->complete);
  69. }
  70. Data* incomplete = (Data*)response->get("incomplete");
  71. if(incomplete != NULL) {
  72. e->torrentMan->incomplete = incomplete->toInt();
  73. e->logger->debug("CUID#%d - incomplete:%d", cuid, e->torrentMan->incomplete);
  74. }
  75. Data* peers = (Data*)response->get("peers");
  76. if(peers != NULL) {
  77. for(int i = 0; i < peers->getLen(); i += 6) {
  78. unsigned int ipaddr1 = (unsigned char)*(peers->getData()+i);
  79. unsigned int ipaddr2 = (unsigned char)*(peers->getData()+i+1);
  80. unsigned int ipaddr3 = (unsigned char)*(peers->getData()+i+2);
  81. unsigned int ipaddr4 = (unsigned char)*(peers->getData()+i+3);
  82. unsigned int port = ntohs(*(unsigned short int*)(peers->getData()+i+4));
  83. char ipaddr[16];
  84. snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d",
  85. ipaddr1, ipaddr2, ipaddr3, ipaddr4);
  86. Peer* peer = new Peer(ipaddr, port, e->torrentMan->pieceLength,
  87. e->torrentMan->getTotalLength());
  88. if(e->torrentMan->addPeer(peer)) {
  89. e->logger->debug("CUID#%d - adding peer %s:%d", cuid,
  90. peer->ipaddr.c_str(), peer->port);
  91. } else {
  92. delete peer;
  93. }
  94. }
  95. } else {
  96. e->logger->info("CUID#%d - no peer list received.", cuid);
  97. }
  98. while(e->torrentMan->isPeerAvailable() &&
  99. e->torrentMan->connections < MAX_PEER_UPDATE) {
  100. Peer* peer = e->torrentMan->getPeer();
  101. int newCuid = e->torrentMan->getNewCuid();
  102. peer->cuid = newCuid;
  103. PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(newCuid, peer, e);
  104. e->commands.push(command);
  105. e->logger->debug("CUID#%d - adding new command CUID#%d", cuid, newCuid);
  106. }
  107. if(req->getTrackerEvent() == Request::STARTED) {
  108. req->setTrackerEvent(Request::AUTO);
  109. }
  110. return true;
  111. }