AnnounceList.cc 7.0 KB

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