TrackerWatcherCommand.cc 5.6 KB

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