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.

445 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, NULL );
  156. } catch (CErrorWlbsControl Err) {
  157. //
  158. // Skip this cluster
  159. //
  160. continue;
  161. }
  162. //spawn an instance of the participating node associator
  163. //for each node found
  164. ppWlbsInstance = new IWbemClassObject *[nNumNodes];
  165. if( !ppWlbsInstance )
  166. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  167. //initialize array
  168. ZeroMemory( ppWlbsInstance, nNumNodes * sizeof(IWbemClassObject *) );
  169. for(int i = 0; i < nNumNodes; i ++ ) {
  170. hRes = pWlbsClass->SpawnInstance( 0, &ppWlbsInstance[i] );
  171. if( FAILED( hRes ) )
  172. throw _com_error( hRes );
  173. FillWbemInstance(ppCluster[iCluster], *(ppWlbsInstance + i), pResponse + i );
  174. }
  175. //send the results back to WinMgMt
  176. hRes = m_pResponseHandler->Indicate( nNumNodes, ppWlbsInstance );
  177. if( FAILED( hRes ) ) {
  178. throw _com_error( hRes );
  179. }
  180. if( ppWlbsInstance ) {
  181. for( i = 0; i < nNumNodes; i++ ) {
  182. if( ppWlbsInstance[i] ) {
  183. ppWlbsInstance[i]->Release();
  184. }
  185. }
  186. delete [] ppWlbsInstance;
  187. }
  188. if( pResponse )
  189. delete [] pResponse;
  190. }
  191. m_pResponseHandler->SetStatus( 0, WBEM_S_NO_ERROR, NULL, NULL );
  192. hRes = WBEM_S_NO_ERROR;
  193. }
  194. catch(CErrorWlbsControl Err) {
  195. IWbemClassObject* pWbemExtStat = NULL;
  196. CreateExtendedStatus( m_pNameSpace,
  197. &pWbemExtStat,
  198. Err.Error(),
  199. (PWCHAR)(Err.Description()) );
  200. m_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  201. if( pWbemExtStat )
  202. pWbemExtStat->Release();
  203. if( ppWlbsInstance ) {
  204. for(int i = 0; i < nNumNodes; i++ ) {
  205. if( ppWlbsInstance[i] ) {
  206. ppWlbsInstance[i]->Release();
  207. }
  208. }
  209. delete [] ppWlbsInstance;
  210. }
  211. if( pResponse )
  212. delete [] pResponse;
  213. //do not return WBEM_E_FAILED, this causes a race condition
  214. hRes = WBEM_S_NO_ERROR;
  215. }
  216. catch(_com_error HResErr ) {
  217. m_pResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  218. if( ppWlbsInstance ) {
  219. for(int i = 0; i < nNumNodes; i++ ) {
  220. if( ppWlbsInstance[i] ) {
  221. ppWlbsInstance[i]->Release();
  222. }
  223. }
  224. delete [] ppWlbsInstance;
  225. }
  226. if( pResponse )
  227. delete [] pResponse;
  228. hRes = HResErr.Error();
  229. }
  230. catch(...) {
  231. if( ppWlbsInstance ) {
  232. for(int i = 0; i < nNumNodes; i++ ) {
  233. if( ppWlbsInstance[i] ) {
  234. ppWlbsInstance[i]->Release();
  235. }
  236. }
  237. delete [] ppWlbsInstance;
  238. }
  239. if( pResponse )
  240. delete [] pResponse;
  241. throw;
  242. }
  243. return hRes;
  244. }
  245. ////////////////////////////////////////////////////////////////////////////////
  246. //
  247. // CWLBS_ParticipatingNode::FindInstance
  248. //
  249. // Purpose: This routine determines if a host is within the local cluster. If
  250. // it is, then the requested associator is returned.
  251. //
  252. ////////////////////////////////////////////////////////////////////////////////
  253. void CWLBS_ParticipatingNode::FindInstance
  254. (
  255. IWbemClassObject** /* a_ppWbemInstance */,
  256. const ParsedObjectPath* /* a_pParsedPath */
  257. )
  258. {
  259. }
  260. ////////////////////////////////////////////////////////////////////////////////
  261. //
  262. // CWLBS_ParticipatingNode::FillWbemInstance
  263. //
  264. // Purpose: This constructs the ParticipatingNode wbem associator.
  265. //
  266. ////////////////////////////////////////////////////////////////////////////////
  267. void CWLBS_ParticipatingNode::FillWbemInstance
  268. (
  269. CWlbsClusterWrapper* pCluster,
  270. IWbemClassObject* a_pWbemInstance,
  271. WLBS_RESPONSE* a_pResponse
  272. )
  273. {
  274. namespace PNODE = MOF_PARTICIPATINGNODE;
  275. ASSERT( a_pWbemInstance );
  276. ASSERT( a_pResponse );
  277. ParsedObjectPath NodePath;
  278. ParsedObjectPath ClusterPath;
  279. LPWSTR szNodePath = NULL;
  280. LPWSTR szClusterPath = NULL;
  281. try {
  282. //set the names of the classes
  283. if( !NodePath.SetClassName( MOF_NODE::szName ) )
  284. throw _com_error( WBEM_E_FAILED );
  285. if( !ClusterPath.SetClassName( MOF_CLUSTER::szName ) )
  286. throw _com_error( WBEM_E_FAILED );
  287. //Get the node name
  288. wstring wstrHostName;
  289. ConstructHostName( wstrHostName, pCluster->GetClusterIpOrIndex(g_pWlbsControl),
  290. a_pResponse->id );
  291. //Get the cluster name
  292. wstring wstrClusterName;
  293. AddressToString( pCluster->GetClusterIpOrIndex(g_pWlbsControl), wstrClusterName );
  294. //set the keys for the node and cluster
  295. if( !NodePath.AddKeyRef( MOF_NODE::pProperties[MOF_NODE::NAME],
  296. &_variant_t(wstrHostName.c_str()) ) )
  297. throw _com_error( WBEM_E_FAILED );
  298. if( !ClusterPath.AddKeyRef( MOF_CLUSTER::pProperties[MOF_CLUSTER::NAME],
  299. &_variant_t(wstrClusterName.c_str())) )
  300. throw _com_error( WBEM_E_FAILED );
  301. //convert parsed object paths to strings
  302. if (CObjectPathParser::Unparse(&NodePath, &szNodePath) != CObjectPathParser::NoError)
  303. throw _com_error( WBEM_E_FAILED );
  304. if (CObjectPathParser::Unparse(&ClusterPath, &szClusterPath) != CObjectPathParser::NoError)
  305. throw _com_error( WBEM_E_FAILED );
  306. //Node reference
  307. HRESULT hRes = a_pWbemInstance->Put
  308. (
  309. _bstr_t( PNODE::pProperties[PNODE::NODE] ),
  310. 0,
  311. &_variant_t(szNodePath),
  312. NULL
  313. );
  314. if( FAILED( hRes ) )
  315. throw _com_error( hRes );
  316. //Cluster reference
  317. hRes = a_pWbemInstance->Put
  318. (
  319. _bstr_t( PNODE::pProperties[PNODE::CLUSTER] ),
  320. 0,
  321. &_variant_t(szClusterPath),
  322. NULL
  323. );
  324. if( FAILED( hRes ) )
  325. throw _com_error( hRes );
  326. //free resources
  327. ClusterPath.ClearKeys();
  328. NodePath.ClearKeys();
  329. if( szNodePath )
  330. delete (szNodePath);
  331. if( szClusterPath )
  332. delete (szClusterPath);
  333. } catch (...) {
  334. ClusterPath.ClearKeys();
  335. NodePath.ClearKeys();
  336. if( szNodePath )
  337. delete (szNodePath);
  338. if( szClusterPath )
  339. delete (szClusterPath);
  340. throw;
  341. }
  342. }