ServerStatMan.cc 6.9 KB

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