FeedbackURISelector.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "FeedbackURISelector.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "ServerStatMan.h"
  39. #include "ServerStat.h"
  40. #include "A2STR.h"
  41. #include "FileEntry.h"
  42. #include "Logger.h"
  43. #include "LogFactory.h"
  44. #include "a2algo.h"
  45. #include "uri.h"
  46. #include "fmt.h"
  47. namespace aria2 {
  48. FeedbackURISelector::FeedbackURISelector
  49. (const SharedHandle<ServerStatMan>& serverStatMan)
  50. : serverStatMan_(serverStatMan)
  51. {}
  52. FeedbackURISelector::~FeedbackURISelector() {}
  53. namespace {
  54. class ServerStatFaster {
  55. public:
  56. bool operator()(const std::pair<SharedHandle<ServerStat>, std::string> lhs,
  57. const std::pair<SharedHandle<ServerStat>, std::string> rhs)
  58. const
  59. {
  60. return lhs.first->getDownloadSpeed() > rhs.first->getDownloadSpeed();
  61. }
  62. };
  63. } // namespace
  64. std::string FeedbackURISelector::select
  65. (FileEntry* fileEntry,
  66. const std::vector<std::pair<size_t, std::string> >& usedHosts)
  67. {
  68. if(A2_LOG_DEBUG_ENABLED) {
  69. for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
  70. usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
  71. A2_LOG_DEBUG(fmt("UsedHost=%lu, %s",
  72. static_cast<unsigned long>((*i).first),
  73. (*i).second.c_str()));
  74. }
  75. }
  76. if(fileEntry->getRemainingUris().empty()) {
  77. return A2STR::NIL;
  78. }
  79. // Select URI with usedHosts first. If no URI is selected, then do
  80. // it again without usedHosts.
  81. std::string uri = selectFaster(fileEntry->getRemainingUris(), usedHosts);
  82. if(uri.empty()) {
  83. A2_LOG_DEBUG("No URI returned from selectFaster()");
  84. uri = selectRarer(fileEntry->getRemainingUris(), usedHosts);
  85. }
  86. if(!uri.empty()) {
  87. std::deque<std::string>& uris = fileEntry->getRemainingUris();
  88. uris.erase(std::find(uris.begin(), uris.end(), uri));
  89. }
  90. A2_LOG_DEBUG(fmt("FeedbackURISelector selected %s", uri.c_str()));
  91. return uri;
  92. }
  93. std::string FeedbackURISelector::selectRarer
  94. (const std::deque<std::string>& uris,
  95. const std::vector<std::pair<size_t, std::string> >& usedHosts)
  96. {
  97. // pair of host and URI
  98. std::vector<std::pair<std::string, std::string> > cands;
  99. for(std::deque<std::string>::const_iterator i = uris.begin(),
  100. eoi = uris.end(); i != eoi; ++i) {
  101. uri::UriStruct us;
  102. if(!uri::parse(us, *i)) {
  103. continue;
  104. }
  105. SharedHandle<ServerStat> ss =
  106. serverStatMan_->find(us.host, us.protocol);
  107. if(ss && ss->isError()) {
  108. A2_LOG_DEBUG(fmt("Error not considered: %s", (*i).c_str()));
  109. continue;
  110. }
  111. cands.push_back(std::make_pair(us.host, *i));
  112. }
  113. for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
  114. usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
  115. for(std::vector<std::pair<std::string, std::string> >::const_iterator j =
  116. cands.begin(), eoj = cands.end(); j != eoj; ++j) {
  117. if((*i).second == (*j).first) {
  118. return (*j).second;
  119. }
  120. }
  121. }
  122. assert(!uris.empty());
  123. return uris.front();
  124. }
  125. std::string FeedbackURISelector::selectFaster
  126. (const std::deque<std::string>& uris,
  127. const std::vector<std::pair<size_t, std::string> >& usedHosts)
  128. {
  129. // Use first 10 good URIs to introduce some randomness.
  130. const size_t NUM_URI = 10;
  131. // Ignore low speed server
  132. const unsigned int SPEED_THRESHOLD = 20*1024;
  133. std::vector<std::pair<SharedHandle<ServerStat>, std::string> > fastCands;
  134. std::vector<std::string> normCands;
  135. for(std::deque<std::string>::const_iterator i = uris.begin(),
  136. eoi = uris.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
  137. uri::UriStruct us;
  138. if(!uri::parse(us, *i)) {
  139. continue;
  140. }
  141. if(findSecond(usedHosts.begin(), usedHosts.end(), us.host) !=
  142. usedHosts.end()) {
  143. A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
  144. continue;
  145. }
  146. SharedHandle<ServerStat> ss =
  147. serverStatMan_->find(us.host, us.protocol);
  148. if(!ss) {
  149. normCands.push_back(*i);
  150. } else if(ss->isOK()) {
  151. if(ss->getDownloadSpeed() > SPEED_THRESHOLD) {
  152. fastCands.push_back(std::make_pair(ss, *i));
  153. } else {
  154. normCands.push_back(*i);
  155. }
  156. }
  157. }
  158. if(fastCands.empty()) {
  159. if(normCands.empty()) {
  160. return A2STR::NIL;
  161. } else {
  162. A2_LOG_DEBUG("Selected from normCands");
  163. return normCands.front();
  164. }
  165. } else {
  166. A2_LOG_DEBUG("Selected from fastCands");
  167. std::sort(fastCands.begin(), fastCands.end(), ServerStatFaster());
  168. return fastCands.front().second;
  169. }
  170. }
  171. } // namespace aria2