mod_healthmanager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #ifndef __MOD_HEALTHMANAGER_H
  2. #define __MOD_HEALTHMANAGER_H
  3. #include <map>
  4. #include "SpBase.h"
  5. #if defined(RVC_OS_LINUX)
  6. #include "modVer.h"
  7. #include "SpTest.h"
  8. #endif
  9. #include "HealthManagerFSM.h"
  10. #include "EventCode.h"
  11. #include "HealthManager_server_g.h"
  12. using namespace HealthManager;
  13. #include "UpgradeManager_client_g.h"
  14. using namespace std;
  15. using namespace UpgradeManager;
  16. #include "GuardianBase.h"
  17. typedef ErrorCodeEnum (*pfShakeHands)(WorkStateEnum &eState);
  18. typedef ErrorCodeEnum (*pfPushUpdateTask)(const char *pszPackName);
  19. typedef bool (*pfIsInstalling)();
  20. typedef ErrorCodeEnum (*pfUpgradeRestart)(const DWORD dwParam1,const DWORD dwParam2);
  21. typedef ErrorCodeEnum (*pfFrameworkQuit)(RebootTriggerEnum eReason);
  22. #define DEPLOYSTATE_NOTINIT 0
  23. #define DEPLOYSTATE_DONE 1
  24. #define DEPLOYSTATE_FAILED 2
  25. struct ModuleRunInfo
  26. {
  27. DWORD dwStart;
  28. int abnormalCount;
  29. bool bAbnormalBusy;
  30. };
  31. struct RestartModeInfo
  32. {
  33. DWORD dwTimes;
  34. DWORD dwInternal;
  35. };
  36. #define MACSESION 6
  37. #include <winpr/sysinfo.h>
  38. static void GetLocalTimeRVC(SYSTEMTIME& stTime)
  39. {
  40. GetLocalTime(&stTime);
  41. }
  42. class HealthManagerSession : public HealthManagerService_ServerSessionBase
  43. {
  44. public:
  45. HealthManagerSession(CHealthManagerEntity *pEntity):m_pEntity(pEntity){}
  46. virtual ~HealthManagerSession(){}
  47. virtual void Handle_EnterState(SpReqAnsContext<HealthManagerService_EnterState_Req, HealthManagerService_EnterState_Ans>::Pointer ctx);
  48. virtual void Handle_ExitState(SpReqAnsContext<HealthManagerService_ExitState_Req, HealthManagerService_ExitState_Ans>::Pointer ctx);
  49. virtual void Handle_DoEvent(SpReqAnsContext<HealthManagerService_DoEvent_Req, HealthManagerService_DoEvent_Ans>::Pointer ctx);
  50. virtual void Handle_RealCheck(SpReqAnsContext<HealthManagerService_RealCheck_Req, HealthManagerService_RealCheck_Ans>::Pointer ctx);
  51. virtual void Handle_GetNetworkState(SpReqAnsContext<HealthManagerService_GetNetworkState_Req, HealthManagerService_GetNetworkState_Ans>::Pointer ctx);
  52. virtual void Handle_QueryHardwareInfo(SpReqAnsContext<HealthManagerService_QueryHardwareInfo_Req, HealthManagerService_QueryHardwareInfo_Ans>::Pointer ctx);
  53. virtual void Handle_ReadCenterConfigStr(SpReqAnsContext<HealthManagerService_ReadCenterConfigStr_Req, HealthManagerService_ReadCenterConfigStr_Ans>::Pointer ctx);
  54. virtual void Handle_GetAuthErrMsg(SpReqAnsContext<HealthManagerService_GetAuthErrMsg_Req, HealthManagerService_GetAuthErrMsg_Ans>::Pointer ctx);
  55. private:
  56. CHealthManagerEntity *m_pEntity;
  57. };
  58. class CHealthManagerEntity : public CEntityBase, public ILogListener, public IEntityStateListener, public ISysVarListener, public IBroadcastListener
  59. ,public ITimerListener, public ICallbackListener
  60. {
  61. public:
  62. CHealthManagerEntity() :
  63. m_bWaitRestartPC(false)
  64. , m_bScreenLock(false)
  65. , m_bSayIdle(false), m_bGuardianRun(false), m_guardianCount(1), m_pUpgMgr(NULL)
  66. , m_bVerRollback(false)
  67. , m_menuChoice(""), m_menuPre(""), m_netState("N"), m_bBrowserIdleFirst(true)
  68. , m_bInMainPage(false), m_stopSelfCheck(0)
  69. , m_bEnterMainPageEver(false),m_bHaveThrowMainPage(false)
  70. ,m_bNeedAuthRetry(false), m_bNeedGuardianRestart(true), m_bNeedGuardian(true)
  71. , m_bToRestartByCenterSetting(false){}
  72. virtual ~CHealthManagerEntity() {}
  73. virtual const char *GetEntityName() const { return "HealthManager"; }
  74. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  75. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  76. {
  77. CSystemRunInfo runInfo = { 0 };
  78. ErrorCodeEnum ec = GetFunction()->GetSystemRunInfo(runInfo);
  79. if (runInfo.autoTest) //测试模式,该模式下无需启动guardian
  80. {
  81. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Test mode, no need to start guardian.");
  82. m_bNeedGuardian = false;
  83. if (CheckGuardianIsRun() > 0)//or true?
  84. {
  85. bool bStop = StopGuardian();
  86. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Test mode, guardian running, to stop guardian:%d", bStop);
  87. }
  88. }
  89. ErrorCodeEnum eErrCode = m_fsm.Init(this);
  90. if (eErrCode == Error_Succeed) {
  91. DoRestart();
  92. }
  93. GetFunction()->SubscribeLog(m_uuidUpdate,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"UpgradeManager");
  94. GetFunction()->SubscribeLog(m_uuidAccessAuth,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"AccessAuthorization");
  95. GetFunction()->SubscribeLog(m_uuidAccessAuth,this,Log_Error,Severity_Middle,Error_IgnoreAll,-1,"AccessAuthorization");
  96. GetFunction()->SubscribeLog(m_uuidFaceTracking,this,Log_Error,Severity_High,Error_IgnoreAll,-1,"FaceTracking");
  97. GetFunction()->SubscribeLog(m_uuidMediaController,this,Log_Error,Severity_High,Error_IgnoreAll,-1,"MediaController");
  98. GetFunction()->SubscribeLog(m_uuidRemoteController,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"RemoteController");
  99. GetFunction()->SubscribeLog(m_uuidGUIConsole,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"GUIConsole");
  100. GetFunction()->SubscribeLog(m_uuidHeartBeat,this,Log_Event,Severity_Middle,Error_IgnoreAll,-1,"HeartBeat",false);
  101. GetFunction()->SubscribeLog(m_uuidCenterS, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "CenterSetting");
  102. GetFunction()->SubscribeLog(m_uuidPublic, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "Chromium");
  103. eErrCode = GetFunction()->SubscribeLog(m_uuidVtmLoader, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "VtmLoader");
  104. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SubscribeLog VtmLoader eErrCode:%d", eErrCode);
  105. GetFunction()->RegistSysVarEvent("UIState", this);
  106. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  107. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  108. eErrCode = pFuncPrivilege->RegistEntityStateEvent(NULL, this);
  109. if (eErrCode != Error_Succeed)
  110. {
  111. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("RegistEntityState failed(%d).", eErrCode);
  112. pTransactionContext->SendAnswer(eErrCode);
  113. return;
  114. }
  115. //UpgradeManager
  116. #if defined(RVC_OS_WIN)
  117. GetFunction()->SubscribeLog(m_uuidPublic, this, Log_Event, Severity_Middle, Error_IgnoreAll, -1, "PinPad");
  118. GetFunction()->SubscribeLog(m_uuid4SIPPhone, this, Log_Event, Severity_High, Error_IgnoreAll, -1, "SIPPhone");
  119. #endif //RVC_OS_WIN
  120. pTransactionContext->SendAnswer(eErrCode) ;
  121. }
  122. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  123. {
  124. pTransactionContext->SendAnswer(Error_Succeed);
  125. }
  126. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  127. {
  128. pTransactionContext->SendAnswer(Error_Succeed);
  129. }
  130. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  131. {
  132. LOG_FUNCTION();
  133. pTransactionContext->SendAnswer(Error_Succeed);
  134. m_fsm.QuitFrameworkAndSaveInfo(RebootTrigger_FrameUpgrade, RebootWay_Framework);
  135. }
  136. void EnterState(SpReqAnsContext<HealthManagerService_EnterState_Req, HealthManagerService_EnterState_Ans>::Pointer ctx)
  137. {
  138. if (ctx->Req.state == "M")
  139. {
  140. FSMEvent *pEvt = new FSMEvent(USER_EVT_MAITAIN);
  141. m_fsm.PostEventFIFO(pEvt);
  142. }
  143. ctx->Answer(Error_Succeed);
  144. }
  145. void ExitState(SpReqAnsContext<HealthManagerService_ExitState_Req, HealthManagerService_ExitState_Ans>::Pointer ctx)
  146. {
  147. if (ctx->Req.state == "M")
  148. {
  149. FSMEvent *pEvt = new FSMEvent(USER_EVT_MAITAIN_FINISHED);
  150. m_fsm.PostEventFIFO(pEvt);
  151. }
  152. ctx->Answer(Error_Succeed);
  153. }
  154. void DoEvent(SpReqAnsContext<HealthManagerService_DoEvent_Req, HealthManagerService_DoEvent_Ans>::Pointer ctx)
  155. {
  156. if (ctx->Req.code == Event_VtmLoader_EntityLoad_Finished || ctx->Req.code == Event_VtmLoader_SIPPhoneEntityLoad_Failed
  157. || ctx->Req.code == Event_VtmLoader_OtherSyncEntityLoad_Failed)
  158. {
  159. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("receive %x,entityList:%s", ctx->Req.code,ctx->Req.entityList.GetData());
  160. if (!ctx->Req.entityList.IsNullOrEmpty())
  161. m_fsm.SetLoadEntityList(ctx->Req.entityList);
  162. FSMEvent* pEvt = new FSMEvent(USER_EVT_VTMLOADER_FINISHED);
  163. if (ctx->Req.code == Event_VtmLoader_EntityLoad_Finished)
  164. {
  165. pEvt->param1 = 0;
  166. //to get entity list
  167. if (!ctx->Req.entityList.IsNullOrEmpty())
  168. {
  169. m_activeEntity.Init(100);
  170. m_activeEntity = ctx->Req.entityList.Split('|');
  171. for (int i = 0; i < m_activeEntity.GetCount(); ++i)
  172. {
  173. m_modRunInfo[m_activeEntity[i]].abnormalCount = 0;
  174. m_modRunInfo[m_activeEntity[i]].bAbnormalBusy = false;
  175. }
  176. }
  177. }
  178. else if (ctx->Req.code == Event_VtmLoader_SIPPhoneEntityLoad_Failed)
  179. pEvt->param1 = 1;
  180. else
  181. pEvt->param1 = 2;
  182. m_fsm.PostEventFIFO(pEvt);
  183. }
  184. ctx->Answer(Error_Succeed);
  185. }
  186. void GetNetworkState(SpReqAnsContext<HealthManagerService_GetNetworkState_Req, HealthManagerService_GetNetworkState_Ans>::Pointer ctx)
  187. {
  188. ctx->Ans.netDeviceState = m_netState;
  189. ctx->Ans.reserved1 = m_fsm.QueryAccessAuthStatus();
  190. Dbg("current net state %s,auth state:%d", (LPCTSTR)m_netState, ctx->Ans.reserved1);
  191. ctx->Answer(Error_Succeed);
  192. }
  193. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
  194. {
  195. return new HealthManagerSession(this);
  196. }
  197. virtual bool IsService()const{return true;}
  198. virtual bool IsMultiThread()const{return true;}
  199. void QueryHardwareInfo(SpReqAnsContext<HealthManagerService_QueryHardwareInfo_Req, HealthManagerService_QueryHardwareInfo_Ans>::Pointer ctx)
  200. {
  201. QueryHardwareInfoTask* pTask = new QueryHardwareInfoTask(&m_fsm);
  202. pTask->ctx = ctx;
  203. GetFunction()->PostThreadPoolTask(pTask);
  204. }
  205. void ReadCenterConfigStr(SpReqAnsContext<HealthManagerService_ReadCenterConfigStr_Req, HealthManagerService_ReadCenterConfigStr_Ans>::Pointer ctx);
  206. void GetAuthErrMsg(SpReqAnsContext<HealthManagerService_GetAuthErrMsg_Req, HealthManagerService_GetAuthErrMsg_Ans>::Pointer ctx);
  207. bool StartManagerDesktopPage(CSimpleStringA pageName);
  208. void WarnAndRestartFramwork();
  209. void RealCheck(SpReqAnsContext<HealthManagerService_RealCheck_Req, HealthManagerService_RealCheck_Ans>::Pointer ctx);
  210. void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp);
  211. ErrorCodeEnum ExceptionErrorProcess(const char* pszEntityName, ErrorCodeEnum eTestResult);
  212. ErrorCodeEnum CheckEntity(const char* pszEntityName, EntityTestEnum eTestType);
  213. void EntityLostProc(CSimpleStringA entityName);
  214. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  215. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  216. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo);
  217. virtual void OnSysVarEvent(const char *pszKey,
  218. const char *pszValue, const char *pszOldValue, const char *pszEntityName);
  219. virtual void OnBroadcastEvent(CUUID SubID, const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer);
  220. virtual void OnTimeout(DWORD dwTimerID);
  221. virtual void OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState);
  222. virtual void OnUserStateHook(const char* pszEntityName, DWORD dwState, DWORD dwLastState) { return; }
  223. virtual void OnCeateConnection(const char* pszCallerEntity, const char* pszServiceEntity) { return; }
  224. virtual void OnCloseConnection(const char* pszCallerEntity, const char* pszServiceEntity) { return; }
  225. private:
  226. CHealthManagerFSM m_fsm;
  227. //the following is useless?just one var is ok? oiltmp
  228. CUUID m_uuidUpdate,m_uuidMediaController,m_uuidFaceTracking,m_uuidAccessAuth
  229. , m_uuidRemoteController, m_uuidGUIConsole, m_uuidHeartBeat, m_uuidIE, m_uuidCenterS, m_uuidVtmLoader;
  230. CUUID m_uuidPublic, m_uuid4SIPPhone;
  231. int m_stopSelfCheck;
  232. int m_restartHour, m_restartMinute, m_lastHour, m_guardianCount, m_maxAbnormalTimes, m_rebootHourBegin, m_rebootHourEnd;;
  233. bool m_bWaitRestartPC, m_bScreenLock, m_bSayIdle
  234. , m_bGuardianRun, m_bVerRollback, m_bBrowserIdleFirst
  235. , m_bInMainPage, m_bEnterMainPageEver, m_bHaveThrowMainPage, m_bToRestartByCenterSetting;
  236. bool m_bNeedAuthRetry/*需要重试准入*/, m_bNeedGuardianRestart/*需要重启*/, m_bNeedGuardian;
  237. DWORD m_dwTimeOfAuthSuc;
  238. int m_maxWaitMainpageTime;
  239. ULONGLONG m_OSRunTicks;
  240. CSimpleStringA m_menuChoice,m_currentVer,m_menuPre,m_netState, m_versionEx;
  241. map<CSimpleStringA,ModuleRunInfo> m_modRunInfo;
  242. UpgradeMgrService_ClientBase *m_pUpgMgr;
  243. CAutoArray<CSimpleStringA> m_activeEntity;
  244. pfShakeHands m_pfShake;
  245. pfUpgradeRestart m_pfUpgradeRestart;
  246. CSystemStaticInfo m_sysStaticInfo;
  247. /** 启动限制配置读取以及监护模块初始化*/
  248. bool DoRestart();
  249. void OnCheckTimeTimeout();
  250. void OnSelfCheckTimeout();
  251. ErrorCodeEnum RestartModule(const char* pEntityName);
  252. void AfterWaitRestartPC();
  253. bool SaveCurrentVersion();
  254. bool SaveFrameStartTimeForUpgrade();
  255. //reture value:
  256. //0:no need guardian; 1:guardian is running; -1:guardian is not running
  257. int CheckGuardianIsRun(bool bStart=false);
  258. bool StopGuardian();
  259. bool StartGuardian();
  260. bool VersionRollBack(const char *pVerion);
  261. CSimpleStringA __ReadCenterConfigStr(CSimpleStringA key, CSimpleStringA entityName);
  262. bool IsTestMode();
  263. void ToCalcRebootHourAndMinute(int restartBegin, int restartEnd);
  264. };
  265. #endif // __MOD_HEALTHMANAGER_H
  266. struct FrameworkRestartTask : public ITaskSp
  267. {
  268. CHealthManagerEntity* m_entity;
  269. FrameworkRestartTask(CHealthManagerEntity* entity) : m_entity(entity) {}
  270. void Process()
  271. {
  272. m_entity->WarnAndRestartFramwork();
  273. }
  274. };
  275. struct WarnPromptTask : public ITaskSp
  276. {
  277. CHealthManagerEntity* m_entity;
  278. WarnPromptTask(CHealthManagerEntity* entity) : m_entity(entity) {}
  279. void Process()
  280. {
  281. m_entity->StartManagerDesktopPage("warnPrompt.html");
  282. }
  283. };
  284. struct EntityLostProcTask : public ITaskSp
  285. {
  286. CHealthManagerEntity* pEntity;
  287. CSimpleStringA entityName;
  288. EntityLostProcTask(CHealthManagerEntity* entity,CSimpleStringA entityName) : pEntity(entity),entityName(entityName) {}
  289. void Process()
  290. {
  291. pEntity->EntityLostProc(entityName);
  292. }
  293. };