TrackerUpdateCommand.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "LogFactory.h"
  24. #include "MetaFileUtil.h"
  25. #include "DlAbortEx.h"
  26. #include "message.h"
  27. #include "PeerInitiateConnectionCommand.h"
  28. #include "SleepCommand.h"
  29. #include "Util.h"
  30. #include <netinet/in.h>
  31. TrackerUpdateCommand::TrackerUpdateCommand(int cuid, TorrentDownloadEngine*e):Command(cuid), e(e) {
  32. logger = LogFactory::getInstance();
  33. }
  34. TrackerUpdateCommand::~TrackerUpdateCommand() {}
  35. bool TrackerUpdateCommand::prepareForRetry() {
  36. e->commands.push_back(this);
  37. return false;
  38. }
  39. char* TrackerUpdateCommand::getTrackerResponse(int& trackerResponseLength) {
  40. int maxBufLength = 2048;
  41. char* buf = new char[maxBufLength];
  42. int bufLength = 0;
  43. char data[2048];
  44. try {
  45. while(1) {
  46. int dataLength = e->segmentMan->diskWriter->readData(data, sizeof(data), bufLength);
  47. if(bufLength+dataLength >= maxBufLength) {
  48. maxBufLength = Util::expandBuffer(&buf, bufLength, bufLength+dataLength);
  49. }
  50. memcpy(buf+bufLength, data, dataLength);
  51. bufLength += dataLength;
  52. if(dataLength != sizeof(data)) {
  53. break;
  54. }
  55. }
  56. trackerResponseLength = bufLength;
  57. return buf;
  58. } catch(Exception* e) {
  59. delete [] buf;
  60. throw;
  61. }
  62. }
  63. bool TrackerUpdateCommand::execute() {
  64. if(!e->segmentMan->finished()) {
  65. return prepareForRetry();
  66. }
  67. char* trackerResponse = NULL;
  68. int trackerResponseLength = 0;
  69. try {
  70. trackerResponse = getTrackerResponse(trackerResponseLength);
  71. SharedHandle<MetaEntry> entry(MetaFileUtil::bdecoding(trackerResponse,
  72. trackerResponseLength));
  73. Dictionary* response = (Dictionary*)entry.get();
  74. Data* failureReason = (Data*)response->get("failure reason");
  75. if(failureReason != NULL) {
  76. throw new DlAbortEx("Tracker returned failure reason: %s", failureReason->toString().c_str());
  77. }
  78. Data* warningMessage = (Data*)response->get("warning message");
  79. if(warningMessage != NULL) {
  80. logger->warn(MSG_TRACKER_WARNING_MESSAGE, warningMessage->toString().c_str());
  81. }
  82. Data* trackerId = (Data*)response->get("tracker id");
  83. if(trackerId != NULL) {
  84. e->torrentMan->trackerId = trackerId->toString();
  85. logger->debug("CUID#%d - Tracker ID:%s",
  86. cuid, e->torrentMan->trackerId.c_str());
  87. }
  88. Data* interval = (Data*)response->get("interval");
  89. if(interval != NULL) {
  90. e->torrentMan->interval = interval->toInt();
  91. logger->debug("CUID#%d - Interval:%d", cuid, e->torrentMan->interval);
  92. }
  93. Data* minInterval = (Data*)response->get("min interval");
  94. if(minInterval != NULL) {
  95. e->torrentMan->minInterval = minInterval->toInt();
  96. logger->debug("CUID#%d - Min interval:%d",
  97. cuid, e->torrentMan->minInterval);
  98. }
  99. if(e->torrentMan->minInterval > e->torrentMan->interval) {
  100. e->torrentMan->minInterval = e->torrentMan->interval;
  101. }
  102. Data* complete = (Data*)response->get("complete");
  103. if(complete != NULL) {
  104. e->torrentMan->complete = complete->toInt();
  105. logger->debug("CUID#%d - Complete:%d", cuid, e->torrentMan->complete);
  106. }
  107. Data* incomplete = (Data*)response->get("incomplete");
  108. if(incomplete != NULL) {
  109. e->torrentMan->incomplete = incomplete->toInt();
  110. logger->debug("CUID#%d - Incomplete:%d",
  111. cuid, e->torrentMan->incomplete);
  112. }
  113. if(!e->torrentMan->isHalt() &&
  114. e->torrentMan->connections < MIN_PEERS &&
  115. dynamic_cast<const Data*>(response->get("peers"))) {
  116. Data* peers = (Data*)response->get("peers");
  117. if(peers != NULL && peers->getLen() > 0) {
  118. for(int i = 0; i < peers->getLen(); i += 6) {
  119. unsigned int ipaddr1 = (unsigned char)*(peers->getData()+i);
  120. unsigned int ipaddr2 = (unsigned char)*(peers->getData()+i+1);
  121. unsigned int ipaddr3 = (unsigned char)*(peers->getData()+i+2);
  122. unsigned int ipaddr4 = (unsigned char)*(peers->getData()+i+3);
  123. unsigned int port = ntohs(*(unsigned short int*)(peers->getData()+i+4));
  124. char ipaddr[16];
  125. snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d",
  126. ipaddr1, ipaddr2, ipaddr3, ipaddr4);
  127. PeerHandle peer =
  128. PeerHandle(new Peer(ipaddr, port, e->torrentMan->pieceLength,
  129. e->torrentMan->getTotalLength()));
  130. if(e->torrentMan->addPeer(peer)) {
  131. logger->debug("CUID#%d - Adding peer %s:%d",
  132. cuid, peer->ipaddr.c_str(), peer->port);
  133. }
  134. }
  135. } else {
  136. logger->info("CUID#%d - No peer list received.", cuid);
  137. }
  138. while(e->torrentMan->isPeerAvailable() &&
  139. e->torrentMan->connections < MIN_PEERS) {
  140. PeerHandle peer = e->torrentMan->getPeer();
  141. int newCuid = e->torrentMan->getNewCuid();
  142. peer->cuid = newCuid;
  143. PeerInitiateConnectionCommand* command =
  144. new PeerInitiateConnectionCommand(newCuid, peer, e);
  145. e->commands.push_back(command);
  146. logger->debug("CUID#%d - Adding new command CUID#%d", cuid, newCuid);
  147. }
  148. }
  149. if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) {
  150. e->torrentMan->req->setTrackerEvent(Request::AUTO);
  151. }
  152. } catch(Exception* err) {
  153. logger->error("CUID#%d - Error occurred while processing tracker response.", cuid, err);
  154. delete err;
  155. }
  156. if(trackerResponse != NULL) {
  157. delete [] trackerResponse;
  158. }
  159. e->torrentMan->trackers = 0;
  160. e->segmentMan->init();
  161. if(e->torrentMan->isHalt()) {
  162. return true;
  163. } else {
  164. return prepareForRetry();
  165. }
  166. }