mod_BootManager.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifndef RVC_MOD_BOOT_MANAGER_H_
  2. #define RVC_MOD_BOOT_MANAGER_H_
  3. #include "SpBase.h"
  4. #include "SpTest.h"
  5. #include "modVer.h"
  6. #include <vector>
  7. #include <memory>
  8. #define ISSUCCEEDED(hr) ((hr) == Error_Succeed)
  9. #define FAILURED(hr) (!(ISSUCCEEDED(hr)))
  10. enum EntityBootType
  11. {
  12. IgnoreResult,
  13. VerifyInAsync,
  14. VerifyInSync
  15. };
  16. enum EntityBootStep
  17. {
  18. NotInit,
  19. CoreBoot,
  20. SafeLoad,
  21. IEBrowser,
  22. Operating
  23. };
  24. class BootManager;
  25. struct EntityBootInfo
  26. {
  27. CSimpleStringA entityName;
  28. EntityBootType bootType;
  29. ErrorCodeEnum bootResult;
  30. EntityBootInfo(const char* name, EntityBootType type)
  31. :entityName(name)
  32. , bootType(type)
  33. ,bootResult(Error_NotInit)
  34. {
  35. }
  36. };
  37. struct BootStep
  38. {
  39. CSimpleStringA strSectionName;
  40. std::vector< EntityBootInfo> entityList;
  41. BootStep() :strSectionName(true) {}
  42. BootStep(const char* stepName) :strSectionName(stepName) { }
  43. virtual ~BootStep() {}
  44. ErrorCodeEnum LoadConfig(const CSimpleStringA& configPath);
  45. bool HasEntity() const {
  46. return(!entityList.empty());
  47. }
  48. ErrorCodeEnum StartStartupEntities(BootManager* pTrigger);
  49. };
  50. struct CoreBootStep : public BootStep
  51. {
  52. CoreBootStep(const CSimpleStringA& sectionPosix)
  53. :BootStep(CSimpleStringA("CoreBoot.") + sectionPosix)
  54. { }
  55. };
  56. struct SafeLoadStep : public BootStep
  57. {
  58. SafeLoadStep(const CSimpleStringA& sectionPosix)
  59. :BootStep(CSimpleStringA("SafeLoad.") + sectionPosix)
  60. { }
  61. };
  62. struct BrowserShowStep : public BootStep
  63. {
  64. BrowserShowStep(const CSimpleStringA& sectionPosix)
  65. :BootStep(CSimpleStringA("IEBrowser.") + sectionPosix + ".Url")
  66. { }
  67. };
  68. struct OperatingStep : public BootStep
  69. {
  70. OperatingStep(const CSimpleStringA& sectionPosix)
  71. : BootStep(CSimpleStringA("Operating.") + sectionPosix)
  72. { }
  73. };
  74. struct StartEntityBaseContext : public IReleasable
  75. {
  76. CEntityBase* theEntity;
  77. virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const { return true; }
  78. StartEntityBaseContext(CEntityBase* pEntity) :theEntity(pEntity) {}
  79. virtual ~StartEntityBaseContext() {}
  80. ErrorCodeEnum StartEntity(const char* entityName, const char* cmdLine, CSmartPointer<IAsynWaitSp>& spWait)
  81. {
  82. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = theEntity->GetFunction()
  83. .ConvertCase<IEntityFunctionPrivilege>();
  84. ErrorCodeEnum result = pFuncPrivilege->StartEntity(entityName, cmdLine, spWait);
  85. return result;
  86. }
  87. virtual ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo) { return Error_NotImpl; }
  88. };
  89. struct StartEntitySyncContenxt : public StartEntityBaseContext
  90. {
  91. using StartEntityBaseContext::StartEntityBaseContext;
  92. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  93. {
  94. ErrorCodeEnum result(Error_Succeed);
  95. CSmartPointer<IAsynWaitSp> spWait;
  96. result = StartEntity(entityInfo->entityName, nullptr, spWait);
  97. if (ISSUCCEEDED(result) && spWait != nullptr) {
  98. result = spWait->WaitAnswer(60 * 1000);
  99. }
  100. return result;
  101. }
  102. };
  103. struct StartEntityIgnoreContenxt : public StartEntityBaseContext
  104. {
  105. using StartEntityBaseContext::StartEntityBaseContext;
  106. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  107. {
  108. CSmartPointer<IAsynWaitSp> spWait;
  109. StartEntity(entityInfo->entityName, nullptr, spWait);
  110. if (spWait != nullptr) {
  111. spWait->WaitAnswer(100);
  112. }
  113. return Error_Succeed;
  114. }
  115. };
  116. struct StartEntityOperation;
  117. struct StartEntityAsyncContenxt : public StartEntityBaseContext, public ICallbackListener
  118. {
  119. enum FinishedState
  120. {
  121. Pending,
  122. End
  123. } theFinishedState;
  124. StartEntityAsyncContenxt(CEntityBase* pEntity)
  125. : StartEntityBaseContext(pEntity)
  126. ,theFinishedState(End)
  127. ,asyncResult(Error_NotInit){ }
  128. virtual bool IsFinished(ErrorCodeEnum* retrieveResult) const
  129. {
  130. if (retrieveResult) { *retrieveResult = asyncResult; }
  131. return (theFinishedState == End);
  132. }
  133. ErrorCodeEnum BootEntity(const EntityBootInfo* entityInfo)
  134. {
  135. LOG_FUNCTION();
  136. ErrorCodeEnum result(Error_Succeed);
  137. CSmartPointer<IAsynWaitSp> spWait;
  138. theFinishedState = Pending;
  139. result = StartEntity(entityInfo->entityName, nullptr, spWait);
  140. if (ISSUCCEEDED(result) && spWait != nullptr) {
  141. spWait->SetCallback(this, nullptr);
  142. result = Error_Pending;
  143. } else {
  144. theFinishedState = End;
  145. Dbg("return directly: %s", SpStrError(result));
  146. }
  147. asyncResult = result;
  148. return result;
  149. }
  150. void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  151. {
  152. LOG_FUNCTION();
  153. asyncResult = pAsynWaitSp->AsyncGetAnswer();
  154. theFinishedState = End;
  155. }
  156. ErrorCodeEnum asyncResult;
  157. };
  158. struct StartEntityOperation
  159. {
  160. StartEntityOperation(CEntityBase* pEntity, EntityBootInfo& entityInfo)
  161. :entInfo(entityInfo)
  162. {
  163. switch (entityInfo.bootType) {
  164. case VerifyInAsync:
  165. theContext = std::make_shared<StartEntityAsyncContenxt>(pEntity);
  166. break;
  167. case VerifyInSync:
  168. theContext = std::make_shared<StartEntitySyncContenxt>(pEntity);
  169. break;
  170. default:
  171. theContext = std::make_shared<StartEntityIgnoreContenxt>(pEntity);
  172. break;
  173. }
  174. }
  175. bool IsDone() { return theContext->IsFinished(&entInfo.bootResult); }
  176. ErrorCodeEnum StartEntity()
  177. {
  178. ErrorCodeEnum result = theContext->BootEntity(&entInfo);
  179. entInfo.bootResult = result;
  180. return result;
  181. }
  182. std::shared_ptr< StartEntityBaseContext> theContext;
  183. EntityBootInfo& entInfo;
  184. };
  185. class BootManager : public CEntityBase
  186. {
  187. public:
  188. BootManager()
  189. {
  190. }
  191. ~BootManager()
  192. {
  193. }
  194. const char* GetEntityName() const override
  195. {
  196. return "BootManager";
  197. }
  198. const char* GetEntityVersion() const override
  199. {
  200. return MODULE_VERSION_FULL;
  201. }
  202. ON_ENTITYT_TEST()
  203. void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  204. CSmartPointer<ITransactionContext> pTransactionContext)
  205. {
  206. pTransactionContext->SendAnswer(Init());
  207. }
  208. void OnStarted() override;
  209. ErrorCodeEnum LoadBootConfigInfo();
  210. ErrorCodeEnum BootStartupEntity();
  211. void SendEntityBootMessage(const char* entityName);
  212. void SendEntityBootResultMessage(const char* entityName, ErrorCodeEnum result);
  213. private:
  214. void QtUIShow();
  215. ErrorCodeEnum Init();
  216. std::shared_ptr<CoreBootStep> mCoreBootSteper;
  217. std::shared_ptr<SafeLoadStep> mSafeLoadSteper;
  218. std::shared_ptr<BrowserShowStep> mBrowserShowSteper;
  219. std::shared_ptr<OperatingStep> mOperatingSteper;
  220. };
  221. class BootEntityTask : public ITaskSp
  222. {
  223. public:
  224. BootEntityTask(BootManager* entity) :pEntity(entity) {}
  225. ~BootEntityTask() {}
  226. void Process() override
  227. {
  228. LOG_FUNCTION();
  229. ErrorCodeEnum result = pEntity->LoadBootConfigInfo();
  230. if (ISSUCCEEDED(result)) {
  231. result = pEntity->BootStartupEntity();
  232. }
  233. }
  234. private:
  235. BootManager* pEntity;
  236. };
  237. #endif //RVC_MOD_BOOT_MANAGER_H_