Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
4.9 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1997 **/
  4. /**********************************************************************/
  5. /*
  6. ScopStat.cpp
  7. The scope statistics dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "ScopStat.h"
  12. #include "scope.h"
  13. #include "server.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. enum
  20. {
  21. SCOPE_STAT_TOTAL_ADDRESSES = 0,
  22. SCOPE_STAT_IN_USE,
  23. SCOPE_STAT_AVAILABLE,
  24. SCOPE_STAT_MAX
  25. };
  26. /*---------------------------------------------------------------------------
  27. CScopeStats implementation
  28. ---------------------------------------------------------------------------*/
  29. const ContainerColumnInfo s_rgScopeStatsColumnInfo[] =
  30. {
  31. { IDS_STATS_TOTAL_ADDRESSES, 0, TRUE },
  32. { IDS_STATS_IN_USE, 0, TRUE },
  33. { IDS_STATS_AVAILABLE, 0, TRUE },
  34. };
  35. CScopeStats::CScopeStats()
  36. : StatsDialog(STATSDLG_VERTICAL)
  37. {
  38. SetColumnInfo(s_rgScopeStatsColumnInfo,
  39. DimensionOf(s_rgScopeStatsColumnInfo));
  40. }
  41. CScopeStats::~CScopeStats()
  42. {
  43. }
  44. BEGIN_MESSAGE_MAP(CScopeStats, StatsDialog)
  45. //{{AFX_MSG_MAP(CScopeStats)
  46. //}}AFX_MSG_MAP
  47. ON_MESSAGE(WM_NEW_STATS_AVAILABLE, OnNewStatsAvailable)
  48. END_MESSAGE_MAP()
  49. HRESULT CScopeStats::RefreshData(BOOL fGrabNewData)
  50. {
  51. if (fGrabNewData)
  52. {
  53. DWORD dwError = 0;
  54. LPDHCP_MIB_INFO pMibInfo = NULL;
  55. BEGIN_WAIT_CURSOR;
  56. dwError = ::DhcpGetMibInfo(m_strServerAddress, &pMibInfo);
  57. END_WAIT_CURSOR;
  58. if (dwError != ERROR_SUCCESS)
  59. {
  60. ::DhcpMessageBox(dwError);
  61. return hrOK;
  62. }
  63. UpdateWindow(pMibInfo);
  64. if (pMibInfo)
  65. ::DhcpRpcFreeMemory(pMibInfo);
  66. }
  67. return hrOK;
  68. }
  69. BOOL CScopeStats::OnInitDialog()
  70. {
  71. CString st, strScopeAddress;
  72. BOOL bRet;
  73. UtilCvtIpAddrToWstr(m_dhcpSubnetAddress, &strScopeAddress);
  74. AfxFormatString1(st, IDS_SCOPE_STATS_TITLE, strScopeAddress);
  75. SetWindowText((LPCTSTR) st);
  76. bRet = StatsDialog::OnInitDialog();
  77. // Set the default column widths to the width of the widest column
  78. SetColumnWidths(2 /* Number of Columns */);
  79. return bRet;
  80. }
  81. void CScopeStats::Sort(UINT nColumnId)
  82. {
  83. // we don't sort any of our stats
  84. }
  85. afx_msg long CScopeStats::OnNewStatsAvailable(UINT wParam, LONG lParam)
  86. {
  87. SPITFSNode spNode;
  88. CDhcpScope * pScope;
  89. CDhcpServer * pServer;
  90. pScope = GETHANDLER(CDhcpScope, m_spNode);
  91. pServer = pScope->GetServerObject();
  92. LPDHCP_MIB_INFO pMibInfo = pServer->DuplicateMibInfo();
  93. Assert(pMibInfo);
  94. if (!pMibInfo)
  95. return 0;
  96. UpdateWindow(pMibInfo);
  97. pServer->FreeDupMibInfo(pMibInfo);
  98. return 0;
  99. }
  100. void CScopeStats::UpdateWindow(LPDHCP_MIB_INFO pMibInfo)
  101. {
  102. Assert (pMibInfo);
  103. UINT i;
  104. int nTotalAddresses = 0, nTotalInUse = 0, nTotalAvailable = 0;
  105. if (pMibInfo)
  106. {
  107. LPSCOPE_MIB_INFO pScopeMibInfo = pMibInfo->ScopeInfo;
  108. // walk the list of scopes and calculate totals
  109. for (i = 0; i < pMibInfo->Scopes; i++)
  110. {
  111. if (pScopeMibInfo[i].Subnet == m_dhcpSubnetAddress)
  112. {
  113. nTotalAddresses += (pScopeMibInfo[i].NumAddressesInuse + pScopeMibInfo[i].NumAddressesFree);
  114. nTotalInUse = pScopeMibInfo[i].NumAddressesInuse;
  115. nTotalAvailable = pScopeMibInfo[i].NumAddressesFree;
  116. break;
  117. }
  118. }
  119. }
  120. int nPercent;
  121. CString st;
  122. TCHAR szFormat[] = _T("%d");
  123. TCHAR szPercentFormat[] = _T("%d (%d%%)");
  124. for (i = 0; i < SCOPE_STAT_MAX; i++)
  125. {
  126. if (!pMibInfo)
  127. st = _T("---");
  128. else
  129. {
  130. switch (i)
  131. {
  132. case SCOPE_STAT_TOTAL_ADDRESSES:
  133. st.Format(szFormat, nTotalAddresses);
  134. break;
  135. case SCOPE_STAT_IN_USE:
  136. if (nTotalAddresses > 0)
  137. nPercent = (int)(((LONGLONG)nTotalInUse * (LONGLONG)100) / nTotalAddresses);
  138. else
  139. nPercent = 0;
  140. st.Format(szPercentFormat, nTotalInUse, nPercent);
  141. break;
  142. case SCOPE_STAT_AVAILABLE:
  143. if (nTotalAddresses > 0)
  144. nPercent = (int)(((LONGLONG)nTotalAvailable * (LONGLONG)100) / nTotalAddresses);
  145. else
  146. nPercent = 0;
  147. st.Format(szPercentFormat, nTotalAvailable, nPercent);
  148. break;
  149. default:
  150. Panic1("Unknown scope stat id : %d", i);
  151. break;
  152. }
  153. }
  154. m_listCtrl.SetItemText(i, 1, (LPCTSTR) st);
  155. }
  156. }