ServerStat.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. _logger->debug("ServerStat:%s: resetting counter since single connection"
  94. " speed dropped", getHostname().c_str());
  95. _counter = 0;
  96. }
  97. _logger->debug("ServerStat:%s: _singleConnectionAvgSpeed old:%.2fKB/s"
  98. " new:%.2fKB/s last:%.2fKB/s",
  99. getHostname().c_str(),
  100. (float) _singleConnectionAvgSpeed/1024,
  101. (float) avgDownloadSpeed/1024,
  102. (float) downloadSpeed / 1024);
  103. _singleConnectionAvgSpeed = (int)avgDownloadSpeed;
  104. }
  105. void ServerStat::setMultiConnectionAvgSpeed
  106. (unsigned int multiConnectionAvgSpeed)
  107. {
  108. _multiConnectionAvgSpeed = multiConnectionAvgSpeed;
  109. }
  110. void ServerStat::updateMultiConnectionAvgSpeed(unsigned int downloadSpeed)
  111. {
  112. float avgDownloadSpeed;
  113. if(_counter == 0)
  114. return;
  115. if(_counter < 5) {
  116. avgDownloadSpeed =
  117. ((((float)_counter-1)/(float)_counter)*(float)_multiConnectionAvgSpeed) +
  118. ((1.0/(float)_counter)*(float)downloadSpeed);
  119. }
  120. else {
  121. avgDownloadSpeed = ((4.0/5.0)*(float)_multiConnectionAvgSpeed) +
  122. ((1.0/5.0)*(float)downloadSpeed);
  123. }
  124. _logger->debug("ServerStat:%s: _multiConnectionAvgSpeed old:%.2fKB/s"
  125. " new:%.2fKB/s last:%.2fKB/s",
  126. getHostname().c_str(),
  127. (float) _multiConnectionAvgSpeed/1024,
  128. (float) avgDownloadSpeed/1024,
  129. (float) downloadSpeed / 1024);
  130. _multiConnectionAvgSpeed = (int)avgDownloadSpeed;
  131. }
  132. void ServerStat::increaseCounter()
  133. {
  134. ++_counter;
  135. }
  136. void ServerStat::setCounter(unsigned int value)
  137. {
  138. _counter = value;
  139. }
  140. void ServerStat::setStatus(STATUS status)
  141. {
  142. _status = status;
  143. }
  144. void ServerStat::setStatus(const std::string& status)
  145. {
  146. size_t len = arrayLength(STATUS_STRING);
  147. const std::string* p = std::find(&STATUS_STRING[0],
  148. &STATUS_STRING[len],
  149. status);
  150. if(p != &STATUS_STRING[len]) {
  151. _status = static_cast<STATUS>(ServerStat::OK+
  152. std::distance(&STATUS_STRING[0], p));
  153. }
  154. }
  155. void ServerStat::setStatusInternal(STATUS status)
  156. {
  157. _logger->debug("ServerStat: set status %s for %s (%s)",
  158. STATUS_STRING[status].c_str(),
  159. _hostname.c_str(), _protocol.c_str());
  160. _status = status;
  161. _lastUpdated.reset();
  162. }
  163. void ServerStat::setOK()
  164. {
  165. setStatusInternal(OK);
  166. }
  167. void ServerStat::setError()
  168. {
  169. setStatusInternal(ERROR);
  170. }
  171. bool ServerStat::operator<(const ServerStat& serverStat) const
  172. {
  173. int c = _hostname.compare(serverStat._hostname);
  174. if(c == 0) {
  175. return _protocol < serverStat._protocol;
  176. } else {
  177. return c < 0;
  178. }
  179. }
  180. bool ServerStat::operator==(const ServerStat& serverStat) const
  181. {
  182. return _hostname == serverStat._hostname && _protocol == serverStat._protocol;
  183. }
  184. std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
  185. {
  186. o << "host=" << serverStat.getHostname() << ", "
  187. << "protocol=" << serverStat.getProtocol() << ", "
  188. << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
  189. << "sc_avg_speed=" << serverStat.getSingleConnectionAvgSpeed() << ", "
  190. << "mc_avg_speed=" << serverStat.getMultiConnectionAvgSpeed() << ", "
  191. << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
  192. << "counter=" << serverStat.getCounter() << ", "
  193. << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
  194. return o;
  195. }
  196. } // namespace aria2