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.

214 lines
5.5 KiB

  1. /*---------------------------------------------------------------------------
  2. File: MonitorRunning.cpp
  3. Comments: This is the entry point for a thread which will periodically try to connect
  4. to the agents that the monitor thinks are running, to see if they are really still running.
  5. This will keep the monitor from getting into a state where it thinks agents
  6. are still running, when they are not.
  7. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  8. Proprietary and confidential to Mission Critical Software, Inc.
  9. REVISION LOG ENTRY
  10. Revision By: Christy Boles
  11. ---------------------------------------------------------------------------
  12. */
  13. #include "stdafx.h"
  14. #include "DetDlg.h"
  15. #include "Common.hpp"
  16. #include "AgRpcUtl.h"
  17. #include "Monitor.h"
  18. #include "ServList.hpp"
  19. #include "ResStr.h"
  20. //#include "..\AgtSvc\AgSvc.h"
  21. #include "AgSvc.h"
  22. /*#import "\bin\McsEADCTAgent.tlb" no_namespace , named_guids
  23. //#import "\bin\McsVarSetMin.tlb" no_namespace */
  24. //#import "Engine.tlb" no_namespace , named_guids //already #imported via DetDlg.h
  25. #import "VarSet.tlb" no_namespace rename("property", "aproperty")
  26. DWORD
  27. TryConnectAgent(
  28. TServerNode * node
  29. )
  30. {
  31. DWORD rc;
  32. HRESULT hr;
  33. HANDLE hBinding = NULL;
  34. WCHAR * sBinding = NULL;
  35. WCHAR server[40];
  36. IUnknown * pUnk = NULL;
  37. IVarSetPtr pVarSet;
  38. IDCTAgentPtr pAgent;
  39. _bstr_t jobID;
  40. BOOL bSuccess = FALSE;
  41. BOOL bQueryFailed = TRUE;
  42. BOOL bFinished = FALSE;
  43. CString status;
  44. server[0] = L'\\';
  45. server[1] = L'\\';
  46. UStrCpy(server+2,node->GetServer());
  47. rc = EaxBindCreate(server,&hBinding,&sBinding,TRUE);
  48. if ( ! rc )
  49. {
  50. hr = CoInitialize(NULL);
  51. if ( SUCCEEDED(hr) )
  52. {
  53. rc = DoRpcQuery(hBinding,&pUnk);
  54. }
  55. else
  56. {
  57. rc = hr;
  58. }
  59. if ( ! rc && pUnk )
  60. {
  61. try {
  62. // we got an interface pointer to the agent: try to query it
  63. pAgent = pUnk;
  64. pUnk->Release();
  65. pUnk = NULL;
  66. hr = pAgent->raw_QueryJobStatus(jobID,&pUnk);
  67. if ( SUCCEEDED(hr) )
  68. {
  69. bQueryFailed = FALSE;
  70. pVarSet = pUnk;
  71. pUnk->Release();
  72. _bstr_t text = pVarSet->get(GET_BSTR(DCTVS_JobStatus));
  73. if ( !UStrICmp(text,GET_STRING(IDS_DCT_Status_Completed)) )
  74. {
  75. // the agent is really finished
  76. status.LoadString(IDS_AgentFinishedNoResults);
  77. bFinished = TRUE;
  78. }
  79. }
  80. }
  81. catch ( ... )
  82. {
  83. // the DCOM connection didn't work
  84. // This means we can't tell whether the agent is running or not
  85. bQueryFailed = TRUE;
  86. }
  87. }
  88. else
  89. {
  90. if ( rc == RPC_S_SERVER_UNAVAILABLE )
  91. {
  92. status.LoadString(IDS_AgentNoResults);
  93. bFinished = TRUE;
  94. }
  95. else if ( rc == E_NOTIMPL )
  96. {
  97. status.LoadString(IDS_CantMonitorOnNt351);
  98. }
  99. else
  100. {
  101. status.LoadString(IDS_CannotConnectToAgent);
  102. }
  103. bQueryFailed = TRUE;
  104. }
  105. }
  106. EaxBindDestroy(&hBinding,&sBinding);
  107. node->SetMessageText(status.GetBuffer(0));
  108. if ( bFinished )
  109. {
  110. node->SetFinished();
  111. // make sure the results can be read
  112. WCHAR directory[MAX_PATH];
  113. WCHAR filename[MAX_PATH];
  114. gData.GetResultDir(directory);
  115. swprintf(filename,GET_STRING(IDS_AgentResultFileFmt),node->GetJobFile());
  116. ProcessResults(node,directory,filename);
  117. if ( *node->GetMessageText() )
  118. node->SetFailed();
  119. }
  120. else if ( bQueryFailed )
  121. {
  122. node->SetQueryFailed();
  123. }
  124. if( bFinished || bQueryFailed )
  125. {
  126. // send a message to the server list
  127. HWND listWnd;
  128. gData.GetListWindow(&listWnd);
  129. // SendMessage(listWnd,DCT_UPDATE_ENTRY,NULL,(long)node);
  130. SendMessage(listWnd,DCT_UPDATE_ENTRY,NULL,(LPARAM)node);
  131. }
  132. return rc;
  133. }
  134. typedef TServerNode * PSERVERNODE;
  135. DWORD __stdcall
  136. MonitorRunningAgents(void * /*arg*/)
  137. {
  138. int nRunning = 0;
  139. DWORD rc = 0;
  140. BOOL bDone;
  141. do
  142. {
  143. // twenty minutes
  144. for ( long l = 0 ; l < 20 * 60 ; l++ )
  145. {
  146. Sleep( 1000 );
  147. // Check to see how many agents are running
  148. gData.GetDone(&bDone);
  149. if ( bDone )
  150. break;
  151. }
  152. if ( bDone )
  153. break;
  154. gData.Lock();
  155. TServerList * sList = gData.GetUnsafeServerList();
  156. TNodeListEnum e;
  157. TServerNode ** Running = new PSERVERNODE[sList->Count()];
  158. TServerNode * node;
  159. nRunning = 0;
  160. for ( node = (TServerNode*)e.OpenFirst(sList) ; node ; node = (TServerNode*)e.Next() )
  161. {
  162. if ( node->IsRunning() )
  163. {
  164. Running[nRunning] = node;
  165. nRunning++;
  166. }
  167. }
  168. gData.Unlock();
  169. // for each running agent, check to see if it is really still running
  170. for ( int i = 0 ; i < nRunning; i++ )
  171. {
  172. rc = TryConnectAgent(Running[i]);
  173. }
  174. delete [] Running;
  175. gData.GetDone(&bDone);
  176. } while ( ! bDone );
  177. return 0;
  178. }