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