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.

448 lines
11 KiB

  1. #include "WLBS_Provider.h"
  2. #include "WLBS_ParticipatingNode.h"
  3. #include "ClusterWrapper.h"
  4. #include "ControlWrapper.h"
  5. #include "utils.h"
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //
  8. // CWLBS_ParticipatingNode::CWLBS_ParticipatingNode
  9. //
  10. // Purpose: Constructor
  11. //
  12. ////////////////////////////////////////////////////////////////////////////////
  13. CWLBS_ParticipatingNode::CWLBS_ParticipatingNode(CWbemServices* a_pNameSpace,
  14. IWbemObjectSink* a_pResponseHandler)
  15. : CWlbs_Root( a_pNameSpace, a_pResponseHandler ), m_pNode(NULL)
  16. {
  17. m_pNode = new CWLBS_Node ( a_pNameSpace, a_pResponseHandler );
  18. if( !m_pNode )
  19. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  20. }
  21. ////////////////////////////////////////////////////////////////////////////////
  22. //
  23. // CWLBS_ParticipatingNode::~CWLBS_ParticipatingNode
  24. //
  25. // Purpose: Destructor
  26. //
  27. ////////////////////////////////////////////////////////////////////////////////
  28. CWLBS_ParticipatingNode::~CWLBS_ParticipatingNode()
  29. {
  30. if( m_pNode )
  31. delete m_pNode;
  32. }
  33. ////////////////////////////////////////////////////////////////////////////////
  34. //
  35. // CWLBS_ParticipatingNode::Create
  36. //
  37. // Purpose: This instantiates this class and is invoked from an array of
  38. // function pointers.
  39. //
  40. ////////////////////////////////////////////////////////////////////////////////
  41. CWlbs_Root* CWLBS_ParticipatingNode::Create
  42. (
  43. CWbemServices* a_pNameSpace,
  44. IWbemObjectSink* a_pResponseHandler
  45. )
  46. {
  47. g_pWlbsControl->CheckMembership();
  48. CWlbs_Root* pRoot = new CWLBS_ParticipatingNode( a_pNameSpace, a_pResponseHandler );
  49. if( !pRoot )
  50. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  51. return pRoot;
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////
  54. //
  55. // CWLBS_ParticipatingNode::GetInstance
  56. //
  57. // Purpose:
  58. //
  59. ////////////////////////////////////////////////////////////////////////////////
  60. HRESULT CWLBS_ParticipatingNode::GetInstance
  61. (
  62. const ParsedObjectPath* /* a_pParsedPath */,
  63. long /* a_lFlags */,
  64. IWbemContext* /* a_pIContex */
  65. )
  66. {
  67. IWbemClassObject* pWlbsInstance = NULL;
  68. HRESULT hRes = 0;
  69. try {
  70. //TODO: remove
  71. throw _com_error( WBEM_E_NOT_SUPPORTED );
  72. /*
  73. //get the node
  74. FindInstance( &pWlbsInstance, a_pParsedPath );
  75. //send the results back to WinMgMt
  76. m_pResponseHandler->Indicate( 1, &pWlbsInstance );
  77. if( pWlbsInstance ) {
  78. pWlbsInstance->Release();
  79. pWlbsInstance = NULL;
  80. }
  81. m_pResponseHandler->SetStatus( 0, WBEM_S_NO_ERROR, NULL, NULL );
  82. */
  83. hRes = WBEM_S_NO_ERROR;
  84. }
  85. catch(CErrorWlbsControl Err) {
  86. IWbemClassObject* pWbemExtStat = NULL;
  87. CreateExtendedStatus( m_pNameSpace,
  88. &pWbemExtStat,
  89. Err.Error(),
  90. (PWCHAR)(Err.Description()) );
  91. m_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  92. if( pWbemExtStat )
  93. pWbemExtStat->Release();
  94. if( pWlbsInstance )
  95. pWlbsInstance->Release();
  96. //do not return WBEM_E_FAILED, this causes a race condition
  97. hRes = WBEM_S_NO_ERROR;
  98. }
  99. catch(_com_error HResErr ) {
  100. m_pResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  101. if( pWlbsInstance )
  102. pWlbsInstance->Release();
  103. hRes = HResErr.Error();
  104. }
  105. catch(...) {
  106. if( pWlbsInstance )
  107. pWlbsInstance->Release();
  108. throw;
  109. }
  110. return hRes;
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////
  113. //
  114. // CWLBS_ParticipatingNode::EnumInstances
  115. //
  116. // Purpose: Queries WLBS for desired node instances then constructs an
  117. // an associator for each node found.
  118. //
  119. ////////////////////////////////////////////////////////////////////////////////
  120. HRESULT CWLBS_ParticipatingNode::EnumInstances
  121. (
  122. BSTR /* a_bstrClass */,
  123. long /* a_lFlags */,
  124. IWbemContext* /* a_pIContex */
  125. )
  126. {
  127. IWbemClassObject** ppWlbsInstance = NULL;
  128. WLBS_RESPONSE* pResponse = NULL;
  129. HRESULT hRes = 0;
  130. long nNumNodes = 0;
  131. try {
  132. DWORD dwNumClusters = 0;
  133. CWlbsClusterWrapper** ppCluster = NULL;
  134. g_pWlbsControl->EnumClusters(ppCluster, &dwNumClusters);
  135. if (dwNumClusters == 0)
  136. {
  137. throw _com_error( WBEM_E_NOT_FOUND );
  138. }
  139. //declare an IWbemClassObject smart pointer
  140. IWbemClassObjectPtr pWlbsClass;
  141. //get the MOF class object
  142. hRes = m_pNameSpace->GetObject(
  143. _bstr_t( MOF_PARTICIPATINGNODE::szName ),
  144. 0,
  145. NULL,
  146. &pWlbsClass,
  147. NULL );
  148. if( FAILED( hRes ) ) {
  149. throw _com_error( hRes );
  150. }
  151. for (DWORD iCluster=0; iCluster<dwNumClusters; iCluster++)
  152. {
  153. //call the API query function to find the nodes
  154. try {
  155. m_pNode->FindAllInstances(ppCluster[iCluster], &pResponse, nNumNodes );
  156. } catch (CErrorWlbsControl Err) {
  157. //
  158. // Skip this cluster
  159. //
  160. TRACE_ERROR1("CWLBS_ParticipatingNode::EnumInstances skiped cluster %x",
  161. ppCluster[iCluster]->GetClusterIP());
  162. continue;
  163. }
  164. //spawn an instance of the participating node associator
  165. //for each node found
  166. ppWlbsInstance = new IWbemClassObject *[nNumNodes];
  167. if( !ppWlbsInstance )
  168. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  169. //initialize array
  170. ZeroMemory( ppWlbsInstance, nNumNodes * sizeof(IWbemClassObject *) );
  171. for(int i = 0; i < nNumNodes; i ++ ) {
  172. hRes = pWlbsClass->SpawnInstance( 0, &ppWlbsInstance[i] );
  173. if( FAILED( hRes ) )
  174. throw _com_error( hRes );
  175. FillWbemInstance(ppCluster[iCluster], *(ppWlbsInstance + i), pResponse + i );
  176. }
  177. //send the results back to WinMgMt
  178. hRes = m_pResponseHandler->Indicate( nNumNodes, ppWlbsInstance );
  179. if( FAILED( hRes ) ) {
  180. throw _com_error( hRes );
  181. }
  182. if( ppWlbsInstance ) {
  183. for( i = 0; i < nNumNodes; i++ ) {
  184. if( ppWlbsInstance[i] ) {
  185. ppWlbsInstance[i]->Release();
  186. }
  187. }
  188. delete [] ppWlbsInstance;
  189. }
  190. if( pResponse )
  191. delete [] pResponse;
  192. }
  193. m_pResponseHandler->SetStatus( 0, WBEM_S_NO_ERROR, NULL, NULL );
  194. hRes = WBEM_S_NO_ERROR;
  195. }
  196. catch(CErrorWlbsControl Err) {
  197. IWbemClassObject* pWbemExtStat = NULL;
  198. CreateExtendedStatus( m_pNameSpace,
  199. &pWbemExtStat,
  200. Err.Error(),
  201. (PWCHAR)(Err.Description()) );
  202. m_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  203. if( pWbemExtStat )
  204. pWbemExtStat->Release();
  205. if( ppWlbsInstance ) {
  206. for(int i = 0; i < nNumNodes; i++ ) {
  207. if( ppWlbsInstance[i] ) {
  208. ppWlbsInstance[i]->Release();
  209. }
  210. }
  211. delete [] ppWlbsInstance;
  212. }
  213. if( pResponse )
  214. delete [] pResponse;
  215. //do not return WBEM_E_FAILED, this causes a race condition
  216. hRes = WBEM_S_NO_ERROR;
  217. }
  218. catch(_com_error HResErr ) {
  219. m_pResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  220. if( ppWlbsInstance ) {
  221. for(int i = 0; i < nNumNodes; i++ ) {
  222. if( ppWlbsInstance[i] ) {
  223. ppWlbsInstance[i]->Release();
  224. }
  225. }
  226. delete [] ppWlbsInstance;
  227. }
  228. if( pResponse )
  229. delete [] pResponse;
  230. hRes = HResErr.Error();
  231. }
  232. catch(...) {
  233. if( ppWlbsInstance ) {
  234. for(int i = 0; i < nNumNodes; i++ ) {
  235. if( ppWlbsInstance[i] ) {
  236. ppWlbsInstance[i]->Release();
  237. }
  238. }
  239. delete [] ppWlbsInstance;
  240. }
  241. if( pResponse )
  242. delete [] pResponse;
  243. throw;
  244. }
  245. return hRes;
  246. }
  247. ////////////////////////////////////////////////////////////////////////////////
  248. //
  249. // CWLBS_ParticipatingNode::FindInstance
  250. //
  251. // Purpose: This routine determines if a host is within the local cluster. If
  252. // it is, then the requested associator is returned.
  253. //
  254. ////////////////////////////////////////////////////////////////////////////////
  255. void CWLBS_ParticipatingNode::FindInstance
  256. (
  257. IWbemClassObject** /* a_ppWbemInstance */,
  258. const ParsedObjectPath* /* a_pParsedPath */
  259. )
  260. {
  261. }
  262. ////////////////////////////////////////////////////////////////////////////////
  263. //
  264. // CWLBS_ParticipatingNode::FillWbemInstance
  265. //
  266. // Purpose: This constructs the ParticipatingNode wbem associator.
  267. //
  268. ////////////////////////////////////////////////////////////////////////////////
  269. void CWLBS_ParticipatingNode::FillWbemInstance
  270. (
  271. CWlbsClusterWrapper* pCluster,
  272. IWbemClassObject* a_pWbemInstance,
  273. WLBS_RESPONSE* a_pResponse
  274. )
  275. {
  276. namespace PNODE = MOF_PARTICIPATINGNODE;
  277. ASSERT( a_pWbemInstance );
  278. ASSERT( a_pResponse );
  279. ParsedObjectPath NodePath;
  280. ParsedObjectPath ClusterPath;
  281. LPWSTR szNodePath = NULL;
  282. LPWSTR szClusterPath = NULL;
  283. try {
  284. //set the names of the classes
  285. if( !NodePath.SetClassName( MOF_NODE::szName ) )
  286. throw _com_error( WBEM_E_FAILED );
  287. if( !ClusterPath.SetClassName( MOF_CLUSTER::szName ) )
  288. throw _com_error( WBEM_E_FAILED );
  289. //Get the node name
  290. wstring wstrHostName;
  291. ConstructHostName( wstrHostName, pCluster->GetClusterIpOrIndex(g_pWlbsControl),
  292. a_pResponse->id );
  293. //Get the cluster name
  294. wstring wstrClusterName;
  295. AddressToString( pCluster->GetClusterIpOrIndex(g_pWlbsControl), wstrClusterName );
  296. //set the keys for the node and cluster
  297. if( !NodePath.AddKeyRef( MOF_NODE::pProperties[MOF_NODE::NAME],
  298. &_variant_t(wstrHostName.c_str()) ) )
  299. throw _com_error( WBEM_E_FAILED );
  300. if( !ClusterPath.AddKeyRef( MOF_CLUSTER::pProperties[MOF_CLUSTER::NAME],
  301. &_variant_t(wstrClusterName.c_str())) )
  302. throw _com_error( WBEM_E_FAILED );
  303. //convert parsed object paths to strings
  304. if (CObjectPathParser::Unparse(&NodePath, &szNodePath) != CObjectPathParser::NoError)
  305. throw _com_error( WBEM_E_FAILED );
  306. if (CObjectPathParser::Unparse(&ClusterPath, &szClusterPath) != CObjectPathParser::NoError)
  307. throw _com_error( WBEM_E_FAILED );
  308. //Node reference
  309. HRESULT hRes = a_pWbemInstance->Put
  310. (
  311. _bstr_t( PNODE::pProperties[PNODE::NODE] ),
  312. 0,
  313. &_variant_t(szNodePath),
  314. NULL
  315. );
  316. if( FAILED( hRes ) )
  317. throw _com_error( hRes );
  318. //Cluster reference
  319. hRes = a_pWbemInstance->Put
  320. (
  321. _bstr_t( PNODE::pProperties[PNODE::CLUSTER] ),
  322. 0,
  323. &_variant_t(szClusterPath),
  324. NULL
  325. );
  326. if( FAILED( hRes ) )
  327. throw _com_error( hRes );
  328. //free resources
  329. ClusterPath.ClearKeys();
  330. NodePath.ClearKeys();
  331. if( szNodePath )
  332. delete (szNodePath);
  333. if( szClusterPath )
  334. delete (szClusterPath);
  335. } catch (...) {
  336. ClusterPath.ClearKeys();
  337. NodePath.ClearKeys();
  338. if( szNodePath )
  339. delete (szNodePath);
  340. if( szClusterPath )
  341. delete (szClusterPath);
  342. throw;
  343. }
  344. }