TrackerWatcherCommand.cc 8.6 KB

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