AnnounceList.cc 7.2 KB

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