TrackerWatcherCommand.cc 6.9 KB

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