TrackerWatcherCommand.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "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 "ByteArrayDiskWriterFactory.h"
  46. #include "RecoverableException.h"
  47. #include "PeerInitiateConnectionCommand.h"
  48. #include "DiskAdaptor.h"
  49. #include "FileEntry.h"
  50. #include "RequestGroup.h"
  51. #include "Option.h"
  52. #include "DlAbortEx.h"
  53. #include "Logger.h"
  54. #include "LogFactory.h"
  55. #include "A2STR.h"
  56. #include "SocketCore.h"
  57. #include "Request.h"
  58. #include "AnnounceTier.h"
  59. #include "DownloadContext.h"
  60. #include "bittorrent_helper.h"
  61. #include "a2functional.h"
  62. #include "util.h"
  63. #include "fmt.h"
  64. namespace aria2 {
  65. TrackerWatcherCommand::TrackerWatcherCommand
  66. (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e)
  67. : Command(cuid),
  68. requestGroup_(requestGroup),
  69. e_(e)
  70. {
  71. requestGroup_->increaseNumCommand();
  72. }
  73. TrackerWatcherCommand::~TrackerWatcherCommand()
  74. {
  75. requestGroup_->decreaseNumCommand();
  76. }
  77. bool TrackerWatcherCommand::execute() {
  78. if(requestGroup_->isForceHaltRequested()) {
  79. if(!trackerRequestGroup_) {
  80. return true;
  81. } else if(trackerRequestGroup_->getNumCommand() == 0 ||
  82. trackerRequestGroup_->downloadFinished()) {
  83. return true;
  84. } else {
  85. trackerRequestGroup_->setForceHaltRequested(true);
  86. e_->addCommand(this);
  87. return false;
  88. }
  89. }
  90. if(btAnnounce_->noMoreAnnounce()) {
  91. A2_LOG_DEBUG("no more announce");
  92. return true;
  93. }
  94. if(!trackerRequestGroup_) {
  95. trackerRequestGroup_ = createAnnounce();
  96. if(trackerRequestGroup_) {
  97. try {
  98. std::vector<Command*>* commands = new std::vector<Command*>();
  99. auto_delete_container<std::vector<Command*> > commandsDel(commands);
  100. trackerRequestGroup_->createInitialCommand(*commands, e_);
  101. e_->addCommand(*commands);
  102. commands->clear();
  103. A2_LOG_DEBUG("added tracker request command");
  104. } catch(RecoverableException& ex) {
  105. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
  106. }
  107. }
  108. } else if(trackerRequestGroup_->downloadFinished()){
  109. try {
  110. std::string trackerResponse = getTrackerResponse(trackerRequestGroup_);
  111. processTrackerResponse(trackerResponse);
  112. btAnnounce_->announceSuccess();
  113. btAnnounce_->resetAnnounce();
  114. } catch(RecoverableException& ex) {
  115. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
  116. btAnnounce_->announceFailure();
  117. if(btAnnounce_->isAllAnnounceFailed()) {
  118. btAnnounce_->resetAnnounce();
  119. }
  120. }
  121. trackerRequestGroup_.reset();
  122. } else if(trackerRequestGroup_->getNumCommand() == 0){
  123. // handle errors here
  124. btAnnounce_->announceFailure(); // inside it, trackers = 0.
  125. trackerRequestGroup_.reset();
  126. if(btAnnounce_->isAllAnnounceFailed()) {
  127. btAnnounce_->resetAnnounce();
  128. }
  129. }
  130. e_->addCommand(this);
  131. return false;
  132. }
  133. std::string TrackerWatcherCommand::getTrackerResponse
  134. (const SharedHandle<RequestGroup>& requestGroup)
  135. {
  136. std::stringstream strm;
  137. unsigned char data[2048];
  138. requestGroup->getPieceStorage()->getDiskAdaptor()->openFile();
  139. while(1) {
  140. ssize_t dataLength = requestGroup->getPieceStorage()->
  141. getDiskAdaptor()->readData(data, sizeof(data), strm.tellp());
  142. if(dataLength == 0) {
  143. break;
  144. }
  145. strm.write(reinterpret_cast<const char*>(data), dataLength);
  146. }
  147. return strm.str();
  148. }
  149. // TODO we have to deal with the exception thrown By BtAnnounce
  150. void TrackerWatcherCommand::processTrackerResponse
  151. (const std::string& trackerResponse)
  152. {
  153. btAnnounce_->processAnnounceResponse
  154. (reinterpret_cast<const unsigned char*>(trackerResponse.c_str()),
  155. trackerResponse.size());
  156. while(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
  157. SharedHandle<Peer> peer = peerStorage_->getUnusedPeer();
  158. if(!peer) {
  159. break;
  160. }
  161. peer->usedBy(e_->newCUID());
  162. PeerInitiateConnectionCommand* command =
  163. new PeerInitiateConnectionCommand
  164. (peer->usedBy(), requestGroup_, peer, e_, btRuntime_);
  165. command->setPeerStorage(peerStorage_);
  166. command->setPieceStorage(pieceStorage_);
  167. e_->addCommand(command);
  168. A2_LOG_DEBUG(fmt("CUID#%lld - Adding new command CUID#%s",
  169. getCuid(),
  170. util::itos(peer->usedBy()).c_str()));
  171. }
  172. }
  173. SharedHandle<RequestGroup> TrackerWatcherCommand::createAnnounce() {
  174. SharedHandle<RequestGroup> rg;
  175. if(btAnnounce_->isAnnounceReady()) {
  176. rg = createRequestGroup(btAnnounce_->getAnnounceUrl());
  177. btAnnounce_->announceStart(); // inside it, trackers++.
  178. }
  179. return rg;
  180. }
  181. namespace {
  182. bool backupTrackerIsAvailable
  183. (const SharedHandle<DownloadContext>& context)
  184. {
  185. SharedHandle<TorrentAttribute> torrentAttrs =
  186. bittorrent::getTorrentAttrs(context);
  187. if(torrentAttrs->announceList.size() >= 2) {
  188. return true;
  189. }
  190. if(torrentAttrs->announceList.empty()) {
  191. return false;
  192. }
  193. if(torrentAttrs->announceList[0].size() >= 2) {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. } // namespace
  200. SharedHandle<RequestGroup>
  201. TrackerWatcherCommand::createRequestGroup(const std::string& uri)
  202. {
  203. std::vector<std::string> uris;
  204. uris.push_back(uri);
  205. SharedHandle<RequestGroup> rg(new RequestGroup(getOption()));
  206. if(backupTrackerIsAvailable(requestGroup_->getDownloadContext())) {
  207. A2_LOG_DEBUG("This is multi-tracker announce.");
  208. } else {
  209. A2_LOG_DEBUG("This is single-tracker announce.");
  210. }
  211. rg->setNumConcurrentCommand(1);
  212. // If backup tracker is available, try 2 times for each tracker
  213. // and if they all fails, then try next one.
  214. rg->getOption()->put(PREF_MAX_TRIES, "2");
  215. // TODO When dry-run mode becomes available in BitTorrent, set
  216. // PREF_DRY_RUN=false too.
  217. rg->getOption()->put(PREF_USE_HEAD, A2_V_FALSE);
  218. // Setting tracker timeouts
  219. rg->setTimeout(rg->getOption()->getAsInt(PREF_BT_TRACKER_TIMEOUT));
  220. rg->getOption()->put(PREF_CONNECT_TIMEOUT,
  221. rg->getOption()->get(PREF_BT_TRACKER_CONNECT_TIMEOUT));
  222. rg->getOption()->put(PREF_REUSE_URI, A2_V_FALSE);
  223. rg->getOption()->put(PREF_SELECT_LEAST_USED_HOST, A2_V_FALSE);
  224. static const std::string TRACKER_ANNOUNCE_FILE("[tracker.announce]");
  225. SharedHandle<DownloadContext> dctx
  226. (new DownloadContext(getOption()->getAsInt(PREF_SEGMENT_SIZE),
  227. 0,
  228. TRACKER_ANNOUNCE_FILE));
  229. dctx->getFileEntries().front()->setUris(uris);
  230. rg->setDownloadContext(dctx);
  231. SharedHandle<DiskWriterFactory> dwf(new ByteArrayDiskWriterFactory());
  232. rg->setDiskWriterFactory(dwf);
  233. rg->setFileAllocationEnabled(false);
  234. rg->setPreLocalFileCheckEnabled(false);
  235. util::removeMetalinkContentTypes(rg);
  236. A2_LOG_INFO(fmt("Creating tracker request group GID#%s",
  237. util::itos(rg->getGID()).c_str()));
  238. return rg;
  239. }
  240. void TrackerWatcherCommand::setBtRuntime
  241. (const SharedHandle<BtRuntime>& btRuntime)
  242. {
  243. btRuntime_ = btRuntime;
  244. }
  245. void TrackerWatcherCommand::setPeerStorage
  246. (const SharedHandle<PeerStorage>& peerStorage)
  247. {
  248. peerStorage_ = peerStorage;
  249. }
  250. void TrackerWatcherCommand::setPieceStorage
  251. (const SharedHandle<PieceStorage>& pieceStorage)
  252. {
  253. pieceStorage_ = pieceStorage;
  254. }
  255. void TrackerWatcherCommand::setBtAnnounce
  256. (const SharedHandle<BtAnnounce>& btAnnounce)
  257. {
  258. btAnnounce_ = btAnnounce;
  259. }
  260. const SharedHandle<Option>& TrackerWatcherCommand::getOption() const
  261. {
  262. return requestGroup_->getOption();
  263. }
  264. } // namespace aria2