AnnounceList.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. currentTier = tiers.begin();
  105. }
  106. currentTracker = (*currentTier)->urls.begin();
  107. }
  108. }
  109. }
  110. AnnounceTier::AnnounceEvent AnnounceList::getEvent() const {
  111. if(currentTrackerInitialized) {
  112. return (*currentTier)->event;
  113. } else {
  114. return AnnounceTier::STARTED;
  115. }
  116. }
  117. void AnnounceList::setEvent(AnnounceTier::AnnounceEvent event) {
  118. if(currentTrackerInitialized) {
  119. (*currentTier)->event = event;
  120. }
  121. }
  122. string AnnounceList::getEventString() const {
  123. if(currentTrackerInitialized) {
  124. switch((*currentTier)->event) {
  125. case AnnounceTier::STARTED:
  126. case AnnounceTier::STARTED_AFTER_COMPLETION:
  127. return "started";
  128. case AnnounceTier::STOPPED:
  129. return "stopped";
  130. case AnnounceTier::COMPLETED:
  131. return "completed";
  132. default:
  133. return "";
  134. }
  135. } else {
  136. return "";
  137. }
  138. }
  139. class FindStoppedAllowedTier {
  140. public:
  141. bool operator()(const AnnounceTierHandle& tier) const {
  142. switch(tier->event) {
  143. case AnnounceTier::DOWNLOADING:
  144. case AnnounceTier::STOPPED:
  145. case AnnounceTier::COMPLETED:
  146. case AnnounceTier::SEEDING:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. };
  153. class FindCompletedAllowedTier {
  154. public:
  155. bool operator()(const AnnounceTierHandle& tier) const {
  156. switch(tier->event) {
  157. case AnnounceTier::DOWNLOADING:
  158. case AnnounceTier::COMPLETED:
  159. return true;
  160. default:
  161. return false;
  162. }
  163. }
  164. };
  165. int AnnounceList::countStoppedAllowedTier() const {
  166. return count_if(tiers.begin(), tiers.end(), FindStoppedAllowedTier());
  167. }
  168. int AnnounceList::countCompletedAllowedTier() const {
  169. return count_if(tiers.begin(), tiers.end(), FindCompletedAllowedTier());
  170. }
  171. void AnnounceList::setCurrentTier(const AnnounceTiers::iterator& itr) {
  172. if(itr != tiers.end()) {
  173. currentTier = itr;
  174. currentTracker = (*currentTier)->urls.begin();
  175. }
  176. }
  177. template<class InputIterator, class Predicate>
  178. InputIterator
  179. find_wrap_if(InputIterator first, InputIterator last,
  180. InputIterator current, Predicate pred) {
  181. InputIterator itr = find_if(current, last,
  182. pred);
  183. if(itr == last) {
  184. itr = find_if(first, current, pred);
  185. }
  186. return itr;
  187. }
  188. void AnnounceList::moveToStoppedAllowedTier() {
  189. AnnounceTiers::iterator itr = find_wrap_if(tiers.begin(), tiers.end(),
  190. currentTier,
  191. FindStoppedAllowedTier());
  192. setCurrentTier(itr);
  193. }
  194. void AnnounceList::moveToCompletedAllowedTier() {
  195. AnnounceTiers::iterator itr = find_wrap_if(tiers.begin(), tiers.end(),
  196. currentTier,
  197. FindCompletedAllowedTier());
  198. setCurrentTier(itr);
  199. }
  200. void AnnounceList::shuffle() {
  201. for(AnnounceTiers::iterator itr = tiers.begin();
  202. itr != tiers.end(); itr++) {
  203. Strings& urls = (*itr)->urls;
  204. random_shuffle(urls.begin(), urls.end());
  205. }
  206. }