AnnounceList.cc 7.6 KB

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