Source code of Windows XP (NT5)
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.

216 lines
6.3 KiB

  1. //
  2. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  3. //
  4. #include <stdio.h>
  5. #include <windows.h>
  6. /* WBEM includes */
  7. #include <wbemcli.h>
  8. #include "provlog.h"
  9. #include "refcount.h"
  10. #include "tree.h"
  11. #include "clsname.h"
  12. #include "wbemcach.h"
  13. // Initialize the statics
  14. const __int64 CWbemCache :: MAX_CACHE_AGE = 60*60*20; // In seconds, 4 Hours
  15. const DWORD CWbemCache :: MAX_CACHE_SIZE = 500;
  16. DWORD CWbemCache:: dwWBEMCacheCount = 0;
  17. DWORD CEnumInfo:: dwCEnumInfoCount = 0;
  18. DWORD CWbemClass:: dwCWbemClassCount = 0;
  19. //***************************************************************************
  20. //
  21. // CWbemCache::CWbemCache
  22. //
  23. // Purpose : Constructor. Creates an empty cache
  24. //
  25. // Parameters:
  26. //***************************************************************************
  27. CWbemCache :: CWbemCache(ProvDebugLog *pLogObject)
  28. {
  29. dwWBEMCacheCount ++;
  30. m_pLogObject = pLogObject;
  31. }
  32. //***************************************************************************
  33. //
  34. // CWbemCache::GetClass
  35. //
  36. // Purpose : Retreives the CWbemClass object, if present in the cache. Otherwise returns NULL
  37. //
  38. // Parameters:
  39. // lpszClassName : The WBEM name of the Class to be retreived.
  40. // ppWbemClass : The address of the pointer where the CWbemClass object will be placed
  41. //
  42. // Return value:
  43. // The COM value representing the return status. The user should release the WBEM cclass
  44. // when done.
  45. //
  46. //***************************************************************************
  47. HRESULT CWbemCache :: GetClass(LPCWSTR lpszWbemClassName, CWbemClass **ppWbemClass )
  48. {
  49. #ifdef NO_WBEM_CACHE
  50. return E_FAIL;
  51. #else
  52. if(*ppWbemClass = (CWbemClass *)m_objectTree.GetElement(lpszWbemClassName))
  53. {
  54. // Get the current time
  55. FILETIME fileTime;
  56. GetSystemTimeAsFileTime(&fileTime);
  57. LARGE_INTEGER currentTime;
  58. memcpy((LPVOID)&currentTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  59. // The QuadPart is in number of 100s of NanoSeconds.
  60. // Delete the object if is too old, and return failure
  61. // timeElapsed is the amount of time in seconds
  62. __int64 timeElapsed = ( currentTime.QuadPart - (*ppWbemClass)->GetCreationTime());
  63. timeElapsed = timeElapsed/(__int64)10000000;
  64. if( timeElapsed > MAX_CACHE_AGE ) // in Seconds
  65. {
  66. (*ppWbemClass)->Release();
  67. *ppWbemClass = NULL;
  68. m_objectTree.DeleteElement(lpszWbemClassName);
  69. m_pLogObject->WriteW( L"CWbemCache :: GetClass() Deleted senile class : %s\r\n", lpszWbemClassName);
  70. return E_FAIL;
  71. }
  72. // Set its last accessed time
  73. (*ppWbemClass)->SetLastAccessTime(currentTime.QuadPart);
  74. return S_OK;
  75. }
  76. return E_FAIL;
  77. #endif
  78. }
  79. //***************************************************************************
  80. //
  81. // CWbemCache::AddClass
  82. //
  83. // Purpose : Adds the CWbemClass object to the cache
  84. //
  85. // Parameters:
  86. // ppWbemClass : The CWbemClass pointer of the object to be added
  87. //
  88. // Return value:
  89. // The COM value representing the return status.
  90. // when done.
  91. //
  92. //***************************************************************************
  93. HRESULT CWbemCache :: AddClass(CWbemClass *pWbemClass )
  94. {
  95. #ifdef NO_WBEM_CACHE
  96. return E_FAIL;
  97. #else
  98. // Delete an element from the tree if its size has reached a limit of 100 nodes
  99. if(m_objectTree.GetNumberOfElements() >= MAX_CACHE_SIZE)
  100. {
  101. if(!m_objectTree.DeleteLeastRecentlyAccessedElement())
  102. return E_FAIL;
  103. m_pLogObject->WriteW( L"CWbemCache :: AddClass() Deleted LRU class from cache\r\n");
  104. }
  105. // Add the new element
  106. if(m_objectTree.AddElement(pWbemClass->GetName(), pWbemClass))
  107. {
  108. m_pLogObject->WriteW( L"CWbemCache :: AddClass() Added a class %s to cache\r\n", pWbemClass->GetName());
  109. return S_OK;
  110. }
  111. return E_FAIL;
  112. #endif
  113. }
  114. //***************************************************************************
  115. //
  116. // CWbemCache::GetEnumInfo
  117. //
  118. // Purpose : Retreives the CEnumInfo object, if present in the cache. Otherwise returns NULL
  119. //
  120. // Parameters:
  121. // lpszClassName : The WBEM name of the Class to be retreived.
  122. // ppEnumInfo : The address of the pointer where the CEnumInfo object will be placed
  123. //
  124. // Return value:
  125. // The COM value representing the return status. The user should release the EnumInfo object
  126. // when done.
  127. //
  128. //***************************************************************************
  129. HRESULT CWbemCache :: GetEnumInfo(LPCWSTR lpszWbemClassName, CEnumInfo **ppEnumInfo )
  130. {
  131. #ifdef NO_WBEM_CACHE
  132. return E_FAIL;
  133. #else
  134. if(*ppEnumInfo = (CEnumInfo *)m_EnumTree.GetElement(lpszWbemClassName))
  135. {
  136. // Get the current time
  137. FILETIME fileTime;
  138. GetSystemTimeAsFileTime(&fileTime);
  139. LARGE_INTEGER currentTime;
  140. memcpy((LPVOID)&currentTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  141. // The QuadPart is in number of 100s of NanoSeconds.
  142. // Delete the object if is too old, and return failure
  143. // timeElapsed is the amount of time in seconds
  144. __int64 timeElapsed = ( currentTime.QuadPart - (*ppEnumInfo)->GetCreationTime());
  145. timeElapsed = timeElapsed/(__int64)10000000;
  146. if( timeElapsed > MAX_CACHE_AGE ) // in Seconds
  147. {
  148. (*ppEnumInfo)->Release();
  149. *ppEnumInfo = NULL;
  150. m_EnumTree.DeleteElement(lpszWbemClassName);
  151. m_pLogObject->WriteW( L"CEnumCache :: GetClass() Deleted senile EnumInfo : %s\r\n", lpszWbemClassName);
  152. return E_FAIL;
  153. }
  154. // Set its last accessed time
  155. (*ppEnumInfo)->SetLastAccessTime(currentTime.QuadPart);
  156. return S_OK;
  157. }
  158. return E_FAIL;
  159. #endif
  160. }
  161. //***************************************************************************
  162. //
  163. // CWbemCache::AddEnumInfo
  164. //
  165. // Purpose : Adds the CEnumInfo object to the cache
  166. //
  167. // Parameters:
  168. // ppEnumInfo : The CEnumInfo pointer of the object to be added
  169. //
  170. // Return value:
  171. // The COM value representing the return status.
  172. // when done.
  173. //
  174. //***************************************************************************
  175. HRESULT CWbemCache :: AddEnumInfo(CEnumInfo *pEnumInfo )
  176. {
  177. #ifdef NO_WBEM_CACHE
  178. return E_FAIL;
  179. #else
  180. // Delete an element from the tree if its size has reached a limit of 100 nodes
  181. if(m_EnumTree.GetNumberOfElements() >= MAX_CACHE_SIZE)
  182. {
  183. if(!m_EnumTree.DeleteLeastRecentlyAccessedElement())
  184. return E_FAIL;
  185. m_pLogObject->WriteW( L"CEnumCache :: AddClass() Deleted LRU class from cache\r\n");
  186. }
  187. // Add the new element
  188. if(m_EnumTree.AddElement(pEnumInfo->GetName(), pEnumInfo))
  189. {
  190. m_pLogObject->WriteW( L"CEnumCache :: AddClass() Added a EnumInfo %s to cache\r\n", pEnumInfo->GetName());
  191. return S_OK;
  192. }
  193. return E_FAIL;
  194. #endif
  195. }