AnnounceList.cc 6.5 KB

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