TrackerWatcherCommand.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. std::vector<Command*> commands;
  102. try {
  103. _trackerRequestGroup->createInitialCommand(commands, _e);
  104. } catch(RecoverableException& ex) {
  105. getLogger()->error(EX_EXCEPTION_CAUGHT, ex);
  106. std::for_each(commands.begin(), commands.end(), Deleter());
  107. commands.clear();
  108. }
  109. _e->addCommand(commands);
  110. if(getLogger()->debug()) {
  111. getLogger()->debug("added tracker request command");
  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. const BDE& announceList =
  193. context->getAttribute(bittorrent::BITTORRENT)[bittorrent::ANNOUNCE_LIST];
  194. if(announceList.size() >= 2) {
  195. return true;
  196. }
  197. if(announceList.empty()) {
  198. return false;
  199. }
  200. if(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. // If backup tracker is available, try 2 times for each tracker
  222. // and if they all fails, then try next one.
  223. rg->getOption()->put(PREF_MAX_TRIES, "2");
  224. // TODO When dry-run mode becomes available in BitTorrent, set
  225. // PREF_DRY_RUN=false too.
  226. rg->getOption()->put(PREF_USE_HEAD, V_FALSE);
  227. // Setting tracker timeouts
  228. rg->setTimeout(rg->getOption()->getAsInt(PREF_BT_TRACKER_TIMEOUT));
  229. rg->getOption()->put(PREF_CONNECT_TIMEOUT,
  230. rg->getOption()->get(PREF_BT_TRACKER_CONNECT_TIMEOUT));
  231. static const std::string TRACKER_ANNOUNCE_FILE("[tracker.announce]");
  232. SharedHandle<DownloadContext> dctx
  233. (new DownloadContext(getOption()->getAsInt(PREF_SEGMENT_SIZE),
  234. 0,
  235. TRACKER_ANNOUNCE_FILE));
  236. dctx->setDir(A2STR::NIL);
  237. dctx->getFileEntries().front()->setUris(uris);
  238. rg->setDownloadContext(dctx);
  239. SharedHandle<DiskWriterFactory> dwf(new ByteArrayDiskWriterFactory());
  240. rg->setDiskWriterFactory(dwf);
  241. rg->setFileAllocationEnabled(false);
  242. rg->setPreLocalFileCheckEnabled(false);
  243. util::removeMetalinkContentTypes(rg);
  244. if(getLogger()->info()) {
  245. getLogger()->info("Creating tracker request group GID#%s",
  246. util::itos(rg->getGID()).c_str());
  247. }
  248. return rg;
  249. }
  250. void TrackerWatcherCommand::setBtRuntime
  251. (const SharedHandle<BtRuntime>& btRuntime)
  252. {
  253. _btRuntime = btRuntime;
  254. }
  255. void TrackerWatcherCommand::setPeerStorage
  256. (const SharedHandle<PeerStorage>& peerStorage)
  257. {
  258. _peerStorage = peerStorage;
  259. }
  260. void TrackerWatcherCommand::setPieceStorage
  261. (const SharedHandle<PieceStorage>& pieceStorage)
  262. {
  263. _pieceStorage = pieceStorage;
  264. }
  265. void TrackerWatcherCommand::setBtAnnounce
  266. (const SharedHandle<BtAnnounce>& btAnnounce)
  267. {
  268. _btAnnounce = btAnnounce;
  269. }
  270. const SharedHandle<Option>& TrackerWatcherCommand::getOption() const
  271. {
  272. return _requestGroup->getOption();
  273. }
  274. } // namespace aria2