EntityBootStruct.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #ifndef HEALTHMANAGER_ENTITY_BOOT_STRUCT_H
  2. #define HEALTHMANAGER_ENTITY_BOOT_STRUCT_H
  3. #include <vector>
  4. #include <memory>
  5. #include <thread>
  6. #include <chrono>
  7. #include <algorithm>
  8. #include "iniutil.h"
  9. #include "SpBase.h"
  10. #define ISSUCCEEDED(hr) ((hr) == Error_Succeed)
  11. #define FAILURED(hr) (!(ISSUCCEEDED(hr)))
  12. enum EntityBootType
  13. {
  14. IgnoreResult,
  15. VerifyInAsync,
  16. VerifyInSync
  17. };
  18. enum EntityBootStep
  19. {
  20. NotInit,
  21. CoreBoot,
  22. SafeLoad,
  23. IEBrowser,
  24. Operating,
  25. NotConfigInit,
  26. NotConfigSecond
  27. };
  28. struct EntityBootInfo
  29. {
  30. CSimpleStringA entityName;
  31. CSimpleStringA cmdLine;
  32. EntityBootType bootType;
  33. ErrorCodeEnum bootResult;
  34. EntityBootInfo(const char* name, EntityBootType type)
  35. :entityName(name)
  36. , bootType(type)
  37. , bootResult(Error_NotInit)
  38. {
  39. }
  40. };
  41. struct StartEntityOperation;
  42. struct BootStep
  43. {
  44. CSimpleStringA strSectionName;
  45. std::vector< EntityBootInfo> entityList;
  46. BootStep() :strSectionName(true) {}
  47. BootStep(const char* stepName) :strSectionName(stepName) {}
  48. virtual ~BootStep() {}
  49. static ErrorCodeEnum WaitForAsyncEntitStartOperation(std::vector<StartEntityOperation*>& entityList);
  50. ErrorCodeEnum LoadConfig(const CSimpleStringA& configPath)
  51. {
  52. array_header_t* keys = inifile_read_section_key_all(configPath, strSectionName);
  53. if (!keys || array_empty(keys)) {
  54. return Error_NotExist;
  55. }
  56. entityList.clear();
  57. for (size_t i = 0; i < keys->nelts; ++i) {
  58. char* entityName = ARRAY_IDX(keys, i, char*);
  59. int bootType = inifile_read_int(configPath, strSectionName, entityName, 0);
  60. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("boot type info: %s=%d", entityName, bootType);
  61. EntityBootInfo entityBootInfo{ entityName, static_cast<EntityBootType>(bootType) };
  62. entityList.push_back(entityBootInfo);
  63. }
  64. toolkit_array_free2(keys);
  65. return Error_Succeed;
  66. }
  67. bool HasEntity() const
  68. {
  69. return(!entityList.empty());
  70. }
  71. ErrorCodeEnum StartStartupEntities(CEntityBase* pTrigger);
  72. };
  73. struct CoreBootStep : public BootStep
  74. {
  75. CoreBootStep(const CSimpleStringA& sectionPosix)
  76. :BootStep(CSimpleStringA("CoreBoot.") + sectionPosix)
  77. {
  78. }
  79. };
  80. struct SafeLoadStep : public BootStep
  81. {
  82. SafeLoadStep(const CSimpleStringA& sectionPosix)
  83. :BootStep(CSimpleStringA("SafeLoad.") + sectionPosix)
  84. {
  85. }
  86. };
  87. struct BrowserShowStep : public BootStep
  88. {
  89. BrowserShowStep(const CSimpleStringA& sectionPosix)
  90. :BootStep(CSimpleStringA("IEBrowser.") + sectionPosix + ".Url")
  91. {
  92. }
  93. };
  94. struct OperatingStep : public BootStep
  95. {
  96. OperatingStep(const CSimpleStringA& sectionPosix)
  97. : BootStep(CSimpleStringA("Operating.") + sectionPosix)
  98. {
  99. }
  100. };
  101. struct TerminalDeployStep : public BootStep
  102. {
  103. TerminalDeployStep(const CSimpleStringA& noUsed)
  104. : BootStep(CSimpleStringA("TerminalDeploy"))
  105. {
  106. }
  107. };
  108. struct TerminalDeploySecondStep : public BootStep
  109. {
  110. TerminalDeploySecondStep(const CSimpleStringA& sectionPosix)
  111. : BootStep(CSimpleStringA("TerminalDeploySec.") + sectionPosix)
  112. {
  113. }
  114. };
  115. struct TerminalDeployThirdStep : public BootStep
  116. {
  117. TerminalDeployThirdStep(const CSimpleStringA& sectionPosix)
  118. : BootStep(CSimpleStringA("TerminalDeployTrd.") + sectionPosix)
  119. {
  120. }
  121. };
  122. struct StartEntityBaseContext : public IReleasable
  123. {
  124. CEntityBase* theEntity;
  125. virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const { return true; }
  126. StartEntityBaseContext(CEntityBase* pEntity) :theEntity(pEntity) {}
  127. virtual ~StartEntityBaseContext() {}
  128. ErrorCodeEnum StartEntity(const char* entityName, const char* cmdLine, CSmartPointer<IAsynWaitSp>& spWait)
  129. {
  130. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = theEntity->GetFunction()
  131. .ConvertCase<IEntityFunctionPrivilege>();
  132. ErrorCodeEnum result = pFuncPrivilege->StartEntity(entityName, cmdLine, spWait);
  133. return result;
  134. }
  135. virtual ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo) { return Error_NotImpl; }
  136. };
  137. struct StartEntitySyncContenxt : public StartEntityBaseContext
  138. {
  139. using StartEntityBaseContext::StartEntityBaseContext;
  140. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  141. {
  142. ErrorCodeEnum result(Error_Succeed);
  143. CSmartPointer<IAsynWaitSp> spWait;
  144. result = StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
  145. if (ISSUCCEEDED(result) && spWait != nullptr) {
  146. result = spWait->WaitAnswer(60 * 1000);
  147. }
  148. return result;
  149. }
  150. };
  151. struct StartEntityOperation;
  152. struct StartEntityAsyncContenxt : public StartEntityBaseContext, public ICallbackListener
  153. {
  154. enum FinishedState
  155. {
  156. Pending,
  157. End
  158. } theFinishedState;
  159. StartEntityAsyncContenxt(CEntityBase* pEntity)
  160. : StartEntityBaseContext(pEntity)
  161. , theFinishedState(End)
  162. , asyncResult(Error_NotInit)
  163. {
  164. }
  165. virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const
  166. {
  167. if (retrieveResult) { *retrieveResult = asyncResult; }
  168. return (theFinishedState == End);
  169. }
  170. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  171. {
  172. LOG_FUNCTION();
  173. ErrorCodeEnum result(Error_Succeed);
  174. CSmartPointer<IAsynWaitSp> spWait;
  175. theFinishedState = Pending;
  176. result = StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
  177. if (ISSUCCEEDED(result) && spWait != nullptr) {
  178. spWait->SetCallback(this, nullptr);
  179. result = Error_Pending;
  180. } else {
  181. theFinishedState = End;
  182. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("return directly: %s", SpStrError(result));
  183. }
  184. asyncResult = result;
  185. return result;
  186. }
  187. void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  188. {
  189. LOG_FUNCTION();
  190. asyncResult = pAsynWaitSp->AsyncGetAnswer();
  191. theFinishedState = End;
  192. }
  193. ErrorCodeEnum asyncResult;
  194. };
  195. struct StartEntityOperation
  196. {
  197. StartEntityOperation(CEntityBase* pEntity, EntityBootInfo& entityInfo)
  198. :entInfo(entityInfo)
  199. {
  200. switch (entityInfo.bootType) {
  201. case VerifyInAsync:
  202. theContext = std::make_shared<StartEntityAsyncContenxt>(pEntity);
  203. break;
  204. case VerifyInSync:
  205. theContext = std::make_shared<StartEntitySyncContenxt>(pEntity);
  206. break;
  207. default:
  208. theContext = std::make_shared<StartEntityIgnoreContenxt>(pEntity);
  209. break;
  210. }
  211. }
  212. bool IsDone() { return theContext->IsFinished(&entInfo.bootResult); }
  213. ErrorCodeEnum StartEntity()
  214. {
  215. ErrorCodeEnum result = theContext->BootEntity(&entInfo);
  216. entInfo.bootResult = result;
  217. return result;
  218. }
  219. std::shared_ptr< StartEntityBaseContext> theContext;
  220. EntityBootInfo& entInfo;
  221. };
  222. ErrorCodeEnum BootStep::WaitForAsyncEntitStartOperation(std::vector<StartEntityOperation*>& entityList)
  223. {
  224. LOG_FUNCTION();
  225. ErrorCodeEnum result = Error_Succeed;
  226. for (; !entityList.empty();) {
  227. auto finishedInstance = find_if(entityList.begin(), entityList.end()
  228. , [](StartEntityOperation* operation) {
  229. return operation->IsDone();
  230. });
  231. if (finishedInstance != entityList.end()) {
  232. StartEntityOperation* operation = *finishedInstance;
  233. entityList.erase(finishedInstance);
  234. auto& entity = operation->entInfo;
  235. if (FAILURED(entity.bootResult)) {
  236. result = entity.bootResult;
  237. }
  238. delete operation;
  239. } else {
  240. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  241. }
  242. }
  243. return result;
  244. }
  245. ErrorCodeEnum BootStep::StartStartupEntities(CEntityBase* pTrigger)
  246. {
  247. ErrorCodeEnum result = Error_Succeed;
  248. if (HasEntity()) {
  249. std::vector <StartEntityOperation*> pendingEntiList;
  250. for (auto& entity : entityList) {
  251. StartEntityOperation* startOperat = new StartEntityOperation(pTrigger, entity);
  252. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to start entity: %s ...", entity.entityName.GetData());
  253. result = startOperat->StartEntity();
  254. if (!startOperat->IsDone()) {
  255. pendingEntiList.push_back(startOperat);
  256. } else {
  257. if (ISSUCCEEDED(result)) {
  258. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start entity: %s successfully :)", entity.entityName.GetData());
  259. } else {
  260. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start entity: %s failed :( EC: %s", entity.entityName.GetData(), SpStrError(result));
  261. }
  262. delete startOperat;
  263. if (FAILURED(result)) {
  264. break;
  265. }
  266. }
  267. }
  268. if (!pendingEntiList.empty()) {
  269. result = WaitForAsyncEntitStartOperation(pendingEntiList);
  270. }
  271. [](std::vector < StartEntityOperation*>& entities) {
  272. for (auto& entity : entities) {
  273. delete entity;
  274. }
  275. }(pendingEntiList);
  276. }
  277. return result;
  278. }
  279. #endif