| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "mod_BootManager.h"
- #include "CommEntityUtil.hpp"
- #include "iniutil.h"
- #include <thread>
- #include <chrono>
- void BootManager::OnStarted()
- {
- QtUIShow();
- GetFunction()->PostEntityTaskFIFO(new BootEntityTask(this));
- }
- ErrorCodeEnum BootManager::LoadBootConfigInfo()
- {
- ErrorCodeEnum result(Error_Succeed);
- CSimpleStringA machineType = SP::Module::Comm::GetCurrMachineType(this);
- CSimpleStringA configPath = SP::Module::Comm::GetCurrEntityConfigPath(this);
- mCoreBootSteper = std::make_shared<CoreBootStep>(machineType);
- mCoreBootSteper->LoadConfig(configPath);
- mSafeLoadSteper = std::make_shared<SafeLoadStep>(machineType);
- mSafeLoadSteper->LoadConfig(configPath);
- mBrowserShowSteper = std::make_shared<BrowserShowStep>(machineType);
- mBrowserShowSteper->LoadConfig(configPath);
- mOperatingSteper = std::make_shared<OperatingStep>(machineType);
- mOperatingSteper->LoadConfig(configPath);
- return result;
- }
- ErrorCodeEnum BootManager::BootStartupEntity()
- {
- ErrorCodeEnum result(Error_Succeed);
- for (; ;) {
- result = mCoreBootSteper->StartStartupEntities(this);
- if(FAILURED(result))
- break;
- result = mSafeLoadSteper->StartStartupEntities(this);
- if (FAILURED(result))
- break;
- result = mOperatingSteper->StartStartupEntities(this);
- break;
- }
- return result;
- }
- void BootManager::SendEntityBootMessage(const char* entityName)
- {
- Dbg("to start entity: %s ...", entityName);
- }
- void BootManager::SendEntityBootResultMessage(const char* entityName, ErrorCodeEnum result)
- {
- if (ISSUCCEEDED(result)) {
- Dbg("start entity: %s successfully :)", entityName);
- } else {
- Dbg("start entity: %s failed :( EC: %s", entityName, SpStrError(result));
- }
- }
- void BootManager::QtUIShow()
- {
-
- }
- ErrorCodeEnum BootManager::Init()
- {
- ErrorCodeEnum result(Error_Succeed);
- return result;
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(BootManager)
- SP_END_ENTITY_MAP()
- ErrorCodeEnum BootStep::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);
- Dbg("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;
- }
- static ErrorCodeEnum WaitForAsyncEntitStartOperation(BootManager* pTrigger, 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;
- pTrigger->SendEntityBootResultMessage(entity.entityName, entity.bootResult);
- if (FAILURED(entity.bootResult)) {
- result = entity.bootResult;
- }
- delete operation;
- } else {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- }
- return result;
- }
- ErrorCodeEnum BootStep::StartStartupEntities(BootManager* pTrigger)
- {
- ErrorCodeEnum result = Error_Succeed;
- if (HasEntity()) {
- std::vector <StartEntityOperation*> pendingEntiList;
- for (auto& entity : entityList) {
- StartEntityOperation* startOperat = new StartEntityOperation(pTrigger, entity);
- pTrigger->SendEntityBootMessage(entity.entityName);
- result = startOperat->StartEntity();
- if (!startOperat->IsDone()) {
- pendingEntiList.push_back(startOperat);
- } else {
- pTrigger->SendEntityBootResultMessage(entity.entityName, entity.bootResult);
- delete startOperat;
- if (FAILURED(result)) {
- break;
- }
- }
- }
- if (!pendingEntiList.empty()) {
- result = WaitForAsyncEntitStartOperation(pTrigger, pendingEntiList);
- }
- [](std::vector < StartEntityOperation*>& entities) {
- for (auto& entity : entities) {
- delete entity;
- }
- }(pendingEntiList);
- }
- return result;
- }
|