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.

575 lines
14 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 1999-2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // SANetEvent.cpp
  7. //
  8. // Description:
  9. // implement the class CSANetEvent
  10. //
  11. // History:
  12. // 1. lustar.li (Guogang Li), creation date in 7-DEC-2000
  13. //
  14. // Notes:
  15. //
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <debug.h>
  21. #include <wbemidl.h>
  22. #include <tchar.h>
  23. #include "oahelp.inl"
  24. #include "SAEventComm.h"
  25. #include "SANetEvent.h"
  26. //
  27. // Define the registry information
  28. //
  29. #define SA_NETWOARKMONITOR_REGKEY \
  30. _T("SOFTWARE\\Microsoft\\ServerAppliance\\DeviceMonitor")
  31. #define SA_NETWORKMONITOR_QUEARY_INTERVAL _T("NetworkQueryInterval")
  32. //////////////////////////////////////////////////////////////////////////////
  33. //++
  34. //
  35. // method:
  36. // CSANetEvent::CSANetEventt
  37. //
  38. // Description:
  39. // Constructor
  40. //
  41. // Arguments:
  42. // NONE
  43. //
  44. // Returns:
  45. // NONE
  46. //
  47. // History: lustar.li Created 12/7/2000
  48. //
  49. //--
  50. //////////////////////////////////////////////////////////////////////////////
  51. CSANetEvent::CSANetEvent()
  52. {
  53. m_cRef = 0;
  54. m_pNs = 0;
  55. m_pSink = 0;
  56. m_pEventClassDef = 0;
  57. m_eStatus = Pending;
  58. m_hThread = 0;
  59. m_pQueryNetInfo=NULL;
  60. }
  61. //////////////////////////////////////////////////////////////////////////////
  62. //++
  63. //
  64. // method:
  65. // CSANetEvent::~CSANetEventt
  66. //
  67. // Description:
  68. // Destructor
  69. //
  70. // Arguments:
  71. // NONE
  72. //
  73. // Returns:
  74. // NONE
  75. //
  76. // History: lustar.li Created 12/7/2000
  77. //
  78. //--
  79. //////////////////////////////////////////////////////////////////////////////
  80. CSANetEvent::~CSANetEvent()
  81. {
  82. if(m_pQueryNetInfo)
  83. delete m_pQueryNetInfo;
  84. if (m_hThread)
  85. CloseHandle(m_hThread);
  86. if (m_pNs)
  87. m_pNs->Release();
  88. if (m_pSink)
  89. m_pSink->Release();
  90. if (m_pEventClassDef)
  91. m_pEventClassDef->Release();
  92. }
  93. //////////////////////////////////////////////////////////////////////////////
  94. //++
  95. //
  96. // method:
  97. // CSANetEvent::QueryInterface
  98. //
  99. // Description:
  100. // access to interfaces on the object
  101. //
  102. // Arguments:
  103. // [in] REFIID - Identifier of the requested interface
  104. // [out] LPVOID - Address of output variable that receives the
  105. // interface pointer requested in iid
  106. // Returns:
  107. // STDMETHODIMP - fail/success
  108. //
  109. // History: lustar.li Created 12/7/2000
  110. //
  111. //--
  112. //////////////////////////////////////////////////////////////////////////////
  113. STDMETHODIMP
  114. CSANetEvent::QueryInterface(
  115. /*[in]*/ REFIID riid,
  116. /*[out]*/ LPVOID * ppv
  117. )
  118. {
  119. *ppv = 0;
  120. if (IID_IUnknown==riid || IID_IWbemEventProvider==riid)
  121. {
  122. *ppv = (IWbemEventProvider *) this;
  123. AddRef();
  124. return NOERROR;
  125. }
  126. if (IID_IWbemProviderInit==riid)
  127. {
  128. *ppv = (IWbemProviderInit *) this;
  129. AddRef();
  130. return NOERROR;
  131. }
  132. TRACE(" SANetworkMonitor: CSANetEvent::QueryInterface failed \
  133. <no interface>");
  134. return E_NOINTERFACE;
  135. }
  136. //////////////////////////////////////////////////////////////////////////////
  137. //++
  138. //
  139. // method:
  140. // CSANetEvent::AddRef
  141. //
  142. // Description:
  143. // inc referrence to the object
  144. //
  145. // Arguments:
  146. // NONE
  147. //
  148. // Returns:
  149. // ULONG - current refferrence number
  150. //
  151. // History: lustar.li Created 12/7/2000
  152. //
  153. //--
  154. //////////////////////////////////////////////////////////////////////////////
  155. ULONG
  156. CSANetEvent::AddRef()
  157. {
  158. return ++m_cRef;
  159. }
  160. //////////////////////////////////////////////////////////////////////////////
  161. //++
  162. //
  163. // method:
  164. // CSANetEvent::Release
  165. //
  166. // Description:
  167. // Dereferrence to the object
  168. //
  169. // Arguments:
  170. // NONE
  171. //
  172. // Returns:
  173. // ULONG - current refferrence number
  174. //
  175. // History: lustar.li Created 12/7/2000
  176. //
  177. //--
  178. //////////////////////////////////////////////////////////////////////////////
  179. ULONG
  180. CSANetEvent::Release()
  181. {
  182. if (0 != --m_cRef)
  183. return m_cRef;
  184. //
  185. // event provider is shutting down.
  186. //
  187. m_eStatus = PendingStop;
  188. return 0;
  189. }
  190. //////////////////////////////////////////////////////////////////////////////
  191. //++
  192. //
  193. // method:
  194. // CSANetEvent::ProvideEvents
  195. //
  196. // Description:
  197. // signal an event provider to begin delivery of its events
  198. //
  199. // Arguments:
  200. // [in] IWbemObjectSink * - pointer to event sink
  201. // [in] long - Reserved, It must be zero
  202. //
  203. // Returns:
  204. // HRESULT
  205. //
  206. // History: lustar.li Created 12/7/2000
  207. //
  208. //--
  209. //////////////////////////////////////////////////////////////////////////////
  210. HRESULT
  211. CSANetEvent::ProvideEvents(
  212. /*[in]*/ IWbemObjectSink *pSink,
  213. /*[in]*/ long lFlags
  214. )
  215. {
  216. //
  217. // Copy the sink.
  218. //
  219. m_pSink = pSink;
  220. m_pSink->AddRef();
  221. //
  222. // Create the event generation thread.
  223. //
  224. DWORD dwTID;
  225. m_hThread = CreateThread(
  226. 0,
  227. 0,
  228. CSANetEvent::EventThread,
  229. this,
  230. 0,
  231. &dwTID
  232. );
  233. if(!m_hThread)
  234. {
  235. TRACE(" SANetworkMonitor: CSANetEvent::ProvideEvents failed \
  236. <CreateThread>");
  237. return WBEM_E_FAILED;
  238. }
  239. //
  240. // Wait for provider to be 'ready'.
  241. //
  242. while (m_eStatus != Running)
  243. Sleep(100);
  244. return WBEM_NO_ERROR;
  245. }
  246. //////////////////////////////////////////////////////////////////////////////
  247. //++
  248. //
  249. // method:
  250. // CSANetEvent::EventThread
  251. //
  252. // Description:
  253. // the thread of generating and delivering event
  254. //
  255. // Arguments:
  256. // [in] LPVOID - the argument input to the thread
  257. //
  258. // Returns:
  259. // DWORD - end status of status
  260. //
  261. // History: lustar.li Created 12/7/2000
  262. //
  263. //--
  264. //////////////////////////////////////////////////////////////////////////////
  265. DWORD WINAPI
  266. CSANetEvent::EventThread(
  267. /*[in]*/ LPVOID pArg
  268. )
  269. {
  270. //
  271. // Make transition to the per-instance method.
  272. //
  273. ((CSANetEvent *)pArg)->InstanceThread();
  274. return 0;
  275. }
  276. //////////////////////////////////////////////////////////////////////////////
  277. //++
  278. //
  279. // method:
  280. // CSANetEvent::InstanceThread
  281. //
  282. // Description:
  283. // the main proccesor of thread
  284. //
  285. // Arguments:
  286. // NONE
  287. //
  288. // Returns:
  289. // NONE
  290. //
  291. // History: lustar.li Created 12/7/2000
  292. //
  293. //--
  294. //////////////////////////////////////////////////////////////////////////////
  295. void
  296. CSANetEvent::InstanceThread()
  297. {
  298. m_eStatus = Running;
  299. CBSTR bstrUniqueName = CBSTR(SA_RESOURCEEVENT_UNIQUENAME);
  300. CBSTR bstrDisplayInfo = CBSTR(SA_RESOURCEEVENT_DISPLAYINFORMATION);
  301. CBSTR bstrCurrentState = CBSTR(SA_RESOURCEEVENT_CURRENTSTATE);
  302. if ( ((BSTR)bstrUniqueName == NULL) ||
  303. ((BSTR)bstrDisplayInfo == NULL) ||
  304. ((BSTR)bstrCurrentState == NULL) )
  305. {
  306. TRACE(" SANetworkMonitor:CSANetEvent::InstanceThread failed on memory allocation ");
  307. return;
  308. }
  309. while (m_eStatus == Running)
  310. {
  311. //
  312. // Spawn a new event object.
  313. //
  314. IWbemClassObject *pEvt = 0;
  315. HRESULT hRes = m_pEventClassDef->SpawnInstance(0, &pEvt);
  316. if (hRes != 0)
  317. continue;
  318. //
  319. // Generate the network event.
  320. //
  321. CVARIANT vUniqueName(SA_NET_EVENT);
  322. pEvt->Put(
  323. bstrUniqueName,
  324. 0,
  325. vUniqueName,
  326. 0
  327. );
  328. CVARIANT vDisplayInformationID(
  329. (LONG)(m_pQueryNetInfo->GetDisplayInformation()));
  330. pEvt->Put(
  331. bstrDisplayInfo,
  332. 0,
  333. vDisplayInformationID,
  334. 0
  335. );
  336. CVARIANT vCurrentState((LONG)SA_RESOURCEEVENT_DEFAULT_CURRENTSTATE);
  337. pEvt->Put(
  338. bstrCurrentState,
  339. 0,
  340. vCurrentState,
  341. 0
  342. );
  343. //
  344. // Deliver the event to CIMOM.
  345. //
  346. hRes = m_pSink->Indicate(1, &pEvt);
  347. if (FAILED (hRes))
  348. {
  349. //
  350. // If failed, ...
  351. //
  352. TRACE(" SANetworkMonitor: CSANetEvent::InstanceThread failed \
  353. <m_pSink->Indicate>");
  354. }
  355. pEvt->Release();
  356. }
  357. //
  358. // When we get to here, we are no longer interested in the
  359. // provider and Release() has long since returned.
  360. //
  361. m_eStatus = Stopped;
  362. delete this;
  363. }
  364. //////////////////////////////////////////////////////////////////////////////
  365. //++
  366. //
  367. // method:
  368. // CSANetEvent::Initialize
  369. //
  370. // Description:
  371. // initialize the event provider
  372. //
  373. // Arguments:
  374. // [in] LPWSTR - Pointer to the user name
  375. // [in] LONG - Reserved. It must be zero
  376. // [in] LPWSTR - Namespace name for which the provider is being
  377. // initialized
  378. // [in] LPWSTR - Locale name for which the provider is being initialized
  379. // [in] IWbemServices * - An IWbemServices pointer back into
  380. // Windows Management
  381. // [in] IWbemContext * - An IWbemContext pointer associated
  382. // with initialization
  383. // [in] IWbemProviderInitSink * - report initialization status
  384. //
  385. // Returns:
  386. // HRESULT
  387. //
  388. // History: lustar.li Created 12/7/2000
  389. //
  390. //--
  391. //////////////////////////////////////////////////////////////////////////////
  392. HRESULT
  393. CSANetEvent::Initialize(
  394. /* [in] */ LPWSTR pszUser,
  395. /* [in] */ LONG lFlags,
  396. /* [in] */ LPWSTR pszNamespace,
  397. /* [in] */ LPWSTR pszLocale,
  398. /* [in] */ IWbemServices *pNamespace,
  399. /* [in] */ IWbemContext *pCtx,
  400. /* [in] */ IWbemProviderInitSink *pInitSink
  401. )
  402. {
  403. HKEY hKey;
  404. UINT uiQueryInterval;
  405. DWORD dwRegType = REG_DWORD;
  406. DWORD dwRegSize = sizeof(DWORD);
  407. //
  408. // We don't care about most of the incoming parameters in this
  409. // simple sample. However, we will save the namespace pointer
  410. // and get our event class definition.
  411. //
  412. m_pNs = pNamespace;
  413. m_pNs->AddRef();
  414. //
  415. // Grab the class definition for the event.
  416. //
  417. IWbemClassObject *pObj = 0;
  418. CBSTR bstrClassName = CBSTR(SA_RESOURCEEVENT_CLASSNAME);
  419. if ( (BSTR)bstrClassName == NULL)
  420. {
  421. TRACE(" SANetworkMonitor:CSANetEvent::Initialize failed on memory allocation ");
  422. return E_OUTOFMEMORY;
  423. }
  424. HRESULT hRes = m_pNs->GetObject(
  425. bstrClassName,
  426. 0,
  427. pCtx,
  428. &pObj,
  429. 0
  430. );
  431. if (hRes != 0)
  432. {
  433. return WBEM_E_FAILED;
  434. }
  435. m_pEventClassDef = pObj;
  436. //
  437. // From registry get the interval of query network
  438. //
  439. LONG lRes = RegOpenKey(
  440. HKEY_LOCAL_MACHINE,
  441. SA_NETWOARKMONITOR_REGKEY,
  442. &hKey);
  443. if (lRes)
  444. {
  445. TRACE(" SANetworkMonitor: CSANetEvent::Initialize failed \
  446. <RegOpenKey>");
  447. //
  448. // Create the Key
  449. //
  450. lRes = RegCreateKey(
  451. HKEY_LOCAL_MACHINE,
  452. SA_NETWOARKMONITOR_REGKEY,
  453. &hKey);
  454. if(lRes)
  455. {
  456. TRACE(" SANetworkMonitor: CSANetEvent::Initialize failed \
  457. <RegCreateKey>");
  458. return WBEM_E_FAILED;
  459. }
  460. }
  461. lRes = RegQueryValueEx(
  462. hKey,
  463. SA_NETWORKMONITOR_QUEARY_INTERVAL,
  464. NULL,
  465. &dwRegType,
  466. (LPBYTE)&uiQueryInterval,
  467. &dwRegSize);
  468. if(lRes)
  469. {
  470. TRACE(" SANetworkMonitor: CSANetEvent::Initialize failed \
  471. <RegQueryValueEx>");
  472. uiQueryInterval = 1000;
  473. lRes = RegSetValueEx(
  474. hKey,
  475. SA_NETWORKMONITOR_QUEARY_INTERVAL,
  476. NULL,
  477. REG_DWORD,
  478. (LPBYTE)&uiQueryInterval,
  479. sizeof(DWORD));
  480. if(lRes)
  481. {
  482. TRACE(" SANetworkMonitor: CSANetEvent::Initialize failed \
  483. <RegSetValueEx>");
  484. RegCloseKey(hKey);
  485. return WBEM_E_FAILED;
  486. }
  487. }
  488. RegCloseKey(hKey);
  489. //
  490. // Initial m_pQueryNetInfo
  491. //
  492. m_pQueryNetInfo = NULL;
  493. m_pQueryNetInfo = new CSAQueryNetInfo(m_pNs, uiQueryInterval);
  494. if( (m_pQueryNetInfo == NULL) || (!m_pQueryNetInfo->Initialize()) )
  495. {
  496. if (m_pQueryNetInfo)
  497. {
  498. delete m_pQueryNetInfo;
  499. }
  500. pObj->Release();
  501. TRACE(" SANetworkMonitor: CSANetEvent::Initialize failed \
  502. <Init CSAQueryNetInfo>");
  503. return WBEM_E_FAILED;
  504. }
  505. //
  506. // Tell CIMOM that we're up and running.
  507. //
  508. pInitSink->SetStatus(WBEM_S_INITIALIZED,0);
  509. return WBEM_NO_ERROR;
  510. }