mod_BootManager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "mod_BootManager.h"
  2. #include "CommEntityUtil.hpp"
  3. #include "iniutil.h"
  4. #include <thread>
  5. #include <chrono>
  6. void BootManager::OnStarted()
  7. {
  8. QtUIShow();
  9. GetFunction()->PostEntityTaskFIFO(new BootEntityTask(this));
  10. }
  11. ErrorCodeEnum BootManager::LoadBootConfigInfo()
  12. {
  13. ErrorCodeEnum result(Error_Succeed);
  14. CSimpleStringA machineType = SP::Module::Comm::GetCurrMachineType(this);
  15. CSimpleStringA configPath = SP::Module::Comm::GetCurrEntityConfigPath(this);
  16. mCoreBootSteper = std::make_shared<CoreBootStep>(machineType);
  17. mCoreBootSteper->LoadConfig(configPath);
  18. mSafeLoadSteper = std::make_shared<SafeLoadStep>(machineType);
  19. mSafeLoadSteper->LoadConfig(configPath);
  20. mBrowserShowSteper = std::make_shared<BrowserShowStep>(machineType);
  21. mBrowserShowSteper->LoadConfig(configPath);
  22. mOperatingSteper = std::make_shared<OperatingStep>(machineType);
  23. mOperatingSteper->LoadConfig(configPath);
  24. return result;
  25. }
  26. ErrorCodeEnum BootManager::BootStartupEntity()
  27. {
  28. ErrorCodeEnum result(Error_Succeed);
  29. for (; ;) {
  30. result = mCoreBootSteper->StartStartupEntities(this);
  31. if(FAILURED(result))
  32. break;
  33. result = mSafeLoadSteper->StartStartupEntities(this);
  34. if (FAILURED(result))
  35. break;
  36. result = mOperatingSteper->StartStartupEntities(this);
  37. break;
  38. }
  39. return result;
  40. }
  41. void BootManager::SendEntityBootMessage(const char* entityName)
  42. {
  43. Dbg("to start entity: %s ...", entityName);
  44. }
  45. void BootManager::SendEntityBootResultMessage(const char* entityName, ErrorCodeEnum result)
  46. {
  47. if (ISSUCCEEDED(result)) {
  48. Dbg("start entity: %s successfully :)", entityName);
  49. } else {
  50. Dbg("start entity: %s failed :( EC: %s", entityName, SpStrError(result));
  51. }
  52. }
  53. void BootManager::QtUIShow()
  54. {
  55. }
  56. ErrorCodeEnum BootManager::Init()
  57. {
  58. ErrorCodeEnum result(Error_Succeed);
  59. return result;
  60. }
  61. SP_BEGIN_ENTITY_MAP()
  62. SP_ENTITY(BootManager)
  63. SP_END_ENTITY_MAP()
  64. ErrorCodeEnum BootStep::LoadConfig(const CSimpleStringA& configPath)
  65. {
  66. array_header_t* keys = inifile_read_section_key_all(configPath, strSectionName);
  67. if (!keys || array_empty(keys)) {
  68. return Error_NotExist;
  69. }
  70. entityList.clear();
  71. for (size_t i = 0; i < keys->nelts; ++i) {
  72. char* entityName = ARRAY_IDX(keys, i, char*);
  73. int bootType = inifile_read_int(configPath, strSectionName, entityName, 0);
  74. Dbg("boot type info: %s=%d", entityName, bootType);
  75. EntityBootInfo entityBootInfo{ entityName, static_cast<EntityBootType>(bootType) };
  76. entityList.push_back(entityBootInfo);
  77. }
  78. toolkit_array_free2(keys);
  79. return Error_Succeed;
  80. }
  81. static ErrorCodeEnum WaitForAsyncEntitStartOperation(BootManager* pTrigger, std::vector<StartEntityOperation*>& entityList)
  82. {
  83. LOG_FUNCTION();
  84. ErrorCodeEnum result = Error_Succeed;
  85. for (; !entityList.empty();) {
  86. auto finishedInstance = find_if(entityList.begin(), entityList.end()
  87. , [](StartEntityOperation* operation) {
  88. return operation->IsDone();
  89. });
  90. if (finishedInstance != entityList.end()) {
  91. StartEntityOperation* operation = *finishedInstance;
  92. entityList.erase(finishedInstance);
  93. auto& entity = operation->entInfo;
  94. pTrigger->SendEntityBootResultMessage(entity.entityName, entity.bootResult);
  95. if (FAILURED(entity.bootResult)) {
  96. result = entity.bootResult;
  97. }
  98. delete operation;
  99. } else {
  100. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  101. }
  102. }
  103. return result;
  104. }
  105. ErrorCodeEnum BootStep::StartStartupEntities(BootManager* pTrigger)
  106. {
  107. ErrorCodeEnum result = Error_Succeed;
  108. if (HasEntity()) {
  109. std::vector <StartEntityOperation*> pendingEntiList;
  110. for (auto& entity : entityList) {
  111. StartEntityOperation* startOperat = new StartEntityOperation(pTrigger, entity);
  112. pTrigger->SendEntityBootMessage(entity.entityName);
  113. result = startOperat->StartEntity();
  114. if (!startOperat->IsDone()) {
  115. pendingEntiList.push_back(startOperat);
  116. } else {
  117. pTrigger->SendEntityBootResultMessage(entity.entityName, entity.bootResult);
  118. delete startOperat;
  119. if (FAILURED(result)) {
  120. break;
  121. }
  122. }
  123. }
  124. if (!pendingEntiList.empty()) {
  125. result = WaitForAsyncEntitStartOperation(pTrigger, pendingEntiList);
  126. }
  127. [](std::vector < StartEntityOperation*>& entities) {
  128. for (auto& entity : entities) {
  129. delete entity;
  130. }
  131. }(pendingEntiList);
  132. }
  133. return result;
  134. }