123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- #ifndef HEALTHMANAGER_ENTITY_BOOT_STRUCT_H
- #define HEALTHMANAGER_ENTITY_BOOT_STRUCT_H
- #include <vector>
- #include <memory>
- #include <thread>
- #include <chrono>
- #include <algorithm>
- #include "iniutil.h"
- #include "SpBase.h"
- #define ISSUCCEEDED(hr) ((hr) == Error_Succeed)
- #define FAILURED(hr) (!(ISSUCCEEDED(hr)))
- enum EntityBootType
- {
- IgnoreResult,
- VerifyInAsync,
- VerifyInSync
- };
- enum EntityBootStep
- {
- NotInit,
- CoreBoot,
- SafeLoad,
- IEBrowser,
- Operating,
- NotConfigInit,
- NotConfigSecond
- };
- struct EntityBootInfo
- {
- CSimpleStringA entityName;
- CSimpleStringA cmdLine;
- EntityBootType bootType;
- ErrorCodeEnum bootResult;
- EntityBootInfo(const char* name, EntityBootType type)
- :entityName(name)
- , bootType(type)
- , bootResult(Error_NotInit)
- {
- }
- };
- struct StartEntityOperation;
- struct BootStep
- {
- CSimpleStringA strSectionName;
- std::vector< EntityBootInfo> entityList;
- BootStep() :strSectionName(true) {}
- BootStep(const char* stepName) :strSectionName(stepName) {}
- virtual ~BootStep() {}
- static ErrorCodeEnum WaitForAsyncEntitStartOperation(std::vector<StartEntityOperation*>& entityList);
- ErrorCodeEnum LoadConfig(const CSimpleStringA& configPath)
- {
- array_header_t* keys = inifile_read_section_key_all(configPath, strSectionName);
- if (!keys || array_empty(keys)) {
- return Error_NotExist;
- }
- entityList.clear();
- for (size_t i = 0; i < keys->nelts; ++i) {
- char* entityName = ARRAY_IDX(keys, i, char*);
- int bootType = inifile_read_int(configPath, strSectionName, entityName, 0);
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("boot type info: %s=%d", entityName, bootType);
- EntityBootInfo entityBootInfo{ entityName, static_cast<EntityBootType>(bootType) };
- entityList.push_back(entityBootInfo);
- }
- toolkit_array_free2(keys);
- return Error_Succeed;
- }
- bool HasEntity() const
- {
- return(!entityList.empty());
- }
- ErrorCodeEnum StartStartupEntities(CEntityBase* pTrigger);
- };
- struct CoreBootStep : public BootStep
- {
- CoreBootStep(const CSimpleStringA& sectionPosix)
- :BootStep(CSimpleStringA("CoreBoot.") + sectionPosix)
- {
- }
- };
- struct SafeLoadStep : public BootStep
- {
- SafeLoadStep(const CSimpleStringA& sectionPosix)
- :BootStep(CSimpleStringA("SafeLoad.") + sectionPosix)
- {
- }
- };
- struct BrowserShowStep : public BootStep
- {
- BrowserShowStep(const CSimpleStringA& sectionPosix)
- :BootStep(CSimpleStringA("IEBrowser.") + sectionPosix + ".Url")
- {
- }
- };
- struct OperatingStep : public BootStep
- {
- OperatingStep(const CSimpleStringA& sectionPosix)
- : BootStep(CSimpleStringA("Operating.") + sectionPosix)
- {
- }
- };
- struct TerminalDeployStep : public BootStep
- {
- TerminalDeployStep(const CSimpleStringA& noUsed)
- : BootStep(CSimpleStringA("TerminalDeploy"))
- {
- }
- };
- struct TerminalDeploySecondStep : public BootStep
- {
- TerminalDeploySecondStep(const CSimpleStringA& sectionPosix)
- : BootStep(CSimpleStringA("TerminalDeploySec.") + sectionPosix)
- {
- }
- };
- struct TerminalDeployThirdStep : public BootStep
- {
- TerminalDeployThirdStep(const CSimpleStringA& sectionPosix)
- : BootStep(CSimpleStringA("TerminalDeployTrd.") + sectionPosix)
- {
- }
- };
- struct StartEntityBaseContext : public IReleasable
- {
- CEntityBase* theEntity;
- virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const { return true; }
- StartEntityBaseContext(CEntityBase* pEntity) :theEntity(pEntity) {}
- virtual ~StartEntityBaseContext() {}
- ErrorCodeEnum StartEntity(const char* entityName, const char* cmdLine, CSmartPointer<IAsynWaitSp>& spWait)
- {
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = theEntity->GetFunction()
- .ConvertCase<IEntityFunctionPrivilege>();
- ErrorCodeEnum result = pFuncPrivilege->StartEntity(entityName, cmdLine, spWait);
- return result;
- }
- virtual ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo) { return Error_NotImpl; }
- };
- struct StartEntitySyncContenxt : public StartEntityBaseContext
- {
- using StartEntityBaseContext::StartEntityBaseContext;
- ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
- {
- ErrorCodeEnum result(Error_Succeed);
- CSmartPointer<IAsynWaitSp> spWait;
- result = StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
- if (ISSUCCEEDED(result) && spWait != nullptr) {
- result = spWait->WaitAnswer(60 * 1000);
- }
- return result;
- }
- };
- struct StartEntityIgnoreContenxt : public StartEntityBaseContext
- {
- using StartEntityBaseContext::StartEntityBaseContext;
- ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
- {
- CSmartPointer<IAsynWaitSp> spWait;
- StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
- if (spWait != nullptr) {
- spWait->WaitAnswer(100);
- }
- return Error_Succeed;
- }
- };
- struct StartEntityOperation;
- struct StartEntityAsyncContenxt : public StartEntityBaseContext, public ICallbackListener
- {
- enum FinishedState
- {
- Pending,
- End
- } theFinishedState;
- StartEntityAsyncContenxt(CEntityBase* pEntity)
- : StartEntityBaseContext(pEntity)
- , theFinishedState(End)
- , asyncResult(Error_NotInit)
- {
- }
- virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const
- {
- if (retrieveResult) { *retrieveResult = asyncResult; }
- return (theFinishedState == End);
- }
- ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
- {
- LOG_FUNCTION();
- ErrorCodeEnum result(Error_Succeed);
- CSmartPointer<IAsynWaitSp> spWait;
- theFinishedState = Pending;
- result = StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
- if (ISSUCCEEDED(result) && spWait != nullptr) {
- spWait->SetCallback(this, nullptr);
- result = Error_Pending;
- } else {
- theFinishedState = End;
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("return directly: %s", SpStrError(result));
- }
- asyncResult = result;
- return result;
- }
- void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
- {
- LOG_FUNCTION();
- asyncResult = pAsynWaitSp->AsyncGetAnswer();
- theFinishedState = End;
- }
- ErrorCodeEnum asyncResult;
- };
- struct StartEntityOperation
- {
- StartEntityOperation(CEntityBase* pEntity, EntityBootInfo& entityInfo)
- :entInfo(entityInfo)
- {
- switch (entityInfo.bootType) {
- case VerifyInAsync:
- theContext = std::make_shared<StartEntityAsyncContenxt>(pEntity);
- break;
- case VerifyInSync:
- theContext = std::make_shared<StartEntitySyncContenxt>(pEntity);
- break;
- default:
- theContext = std::make_shared<StartEntityIgnoreContenxt>(pEntity);
- break;
- }
- }
- bool IsDone() { return theContext->IsFinished(&entInfo.bootResult); }
- ErrorCodeEnum StartEntity()
- {
- ErrorCodeEnum result = theContext->BootEntity(&entInfo);
- entInfo.bootResult = result;
- return result;
- }
- std::shared_ptr< StartEntityBaseContext> theContext;
- EntityBootInfo& entInfo;
- };
- ErrorCodeEnum BootStep::WaitForAsyncEntitStartOperation(std::vector<StartEntityOperation*>& entityList)
- {
- LOG_FUNCTION();
- ErrorCodeEnum result = Error_Succeed;
- for (; !entityList.empty();) {
- auto finishedInstance = find_if(entityList.begin(), entityList.end()
- , [](StartEntityOperation* operation) {
- return operation->IsDone();
- });
- if (finishedInstance != entityList.end()) {
- StartEntityOperation* operation = *finishedInstance;
- entityList.erase(finishedInstance);
- auto& entity = operation->entInfo;
- if (FAILURED(entity.bootResult)) {
- result = entity.bootResult;
- }
- delete operation;
- } else {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- }
- return result;
- }
- ErrorCodeEnum BootStep::StartStartupEntities(CEntityBase* pTrigger)
- {
- ErrorCodeEnum result = Error_Succeed;
- if (HasEntity()) {
- std::vector <StartEntityOperation*> pendingEntiList;
- for (auto& entity : entityList) {
- StartEntityOperation* startOperat = new StartEntityOperation(pTrigger, entity);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to start entity: %s ...", entity.entityName.GetData());
- result = startOperat->StartEntity();
- if (!startOperat->IsDone()) {
- pendingEntiList.push_back(startOperat);
- } else {
- if (ISSUCCEEDED(result)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start entity: %s successfully :)", entity.entityName.GetData());
- } else {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start entity: %s failed :( EC: %s", entity.entityName.GetData(), SpStrError(result));
- }
- delete startOperat;
- if (FAILURED(result)) {
- break;
- }
- }
- }
- if (!pendingEntiList.empty()) {
- result = WaitForAsyncEntitStartOperation(pendingEntiList);
- }
- [](std::vector < StartEntityOperation*>& entities) {
- for (auto& entity : entities) {
- delete entity;
- }
- }(pendingEntiList);
- }
- return result;
- }
- #endif
|