AnnounceList.cc 7.2 KB

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