AnnounceList.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "bencode.h"
  40. #include "util.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(const BDE& 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(const BDE& announceList)
  55. {
  56. if(announceList.isList()) {
  57. for(BDE::List::const_iterator itr = announceList.listBegin(),
  58. eoi = announceList.listEnd(); itr != eoi; ++itr) {
  59. const BDE& elemList = *itr;
  60. if(!elemList.isList()) {
  61. continue;
  62. }
  63. std::deque<std::string> urls;
  64. for(BDE::List::const_iterator elemItr = elemList.listBegin(),
  65. eoi2 = elemList.listEnd(); elemItr != eoi2; ++elemItr) {
  66. const BDE& data = *elemItr;
  67. if(data.isString()) {
  68. urls.push_back(data.s());
  69. }
  70. }
  71. if(!urls.empty()) {
  72. SharedHandle<AnnounceTier> tier(new AnnounceTier(urls));
  73. tiers.push_back(tier);
  74. }
  75. }
  76. resetIterator();
  77. }
  78. }
  79. void AnnounceList::reconfigure(const std::string& url) {
  80. std::deque<std::string> urls;
  81. urls.push_back(url);
  82. tiers.push_back(SharedHandle<AnnounceTier>(new AnnounceTier(urls)));
  83. resetIterator();
  84. }
  85. void AnnounceList::resetIterator() {
  86. currentTier = tiers.begin();
  87. if(currentTier != tiers.end() && (*currentTier)->urls.size()) {
  88. currentTracker = (*currentTier)->urls.begin();
  89. currentTrackerInitialized = true;
  90. } else {
  91. currentTrackerInitialized = false;
  92. }
  93. }
  94. std::string AnnounceList::getAnnounce() const {
  95. if(currentTrackerInitialized) {
  96. return *currentTracker;
  97. } else {
  98. return A2STR::NIL;
  99. }
  100. }
  101. void AnnounceList::announceSuccess() {
  102. if(currentTrackerInitialized) {
  103. (*currentTier)->nextEvent();
  104. std::string url = *currentTracker;
  105. (*currentTier)->urls.erase(currentTracker);
  106. (*currentTier)->urls.push_front(url);
  107. currentTier = tiers.begin();
  108. currentTracker = (*currentTier)->urls.begin();
  109. }
  110. }
  111. void AnnounceList::announceFailure() {
  112. if(currentTrackerInitialized) {
  113. ++currentTracker;
  114. if(currentTracker == (*currentTier)->urls.end()) {
  115. // force next event
  116. (*currentTier)->nextEventIfAfterStarted();
  117. ++currentTier;
  118. if(currentTier == tiers.end()) {
  119. currentTrackerInitialized = false;
  120. } else {
  121. currentTracker = (*currentTier)->urls.begin();
  122. }
  123. }
  124. }
  125. }
  126. AnnounceTier::AnnounceEvent AnnounceList::getEvent() const {
  127. if(currentTrackerInitialized) {
  128. return (*currentTier)->event;
  129. } else {
  130. return AnnounceTier::STARTED;
  131. }
  132. }
  133. void AnnounceList::setEvent(AnnounceTier::AnnounceEvent event) {
  134. if(currentTrackerInitialized) {
  135. (*currentTier)->event = event;
  136. }
  137. }
  138. std::string AnnounceList::getEventString() const {
  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 A2STR::NIL;
  150. }
  151. } else {
  152. return A2STR::NIL;
  153. }
  154. }
  155. class FindStoppedAllowedTier {
  156. public:
  157. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  158. switch(tier->event) {
  159. case AnnounceTier::DOWNLOADING:
  160. case AnnounceTier::STOPPED:
  161. case AnnounceTier::COMPLETED:
  162. case AnnounceTier::SEEDING:
  163. return true;
  164. default:
  165. return false;
  166. }
  167. }
  168. };
  169. class FindCompletedAllowedTier {
  170. public:
  171. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  172. switch(tier->event) {
  173. case AnnounceTier::DOWNLOADING:
  174. case AnnounceTier::COMPLETED:
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. };
  181. size_t AnnounceList::countStoppedAllowedTier() const {
  182. return count_if(tiers.begin(), tiers.end(), FindStoppedAllowedTier());
  183. }
  184. size_t AnnounceList::countCompletedAllowedTier() const {
  185. return count_if(tiers.begin(), tiers.end(), FindCompletedAllowedTier());
  186. }
  187. void AnnounceList::setCurrentTier
  188. (const std::deque<SharedHandle<AnnounceTier> >::iterator& itr) {
  189. if(itr != tiers.end()) {
  190. currentTier = itr;
  191. currentTracker = (*currentTier)->urls.begin();
  192. }
  193. }
  194. template<class InputIterator, class Predicate>
  195. InputIterator
  196. find_wrap_if(InputIterator first, InputIterator last,
  197. InputIterator current, Predicate pred) {
  198. InputIterator itr = std::find_if(current, last, pred);
  199. if(itr == last) {
  200. itr = std::find_if(first, current, pred);
  201. }
  202. return itr;
  203. }
  204. void AnnounceList::moveToStoppedAllowedTier() {
  205. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  206. find_wrap_if(tiers.begin(), tiers.end(),
  207. currentTier,
  208. FindStoppedAllowedTier());
  209. setCurrentTier(itr);
  210. }
  211. void AnnounceList::moveToCompletedAllowedTier() {
  212. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  213. find_wrap_if(tiers.begin(), tiers.end(),
  214. currentTier,
  215. FindCompletedAllowedTier());
  216. setCurrentTier(itr);
  217. }
  218. void AnnounceList::shuffle() {
  219. for(std::deque<SharedHandle<AnnounceTier> >::const_iterator itr =
  220. tiers.begin(), eoi = tiers.end(); itr != eoi; ++itr) {
  221. std::deque<std::string>& urls = (*itr)->urls;
  222. std::random_shuffle(urls.begin(), urls.end(),
  223. *(SimpleRandomizer::getInstance().get()));
  224. }
  225. }
  226. bool AnnounceList::allTiersFailed() const
  227. {
  228. return currentTier == tiers.end();
  229. }
  230. void AnnounceList::resetTier()
  231. {
  232. resetIterator();
  233. }
  234. bool AnnounceList::currentTierAcceptsStoppedEvent() const
  235. {
  236. if(currentTrackerInitialized) {
  237. return FindStoppedAllowedTier()(*currentTier);
  238. } else {
  239. return false;
  240. }
  241. }
  242. bool AnnounceList::currentTierAcceptsCompletedEvent() const
  243. {
  244. if(currentTrackerInitialized) {
  245. return FindCompletedAllowedTier()(*currentTier);
  246. } else {
  247. return false;
  248. }
  249. }
  250. } // namespace aria2