TrackerWatcherCommand.cc 8.4 KB

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