ServerStat.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "ServerStat.h"
  36. #include <ostream>
  37. #include <algorithm>
  38. #include "array_fun.h"
  39. #include "LogFactory.h"
  40. namespace aria2 {
  41. const std::string ServerStat::STATUS_STRING[] = {
  42. "OK",
  43. "ERROR"
  44. };
  45. ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
  46. :
  47. _hostname(hostname),
  48. _protocol(protocol),
  49. _downloadSpeed(0),
  50. _singleConnectionAvgSpeed(0),
  51. _multiConnectionAvgSpeed(0),
  52. _counter(0),
  53. _status(OK),
  54. _logger(LogFactory::getInstance())
  55. {}
  56. ServerStat::~ServerStat() {}
  57. const std::string& ServerStat::getHostname() const
  58. {
  59. return _hostname;
  60. }
  61. const std::string& ServerStat::getProtocol() const
  62. {
  63. return _protocol;
  64. }
  65. const Time& ServerStat::getLastUpdated() const
  66. {
  67. return _lastUpdated;
  68. }
  69. void ServerStat::setLastUpdated(const Time& time)
  70. {
  71. _lastUpdated = time;
  72. }
  73. unsigned int ServerStat::getDownloadSpeed() const
  74. {
  75. return _downloadSpeed;
  76. }
  77. void ServerStat::setDownloadSpeed(unsigned int downloadSpeed)
  78. {
  79. _downloadSpeed = downloadSpeed;
  80. }
  81. void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed)
  82. {
  83. _downloadSpeed = downloadSpeed;
  84. if(downloadSpeed > 0) {
  85. _status = OK;
  86. }
  87. _lastUpdated.reset();
  88. }
  89. unsigned int ServerStat::getSingleConnectionAvgSpeed() const
  90. {
  91. return _singleConnectionAvgSpeed;
  92. }
  93. void ServerStat::setSingleConnectionAvgSpeed
  94. (unsigned int singleConnectionAvgSpeed)
  95. {
  96. _singleConnectionAvgSpeed = singleConnectionAvgSpeed;
  97. }
  98. void ServerStat::updateSingleConnectionAvgSpeed(unsigned int downloadSpeed)
  99. {
  100. float avgDownloadSpeed;
  101. if(_counter == 0)
  102. return;
  103. if(_counter < 5) {
  104. avgDownloadSpeed =
  105. ((((float)_counter-1)/(float)_counter)*(float)_singleConnectionAvgSpeed)+
  106. ((1.0/(float)_counter)*(float)downloadSpeed);
  107. }
  108. else {
  109. avgDownloadSpeed = ((4.0/5.0)*(float)_singleConnectionAvgSpeed) +
  110. ((1.0/5.0)*(float)downloadSpeed);
  111. }
  112. if(avgDownloadSpeed < (int)(0.80*_singleConnectionAvgSpeed)) {
  113. _logger->debug("ServerStat:%s: resetting counter since single connection"
  114. " speed dropped", getHostname().c_str());
  115. _counter = 0;
  116. }
  117. _logger->debug("ServerStat:%s: _singleConnectionAvgSpeed old:%.2fKB/s"
  118. " new:%.2fKB/s last:%.2fKB/s",
  119. getHostname().c_str(),
  120. (float) _singleConnectionAvgSpeed/1024,
  121. (float) avgDownloadSpeed/1024,
  122. (float) downloadSpeed / 1024);
  123. _singleConnectionAvgSpeed = (int)avgDownloadSpeed;
  124. }
  125. unsigned int ServerStat::getMultiConnectionAvgSpeed() const
  126. {
  127. return _multiConnectionAvgSpeed;
  128. }
  129. void ServerStat::setMultiConnectionAvgSpeed
  130. (unsigned int multiConnectionAvgSpeed)
  131. {
  132. _multiConnectionAvgSpeed = multiConnectionAvgSpeed;
  133. }
  134. void ServerStat::updateMultiConnectionAvgSpeed(unsigned int downloadSpeed)
  135. {
  136. float avgDownloadSpeed;
  137. if(_counter == 0)
  138. return;
  139. if(_counter < 5) {
  140. avgDownloadSpeed =
  141. ((((float)_counter-1)/(float)_counter)*(float)_multiConnectionAvgSpeed) +
  142. ((1.0/(float)_counter)*(float)downloadSpeed);
  143. }
  144. else {
  145. avgDownloadSpeed = ((4.0/5.0)*(float)_multiConnectionAvgSpeed) +
  146. ((1.0/5.0)*(float)downloadSpeed);
  147. }
  148. _logger->debug("ServerStat:%s: _multiConnectionAvgSpeed old:%.2fKB/s"
  149. " new:%.2fKB/s last:%.2fKB/s",
  150. getHostname().c_str(),
  151. (float) _multiConnectionAvgSpeed/1024,
  152. (float) avgDownloadSpeed/1024,
  153. (float) downloadSpeed / 1024);
  154. _multiConnectionAvgSpeed = (int)avgDownloadSpeed;
  155. }
  156. unsigned int ServerStat::getCounter() const
  157. {
  158. return _counter;
  159. }
  160. void ServerStat::increaseCounter()
  161. {
  162. _counter++;
  163. }
  164. void ServerStat::setCounter(unsigned int value)
  165. {
  166. _counter = value;
  167. }
  168. void ServerStat::setStatus(STATUS status)
  169. {
  170. _status = status;
  171. }
  172. void ServerStat::setStatus(const std::string& status)
  173. {
  174. size_t len = arrayLength(STATUS_STRING);
  175. const std::string* p = std::find(&STATUS_STRING[0],
  176. &STATUS_STRING[len],
  177. status);
  178. if(p != &STATUS_STRING[len]) {
  179. _status = static_cast<STATUS>(ServerStat::OK+
  180. std::distance(&STATUS_STRING[0], p));
  181. }
  182. }
  183. void ServerStat::setStatusInternal(STATUS status)
  184. {
  185. _status = status;
  186. _lastUpdated.reset();
  187. }
  188. ServerStat::STATUS ServerStat::getStatus() const
  189. {
  190. return _status;
  191. }
  192. bool ServerStat::isOK() const
  193. {
  194. return _status == OK;
  195. }
  196. void ServerStat::setOK()
  197. {
  198. setStatusInternal(OK);
  199. }
  200. bool ServerStat::isError() const
  201. {
  202. return _status == ERROR;
  203. }
  204. void ServerStat::setError()
  205. {
  206. setStatusInternal(ERROR);
  207. }
  208. bool ServerStat::operator<(const ServerStat& serverStat) const
  209. {
  210. int c = _hostname.compare(serverStat._hostname);
  211. if(c == 0) {
  212. return _protocol < serverStat._protocol;
  213. } else {
  214. return c < 0;
  215. }
  216. }
  217. bool ServerStat::operator==(const ServerStat& serverStat) const
  218. {
  219. return _hostname == serverStat._hostname && _protocol == serverStat._protocol;
  220. }
  221. std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
  222. {
  223. o << "host=" << serverStat.getHostname() << ", "
  224. << "protocol=" << serverStat.getProtocol() << ", "
  225. << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
  226. << "sc_avg_speed=" << serverStat.getSingleConnectionAvgSpeed() << ", "
  227. << "mc_avg_speed=" << serverStat.getMultiConnectionAvgSpeed() << ", "
  228. << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
  229. << "counter=" << serverStat.getCounter() << ", "
  230. << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
  231. return o;
  232. }
  233. } // namespace aria2