AnnounceList.cc 7.2 KB

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