AnnounceList.cc 7.4 KB

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