#ifndef __SELFCHECKER_FSM_H #define __SELFCHECKER_FSM_H #pragma once #include "stdafx.h" #define WIN32_LEAN_AND_MEAN #include "tinyxml2.h" #include #include #include #include //#include #ifdef RVC_OS_WIN #include #include #else #endif //RVC_OS_WIN //_CrtMemState startMemState; //_CrtMemState endMemState; #include #include #include #include #include #include "SpFSM.h" using namespace tinyxml2; using namespace std; #include "../mod_healthmanager/HealthManager_client_g.h" using namespace HealthManager; const int MAX_KEY_VALUE_SIZE = 1024; class CSelfCheckerEntity; class CSelfCheckerFSM; enum EvtType { USER_EVT_TEST = EVT_USER+1, USER_EVT_QUIT, USER_EVT_INIT, }; enum CheckPattern { CHECK_UNKNOWN, CHECK_HANDSHAKE, CHECK_EXAMINE, CHECK_RESET, CHECK_RESTART, }; enum TestActionEnum { ACTION_NOTHINGTODO, ACTION_HANDSHAKE, ACTION_EXAMINE, ACTION_RESET, ACTION_CLOSE, ACTION_ENTITY_RESTART, ACTION_OS_RESTART, ACTION_POWER_RESTART, }; struct callback_entry : public IReleasable { virtual ~callback_entry() {} CSimpleStringA EntityName; union { void *pRawData; int state; }; int op; ErrorCodeEnum ErrorResult; }; struct ErrorCfgInfo { //CheckPattern eType; ErrorCodeEnum eErrCode; TestActionEnum eAction; }; struct EntityCfgInfo { //int resetCount; int entityRestartCount; int osRestartCount; int powerRestartCount; bool bWaitRestart; map hsInfo;//handshake map examInfo;//examine map resetInfo;//reset map restartInfo;//restart }; struct EntityRunInfo { bool bGetLoadOpt; bool bRestarting; int eState; int eTest; int loadOpt; int memoryHighCount; int cpuRatio; FILETIME prevSysKernel, prevSysUser; FILETIME prevProcKernel, prevProcUser; }; struct ProcItem { DWORD code; DWORD action; int upgradecount; DWORD upgradeaction; int upgradetime; vectorproctime; }; struct EntityCfg { vector vShake; vector vWarn; }; struct EntityNameCompare { public: bool operator()(const std::string x, const std::string y)const { return x.compare(y) < 0; } }; enum ProcType { ProcType_None = 0, ProcType_Shake, ProcType_Warn, }; class HealthManagerClient : public HealthManagerService_ClientBase { public: HealthManagerClient(CEntityBase *pEntity); ~HealthManagerClient() {} }; class CSelfCheckerFSM : public FSMImpl,public ICallbackListener { public: enum {s0,s1,s2,s3}; BEGIN_FSM_STATE(CSelfCheckerFSM) FSM_STATE_ENTRY(s0, "Init", s0_on_entry, s0_on_exit, s0_on_event) FSM_STATE_ENTRY(s1, "Normal", s1_on_entry, s1_on_exit, s1_on_event) FSM_STATE_ENTRY(s2, "Check", s2_on_entry, s2_on_exit, s2_on_event) FSM_STATE_ENTRY(s3, "SingleCheck", s3_on_entry, s3_on_exit, s3_on_event) END_FSM_STATE() BEGIN_FSM_RULE(CSelfCheckerFSM, s0) FSM_RULE_ENTRY(s0, s1, USER_EVT_INIT, 0) END_FSM_RULE() CSelfCheckerFSM() :m_warmLevel(0), m_bFirstCalcCpu(true), m_pHealthClient(NULL) , m_diskLastWarnHour(1) , m_bEverInMainPage(false){} ~CSelfCheckerFSM(){} virtual ErrorCodeEnum OnInit(); virtual ErrorCodeEnum OnExit(); void s0_on_entry(); void s0_on_exit(); unsigned int s0_on_event(FSMEvent* event); void s1_on_entry(); void s1_on_exit(); unsigned int s1_on_event(FSMEvent* event); void s2_on_entry(); void s2_on_exit(); unsigned int s2_on_event(FSMEvent* event); void s3_on_entry(); void s3_on_exit(); unsigned int s3_on_event(FSMEvent* event); //ErrorCodeEnum AddEntity(const char *pszEntityName); //ErrorCodeEnum RemoveEntity(const char *pszEntityName); ErrorCodeEnum ExceptionErrorProcess(const char *pszEntityName,ErrorCodeEnum eErrCode); ErrorCodeEnum ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault = false, const char *pszMessage=""); void DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity); void DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity); void DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode); ErrorCodeEnum GetAllLiveEntity(CAutoArray &allEntitys); ErrorCodeEnum CheckEntity(const char *pszEntityName,EntityTestEnum eTestType); int AddEntityState(const char *pszEntityName,EntityStateEnum eState); ErrorCodeEnum GetEntityErrorList(int &warmLevel,CSimpleStringA &strList); void SetEntityCfgInfo(const char *pszEntityName,int loadOpt){m_entRunInfo[pszEntityName].loadOpt = loadOpt;Dbg("%s,%d,%d",(LPCTSTR)pszEntityName,loadOpt,m_entRunInfo[pszEntityName].loadOpt);}; void SetEntityRestarting(const char* pszEntityName,bool flag) { m_entRunInfo[pszEntityName].bRestarting = flag;} int Proc(string entity, ProcType eType, DWORD dwCode,const char *pszMessage=""); void SetEverEnterMainPageFlag(bool bValue = true) { m_bEverInMainPage = bValue; } private: ErrorCodeEnum Initial(); void OnNormalWorkTimerout(void *pData); void OnCalcCpuUsageTimerout(void *pData); virtual void OnAnswer(CSmartPointer pAsynWaitSp); void LogErrInfo(const char* msgHead,const char* msgBody,const int errCode); void LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct); void UpgradeActionProcess(const char *pszEntityName, const char *pAction); void CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &Info); void CalcEntityCpuUsage(const char *pszEntityName, CEntityRunInfo &Info, bool bFirst); void GetSystemMemoryStatus(); void GetSystemCPUStatus(); void GetSystemDiskStatus(); bool IsKeyEntity(const char *pszEntityName); bool ReadXmlFile(const char *szFileName); ErrorCodeEnum ConnectToHealthManager(); private: map m_entCfgInfo; map m_entRunInfo; map m_mapEntity; vector m_activeEntity,m_allEntity,m_vKeyEntity,m_vQueryStateEntity; int m_restartNormal,m_restartSpecial,m_maxOsRestart,m_maxPowerRestart ,m_cpuHighPercent,m_memHighPercent,m_diskHighPercent,m_warmLevel,m_diskLastWarnHour; DWORD m_dwIssueCount,m_dwCaptureCount; bool m_bInIssue,m_bFirstCalcCpu, m_bEverInMainPage; int m_simulateTest; CSimpleStringA m_csKeyEntity,m_csMachineType,m_csSite; __int64 m_xIdlePre,m_xKernelPre,m_xUserPre; HealthManagerClient *m_pHealthClient; }; template class TimerOutHelper : public ITimerListener { public: typedef void (T::*FuncTimer)(void *pUserdata); TimerOutHelper(T *p, FuncTimer pTimerFunc,void *pData,bool bDeleteSelf = false) : m_pObject(p),m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {} virtual void OnTimeout(DWORD dwTimerID) { (m_pObject->*m_pTimer)(m_pUserData); if (m_bDeleteSelf) delete this; } private: void *m_pUserData; T *m_pObject; FuncTimer m_pTimer; bool m_bDeleteSelf; }; #endif // __SELFCHECKER_FSM_H