DefaultBtAnnounce.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "DefaultBtAnnounce.h"
  36. #include "BtRegistry.h"
  37. #include "LogFactory.h"
  38. #include "MetaFileUtil.h"
  39. #include "Dictionary.h"
  40. #include "List.h"
  41. #include "Data.h"
  42. #include "DelegatingPeerListProcessor.h"
  43. #include "Util.h"
  44. #include "prefs.h"
  45. #include "DlAbortEx.h"
  46. #include "message.h"
  47. DefaultBtAnnounce::DefaultBtAnnounce(BtContextHandle btContext,
  48. const Option* option):
  49. btContext(btContext),
  50. trackers(0),
  51. interval(DEFAULT_ANNOUNCE_INTERVAL),
  52. minInterval(DEFAULT_ANNOUNCE_INTERVAL),
  53. complete(0),
  54. incomplete(0),
  55. announceList(btContext->getAnnounceTiers()),
  56. trackerNumTry(0),
  57. option(option),
  58. btRuntime(BT_RUNTIME(btContext)),
  59. pieceStorage(PIECE_STORAGE(btContext)),
  60. peerStorage(PEER_STORAGE(btContext))
  61. {
  62. prevAnnounceTime.setTimeInSec(0);
  63. key = generateKey();
  64. logger = LogFactory::getInstance();
  65. }
  66. DefaultBtAnnounce::~DefaultBtAnnounce() {
  67. }
  68. string DefaultBtAnnounce::generateKey() const {
  69. return Util::randomAlpha(8);
  70. }
  71. bool DefaultBtAnnounce::isDefaultAnnounceReady() {
  72. return (trackers == 0 && prevAnnounceTime.elapsed(minInterval));
  73. }
  74. bool DefaultBtAnnounce::isStoppedAnnounceReady() {
  75. return (trackers == 0 &&
  76. btRuntime->isHalt() &&
  77. announceList.countStoppedAllowedTier());
  78. }
  79. bool DefaultBtAnnounce::isCompletedAnnounceReady() {
  80. return (trackers == 0 &&
  81. pieceStorage->allDownloadFinished() &&
  82. announceList.countCompletedAllowedTier());
  83. }
  84. bool DefaultBtAnnounce::isAnnounceReady() {
  85. return
  86. isStoppedAnnounceReady() ||
  87. isCompletedAnnounceReady() ||
  88. isDefaultAnnounceReady();
  89. }
  90. string DefaultBtAnnounce::getAnnounceUrl() {
  91. if(isStoppedAnnounceReady()) {
  92. announceList.moveToStoppedAllowedTier();
  93. announceList.setEvent(AnnounceTier::STOPPED);
  94. } else if(isCompletedAnnounceReady()) {
  95. announceList.moveToCompletedAllowedTier();
  96. announceList.setEvent(AnnounceTier::COMPLETED);
  97. } else if(isDefaultAnnounceReady()) {
  98. // If download completed before "started" event is sent to a tracker,
  99. // we change the event to something else to prevent us from
  100. // sending "completed" event.
  101. if(pieceStorage->allDownloadFinished() &&
  102. announceList.getEvent() == AnnounceTier::STARTED) {
  103. announceList.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION);
  104. }
  105. }
  106. int numWant = 50;
  107. if(!btRuntime->lessThanEqMinPeer() ||
  108. btRuntime->isHalt()) {
  109. numWant = 0;
  110. }
  111. TransferStat stat = peerStorage->calculateStat();
  112. long long int left = pieceStorage->getTotalLength()-pieceStorage->getCompletedLength();
  113. if(left < 0) {
  114. left = 0;
  115. }
  116. string url = announceList.getAnnounce()+"?"+
  117. "info_hash="+Util::torrentUrlencode(btContext->getInfoHash(),
  118. btContext->getInfoHashLength())+"&"+
  119. "peer_id="+Util::torrentUrlencode(btContext->getPeerId(), 20)+"&"+
  120. "port="+Util::itos(btRuntime->getListenPort())+"&"+
  121. "uploaded="+Util::llitos(stat.getSessionUploadLength())+"&"+
  122. "downloaded="+Util::llitos(stat.getSessionDownloadLength())+"&"+
  123. "left="+Util::llitos(left)+"&"+
  124. "compact=1"+"&"+
  125. "key="+key+"&"+
  126. "numwant="+Util::itos(numWant)+"&"+
  127. "no_peer_id=1";
  128. string event = announceList.getEventString();
  129. if(!event.empty()) {
  130. url += string("&")+"event="+event;
  131. }
  132. if(!trackerId.empty()) {
  133. url += string("&")+"trackerid="+Util::torrentUrlencode((const unsigned char*)trackerId.c_str(),
  134. trackerId.size());
  135. }
  136. return url;
  137. }
  138. void DefaultBtAnnounce::announceStart() {
  139. trackers++;
  140. }
  141. void DefaultBtAnnounce::announceSuccess() {
  142. trackers = 0;
  143. announceList.announceSuccess();
  144. }
  145. void DefaultBtAnnounce::announceFailure() {
  146. trackers = 0;
  147. trackerNumTry++;
  148. announceList.announceFailure();
  149. }
  150. bool DefaultBtAnnounce::isAllAnnounceFailed() {
  151. return
  152. trackerNumTry >= option->getAsInt(PREF_TRACKER_MAX_TRIES);
  153. }
  154. void DefaultBtAnnounce::resetAnnounce() {
  155. prevAnnounceTime.reset();
  156. trackerNumTry = 0;
  157. }
  158. void
  159. DefaultBtAnnounce::processAnnounceResponse(const char* trackerResponse,
  160. size_t trackerResponseLength)
  161. {
  162. SharedHandle<MetaEntry> entry(MetaFileUtil::bdecoding(trackerResponse,
  163. trackerResponseLength));
  164. Dictionary* response = (Dictionary*)entry.get();
  165. Data* failureReasonData = (Data*)response->get("failure reason");
  166. if(failureReasonData) {
  167. throw new DlAbortEx("Tracker returned failure reason: %s",
  168. failureReasonData->toString().c_str());
  169. }
  170. Data* warningMessageData = (Data*)response->get("warning message");
  171. if(warningMessageData) {
  172. logger->warn(MSG_TRACKER_WARNING_MESSAGE,
  173. warningMessageData->toString().c_str());
  174. }
  175. Data* trackerIdData = (Data*)response->get("tracker id");
  176. if(trackerIdData) {
  177. trackerId = trackerIdData->toString();
  178. logger->debug("Tracker ID:%s", trackerId.c_str());
  179. }
  180. Data* intervalData = (Data*)response->get("interval");
  181. if(intervalData) {
  182. interval = intervalData->toInt();
  183. logger->debug("Interval:%d", interval);
  184. }
  185. Data* minIntervalData = (Data*)response->get("min interval");
  186. if(minIntervalData) {
  187. minInterval = minIntervalData->toInt();
  188. logger->debug("Min interval:%d", minInterval);
  189. }
  190. if(minInterval > interval) {
  191. minInterval = interval;
  192. }
  193. Data* completeData = (Data*)response->get("complete");
  194. if(completeData) {
  195. complete = completeData->toInt();
  196. logger->debug("Complete:%d", complete);
  197. }
  198. Data* incompleteData = (Data*)response->get("incomplete");
  199. if(incompleteData) {
  200. incomplete = incompleteData->toInt();
  201. logger->debug("Incomplete:%d", incomplete);
  202. }
  203. const MetaEntry* peersEntry = response->get("peers");
  204. if(peersEntry &&
  205. !btRuntime->isHalt() &&
  206. btRuntime->lessThanMinPeer()) {
  207. DelegatingPeerListProcessor proc(btContext->getPieceLength(),
  208. btContext->getTotalLength());
  209. Peers peers = proc.extractPeer(peersEntry);
  210. peerStorage->addPeer(peers);
  211. }
  212. if(!peersEntry) {
  213. logger->info("No peer list received.");
  214. }
  215. }
  216. bool DefaultBtAnnounce::noMoreAnnounce() {
  217. return (trackers == 0 &&
  218. btRuntime->isHalt() &&
  219. !announceList.countStoppedAllowedTier());
  220. }
  221. void DefaultBtAnnounce::shuffleAnnounce() {
  222. announceList.shuffle();
  223. }