ServerStatMan.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "ServerStatMan.h"
  36. #include <algorithm>
  37. #include <ostream>
  38. #include <iterator>
  39. #include <map>
  40. #include <vector>
  41. #include "ServerStat.h"
  42. #include "util.h"
  43. #include "RecoverableException.h"
  44. #include "wallclock.h"
  45. namespace aria2 {
  46. ServerStatMan::ServerStatMan() {}
  47. ServerStatMan::~ServerStatMan() {}
  48. SharedHandle<ServerStat> ServerStatMan::find(const std::string& hostname,
  49. const std::string& protocol) const
  50. {
  51. SharedHandle<ServerStat> ss(new ServerStat(hostname, protocol));
  52. std::deque<SharedHandle<ServerStat> >::const_iterator i =
  53. std::lower_bound(_serverStats.begin(), _serverStats.end(), ss);
  54. if(i != _serverStats.end() &&
  55. (*i)->getHostname() == hostname && (*i)->getProtocol() == protocol) {
  56. return *i;
  57. } else {
  58. return SharedHandle<ServerStat>();
  59. }
  60. }
  61. bool ServerStatMan::add(const SharedHandle<ServerStat>& serverStat)
  62. {
  63. std::deque<SharedHandle<ServerStat> >::iterator i =
  64. std::lower_bound(_serverStats.begin(), _serverStats.end(), serverStat);
  65. if(i != _serverStats.end() && (*i) == serverStat) {
  66. return false;
  67. } else {
  68. _serverStats.insert(i, serverStat);
  69. return true;
  70. }
  71. }
  72. bool ServerStatMan::save(std::ostream& out) const
  73. {
  74. std::copy(_serverStats.begin(), _serverStats.end(),
  75. std::ostream_iterator<SharedHandle<ServerStat> >(out, "\n"));
  76. out.flush();
  77. return !out.bad();
  78. }
  79. bool ServerStatMan::load(std::istream& in)
  80. {
  81. static const std::string S_HOST = "host";
  82. static const std::string S_PROTOCOL = "protocol";
  83. static const std::string S_DL_SPEED = "dl_speed";
  84. static const std::string S_SC_AVG_SPEED = "sc_avg_speed";
  85. static const std::string S_MC_AVG_SPEED = "mc_avg_speed";
  86. static const std::string S_LAST_UPDATED = "last_updated";
  87. static const std::string S_COUNTER = "counter";
  88. static const std::string S_STATUS = "status";
  89. std::string line;
  90. while(getline(in, line)) {
  91. util::trimSelf(line);
  92. if(line.empty()) {
  93. continue;
  94. }
  95. std::vector<std::string> items;
  96. util::split(line, std::back_inserter(items), ",");
  97. std::map<std::string, std::string> m;
  98. for(std::vector<std::string>::const_iterator i = items.begin(),
  99. eoi = items.end(); i != eoi; ++i) {
  100. std::pair<std::string, std::string> p = util::split(*i, "=");
  101. util::trimSelf(p.first);
  102. util::trimSelf(p.second);
  103. m[p.first] = p.second;
  104. }
  105. if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {
  106. continue;
  107. }
  108. SharedHandle<ServerStat> sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL]));
  109. try {
  110. sstat->setDownloadSpeed(util::parseUInt(m[S_DL_SPEED]));
  111. // Old serverstat file doesn't contains SC_AVG_SPEED
  112. if(m.find(S_SC_AVG_SPEED) != m.end()) {
  113. sstat->setSingleConnectionAvgSpeed(util::parseUInt(m[S_SC_AVG_SPEED]));
  114. }
  115. // Old serverstat file doesn't contains MC_AVG_SPEED
  116. if(m.find(S_MC_AVG_SPEED) != m.end()) {
  117. sstat->setMultiConnectionAvgSpeed(util::parseUInt(m[S_MC_AVG_SPEED]));
  118. }
  119. // Old serverstat file doesn't contains COUNTER_SPEED
  120. if(m.find(S_COUNTER) != m.end()) {
  121. sstat->setCounter(util::parseUInt(m[S_COUNTER]));
  122. }
  123. sstat->setLastUpdated(Time(util::parseInt(m[S_LAST_UPDATED])));
  124. sstat->setStatus(m[S_STATUS]);
  125. add(sstat);
  126. } catch(RecoverableException& e) {
  127. continue;
  128. }
  129. }
  130. return !in.bad();
  131. }
  132. class FindStaleServerStat {
  133. private:
  134. time_t _timeout;
  135. public:
  136. FindStaleServerStat(time_t timeout):_timeout(timeout) {}
  137. bool operator()(const SharedHandle<ServerStat>& ss) const
  138. {
  139. return ss->getLastUpdated().difference(global::wallclock) >= _timeout;
  140. }
  141. };
  142. void ServerStatMan::removeStaleServerStat(time_t timeout)
  143. {
  144. _serverStats.erase(std::remove_if(_serverStats.begin(), _serverStats.end(),
  145. FindStaleServerStat(timeout)),
  146. _serverStats.end());
  147. }
  148. } // namespace aria2