TrackerWatcherCommand.cc 6.4 KB

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