TrackerUpdateCommand.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "TrackerUpdateCommand.h"
  36. #include "LogFactory.h"
  37. #include "DlAbortEx.h"
  38. #include "message.h"
  39. #include "PeerInitiateConnectionCommand.h"
  40. #include "SleepCommand.h"
  41. #include "Util.h"
  42. TrackerUpdateCommand::TrackerUpdateCommand(int cuid, TorrentDownloadEngine* e):Command(cuid), e(e) {
  43. logger = LogFactory::getInstance();
  44. }
  45. TrackerUpdateCommand::~TrackerUpdateCommand() {}
  46. bool TrackerUpdateCommand::prepareForRetry() {
  47. e->commands.push_back(this);
  48. return false;
  49. }
  50. char* TrackerUpdateCommand::getTrackerResponse(size_t& trackerResponseLength) {
  51. int maxBufLength = 2048;
  52. char* buf = new char[maxBufLength];
  53. int bufLength = 0;
  54. char data[2048];
  55. try {
  56. while(1) {
  57. int dataLength = e->segmentMan->diskWriter->readData(data, sizeof(data), bufLength);
  58. if(bufLength+dataLength >= maxBufLength) {
  59. maxBufLength = Util::expandBuffer(&buf, bufLength, bufLength+dataLength);
  60. }
  61. memcpy(buf+bufLength, data, dataLength);
  62. bufLength += dataLength;
  63. if(dataLength != sizeof(data)) {
  64. break;
  65. }
  66. }
  67. trackerResponseLength = bufLength;
  68. return buf;
  69. } catch(Exception* e) {
  70. delete [] buf;
  71. throw;
  72. }
  73. }
  74. bool TrackerUpdateCommand::execute() {
  75. if(e->segmentMan->errors > 0 && e->torrentMan->isHalt()) {
  76. return true;
  77. }
  78. if(!e->segmentMan->finished()) {
  79. return prepareForRetry();
  80. }
  81. char* trackerResponse = NULL;
  82. size_t trackerResponseLength = 0;
  83. try {
  84. trackerResponse = getTrackerResponse(trackerResponseLength);
  85. e->torrentMan->processAnnounceResponse(trackerResponse,
  86. trackerResponseLength);
  87. while(e->torrentMan->needMorePeerConnection()) {
  88. PeerHandle peer = e->torrentMan->getPeer();
  89. int newCuid = e->torrentMan->getNewCuid();
  90. peer->cuid = newCuid;
  91. PeerInitiateConnectionCommand* command =
  92. new PeerInitiateConnectionCommand(newCuid, peer, e);
  93. e->commands.push_back(command);
  94. logger->debug("CUID#%d - Adding new command CUID#%d", cuid, newCuid);
  95. }
  96. e->torrentMan->announceSuccess();
  97. e->torrentMan->resetAnnounce();
  98. e->segmentMan->init();
  99. } catch(Exception* err) {
  100. logger->error("CUID#%d - Error occurred while processing tracker response.", cuid, err);
  101. e->segmentMan->errors++;
  102. delete err;
  103. }
  104. if(trackerResponse) {
  105. delete [] trackerResponse;
  106. }
  107. if(e->torrentMan->isHalt()) {
  108. return true;
  109. } else {
  110. return prepareForRetry();
  111. }
  112. }