ServerStat.cc 6.6 KB

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