#ifndef RVC_MOD_UPGRADEMGR_FSM_H_ #define RVC_MOD_UPGRADEMGR_FSM_H_ #include "SimpleString.h" #include "SpBase.h" #include "SpFSM.h" #include "UpgradeMgrConn.h" #include #include #ifdef RVC_OS_WIN #include #else #endif using namespace std; #define NOT_MANUALPACK_ERR 0xF0000001 // 不是手动升级包 #define IS_UPDATEING_ERR 0xF0000002 // 正在升级 #define NULL_PARAM_ERR 0xF0000003 // 参数为空 #define NO_EXIST_MANUAL_PACK_ERR 0xF0000004 // 不存在手动升级包 // 上报状态机(并负责轮询升级) class CReportMgrFSM : public FSMImpl, public IFSMStateHooker { public: enum { Event_StartConnect = EVT_USER+1, Event_Disconnected, Event_Connected, Event_ReportState, Event_StartPoll, Event_SwitchConfirm, Event_QueryExecInfo, Event_ReportSysCustomVer, Event_SendMD5list, }; struct ReportStateEvent : public FSMEvent { ReportStateEvent(const char *pszPack, const CVersion &version, DWORD dwError, char cState, const char *pszComment = "") :FSMEvent(Event_ReportState), strPackName(pszPack), InstallVersion(version), dwErrorCode(dwError), cInstallState(cState), strComment(pszComment) {} CSimpleStringA strPackName; CVersion InstallVersion; DWORD dwErrorCode; char cInstallState; CSimpleStringA strComment; }; struct ReportSysCustomVerEvent : public FSMEvent { ReportSysCustomVerEvent(const CSimpleStringA &strPack, const CSimpleStringA &version, const CSimpleStringA &FWID, const CSimpleStringA &SysPatchName) :FSMEvent(Event_ReportSysCustomVer), strPackName(strPack), strSysCustomVer(version), strFWID(FWID), strSysPatchName(SysPatchName) {} CSimpleStringA strPackName; CSimpleStringA strSysCustomVer; CSimpleStringA strFWID; CSimpleStringA strSysPatchName; }; struct SendMD5ListEvent : public FSMEvent { SendMD5ListEvent(const CSimpleStringA &strBlockId) :FSMEvent(Event_SendMD5list), BlockId(strBlockId) {} CSimpleStringA BlockId; }; struct SwitchConfirmEvent : public FSMEvent { CSimpleStringA strPackExecID; SwitchConfirmEvent(const CSimpleStringA &strExecID) : FSMEvent(Event_SwitchConfirm), strPackExecID(strExecID){} }; struct QueryExecInfoEvent : public FSMEvent { CSimpleStringA strPackExecID; QueryExecInfoEvent(const CSimpleStringA &strExecID) : FSMEvent(Event_QueryExecInfo), strPackExecID(strExecID) {} }; CReportMgrFSM():m_pConnection(NULL),m_pOuterFSM(NULL),m_LastPollTime(0), m_LastReqTime(0), m_bNeedPoll(false){} ~CReportMgrFSM(){} void SetOuterFSM(FSMBase *pFSM){m_pOuterFSM = pFSM;} private: virtual ErrorCodeEnum OnInit(); virtual ErrorCodeEnum OnExit() { RemoveStateHooker(this); return Error_Succeed; } enum{s1, s2, s3}; BEGIN_FSM_STATE(CReportMgrFSM) FSM_STATE_ENTRY(s1, "Disable",s1_on_entry,s1_on_exit,s1_on_event) FSM_STATE_ENTRY(s2, "Connecting",s2_on_entry,s2_on_exit,s2_on_event) FSM_STATE_ENTRY(s3, "Connected",s3_on_entry,s3_on_exit,s3_on_event) END_FSM_STATE() BEGIN_FSM_RULE(CReportMgrFSM,s1) FSM_RULE_ENTRY_ANY(s1, s2, Event_StartConnect) FSM_RULE_ENTRY_ANY(s2, s3, Event_Connected) FSM_RULE_ENTRY_ANY(s3, s1, Event_Disconnected) END_FSM_RULE() virtual void OnStateTrans(int iSrcState, int iDstState); 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 SecureClientConnect(); ErrorCodeEnum SecureClientRelease(); ErrorCodeEnum StartPoll();//发起策略查询 bool GetInstalledSysPackList();//查询已安装体系外升级包 public: UpgradeConnect::CUpgradeMgrConn *m_pConnection; private: FSMBase *m_pOuterFSM; //CUpgradeMgrConn *m_pConnection; CSmallDateTime m_LastPollTime; CSmallDateTime m_LastReqTime; // 等待上报事件 struct CReportInfo { CReportInfo(const CSimpleStringA &pack, const CVersion &ver, DWORD error, char state, const CSimpleStringA &comment) :strPackName(pack), InstallVersion(ver), dwFailCode(error), cInstallState(state), dwStateTime(CSmallDateTime::GetNow()),strComment(comment) {} CSimpleStringA strPackName; CVersion InstallVersion; DWORD dwStateTime; DWORD dwFailCode; char cInstallState; CSimpleStringA strComment; }; list m_PendingReports; // 等待上报信息 //list m_ConfirmPacks; // 等待确认安装包 CSimpleStringA m_strToConfirmExecID; // 待确认策略ID CSimpleStringA m_strToQueryExecID; // 待查询策略ID bool m_bNeedPoll; // 是否轮询触发 CSimpleStringA m_strSysCustomVer; CSimpleStringA m_strFWID; CSimpleStringA m_strSysPatchName; CSimpleStringA m_strSysCustomPack; CSimpleStringA m_strBlockId; public: list m_InstallSysPackList; //已安装的体系外升级包 }; // 升级管理状态机 class CUpgradeMgrFSM : public FSMImpl, public IFSMStateHooker { public: enum { Event_EntryPermit = EVT_USER+1, Event_StartPoll, Event_EndPoll, Event_StartDownload, Event_EndDownload, Event_StartInstall, Event_InstallCheck, Event_EndInstall, Event_WaitConfirm, Event_EndConfirm, Event_PreRestart, Event_CancelUpgrade, Event_StartSwitch, Event_EndQueryExec, Event_SwitchNow, /*Event_StartMD5, Event_ContinueMD5, Event_RetryMD5, Event_EndMD5,*/ }; struct RvcCommRetEvent : public FSMEvent { RvcCommRetEvent(int nEventID, BYTE *pBuf, int nArrayNum) : FSMEvent(nEventID) { param1 = (param_size_t)pBuf; param2 = nArrayNum; } virtual ~RvcCommRetEvent() { if (param1 != 0 && param2>0) delete[] (BYTE*)param1; } int GetArrayNum() { return param2; } BYTE* GetRetData() { return (BYTE*)param1; } }; struct DownloadedEvent: public FSMEvent { CSimpleStringA strFileName; ErrorCodeEnum ErrorCode; CSimpleStringA strErrorMsg; DownloadedEvent(const char *pFile, unsigned int error, const char *pszErrMsg) : FSMEvent(Event_EndDownload),strFileName(pFile), ErrorCode((ErrorCodeEnum)error), strErrorMsg(pszErrMsg) {} }; struct UpgradeRunCheckEvent :public FSMEvent { CSimpleStringA strPackName; int nErrorCode; CAutoArray CoverList; CSimpleStringA strComment; UpgradeRunCheckEvent(const CSimpleStringA& pack, int error, const CAutoArray arr,const CSimpleStringA &comment) : FSMEvent(Event_InstallCheck), strPackName(pack), nErrorCode(error), CoverList(arr), strComment(comment) {} }; struct UpgradeRunDoneEvent:public FSMEvent { CSimpleStringA strPackName; int nErrorCode; bool bSysInstall; bool bLightPack; CSimpleStringA strNewVersion; CSimpleStringA strFWID; CSimpleStringA strSysPatchName; CSimpleStringA strComment; UpgradeRunDoneEvent(const CSimpleStringA &pack, int error, bool sysInstall, bool lightInstall, const CSimpleStringA &newVer, const CSimpleStringA &newFWID, const CSimpleStringA &newSysPatchName,const CSimpleStringA &comment) :FSMEvent(Event_EndInstall), strPackName(pack), nErrorCode(error), bSysInstall(sysInstall), bLightPack(lightInstall), strNewVersion(newVer), strFWID(newFWID), strSysPatchName(newSysPatchName),strComment(comment) {} }; // 待安装包信息 struct CPendingPackInfo { CSimpleStringA strPackName; BYTE nLevel; // 3级优先级 char cTriggerType; // 更新触发方式 I(立即)|T(定时)|C(控制) DWORD dwTriggerTimer; // 触发时间,2000后的秒数 char cPendingState; // 初始状态N、下载中U、已下载D、安装中I、等待确认C、等待切换S bool bSysInstall; // 是否系统升级 CSimpleStringA strPackExecID; // 执行策略 }; // 已安装或取消安装包信息 struct CInstalledPackInfo { CSimpleStringA strPackName; CSmallDateTime InstallTime; char cInstallState; // 安装失败F、已经安装D、取消安装C }; struct CUpgradeProcess { CSimpleStringA strPackName;//包名 CVersion installVersion;//安装版本号 CVersion CurrentVersion;//当前版本号 CSimpleStringA cInstallState;//升级状态 CSimpleStringA strInstallComment;//升级状态备注 }; public: CUpgradeMgrFSM(); ~CUpgradeMgrFSM(){} void PushCancelUpgradePack(const CSimpleStringA &strPackName); bool HasDownloadingPack(); bool HasRunningUpdate(); ErrorCodeEnum RegistLocalPack(const CSimpleStringA &strPackName); DWORD RegistManualPack(const CSimpleStringA &strPackName); DWORD GetManualPacks(CSimpleStringA &strManualPacks); ErrorCodeEnum GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID, char &nInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion); char GetInstallState(int nState); bool UpdateManualTaskState(); bool IsManualInstallOk(CSimpleStringA strPackName); bool GetServerPackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg); bool GetFirmwarePackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg); CSimpleStringA GetSysPatchNameFromPackName(const char *pszPackName); ErrorCodeEnum GetPackInstallFailedCnts(UINT64 &nCnts); ErrorCodeEnum SetPackInstallFailedCnts(UINT64 nCnts); ErrorCodeEnum GetInstallFailedPackName(CSimpleStringA &strPackName); ErrorCodeEnum SetInstallFailedPackName(CSimpleStringA strPackName); ErrorCodeEnum SetInstallFailedPackInfo(CSimpleStringA strPackName); ErrorCodeEnum GetInstallFailedPackInfo(CSimpleStringA &strPackName); ErrorCodeEnum UpdateInstallFailedPackInfo(CSimpleStringA strPackName); void SendSM3ListEvent(); private: virtual ErrorCodeEnum OnInit(); virtual ErrorCodeEnum OnExit(); virtual void OnStateTrans(int iSrcState, int iDstState); enum{s1, s2, s3, s4, s5, s6}; BEGIN_FSM_STATE(CUpgradeMgrFSM) FSM_STATE_ENTRY(s1, "Disable",s1_on_entry,s1_on_exit,s1_on_event) FSM_STATE_ENTRY(s2, "Poll",s2_on_entry,s2_on_exit,s2_on_event) FSM_STATE_ENTRY(s3, "Download",s3_on_entry,s3_on_exit,s3_on_event) FSM_STATE_ENTRY(s4, "Install", s4_on_entry, s4_on_exit, s4_on_event) FSM_STATE_ENTRY(s5, "Confirm", s5_on_entry, s5_on_exit, s5_on_event) FSM_STATE_ENTRY(s6, "Switch", s6_on_entry, s6_on_exit, s6_on_event) END_FSM_STATE() BEGIN_FSM_RULE(CUpgradeMgrFSM,s1) FSM_RULE_ENTRY(s1, s2, Event_EntryPermit, 0) FSM_RULE_ENTRY(s1, s3, Event_EntryPermit, 3) FSM_RULE_ENTRY(s1, s4, Event_EntryPermit, 4) FSM_RULE_ENTRY(s1, s5, Event_EntryPermit, 5) FSM_RULE_ENTRY(s1, s6, Event_EntryPermit, 6) FSM_RULE_ENTRY_ANY(s2, s3, Event_StartDownload) FSM_RULE_ENTRY_ANY(s2, s4, Event_StartInstall) FSM_RULE_ENTRY_ANY(s2, s5, Event_WaitConfirm) FSM_RULE_ENTRY_ANY(s3, s4, Event_StartInstall) FSM_RULE_ENTRY_ANY(s3, s2, Event_StartPoll) FSM_RULE_ENTRY_ANY(s4, s5, Event_WaitConfirm) FSM_RULE_ENTRY_ANY(s4, s2, Event_StartPoll) FSM_RULE_ENTRY_ANY(s4, s1, Event_PreRestart) FSM_RULE_ENTRY_ANY(s5, s6, Event_StartSwitch) FSM_RULE_ENTRY_ANY(s5, s2, Event_StartPoll) FSM_RULE_ENTRY_ANY(s6, s1, Event_PreRestart) FSM_RULE_ENTRY_ANY(s6, s2, Event_StartPoll) END_FSM_RULE() private: 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); void s4_on_entry(); void s4_on_exit(); unsigned int s4_on_event(FSMEvent* event); void s5_on_entry(); void s5_on_exit(); unsigned int s5_on_event(FSMEvent* event); void s6_on_entry(); void s6_on_exit(); unsigned int s6_on_event(FSMEvent* event); bool IsPackDownloaded(const char *pPackName); bool IsPackCancelled(const char *pPackName); bool IsPackInstalled(const char *pPackName); bool IsPackFialOver3Times(const char *pPackName); ErrorCodeEnum LoadPersistInfo(); CPendingPackInfo* GetPriorInstallPack(); bool CheckIfPackConfirmed(); bool CheckIfInstallCancelled(); void CancelUpgradePacks(RvcCommRetEvent *pEvent); ErrorCodeEnum SwitchUpgradeNow(); bool CheckIfCanSwitchNow(); bool WriteInstallLog(const char *pszPackName, const char *pszLogText); bool RemoveInstallLog(const char *pszPackName); ErrorCodeEnum SetRunConfigStrValue(const char *pszSection, const char *pszKey, const char *pszValue); ErrorCodeEnum GetRunConfigStrValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue); ErrorCodeEnum SetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 nValue); ErrorCodeEnum GetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 &nValue); void sendUpgradeProgress(CUpgradeProcess &process); private: CReportMgrFSM m_ReportFSM; CSimpleStringA m_strIntallingPack; // 当前正在安装的项 int m_nIntallingPackType; // 当前正在安装的项的包类型,1,自动安装;2,手动安装 CSimpleStringA m_strNewVersion; // 新版本对应目录 map m_PendingPacks; // 等待安装包 list m_CancelledPacks; // 被主动取消安装包 list m_InstallFailPacks; // 安装或检查失败的包,防止重复安装 list m_ManualPendingPacks; // 等待手动安装包 // map m_InstalledPacks; // 当前版本已安装包 //list m_ConfirmedPacks; // 提前确认安装包 DWORD m_dwDownloadTime; // 当前状态开始时间 unsigned int m_nPackInstallFailCnts; public: list m_DepWhitelist; //dep文件夹白名单 list m_DirBlacklist; //整体文件夹黑名单 list m_FileBlacklist; //整体文件黑名单 }; #endif //RVC_MOD_UPGRADEMGR_FSM_H_