TrackerWatcherCommand.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "TrackerWatcherCommand.h"
  36. #include "DownloadEngine.h"
  37. #include "Util.h"
  38. #include "prefs.h"
  39. #include "message.h"
  40. #include "SingleFileDownloadContext.h"
  41. #include "ByteArrayDiskWriterFactory.h"
  42. #include "RecoverableException.h"
  43. #include "CUIDCounter.h"
  44. #include "PeerInitiateConnectionCommand.h"
  45. #include "DiskAdaptor.h"
  46. #include "RequestGroup.h"
  47. #include "Option.h"
  48. #include "DlRetryEx.h"
  49. #include "DlAbortEx.h"
  50. TrackerWatcherCommand::TrackerWatcherCommand(int32_t cuid,
  51. RequestGroup* requestGroup,
  52. DownloadEngine* e,
  53. const BtContextHandle& btContext):
  54. Command(cuid),
  55. BtContextAwareCommand(btContext),
  56. RequestGroupAware(requestGroup),
  57. e(e),
  58. _trackerRequestGroup(0)
  59. {
  60. }
  61. TrackerWatcherCommand::~TrackerWatcherCommand() {}
  62. bool TrackerWatcherCommand::execute() {
  63. if(_requestGroup->isForceHaltRequested()) {
  64. if(_trackerRequestGroup.isNull()) {
  65. return true;
  66. } else if(_trackerRequestGroup->getNumCommand() == 0 ||
  67. _trackerRequestGroup->downloadFinished()) {
  68. return true;
  69. } else {
  70. _trackerRequestGroup->setForceHaltRequested(true);
  71. return false;
  72. }
  73. }
  74. if(btAnnounce->noMoreAnnounce()) {
  75. logger->debug("no more announce");
  76. return true;
  77. }
  78. if(_trackerRequestGroup.isNull()) {
  79. _trackerRequestGroup = createAnnounce();
  80. if(!_trackerRequestGroup.isNull()) {
  81. e->addCommand(_trackerRequestGroup->createInitialCommand(e));
  82. logger->debug("added tracker request command");
  83. }
  84. } else if(_trackerRequestGroup->downloadFinished()){
  85. try {
  86. string trackerResponse = getTrackerResponse(_trackerRequestGroup);
  87. processTrackerResponse(trackerResponse);
  88. btAnnounce->announceSuccess();
  89. btAnnounce->resetAnnounce();
  90. } catch(RecoverableException* ex) {
  91. logger->error(EX_EXCEPTION_CAUGHT, ex);
  92. delete ex;
  93. btAnnounce->announceFailure();
  94. if(btAnnounce->isAllAnnounceFailed()) {
  95. btAnnounce->resetAnnounce();
  96. }
  97. }
  98. _trackerRequestGroup = 0;
  99. } else if(_trackerRequestGroup->getNumCommand() == 0){
  100. // handle errors here
  101. btAnnounce->announceFailure(); // inside it, trackers = 0.
  102. _trackerRequestGroup = 0;
  103. if(btAnnounce->isAllAnnounceFailed()) {
  104. btAnnounce->resetAnnounce();
  105. }
  106. }
  107. e->commands.push_back(this);
  108. return false;
  109. }
  110. string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& requestGroup)
  111. {
  112. stringstream strm;
  113. char data[2048];
  114. requestGroup->getPieceStorage()->getDiskAdaptor()->openFile();
  115. while(1) {
  116. int32_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData((unsigned char*)data, sizeof(data), strm.tellp());
  117. strm.write(data, dataLength);
  118. if(dataLength == 0) {
  119. break;
  120. }
  121. }
  122. return strm.str();
  123. }
  124. // TODO we have to deal with the exception thrown By BtAnnounce
  125. void TrackerWatcherCommand::processTrackerResponse(const string& trackerResponse)
  126. {
  127. btAnnounce->processAnnounceResponse(trackerResponse.c_str(),
  128. trackerResponse.size());
  129. while(!btRuntime->isHalt() && btRuntime->lessThanMinPeer()) {
  130. PeerHandle peer = peerStorage->getUnusedPeer();
  131. if(peer.isNull()) {
  132. break;
  133. }
  134. peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
  135. PeerInitiateConnectionCommand* command =
  136. new PeerInitiateConnectionCommand(peer->cuid,
  137. _requestGroup,
  138. peer,
  139. e,
  140. btContext);
  141. e->commands.push_back(command);
  142. logger->debug("CUID#%d - Adding new command CUID#%d", cuid, peer->cuid);
  143. }
  144. }
  145. RequestGroupHandle TrackerWatcherCommand::createAnnounce() {
  146. RequestGroupHandle rg = 0;
  147. if(btAnnounce->isAnnounceReady()) {
  148. rg = createRequestGroup(btAnnounce->getAnnounceUrl());
  149. btAnnounce->announceStart(); // inside it, trackers++.
  150. }
  151. return rg;
  152. }
  153. RequestGroupHandle
  154. TrackerWatcherCommand::createRequestGroup(const string& uri)
  155. {
  156. Strings uris;
  157. uris.push_back(uri);
  158. RequestGroupHandle rg = new RequestGroup(e->option, uris);
  159. SingleFileDownloadContextHandle dctx =
  160. new SingleFileDownloadContext(e->option->getAsInt(PREF_SEGMENT_SIZE),
  161. 0,
  162. "",
  163. "[tracker.announce]");
  164. dctx->setDir("");
  165. rg->setDownloadContext(dctx);
  166. rg->setDiskWriterFactory(new ByteArrayDiskWriterFactory());
  167. rg->setFileAllocationEnabled(false);
  168. rg->setPreLocalFileCheckEnabled(false);
  169. logger->info("Creating tracker request group GID#%d", rg->getGID());
  170. return rg;
  171. }