stats.cpp
上传用户:chn_coc
上传日期:2007-12-20
资源大小:563k
文件大小:3k
源码类别:

P2P编程

开发平台:

Windows_Unix

  1. // ------------------------------------------------
  2. // File : stats.cpp
  3. // Date: 4-apr-2002
  4. // Author: giles
  5. // Desc: 
  6. // Statistic logging
  7. //
  8. // (c) 2002 peercast.org
  9. // ------------------------------------------------
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation; either version 2 of the License, or
  13. // (at your option) any later version.
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. // GNU General Public License for more details.
  18. // ------------------------------------------------
  19. #include "stats.h"
  20. #include "common.h"
  21. #include "sys.h"
  22. #include "stream.h"
  23. Stats stats;
  24. // ------------------------------------
  25. void Stats::clear()
  26. {
  27. for(int i=0; i<Stats::MAX; i++)
  28. {
  29. current[i] = 0;
  30. last[i] = 0;
  31. perSec[i] = 0;
  32. }
  33. lastUpdate = 0;
  34. }
  35. // ------------------------------------
  36. void Stats::update()
  37. {
  38. unsigned int ctime = sys->getTime();
  39. unsigned int diff = ctime - lastUpdate;
  40. if (diff >= 5)
  41. {
  42. for(int i=0; i<Stats::MAX; i++)
  43. {
  44. perSec[i] = (current[i]-last[i])/diff;
  45. last[i] = current[i];
  46. }
  47. lastUpdate = ctime;
  48. }
  49. }
  50. // ------------------------------------
  51. bool Stats::writeVariable(Stream &out,const String &var)
  52. {
  53. char buf[1024];
  54. if (var == "totalInPerSec")
  55. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)));
  56. else if (var == "totalOutPerSec")
  57. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESOUT)));
  58. else if (var == "totalPerSec")
  59. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)+getPerSecond(Stats::BYTESOUT)));
  60. else if (var == "wanInPerSec")
  61. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)-getPerSecond(Stats::LOCALBYTESIN)));
  62. else if (var == "wanOutPerSec")
  63. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESOUT)-getPerSecond(Stats::LOCALBYTESOUT)));
  64. else if (var == "wanTotalPerSec")
  65. sprintf(buf,"%.1f",BYTES_TO_KBPS((getPerSecond(Stats::BYTESIN)-getPerSecond(Stats::LOCALBYTESIN))+(getPerSecond(Stats::BYTESOUT)-getPerSecond(Stats::LOCALBYTESOUT))));
  66. else if (var == "netInPerSec")
  67. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAIN)));
  68. else if (var == "netOutPerSec")
  69. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAOUT)));
  70. else if (var == "netTotalPerSec")
  71. sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAOUT)+getPerSecond(Stats::PACKETDATAIN)));
  72. else if (var == "packInPerSec")
  73. sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSIN));
  74. else if (var == "packOutPerSec")
  75. sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSOUT));
  76. else if (var == "packTotalPerSec")
  77. sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSOUT)+getPerSecond(Stats::NUMPACKETSIN));
  78. else
  79. return false;
  80. out.writeString(buf);
  81. return true;
  82. }