EntityBootStruct.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 StartEntityIgnoreContenxt : public StartEntityBaseContext
  152. {
  153. using StartEntityBaseContext::StartEntityBaseContext;
  154. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  155. {
  156. CSmartPointer<IAsynWaitSp> spWait;
  157. StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
  158. if (spWait != nullptr) {
  159. spWait->WaitAnswer(100);
  160. }
  161. return Error_Succeed;
  162. }
  163. };
  164. struct StartEntityOperation;
  165. struct StartEntityAsyncContenxt : public StartEntityBaseContext, public ICallbackListener
  166. {
  167. enum FinishedState
  168. {
  169. Pending,
  170. End
  171. } theFinishedState;
  172. StartEntityAsyncContenxt(CEntityBase* pEntity)
  173. : StartEntityBaseContext(pEntity)
  174. , theFinishedState(End)
  175. , asyncResult(Error_NotInit)
  176. {
  177. }
  178. virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const
  179. {
  180. if (retrieveResult) { *retrieveResult = asyncResult; }
  181. return (theFinishedState == End);
  182. }
  183. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  184. {
  185. LOG_FUNCTION();
  186. ErrorCodeEnum result(Error_Succeed);
  187. CSmartPointer<IAsynWaitSp> spWait;
  188. theFinishedState = Pending;
  189. result = StartEntity(entityInfo->entityName, entityInfo->cmdLine, spWait);
  190. if (ISSUCCEEDED(result) && spWait != nullptr) {
  191. spWait->SetCallback(this, nullptr);
  192. result = Error_Pending;
  193. } else {
  194. theFinishedState = End;
  195. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("return directly: %s", SpStrError(result));
  196. }
  197. asyncResult = result;
  198. return result;
  199. }
  200. void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  201. {
  202. LOG_FUNCTION();
  203. asyncResult = pAsynWaitSp->AsyncGetAnswer();
  204. theFinishedState = End;
  205. }
  206. ErrorCodeEnum asyncResult;
  207. };
  208. struct StartEntityOperation
  209. {
  210. StartEntityOperation(CEntityBase* pEntity, EntityBootInfo& entityInfo)
  211. :entInfo(entityInfo)
  212. {
  213. switch (entityInfo.bootType) {
  214. case VerifyInAsync:
  215. theContext = std::make_shared<StartEntityAsyncContenxt>(pEntity);
  216. break;
  217. case VerifyInSync:
  218. theContext = std::make_shared<StartEntitySyncContenxt>(pEntity);
  219. break;
  220. default:
  221. theContext = std::make_shared<StartEntityIgnoreContenxt>(pEntity);
  222. break;
  223. }
  224. }
  225. bool IsDone() { return theContext->IsFinished(&entInfo.bootResult); }
  226. ErrorCodeEnum StartEntity()
  227. {
  228. ErrorCodeEnum result = theContext->BootEntity(&entInfo);
  229. entInfo.bootResult = result;
  230. return result;
  231. }
  232. std::shared_ptr< StartEntityBaseContext> theContext;
  233. EntityBootInfo& entInfo;
  234. };
  235. ErrorCodeEnum BootStep::WaitForAsyncEntitStartOperation(std::vector<StartEntityOperation*>& entityList)
  236. {
  237. LOG_FUNCTION();
  238. ErrorCodeEnum result = Error_Succeed;
  239. for (; !entityList.empty();) {
  240. auto finishedInstance = find_if(entityList.begin(), entityList.end()
  241. , [](StartEntityOperation* operation) {
  242. return operation->IsDone();
  243. });
  244. if (finishedInstance != entityList.end()) {
  245. StartEntityOperation* operation = *finishedInstance;
  246. entityList.erase(finishedInstance);
  247. auto& entity = operation->entInfo;
  248. if (FAILURED(entity.bootResult)) {
  249. result = entity.bootResult;
  250. }
  251. delete operation;
  252. } else {
  253. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  254. }
  255. }
  256. return result;
  257. }
  258. ErrorCodeEnum BootStep::StartStartupEntities(CEntityBase* pTrigger)
  259. {
  260. ErrorCodeEnum result = Error_Succeed;
  261. if (HasEntity()) {
  262. std::vector <StartEntityOperation*> pendingEntiList;
  263. for (auto& entity : entityList) {
  264. StartEntityOperation* startOperat = new StartEntityOperation(pTrigger, entity);
  265. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to start entity: %s ...", entity.entityName.GetData());
  266. result = startOperat->StartEntity();
  267. if (!startOperat->IsDone()) {
  268. pendingEntiList.push_back(startOperat);
  269. } else {
  270. if (ISSUCCEEDED(result)) {
  271. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start entity: %s successfully :)", entity.entityName.GetData());
  272. } else {
  273. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start entity: %s failed :( EC: %s", entity.entityName.GetData(), SpStrError(result));
  274. }
  275. delete startOperat;
  276. if (FAILURED(result)) {
  277. break;
  278. }
  279. }
  280. }
  281. if (!pendingEntiList.empty()) {
  282. result = WaitForAsyncEntitStartOperation(pendingEntiList);
  283. }
  284. [](std::vector < StartEntityOperation*>& entities) {
  285. for (auto& entity : entities) {
  286. delete entity;
  287. }
  288. }(pendingEntiList);
  289. }
  290. return result;
  291. }
  292. #endif