ServerStatMan.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. auto p = util::divide((*i).first, (*i).second, '=');
  177. int id = idField(p.first.first, p.first.second);
  178. if(id != MAX_FIELD) {
  179. m[id].assign(p.second.first, p.second.second);
  180. }
  181. }
  182. if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {
  183. continue;
  184. }
  185. std::shared_ptr<ServerStat> sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL]));
  186. uint32_t uintval;
  187. if(!util::parseUIntNoThrow(uintval, m[S_DL_SPEED])) {
  188. continue;
  189. }
  190. sstat->setDownloadSpeed(uintval);
  191. // Old serverstat file doesn't contains SC_AVG_SPEED
  192. if(!m[S_SC_AVG_SPEED].empty()) {
  193. if(!util::parseUIntNoThrow(uintval, m[S_SC_AVG_SPEED])) {
  194. continue;
  195. }
  196. sstat->setSingleConnectionAvgSpeed(uintval);
  197. }
  198. // Old serverstat file doesn't contains MC_AVG_SPEED
  199. if(!m[S_MC_AVG_SPEED].empty()) {
  200. if(!util::parseUIntNoThrow(uintval, m[S_MC_AVG_SPEED])) {
  201. continue;
  202. }
  203. sstat->setMultiConnectionAvgSpeed(uintval);
  204. }
  205. // Old serverstat file doesn't contains COUNTER_SPEED
  206. if(!m[S_COUNTER].empty()) {
  207. if(!util::parseUIntNoThrow(uintval, m[S_COUNTER])) {
  208. continue;
  209. }
  210. sstat->setCounter(uintval);
  211. }
  212. int32_t intval;
  213. if(!util::parseIntNoThrow(intval, m[S_LAST_UPDATED])) {
  214. continue;
  215. }
  216. sstat->setLastUpdated(Time(intval));
  217. sstat->setStatus(m[S_STATUS]);
  218. add(sstat);
  219. }
  220. A2_LOG_NOTICE(fmt(MSG_SERVER_STAT_LOADED, filename.c_str()));
  221. return true;
  222. }
  223. namespace {
  224. class FindStaleServerStat {
  225. private:
  226. time_t timeout_;
  227. Time time_;
  228. public:
  229. FindStaleServerStat(time_t timeout):timeout_(timeout) {}
  230. bool operator()(const std::shared_ptr<ServerStat>& ss) const
  231. {
  232. return ss->getLastUpdated().difference(time_) >= timeout_;
  233. }
  234. };
  235. } // namespace
  236. void ServerStatMan::removeStaleServerStat(time_t timeout)
  237. {
  238. FindStaleServerStat finder(timeout);
  239. for(ServerStatSet::iterator i = serverStats_.begin(),
  240. eoi = serverStats_.end(); i != eoi;) {
  241. if(finder(*i)) {
  242. serverStats_.erase(i++);
  243. } else {
  244. ++i;
  245. }
  246. }
  247. }
  248. } // namespace aria2