123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- #include "stdafx.h"
- #include "mod_UpgradeMgr.h"
- #include "..\mod_UpgradeRun\UpgradeRun_client_g.h"
- using namespace UpgradeRun;
- #include "..\mod_download\Download_client_g.h"
- using namespace Download;
- #include "..\mod_accessauth\AccessAuthorization_client_g.h"
- using namespace AccessAuthorization;
- #include "LocalMediaPlay_client_g.h"
- //using namespace LocalMediaPlay;
- #include "EventCode.h"
- #include "md5file.h"
- #include <io.h>
- #include "fileutil.h"
- #include <regex>
- // 升级管理 UpgradeManager 0x506
- void CUpgradeMgrEntity::OnStarted()
- {
- // 初始化状态机
- m_fsm.Init(this);
- auto pFunc = GetFunction();
- CUUID subID;
- auto rc = pFunc->SubscribeBroadcast("UpgradeRun", NULL, this, subID);
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc, 0, "subscribe entity [UpgradeRun] broadcast msg fail");
- //return;
- }
- //// 监视准入状态
- //CSimpleStringA strValue;
- //if (pFunc->GetSysVar("EntryPermit", strValue) == Error_Succeed && strValue.Compare("L") ==0)
- //{
- // // 已经进入准入状态
- // m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- //}
- //else
- //{
- // rc= pFunc->RegistSysVarEvent("EntryPermit", this);
- // assert(rc == Error_Succeed);
- //}
- //zl@20190311 每次启动时生成新的MD5List
- rc = GetMD5List(m_strMD5List);
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc, 0, "GetMD5List fail");
- }
- else
- {
- Dbg("get MD5 list success, len=%d", m_strMD5List.GetLength());
- }
- // 由于准入未上线或可能不稳定,为了保持升级服务的可用性,一直保持可升级状态
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- }
- void CUpgradeMgrEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- GetFunction()->UnsubscribeBroadcast("Download");
- GetFunction()->UnsubscribeBroadcast("UpgradeRun");
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- void CUpgradeMgrEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (strcmp(pszKey, "EntryPermit") ==0)
- {
- if (strcmp(pszValue, "L") ==0)
- {
- // 准入成功
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- // 取消监视
- this->GetFunction()->UnregistSysVarEvent("EntryPermit");
- }
- }
- }
- void CUpgradeMgrEntity::OnDownloadEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, Download::DownloadResult &evt)
- {
- Dbg("OnDownloadEvent, file:%s, result:%d, errMsg:%s", (const char*)evt.strFileName, evt.errorCode, (const char*)evt.errorMsg);
- m_fsm.PostEventFIFO(new CUpgradeMgrFSM::DownloadedEvent(evt.strFileName, evt.errorCode, evt.errorMsg));
- }
- void CUpgradeMgrEntity::OnUpgradeCheckEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeCheckEvent &evt)
- {
- Dbg("OnUpgradeCheckEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
- auto pEvent = new CUpgradeMgrFSM::UpgradeRunCheckEvent(evt.strPackName, evt.error, evt.coverList, evt.strComment);
- m_fsm.PostEventFIFO(pEvent);
- }
- void CUpgradeMgrEntity::OnUpgradeDoneEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeDoneEvent &evt)
- {
- Dbg("OnUpgradeDoneEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
- auto pEvent = new CUpgradeMgrFSM::UpgradeRunDoneEvent(evt.strPackName, evt.error, evt.bSysInstall, evt.bLightPack, evt.strNewVersion, evt.strFWID, evt.strSysPatchName,evt.strComment);
- m_fsm.PostEventFIFO(pEvent);
- //add by zl 20190221
- // 终端置为非升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
- auto rc = GetFunction()->SetSysVar("UpdateState", "0", true);
- assert(rc == Error_Succeed);
- }
- char CUpgradeMgrEntity::GetInstallStateVal(const InstallStateEnum enumVal)
- {
- const struct {
- int ch;
- InstallStateEnum val;
- } tbl[] = {
- {'A', Install_Active},
- {'I', Install_Pending},
- {'S', Install_SetToStart},
- {'F', Install_FailRun},
- {'R', Install_RollBack},
- {'U', Install_Upgraded},
- {'C',Install_Cancelled},
- {'W',Install_WaitConfirm},
- {'D', Install_Installed},
- };
- int i;
- for (i = 0; i < sizeof(tbl)/sizeof(tbl[0]); ++i) {
- if (tbl[i].val == enumVal) {
- return tbl[i].ch;
- }
- }
- return ' '; // error
- }
- ErrorCodeEnum CUpgradeMgrEntity::RegistLocalPack(const CSimpleStringA &strPackFile)
- {
- // 手动拷贝升级包,本地升级
- return m_fsm.RegistLocalPack(strPackFile);
- }
- DWORD CUpgradeMgrEntity::RegistManualPack(const CSimpleStringA &strPackFile)
- {
- // 用户桌面手动升级
- return m_fsm.RegistManualPack(strPackFile);
- }
- ErrorCodeEnum CUpgradeMgrEntity::DownloadPack(const CSimpleStringA &strPackFile)
- {
- // 通知下载实体准备下载
- auto pClient = new DownloadService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- DownloadService_DownloadFile_Req req = {};
- DownloadService_DownloadFile_Ans ans = {};
- req.strFileName = strPackFile;
- req.dwExpireTime = CSmallDateTime::GetNow() + 3600 * 24 * 14;
- rc = pClient->DownloadFile(req, ans, 5000);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to download entity fail: %d", rc);
- }
- pClient->SafeDelete();
- return rc;
- }
- ErrorCodeEnum CUpgradeMgrEntity::CancelDownloadPack(const CSimpleStringA &strPackFile)
- {
- // 通知下载实体准备下载
- auto pClient = new DownloadService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- DownloadService_CancelDownloadFile_Req req = {};
- DownloadService_CancelDownloadFile_Ans ans = {};
- req.strFileName = strPackFile;
- rc = pClient->CancelDownloadFile(req, ans, 5000);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to download entity fail: %d", rc);
- }
- pClient->SafeDelete();
- return rc;
- }
- ErrorCodeEnum CUpgradeMgrEntity::CancelUpdate(const CSimpleStringA &strPackFile)
- {
- // 取消安装
- m_fsm.PushCancelUpgradePack(strPackFile);
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::RollbackUpdate(const CSimpleStringA &strVersion)
- {
- // 调用框架接口回滚升级
- auto pFunc = GetFunction()->GetPrivilegeFunction();
- assert(pFunc != NULL);
- auto rc = Error_Succeed;
- if (strVersion.GetLength() == 0)
- {
- Dbg("try rollback to previous version");
- rc = pFunc->RollBackToPreviousVersion();
- }
- else
- {
- Dbg("try rollback to version: [%s]", (const char*)strVersion);
- int w1, w2, w3, w4;
- int rc = sscanf(strVersion, "%d.%d.%d.%d", &w1, &w2, &w3, &w4);
- if (rc <4)
- {
- Dbg("version [%s] parse fail", (const char*)strVersion);
- rc= Error_Param;
- }
- else
- {
- rc = pFunc->RollBackToHistoryVersion(CVersion(w1, w2, w3, w4));
- }
- }
- if (rc == Error_Succeed)
- {
- // 通过事件通知健康实体
- LogEvent(Severity_Middle, Event_Req_Framework_Rollback, "rollback upgrade succeed");
- ////更新安装失败包信息(包名和失败次数)
- //UpdatePackFailInfo();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "rollback upgrade fail");
- }
- return rc;
- }
- DWORD CUpgradeMgrEntity::GetManualPacks(CSimpleStringA &strManualPacks)
- {
- return m_fsm.GetManualPacks(strManualPacks);
- }
- ErrorCodeEnum CUpgradeMgrEntity::SyncTime()
- {
- // 通知准入实体同步时间
- AccessAuthService_ClientBase *pClient = new AccessAuthService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- Dbg("connect to entity [AccessAuthorization] succeed, start syncTime now");
- rc = pClient->SyncTime();
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "connect to entity [AccessAuthorization] fail");
- }
- pClient->SafeDelete();
- return rc;
- }
- // 将升级任务推送到运行实体
- ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName, const int nPackType)
- //ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName)
- {
- auto pClient = new UpgradeRunService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- UpgradeRunService_PushUpdateTask_Info info;
- info.strPackName = strPackName;
- info.nTaskType = nPackType;
- rc = pClient->PushUpdateTask(info);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to upgrade run entity fail: %d", rc);
- }
- pClient->SafeDelete();
- return rc;
- }
- CServerSessionBase* CUpgradeMgrEntity::OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
- {
- return new CUpgradeMgrSession(this);
- }
- ErrorCodeEnum CUpgradeMgrEntity::SwitchUpgrade(const CSimpleStringA &strPack)
- {
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_SwitchNow));
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
- char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
- {
- return m_fsm.GetUpgradeState(bInstalling, strPackFile, strExecID, cInstallState, bSysInstall, bLightPack, strNewVersion);
- }
- ErrorCodeEnum CUpgradeMgrEntity::MD5File(CSimpleStringA strFilePath, CSimpleStringA &strMD5)
- {
- char* pMd5 = MD5FILE::MD5_file((char*)strFilePath.GetData(), 32);
- if (NULL == pMd5)
- {
- Dbg("Get %s MD5 value fail");
- return Error_Unexpect;
- }
- strMD5 = pMd5;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::MD5Data(CSimpleStringA strData, CSimpleStringA &strMD5)
- {
- char* pMd5 = MD5FILE::MD5_data((char*)strData.GetData(), 32);
- if (NULL == pMd5)
- {
- Dbg("Get %s MD5 value fail");
- return Error_Unexpect;
- }
- strMD5 = pMd5;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::MD5Folder(CSimpleStringA strFolderPath,bool isDepDIr)
- {
- if (strFolderPath.IsNullOrEmpty())
- {
- return Error_Null;
- }
- Dbg("Start to get file hash list, dir=[%s] ", strFolderPath);
- _finddata_t FileInfo;
- CSimpleStringA strfind = strFolderPath + "\\*";
- long Handle = _findfirst(strfind, &FileInfo);
- if (-1L == Handle)
- {
- _findclose(Handle);
- Dbg("%s文件夹为空", strFolderPath);
- return Error_Succeed;
- }
- CSimpleStringA newPath;
- do{
- if (FileInfo.attrib & _A_SUBDIR)
- {
- if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
- {
-
- CSimpleStringA strFindName = FileInfo.name;
- //整个黑名单文件夹过滤
- bool isBlackDir = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DirBlacklist.begin();
- while(itor!=m_fsm.m_DirBlacklist.end())
- {
- CSimpleStringA dirBlack = *itor;
- if(strcmp(dirBlack.GetData(),FileInfo.name)==0){
- isBlackDir=true;
- break;
- }
- itor++;
- }
-
- if(isBlackDir){
- CSimpleStringA dirPath = strFolderPath + "\\" + FileInfo.name;
- Dbg("BlackDir Filter don't add to md5 list, dir=[%s]", dirPath.GetData());
- continue;//跳过文件夹
- }else{
- //判断是否是dep文件夹
- if(strcmp(FileInfo.name, "dep") == 0){
- newPath = strFolderPath + "\\" + FileInfo.name;
- MD5Folder(newPath,true);
- }else{
- newPath = strFolderPath + "\\" + FileInfo.name;
- MD5Folder(newPath);
- }
- }
- }
- }
- else
- {
- CSimpleStringA strFindName = FileInfo.name;
- {
- if (strFindName.IndexOf(".") == -1)
- {
- Dbg("%s file name is illegal", strFindName);
- continue;
- }
- //Dbg("计算hash码文件:%s",strFindName);
- //整体黑名单文件过滤
- bool isBlackFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_FileBlacklist.begin();
- while(itor!=m_fsm.m_FileBlacklist.end())
- {
- CSimpleStringA fileBlack = *itor;
- if(strFindName.IsEndWith(fileBlack.GetData(),true)){
- isBlackFile=true;
- break;
- }
- itor++;
- }
- if(isBlackFile){
- Dbg("BlackFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- //对历史遗留的centerSetting错误缓存文件特殊处理,不纳入hash文件列表
- //对所有集中配置文件都过滤,规则是CenterSetting.*.ini*
- regex e("CenterSetting[.][^.]*[.]ini.*");
- if(std::regex_match(strFindName.GetData(),e,regex_constants::match_default)){
- continue;
- }
- /*if(strFindName.IndexOf("CenterSetting.")>-1&&strFindName.IndexOf(".ini")>-1){
- continue;
- }*/
- // 过滤install.ini,集中配置,log文件和cfg\certs目录
- // TODO:改成配置
- //if (strFindName.IsEndWith("install.ini", true)
- // || strFindName.IsEndWith("CenterSetting.DMZ.ini", true)
- // || strFindName.IsEndWith("CenterSetting.LAN.ini", true)
- // || strFindName.IsEndWith(".log", true)
- // || strFindName.IsEndWith("RootCert.pem", true)
- // || strFindName.IsEndWith("CaCert.pem", true)
- // || strFindName.IsEndWith("userCert.pem", true)
- // || strFindName.IsEndWith("CertBlackList.txt", true)
- // || strFindName.IsEndWith("idfront.jpg", true)
- // || strFindName.IsEndWith("idback.jpg", true))
- //{
- // Dbg("don't add %s to md5 list", strFindName);
- // continue;
- //}
- //判断是否需要用dep白名单过滤
- if(isDepDIr){
- bool isWhiteFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DepWhitelist.begin();
- while(itor!=m_fsm.m_DepWhitelist.end())
- {
- CSimpleStringA fileWhite = *itor;
- if(strFindName.IsEndWith(fileWhite.GetData(),true)){
- isWhiteFile=true;
- break;
- }
- itor++;
- }
- //不是白名单也不是默认dll文件,不需要加入hash计算
- if(!isWhiteFile&&!strFindName.IsEndWith(".dll", true)){
- Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- }
- CSimpleStringA strFilePath = strFolderPath + "\\" + strFindName;
- CSimpleStringA strMD5Val;
- ErrorCodeEnum rErrcode = MD5File(strFilePath, strMD5Val);
- if (Error_Succeed != rErrcode)
- {
- Dbg("%s获取MD5失败,错误码:%d", strFilePath.GetData(), (int)rErrcode);
- return rErrcode;
- }
- m_FileHashMap[strFilePath] = strMD5Val;
- }
- }
- } while (_findnext(Handle, &FileInfo) == 0);
- _findclose(Handle);
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::GetMD5List(CSimpleStringA &strMD5List)
- {
- if (!m_strMD5List.IsNullOrEmpty())
- {
- strMD5List = m_strMD5List;
- return Error_Succeed;
- }
- m_FileHashMap.clear();//生成文件hash列表前先清空
- CSystemStaticInfo ssInfo;
- this->GetFunction()->GetSystemStaticInfo(ssInfo);
- CSimpleStringA curVer = ssInfo.InstallVersion.ToString();
- if (curVer.IsNullOrEmpty())
- {
- Dbg("InstallVersion is null");
- return Error_Null;
- }
- CSimpleStringA strRootVerPath;
- this->GetFunction()->GetPath("RootVer", strRootVerPath);
- Dbg("strRootVerPath %s,curVer:%s", strRootVerPath.GetData(),curVer.GetData());
- CSimpleStringA strCurVerPath = CSimpleStringA::Format("%s\\%s", (const char*)strRootVerPath, (const char*)curVer);
- if (!ExistsDirA(strCurVerPath))
- {
- Dbg("%d not exist", strCurVerPath);
- return Error_Unexpect;
- }
- ErrorCodeEnum rc = MD5Folder(strCurVerPath);
- if (Error_Succeed != rc)
- {
- return rc;
- }
- for(auto it=m_FileHashMap.begin(); it!=m_FileHashMap.end(); it++)
- {
- strMD5List += it->first;
- strMD5List += ",";
- strMD5List += it->second;
- strMD5List += ";";
- }
- m_strMD5List = strMD5List;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::StopMediaPlay()
- {
- // 通知准入实体同步时间
- LocalMediaPlay::PlayService_ClientBase *pClient = new LocalMediaPlay::PlayService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- Dbg("connect to entity [LocalMediaPlay] succeed, start StopMediaPlay now");
- LocalMediaPlay::PlayService_StopPlayVideo_Req req1 = {};
- LocalMediaPlay::PlayService_StopPlayVideo_Ans ans1 = {};
- req1.CfgInx = 1;
- if (Error_Succeed == pClient->StopPlayVideo(req1, ans1, 5000))
- {
- Dbg("StopPlayVideo success");
- }
- LocalMediaPlay::PlayService_StopPlayAudio_Req req2 = {};
- LocalMediaPlay::PlayService_StopPlayAudio_Ans ans2 = {};
- if (Error_Succeed == pClient->StopPlayAudio(req2, ans2, 5000))
- {
- Dbg("StopPlayAudio success");
- }
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "connect to entity [LocalMediaPlay] fail");
- }
- pClient->SafeDelete();
- return rc;
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CUpgradeMgrEntity)
- SP_END_ENTITY_MAP()
|