AnnounceList.cc 7.0 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 "AnnounceList.h"
  36. #include <algorithm>
  37. #include "A2STR.h"
  38. #include "SimpleRandomizer.h"
  39. #include "a2algo.h"
  40. namespace aria2 {
  41. AnnounceList::AnnounceList() : currentTrackerInitialized_(false) {}
  42. AnnounceList::AnnounceList(
  43. const std::vector<std::vector<std::string>>& announceList)
  44. : currentTrackerInitialized_(false)
  45. {
  46. reconfigure(announceList);
  47. }
  48. AnnounceList::AnnounceList(
  49. const std::deque<std::shared_ptr<AnnounceTier>>& announceTiers)
  50. : tiers_(announceTiers), currentTrackerInitialized_(false)
  51. {
  52. resetIterator();
  53. }
  54. AnnounceList::~AnnounceList() {}
  55. void AnnounceList::reconfigure(
  56. const std::vector<std::vector<std::string>>& announceList)
  57. {
  58. for (const auto& vec : announceList) {
  59. if (vec.empty()) {
  60. continue;
  61. }
  62. std::deque<std::string> uris(std::begin(vec), std::end(vec));
  63. auto tier = std::make_shared<AnnounceTier>(std::move(uris));
  64. tiers_.push_back(std::move(tier));
  65. }
  66. resetIterator();
  67. }
  68. void AnnounceList::reconfigure(const std::string& url)
  69. {
  70. std::deque<std::string> urls{url};
  71. tiers_.push_back(std::make_shared<AnnounceTier>(std::move(urls)));
  72. resetIterator();
  73. }
  74. void AnnounceList::resetIterator()
  75. {
  76. currentTier_ = std::begin(tiers_);
  77. if (currentTier_ != std::end(tiers_) && (*currentTier_)->urls.size()) {
  78. currentTracker_ = std::begin((*currentTier_)->urls);
  79. currentTrackerInitialized_ = true;
  80. }
  81. else {
  82. currentTrackerInitialized_ = false;
  83. }
  84. }
  85. std::string AnnounceList::getAnnounce() const
  86. {
  87. if (currentTrackerInitialized_) {
  88. return *currentTracker_;
  89. }
  90. else {
  91. return A2STR::NIL;
  92. }
  93. }
  94. void AnnounceList::announceSuccess()
  95. {
  96. if (currentTrackerInitialized_) {
  97. (*currentTier_)->nextEvent();
  98. auto url = *currentTracker_;
  99. (*currentTier_)->urls.erase(currentTracker_);
  100. (*currentTier_)->urls.push_front(std::move(url));
  101. currentTier_ = std::begin(tiers_);
  102. currentTracker_ = std::begin((*currentTier_)->urls);
  103. }
  104. }
  105. void AnnounceList::announceFailure()
  106. {
  107. if (currentTrackerInitialized_) {
  108. ++currentTracker_;
  109. if (currentTracker_ == std::end((*currentTier_)->urls)) {
  110. // force next event
  111. (*currentTier_)->nextEventIfAfterStarted();
  112. ++currentTier_;
  113. if (currentTier_ == std::end(tiers_)) {
  114. currentTrackerInitialized_ = false;
  115. }
  116. else {
  117. currentTracker_ = std::begin((*currentTier_)->urls);
  118. }
  119. }
  120. }
  121. }
  122. AnnounceTier::AnnounceEvent AnnounceList::getEvent() const
  123. {
  124. if (currentTrackerInitialized_) {
  125. return (*currentTier_)->event;
  126. }
  127. else {
  128. return AnnounceTier::STARTED;
  129. }
  130. }
  131. void AnnounceList::setEvent(AnnounceTier::AnnounceEvent event)
  132. {
  133. if (currentTrackerInitialized_) {
  134. (*currentTier_)->event = event;
  135. }
  136. }
  137. const char* AnnounceList::getEventString() const
  138. {
  139. if (currentTrackerInitialized_) {
  140. switch ((*currentTier_)->event) {
  141. case AnnounceTier::STARTED:
  142. case AnnounceTier::STARTED_AFTER_COMPLETION:
  143. return "started";
  144. case AnnounceTier::STOPPED:
  145. return "stopped";
  146. case AnnounceTier::COMPLETED:
  147. return "completed";
  148. default:
  149. return "";
  150. }
  151. }
  152. else {
  153. return "";
  154. }
  155. }
  156. namespace {
  157. class FindStoppedAllowedTier {
  158. public:
  159. bool operator()(const std::shared_ptr<AnnounceTier>& tier) const
  160. {
  161. switch (tier->event) {
  162. case AnnounceTier::DOWNLOADING:
  163. case AnnounceTier::STOPPED:
  164. case AnnounceTier::COMPLETED:
  165. case AnnounceTier::SEEDING:
  166. return true;
  167. default:
  168. return false;
  169. }
  170. }
  171. };
  172. } // namespace
  173. namespace {
  174. class FindCompletedAllowedTier {
  175. public:
  176. bool operator()(const std::shared_ptr<AnnounceTier>& tier) const
  177. {
  178. switch (tier->event) {
  179. case AnnounceTier::DOWNLOADING:
  180. case AnnounceTier::COMPLETED:
  181. return true;
  182. default:
  183. return false;
  184. }
  185. }
  186. };
  187. } // namespace
  188. size_t AnnounceList::countStoppedAllowedTier() const
  189. {
  190. return count_if(std::begin(tiers_), std::end(tiers_),
  191. FindStoppedAllowedTier());
  192. }
  193. size_t AnnounceList::countCompletedAllowedTier() const
  194. {
  195. return count_if(std::begin(tiers_), std::end(tiers_),
  196. FindCompletedAllowedTier());
  197. }
  198. void AnnounceList::setCurrentTier(
  199. std::deque<std::shared_ptr<AnnounceTier>>::iterator itr)
  200. {
  201. if (itr != std::end(tiers_)) {
  202. currentTier_ = std::move(itr);
  203. currentTracker_ = std::begin((*currentTier_)->urls);
  204. }
  205. }
  206. void AnnounceList::moveToStoppedAllowedTier()
  207. {
  208. auto itr = find_wrap_if(std::begin(tiers_), std::end(tiers_), currentTier_,
  209. FindStoppedAllowedTier());
  210. setCurrentTier(std::move(itr));
  211. }
  212. void AnnounceList::moveToCompletedAllowedTier()
  213. {
  214. auto itr = find_wrap_if(std::begin(tiers_), std::end(tiers_), currentTier_,
  215. FindCompletedAllowedTier());
  216. setCurrentTier(std::move(itr));
  217. }
  218. void AnnounceList::shuffle()
  219. {
  220. for (const auto& tier : tiers_) {
  221. auto& urls = tier->urls;
  222. std::shuffle(std::begin(urls), std::end(urls),
  223. *SimpleRandomizer::getInstance());
  224. }
  225. }
  226. bool AnnounceList::allTiersFailed() const
  227. {
  228. return currentTier_ == std::end(tiers_);
  229. }
  230. void AnnounceList::resetTier() { resetIterator(); }
  231. bool AnnounceList::currentTierAcceptsStoppedEvent() const
  232. {
  233. if (currentTrackerInitialized_) {
  234. return FindStoppedAllowedTier()(*currentTier_);
  235. }
  236. return false;
  237. }
  238. bool AnnounceList::currentTierAcceptsCompletedEvent() const
  239. {
  240. if (currentTrackerInitialized_) {
  241. return FindCompletedAllowedTier()(*currentTier_);
  242. }
  243. return false;
  244. }
  245. size_t AnnounceList::countTier() const { return tiers_.size(); }
  246. } // namespace aria2