TrackerWatcherCommand.cc 9.3 KB

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