ServerStat.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "array_fun.h"
  37. #include <ostream>
  38. #include <algorithm>
  39. namespace aria2 {
  40. const std::string ServerStat::STATUS_STRING[] = {
  41. "OK",
  42. "ERROR"
  43. };
  44. ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
  45. :
  46. _hostname(hostname),
  47. _protocol(protocol),
  48. _downloadSpeed(0),
  49. _status(OK) {}
  50. ServerStat::~ServerStat() {}
  51. const std::string& ServerStat::getHostname() const
  52. {
  53. return _hostname;
  54. }
  55. const std::string& ServerStat::getProtocol() const
  56. {
  57. return _protocol;
  58. }
  59. const Time& ServerStat::getLastUpdated() const
  60. {
  61. return _lastUpdated;
  62. }
  63. void ServerStat::setLastUpdated(const Time& time)
  64. {
  65. _lastUpdated = time;
  66. }
  67. unsigned int ServerStat::getDownloadSpeed() const
  68. {
  69. return _downloadSpeed;
  70. }
  71. void ServerStat::setDownloadSpeed(unsigned int downloadSpeed)
  72. {
  73. _downloadSpeed = downloadSpeed;
  74. }
  75. void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed)
  76. {
  77. _downloadSpeed = downloadSpeed;
  78. if(downloadSpeed > 0) {
  79. _status = OK;
  80. }
  81. _lastUpdated.reset();
  82. }
  83. void ServerStat::setStatus(STATUS status)
  84. {
  85. _status = status;
  86. }
  87. void ServerStat::setStatus(const std::string& status)
  88. {
  89. size_t len = arrayLength(STATUS_STRING);
  90. const std::string* p = std::find(&STATUS_STRING[0],
  91. &STATUS_STRING[len],
  92. status);
  93. if(p != &STATUS_STRING[len]) {
  94. _status = static_cast<STATUS>(ServerStat::OK+
  95. std::distance(&STATUS_STRING[0], p));
  96. }
  97. }
  98. void ServerStat::setStatusInternal(STATUS status)
  99. {
  100. _status = status;
  101. _lastUpdated.reset();
  102. }
  103. ServerStat::STATUS ServerStat::getStatus() const
  104. {
  105. return _status;
  106. }
  107. bool ServerStat::isOK() const
  108. {
  109. return _status == OK;
  110. }
  111. void ServerStat::setOK()
  112. {
  113. setStatusInternal(OK);
  114. }
  115. bool ServerStat::isError() const
  116. {
  117. return _status == ERROR;
  118. }
  119. void ServerStat::setError()
  120. {
  121. setStatusInternal(ERROR);
  122. }
  123. bool ServerStat::operator<(const ServerStat& serverStat) const
  124. {
  125. int c = _hostname.compare(serverStat._hostname);
  126. if(c == 0) {
  127. return _protocol < serverStat._protocol;
  128. } else {
  129. return c < 0;
  130. }
  131. }
  132. bool ServerStat::operator==(const ServerStat& serverStat) const
  133. {
  134. return _hostname == serverStat._hostname && _protocol == serverStat._protocol;
  135. }
  136. std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
  137. {
  138. o << "host=" << serverStat.getHostname() << ", "
  139. << "protocol=" << serverStat.getProtocol() << ", "
  140. << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
  141. << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
  142. << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
  143. return o;
  144. }
  145. } // namespace aria2