AnnounceList.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "util.h"
  40. namespace aria2 {
  41. const std::string AnnounceList::STARTED("started");
  42. const std::string AnnounceList::STOPPED("stopped");
  43. const std::string AnnounceList::COMPLETED("completed");
  44. AnnounceList::AnnounceList
  45. (const std::vector<std::vector<std::string> >& announceList):
  46. currentTrackerInitialized_(false) {
  47. reconfigure(announceList);
  48. }
  49. AnnounceList::AnnounceList
  50. (const std::deque<SharedHandle<AnnounceTier> >& announceTiers):
  51. tiers_(announceTiers), currentTrackerInitialized_(false) {
  52. resetIterator();
  53. }
  54. void AnnounceList::reconfigure
  55. (const std::vector<std::vector<std::string> >& announceList)
  56. {
  57. for(std::vector<std::vector<std::string> >::const_iterator itr =
  58. announceList.begin(), eoi = announceList.end(); itr != eoi; ++itr) {
  59. if((*itr).empty()) {
  60. continue;
  61. }
  62. std::deque<std::string> urls((*itr).begin(), (*itr).end());
  63. SharedHandle<AnnounceTier> tier(new AnnounceTier(urls));
  64. tiers_.push_back(tier);
  65. }
  66. resetIterator();
  67. }
  68. void AnnounceList::reconfigure(const std::string& url) {
  69. std::deque<std::string> urls;
  70. urls.push_back(url);
  71. tiers_.push_back(SharedHandle<AnnounceTier>(new AnnounceTier(urls)));
  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. std::string 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 A2STR::NIL;
  139. }
  140. } else {
  141. return A2STR::NIL;
  142. }
  143. }
  144. class FindStoppedAllowedTier {
  145. public:
  146. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  147. switch(tier->event) {
  148. case AnnounceTier::DOWNLOADING:
  149. case AnnounceTier::STOPPED:
  150. case AnnounceTier::COMPLETED:
  151. case AnnounceTier::SEEDING:
  152. return true;
  153. default:
  154. return false;
  155. }
  156. }
  157. };
  158. class FindCompletedAllowedTier {
  159. public:
  160. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  161. switch(tier->event) {
  162. case AnnounceTier::DOWNLOADING:
  163. case AnnounceTier::COMPLETED:
  164. return true;
  165. default:
  166. return false;
  167. }
  168. }
  169. };
  170. size_t AnnounceList::countStoppedAllowedTier() const {
  171. return count_if(tiers_.begin(), tiers_.end(), FindStoppedAllowedTier());
  172. }
  173. size_t AnnounceList::countCompletedAllowedTier() const {
  174. return count_if(tiers_.begin(), tiers_.end(), FindCompletedAllowedTier());
  175. }
  176. void AnnounceList::setCurrentTier
  177. (const std::deque<SharedHandle<AnnounceTier> >::iterator& itr) {
  178. if(itr != tiers_.end()) {
  179. currentTier_ = itr;
  180. currentTracker_ = (*currentTier_)->urls.begin();
  181. }
  182. }
  183. template<class InputIterator, class Predicate>
  184. InputIterator
  185. find_wrap_if(InputIterator first, InputIterator last,
  186. InputIterator current, Predicate pred) {
  187. InputIterator itr = std::find_if(current, last, pred);
  188. if(itr == last) {
  189. itr = std::find_if(first, current, pred);
  190. }
  191. return itr;
  192. }
  193. void AnnounceList::moveToStoppedAllowedTier() {
  194. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  195. find_wrap_if(tiers_.begin(), tiers_.end(),
  196. currentTier_,
  197. FindStoppedAllowedTier());
  198. setCurrentTier(itr);
  199. }
  200. void AnnounceList::moveToCompletedAllowedTier() {
  201. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  202. find_wrap_if(tiers_.begin(), tiers_.end(),
  203. currentTier_,
  204. FindCompletedAllowedTier());
  205. setCurrentTier(itr);
  206. }
  207. void AnnounceList::shuffle() {
  208. for(std::deque<SharedHandle<AnnounceTier> >::const_iterator itr =
  209. tiers_.begin(), eoi = tiers_.end(); itr != eoi; ++itr) {
  210. std::deque<std::string>& urls = (*itr)->urls;
  211. std::random_shuffle(urls.begin(), urls.end(),
  212. *(SimpleRandomizer::getInstance().get()));
  213. }
  214. }
  215. bool AnnounceList::allTiersFailed() const
  216. {
  217. return currentTier_ == tiers_.end();
  218. }
  219. void AnnounceList::resetTier()
  220. {
  221. resetIterator();
  222. }
  223. bool AnnounceList::currentTierAcceptsStoppedEvent() const
  224. {
  225. if(currentTrackerInitialized_) {
  226. return FindStoppedAllowedTier()(*currentTier_);
  227. } else {
  228. return false;
  229. }
  230. }
  231. bool AnnounceList::currentTierAcceptsCompletedEvent() const
  232. {
  233. if(currentTrackerInitialized_) {
  234. return FindCompletedAllowedTier()(*currentTier_);
  235. } else {
  236. return false;
  237. }
  238. }
  239. } // namespace aria2