ServerStatMan.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <vector>
  41. #include "ServerStat.h"
  42. #include "util.h"
  43. #include "RecoverableException.h"
  44. #include "a2functional.h"
  45. #include "BufferedFile.h"
  46. #include "message.h"
  47. #include "fmt.h"
  48. #include "LogFactory.h"
  49. #include "File.h"
  50. namespace aria2 {
  51. ServerStatMan::ServerStatMan() {}
  52. ServerStatMan::~ServerStatMan() {}
  53. std::shared_ptr<ServerStat> ServerStatMan::find(const std::string& hostname,
  54. const std::string& protocol) const
  55. {
  56. std::shared_ptr<ServerStat> ss(new ServerStat(hostname, protocol));
  57. ServerStatSet::iterator i = serverStats_.find(ss);
  58. if(i == serverStats_.end()) {
  59. return std::shared_ptr<ServerStat>();
  60. } else {
  61. return *i;
  62. }
  63. }
  64. bool ServerStatMan::add(const std::shared_ptr<ServerStat>& serverStat)
  65. {
  66. ServerStatSet::iterator i = serverStats_.lower_bound(serverStat);
  67. if(i != serverStats_.end() && *(*i) == *serverStat) {
  68. return false;
  69. } else {
  70. serverStats_.insert(i, serverStat);
  71. return true;
  72. }
  73. }
  74. bool ServerStatMan::save(const std::string& filename) const
  75. {
  76. std::string tempfile = filename;
  77. tempfile += "__temp";
  78. {
  79. BufferedFile fp(tempfile.c_str(), BufferedFile::WRITE);
  80. if(!fp) {
  81. A2_LOG_ERROR(fmt(MSG_OPENING_WRITABLE_SERVER_STAT_FILE_FAILED,
  82. filename.c_str()));
  83. return false;
  84. }
  85. for(ServerStatSet::iterator i = serverStats_.begin(),
  86. eoi = serverStats_.end(); i != eoi; ++i) {
  87. std::string l = (*i)->toString();
  88. l += "\n";
  89. if(fp.write(l.data(), l.size()) != l.size()) {
  90. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED,
  91. filename.c_str()));
  92. }
  93. }
  94. if(fp.close() == EOF) {
  95. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED, filename.c_str()));
  96. return false;
  97. }
  98. }
  99. if(File(tempfile).renameTo(filename)) {
  100. A2_LOG_NOTICE(fmt(MSG_SERVER_STAT_SAVED, filename.c_str()));
  101. return true;
  102. } else {
  103. A2_LOG_ERROR(fmt(MSG_WRITING_SERVER_STAT_FILE_FAILED, filename.c_str()));
  104. return false;
  105. }
  106. }
  107. namespace {
  108. // Field and FIELD_NAMES must have same order except for MAX_FIELD.
  109. enum Field {
  110. S_COUNTER,
  111. S_DL_SPEED,
  112. S_HOST,
  113. S_LAST_UPDATED,
  114. S_MC_AVG_SPEED,
  115. S_PROTOCOL,
  116. S_SC_AVG_SPEED,
  117. S_STATUS,
  118. MAX_FIELD
  119. };
  120. const char* FIELD_NAMES[] = {
  121. "counter",
  122. "dl_speed",
  123. "host",
  124. "last_updated",
  125. "mc_avg_speed",
  126. "protocol",
  127. "sc_avg_speed",
  128. "status",
  129. };
  130. } // namespace
  131. namespace {
  132. int idField(std::string::const_iterator first,
  133. std::string::const_iterator last)
  134. {
  135. int i;
  136. for(i = 0; i < MAX_FIELD; ++i) {
  137. if(util::streq(first, last, FIELD_NAMES[i])) {
  138. return i;
  139. }
  140. }
  141. return i;
  142. }
  143. } // namespace
  144. bool ServerStatMan::load(const std::string& filename)
  145. {
  146. BufferedFile fp(filename.c_str(), BufferedFile::READ);
  147. if(!fp) {
  148. A2_LOG_ERROR(fmt(MSG_OPENING_READABLE_SERVER_STAT_FILE_FAILED,
  149. filename.c_str()));
  150. return false;
  151. }
  152. while(1) {
  153. std::string line = fp.getLine();
  154. if(line.empty()) {
  155. if(fp.eof()) {
  156. break;
  157. } else if(!fp) {
  158. A2_LOG_ERROR(fmt(MSG_READING_SERVER_STAT_FILE_FAILED,
  159. filename.c_str()));
  160. return false;
  161. } else {
  162. continue;
  163. }
  164. }
  165. std::pair<std::string::const_iterator,
  166. std::string::const_iterator> p =
  167. util::stripIter(line.begin(), line.end());
  168. if(p.first == p.second) {
  169. continue;
  170. }
  171. std::vector<Scip> items;
  172. util::splitIter(p.first, p.second, std::back_inserter(items), ',');
  173. std::vector<std::string> m(MAX_FIELD);
  174. for(std::vector<Scip>::const_iterator i = items.begin(),
  175. eoi = items.end(); i != eoi; ++i) {
  176. std::pair<Scip, Scip> p;
  177. util::divide(p, (*i).first, (*i).second, '=');
  178. int id = idField(p.first.first, p.first.second);
  179. if(id != MAX_FIELD) {
  180. m[id].assign(p.second.first, p.second.second);
  181. }
  182. }
  183. if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {
  184. continue;
  185. }
  186. std::shared_ptr<ServerStat> sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL]));
  187. uint32_t uintval;
  188. if(!util::parseUIntNoThrow(uintval, m[S_DL_SPEED])) {
  189. continue;
  190. }
  191. sstat->setDownloadSpeed(uintval);
  192. // Old serverstat file doesn't contains SC_AVG_SPEED
  193. if(!m[S_SC_AVG_SPEED].empty()) {
  194. if(!util::parseUIntNoThrow(uintval, m[S_SC_AVG_SPEED])) {
  195. continue;
  196. }
  197. sstat->setSingleConnectionAvgSpeed(uintval);
  198. }
  199. // Old serverstat file doesn't contains MC_AVG_SPEED
  200. if(!m[S_MC_AVG_SPEED].empty()) {
  201. if(!util::parseUIntNoThrow(uintval, m[S_MC_AVG_SPEED])) {
  202. continue;
  203. }
  204. sstat->setMultiConnectionAvgSpeed(uintval);
  205. }
  206. // Old serverstat file doesn't contains COUNTER_SPEED
  207. if(!m[S_COUNTER].empty()) {
  208. if(!util::parseUIntNoThrow(uintval, m[S_COUNTER])) {
  209. continue;
  210. }
  211. sstat->setCounter(uintval);
  212. }
  213. int32_t intval;
  214. if(!util::parseIntNoThrow(intval, m[S_LAST_UPDATED])) {
  215. continue;
  216. }
  217. sstat->setLastUpdated(Time(intval));
  218. sstat->setStatus(m[S_STATUS]);
  219. add(sstat);
  220. }
  221. A2_LOG_NOTICE(fmt(MSG_SERVER_STAT_LOADED, filename.c_str()));
  222. return true;
  223. }
  224. namespace {
  225. class FindStaleServerStat {
  226. private:
  227. time_t timeout_;
  228. Time time_;
  229. public:
  230. FindStaleServerStat(time_t timeout):timeout_(timeout) {}
  231. bool operator()(const std::shared_ptr<ServerStat>& ss) const
  232. {
  233. return ss->getLastUpdated().difference(time_) >= timeout_;
  234. }
  235. };
  236. } // namespace
  237. void ServerStatMan::removeStaleServerStat(time_t timeout)
  238. {
  239. FindStaleServerStat finder(timeout);
  240. for(ServerStatSet::iterator i = serverStats_.begin(),
  241. eoi = serverStats_.end(); i != eoi;) {
  242. if(finder(*i)) {
  243. serverStats_.erase(i++);
  244. } else {
  245. ++i;
  246. }
  247. }
  248. }
  249. } // namespace aria2