AnnounceList.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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
  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. class FindStoppedAllowedTier {
  146. public:
  147. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  148. switch(tier->event) {
  149. case AnnounceTier::DOWNLOADING:
  150. case AnnounceTier::STOPPED:
  151. case AnnounceTier::COMPLETED:
  152. case AnnounceTier::SEEDING:
  153. return true;
  154. default:
  155. return false;
  156. }
  157. }
  158. };
  159. class FindCompletedAllowedTier {
  160. public:
  161. bool operator()(const SharedHandle<AnnounceTier>& tier) const {
  162. switch(tier->event) {
  163. case AnnounceTier::DOWNLOADING:
  164. case AnnounceTier::COMPLETED:
  165. return true;
  166. default:
  167. return false;
  168. }
  169. }
  170. };
  171. size_t AnnounceList::countStoppedAllowedTier() const {
  172. return count_if(_tiers.begin(), _tiers.end(), FindStoppedAllowedTier());
  173. }
  174. size_t AnnounceList::countCompletedAllowedTier() const {
  175. return count_if(_tiers.begin(), _tiers.end(), FindCompletedAllowedTier());
  176. }
  177. void AnnounceList::setCurrentTier
  178. (const std::deque<SharedHandle<AnnounceTier> >::iterator& itr) {
  179. if(itr != _tiers.end()) {
  180. _currentTier = itr;
  181. _currentTracker = (*_currentTier)->urls.begin();
  182. }
  183. }
  184. template<class InputIterator, class Predicate>
  185. InputIterator
  186. find_wrap_if(InputIterator first, InputIterator last,
  187. InputIterator current, Predicate pred) {
  188. InputIterator itr = std::find_if(current, last, pred);
  189. if(itr == last) {
  190. itr = std::find_if(first, current, pred);
  191. }
  192. return itr;
  193. }
  194. void AnnounceList::moveToStoppedAllowedTier() {
  195. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  196. find_wrap_if(_tiers.begin(), _tiers.end(),
  197. _currentTier,
  198. FindStoppedAllowedTier());
  199. setCurrentTier(itr);
  200. }
  201. void AnnounceList::moveToCompletedAllowedTier() {
  202. std::deque<SharedHandle<AnnounceTier> >::iterator itr =
  203. find_wrap_if(_tiers.begin(), _tiers.end(),
  204. _currentTier,
  205. FindCompletedAllowedTier());
  206. setCurrentTier(itr);
  207. }
  208. void AnnounceList::shuffle() {
  209. for(std::deque<SharedHandle<AnnounceTier> >::const_iterator itr =
  210. _tiers.begin(), eoi = _tiers.end(); itr != eoi; ++itr) {
  211. std::deque<std::string>& urls = (*itr)->urls;
  212. std::random_shuffle(urls.begin(), urls.end(),
  213. *(SimpleRandomizer::getInstance().get()));
  214. }
  215. }
  216. bool AnnounceList::allTiersFailed() const
  217. {
  218. return _currentTier == _tiers.end();
  219. }
  220. void AnnounceList::resetTier()
  221. {
  222. resetIterator();
  223. }
  224. bool AnnounceList::currentTierAcceptsStoppedEvent() const
  225. {
  226. if(_currentTrackerInitialized) {
  227. return FindStoppedAllowedTier()(*_currentTier);
  228. } else {
  229. return false;
  230. }
  231. }
  232. bool AnnounceList::currentTierAcceptsCompletedEvent() const
  233. {
  234. if(_currentTrackerInitialized) {
  235. return FindCompletedAllowedTier()(*_currentTier);
  236. } else {
  237. return false;
  238. }
  239. }
  240. } // namespace aria2