ServerStatMan.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <cstring>
  37. #include <cstdio>
  38. #include <algorithm>
  39. #include <iterator>
  40. #include <map>
  41. #include <vector>
  42. #include "ServerStat.h"
  43. #include "util.h"
  44. #include "RecoverableException.h"
  45. #include "a2functional.h"
  46. #include "BufferedFile.h"
  47. #include "message.h"
  48. #include "fmt.h"
  49. #include "LogFactory.h"
  50. #include "File.h"
  51. namespace aria2 {
  52. ServerStatMan::ServerStatMan() {}
  53. ServerStatMan::~ServerStatMan() {}
  54. SharedHandle<ServerStat> ServerStatMan::find(const std::string& hostname,
  55. const std::string& protocol) const
  56. {
  57. SharedHandle<ServerStat> ss(new ServerStat(hostname, protocol));
  58. std::deque<SharedHandle<ServerStat> >::const_iterator i =
  59. std::lower_bound(serverStats_.begin(), serverStats_.end(), ss,
  60. DerefLess<SharedHandle<ServerStat> >());
  61. if(i != serverStats_.end() &&
  62. (*i)->getHostname() == hostname && (*i)->getProtocol() == protocol) {
  63. return *i;
  64. } else {
  65. return SharedHandle<ServerStat>();
  66. }
  67. }
  68. bool ServerStatMan::add(const SharedHandle<ServerStat>& serverStat)
  69. {
  70. std::deque<SharedHandle<ServerStat> >::iterator i =
  71. std::lower_bound(serverStats_.begin(), serverStats_.end(), serverStat,
  72. DerefLess<SharedHandle<ServerStat> >());
  73. if(i != serverStats_.end() && *(*i) == *serverStat) {
  74. return false;
  75. } else {
  76. serverStats_.insert(i, serverStat);
  77. return true;
  78. }
  79. }
  80. bool ServerStatMan::save(const std::string& filename) const
  81. {
  82. std::string tempfile = filename;
  83. tempfile += "__temp";
  84. {
  85. BufferedFile fp(tempfile, BufferedFile::WRITE);
  86. if(!fp) {
  87. A2_LOG_ERROR(fmt(MSG_OPENING_WRITABLE_SERVER_STAT_FILE_FAILED,
  88. filename.c_str()));
  89. return false;
  90. }
  91. for(std::deque<SharedHandle<ServerStat> >::const_iterator i =
  92. serverStats_.begin(), eoi = serverStats_.end(); i != eoi; ++i) {
  93. std::string l = (*i)->toString();
  94. l += "\n";
  95. if(fp.write(l.data(), l.size()) != l.size()) {
  96. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED,
  97. filename.c_str()));
  98. }
  99. }
  100. if(fp.close() == EOF) {
  101. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED, filename.c_str()));
  102. return false;
  103. }
  104. }
  105. if(File(tempfile).renameTo(filename)) {
  106. A2_LOG_NOTICE(fmt(MSG_SERVER_STAT_SAVED, filename.c_str()));
  107. return true;
  108. } else {
  109. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED, filename.c_str()));
  110. return false;
  111. }
  112. }
  113. bool ServerStatMan::load(const std::string& filename)
  114. {
  115. static const std::string S_HOST = "host";
  116. static const std::string S_PROTOCOL = "protocol";
  117. static const std::string S_DL_SPEED = "dl_speed";
  118. static const std::string S_SC_AVG_SPEED = "sc_avg_speed";
  119. static const std::string S_MC_AVG_SPEED = "mc_avg_speed";
  120. static const std::string S_LAST_UPDATED = "last_updated";
  121. static const std::string S_COUNTER = "counter";
  122. static const std::string S_STATUS = "status";
  123. BufferedFile fp(filename, BufferedFile::READ);
  124. if(!fp) {
  125. A2_LOG_ERROR(fmt(MSG_OPENING_READABLE_SERVER_STAT_FILE_FAILED,
  126. filename.c_str()));
  127. return false;
  128. }
  129. char buf[4096];
  130. while(1) {
  131. if(!fp.getsn(buf, sizeof(buf))) {
  132. if(fp.eof()) {
  133. break;
  134. } else {
  135. A2_LOG_ERROR(fmt(MSG_READING_SERVER_STAT_FILE_FAILED,
  136. filename.c_str()));
  137. return false;
  138. }
  139. }
  140. std::pair<std::string::const_iterator,
  141. std::string::const_iterator> p =
  142. util::stripIter(&buf[0], &buf[strlen(buf)]);
  143. if(p.first == p.second) {
  144. continue;
  145. }
  146. std::string line(p.first, p.second);
  147. std::vector<std::string> items;
  148. util::split(line, std::back_inserter(items), ",");
  149. std::map<std::string, std::string> m;
  150. for(std::vector<std::string>::const_iterator i = items.begin(),
  151. eoi = items.end(); i != eoi; ++i) {
  152. std::pair<Scip, Scip> p;
  153. util::divide(p, (*i).begin(), (*i).end(), '=');
  154. m[std::string(p.first.first, p.first.second)] =
  155. std::string(p.second.first, p.second.second);
  156. }
  157. if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {
  158. continue;
  159. }
  160. SharedHandle<ServerStat> sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL]));
  161. try {
  162. const std::string& dlSpeed = m[S_DL_SPEED];
  163. sstat->setDownloadSpeed(util::parseUInt(dlSpeed.begin(), dlSpeed.end()));
  164. // Old serverstat file doesn't contains SC_AVG_SPEED
  165. if(m.find(S_SC_AVG_SPEED) != m.end()) {
  166. const std::string& s = m[S_SC_AVG_SPEED];
  167. sstat->setSingleConnectionAvgSpeed(util::parseUInt(s.begin(), s.end()));
  168. }
  169. // Old serverstat file doesn't contains MC_AVG_SPEED
  170. if(m.find(S_MC_AVG_SPEED) != m.end()) {
  171. const std::string& s = m[S_MC_AVG_SPEED];
  172. sstat->setMultiConnectionAvgSpeed(util::parseUInt(s.begin(), s.end()));
  173. }
  174. // Old serverstat file doesn't contains COUNTER_SPEED
  175. if(m.find(S_COUNTER) != m.end()) {
  176. const std::string& s = m[S_COUNTER];
  177. sstat->setCounter(util::parseUInt(s.begin(), s.end()));
  178. }
  179. const std::string& lastUpdated = m[S_LAST_UPDATED];
  180. sstat->setLastUpdated
  181. (Time(util::parseInt(lastUpdated.begin(), lastUpdated.end())));
  182. sstat->setStatus(m[S_STATUS]);
  183. add(sstat);
  184. } catch(RecoverableException& e) {
  185. continue;
  186. }
  187. }
  188. A2_LOG_NOTICE(fmt(MSG_SERVER_STAT_LOADED, filename.c_str()));
  189. return true;
  190. }
  191. namespace {
  192. class FindStaleServerStat {
  193. private:
  194. time_t timeout_;
  195. Time time_;
  196. public:
  197. FindStaleServerStat(time_t timeout):timeout_(timeout) {}
  198. bool operator()(const SharedHandle<ServerStat>& ss) const
  199. {
  200. return ss->getLastUpdated().difference(time_) >= timeout_;
  201. }
  202. };
  203. } // namespace
  204. void ServerStatMan::removeStaleServerStat(time_t timeout)
  205. {
  206. serverStats_.erase(std::remove_if(serverStats_.begin(), serverStats_.end(),
  207. FindStaleServerStat(timeout)),
  208. serverStats_.end());
  209. }
  210. } // namespace aria2