mod_guiconsole.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #include "mod_GuiConsole.h"
  2. #include "publicFunExport.h"
  3. #include <EventCode.h>
  4. #include "GUIConsole_msg_g.h"
  5. bool isPad = false;
  6. void CGUIConsoleEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  7. {
  8. pTransactionContext->SendAnswer(Error_Succeed);
  9. }
  10. void CGUIConsoleEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
  11. {
  12. pTransactionContext->SendAnswer(Error_Succeed);
  13. }
  14. void CGUIConsoleEntity::OnLog(const CAutoArray<CUUID>& SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  15. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  16. const CAutoArray<DWORD>& Param, const char* pszEntityName, const char* pszModuleName, const char* pszMessage, const linkContext& pLinkInfo)
  17. {
  18. // 忽略GPIO事件
  19. if (dwUserCode == 0x2090000A || dwUserCode == 0x20900009)
  20. return;
  21. //did not open log sender
  22. if (!m_isOpenLogSender)
  23. return;
  24. if (pszMessage != NULL && strlen(pszMessage) > 2)
  25. {
  26. CSimpleStringA str = pszMessage;
  27. LogInfo msg;
  28. msg.sysError = dwSysError;
  29. msg.userCode = dwUserCode;
  30. msg.timeStr = GenerateTimeStr().c_str();
  31. msg.entityName = pszEntityName;
  32. msg.LogType = LogTypeEnumToString(eLogType).c_str();
  33. msg.message = str;
  34. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  35. }
  36. }
  37. void CGUIConsoleEntity::OnEntityStateHook(const char* pszEntityName, const char* pszTriggerEntity, EntityStateEnum eState, EntityStateEnum eLastState)
  38. {
  39. //did not open log sender
  40. if (!m_isOpenLogSender)
  41. return;
  42. LogInfo msg;
  43. if (eLastState == EntityState_Idle && eState != EntityState_Idle)
  44. {
  45. msg.sysError = ErrorCodeEnum::Error_Closed;
  46. msg.userCode = ErrorCodeEnum::Error_Closed;
  47. msg.message = CSimpleString::Format("%s实体异常,重新启动...", pszEntityName);
  48. }
  49. else
  50. {
  51. msg.sysError = ErrorCodeEnum::Error_Succeed;
  52. msg.userCode = ErrorCodeEnum::Error_Succeed;
  53. if (eState == EntityState_Starting)
  54. msg.message = CSimpleString::Format("正在启动实体%s...", pszEntityName);
  55. else if(eState == EntityState_Idle)
  56. msg.message = CSimpleString::Format("%s 实体启动成功", pszEntityName);
  57. }
  58. msg.timeStr = GenerateTimeStr().c_str();
  59. msg.entityName = pszEntityName;
  60. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  61. }
  62. void CGUIConsoleEntity::OnTimeout(DWORD dwTimerID)
  63. {
  64. }
  65. std::pair<DWORD, std::string> CGUIConsoleEntity::openLogSender(SpReqAnsContext<GUIConsoleService_OpenLogSender_Req, GUIConsoleService_OpenLogSender_Ans>::Pointer ctx)
  66. {
  67. if ((__int64)m_logSubID != 0)
  68. {
  69. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  70. m_isOpenLogSender = true;
  71. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_REGISTER, "openLogSender: already register");
  72. }
  73. else
  74. {
  75. auto logType = StringToLogTypeEnum(ctx->Req.LogType.GetData());
  76. //check valid input for this function
  77. if (logType != LogTypeEnum::Log_Ignore && logType != LogTypeEnum::Log_Event && logType != LogTypeEnum::Log_Warning && logType != LogTypeEnum::Log_Error)
  78. {
  79. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NotSupport, WARN_GUICONSOLE_LOGSENDER_UNVAILD_INPUT,
  80. CSimpleString::Format("openLogSender: not valid input: %s, use default warning", ctx->Req.LogType.GetData()).GetData());
  81. m_sendLogType = LogTypeEnum::Log_Warning;
  82. }
  83. else
  84. m_sendLogType = logType;
  85. auto rc = GetFunction()->SubscribeLog(m_logSubID, this, (LogTypeEnum)m_sendLogType, Severity_None, Error_IgnoreAll, -1, NULL, false);
  86. m_isOpenLogSender = (rc == ErrorCodeEnum::Error_Succeed);
  87. return std::pair<DWORD, std::string>(rc, "");
  88. }
  89. }
  90. std::pair<DWORD, std::string> CGUIConsoleEntity::closeLogSender()
  91. {
  92. if ((__int64)m_logSubID == 0)
  93. {
  94. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_AlreadyExist, WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "openLogSender: already unregister");
  95. m_isOpenLogSender = false;
  96. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_LOG_ALREADY_UNREGISTER, "openLogSender: already register");
  97. }
  98. else
  99. {
  100. auto rc = GetFunction()->UnsubscribeLog(m_logSubID);
  101. if(rc == ErrorCodeEnum::Error_Succeed)
  102. m_logSubID = 0;
  103. m_isOpenLogSender = !(rc == ErrorCodeEnum::Error_Succeed);
  104. return std::pair<DWORD, std::string>(rc, "");
  105. }
  106. }
  107. std::pair<DWORD, std::string> CGUIConsoleEntity::openEntityMonitor()
  108. {
  109. m_isOpenEntityMonitor = true;
  110. /*
  111. timer->stop();
  112. ui->performTableWidget->clearContents();
  113. process->start("/bin/bash");
  114. if(process->waitForStarted(3000)) {
  115. process->write(cmd.toLatin1());
  116. process->closeWriteChannel();
  117. process->waitForFinished(3000);
  118. QString result = process->readAllStandardOutput();
  119. QRegularExpression reg;
  120. reg.setPattern(" +");
  121. result = result.replace(reg," ");
  122. QStringList elemList = result.split("\n");
  123. elemList.removeFirst();
  124. elemList.removeLast();
  125. ui->performTableWidget->setRowCount(elemList.length());
  126. for(int i=0;i< elemList.length();i++) {
  127. QString line = elemList.at(i);
  128. QStringList elems = line.split(" ");
  129. QString strPID = elems.at(RAW_PID_INDEX);
  130. bool root = false, related = false;
  131. for(int j=0; j<ui->performTableWidget->columnCount(); j++) {
  132. QTableWidgetItem* item = new QTableWidgetItem;
  133. item->setBackground((i % 2 != 0) ? QColor("#f5f5f5") : QColor("#ffffff"));
  134. if(j== COLUMN_NAME_INDEX) {
  135. item->setWhatsThis(strPID);
  136. line = elems.at(RAW_COMMAND_INDEX);
  137. if (line.compare("spshell", Qt::CaseInsensitive) == 0
  138. || line.compare("sphost", Qt::CaseInsensitive) == 0
  139. || line.compare("cefclient", Qt::CaseInsensitive) == 0
  140. || line.compare("guardian", Qt::CaseInsensitive) == 0) {
  141. related = true;
  142. item->setForeground(QBrush(Qt::red));
  143. if (line.compare("sphost", Qt::CaseInsensitive) == 0 && elems.count() > 7 && elems.at(7).startsWith("mod_")) {
  144. line = elems.at(7);
  145. }
  146. }
  147. item->setToolTip(line);
  148. item->setText(line);
  149. }
  150. else if (j == COLUMN_PID_INDEX) {
  151. item->setTextAlignment(Qt::AlignHCenter);
  152. item->setText(elems.at(RAW_PID_INDEX));
  153. }
  154. else if (j == COLUMN_CPU_INDEX) {
  155. item->setTextAlignment(Qt::AlignHCenter);
  156. item->setText(elems.at(RAW_CPU_INDEX));
  157. }
  158. else if (j == COLUMN_MEM_INDEX) {
  159. item->setTextAlignment(Qt::AlignHCenter);
  160. item->setText(elems.at(RAW_MEM_INDEX));
  161. }
  162. else if(j== COLUMN_COMMAND_INDEX)
  163. {
  164. line = elems.mid(5).join(" ");
  165. item->setText(line);
  166. item->setToolTip(line);
  167. } else if(j == COLUMN_FDS_INDEX) {
  168. if (related) {
  169. int fds = GetProcessFdsImpl(pro, strPID.toInt());
  170. item->setText(QString::number(fds));
  171. } else {
  172. item->setText("NA");
  173. }
  174. }
  175. ui->performTableWidget->setItem(i, j, item);
  176. }
  177. }
  178. }
  179. timer->start();
  180. */
  181. return std::pair<DWORD, std::string>(Error_Succeed, "");
  182. }
  183. std::pair<DWORD, std::string> CGUIConsoleEntity::closeEntityMonitor()
  184. {
  185. m_isOpenEntityMonitor = false;
  186. return std::pair<DWORD, std::string>(Error_Succeed, "");
  187. }
  188. std::pair<DWORD, std::string> CGUIConsoleEntity::openPerformanceSender(SpReqAnsContext<GUIConsoleService_OpenPerformanceSender_Req, GUIConsoleService_OpenPerformanceSender_Ans>::Pointer ctx)
  189. {
  190. m_isOpenPerformanceSender = true;
  191. if (ctx->Req.sendFrequence <= 1)
  192. m_performanceSenderFrequence = 1;
  193. else
  194. m_performanceSenderFrequence = ctx->Req.sendFrequence;
  195. auto performanceSender = [this] {
  196. while (m_isOpenPerformanceSender)
  197. {
  198. system_monitor_status curStatus;
  199. auto pullret = PollProcessList(m_performanceSenderFrequence, curStatus);
  200. PerformanceList dst;
  201. dst.NameArr.Init(curStatus.processList.size());
  202. dst.pidArr.Init(curStatus.processList.size());
  203. dst.cpuArr.Init(curStatus.processList.size());
  204. dst.memoryArr.Init(curStatus.processList.size());
  205. dst.cmdlineArr.Init(curStatus.processList.size());
  206. dst.handleNumArr.Init(curStatus.processList.size());
  207. for (int i = 0; i < curStatus.processList.size(); i++)
  208. {
  209. dst.NameArr[i] = curStatus.processList[i].ExeName;
  210. dst.pidArr[i] = curStatus.processList[i].ID;
  211. dst.cpuArr[i] = curStatus.processList[i].PercentProcessorTime;
  212. dst.memoryArr[i] = curStatus.processList[i].PercentMemory;
  213. dst.cmdlineArr[i] = "";
  214. dst.handleNumArr[i] = curStatus.processList[i].ThreadCount;
  215. }
  216. }
  217. };
  218. return std::pair<DWORD, std::string>(Error_Succeed, "");
  219. }
  220. std::pair<DWORD, std::string> CGUIConsoleEntity::closePerformanceSender()
  221. {
  222. m_isOpenPerformanceSender = false;
  223. return std::pair<DWORD, std::string>(Error_Succeed, "");
  224. }
  225. std::pair<DWORD, std::string> CGUIConsoleEntity::VTMSystemControl(SpReqAnsContext<GUIConsoleService_VTMSystemControl_Req, GUIConsoleService_VTMSystemControl_Ans>::Pointer ctx)
  226. {
  227. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  228. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  229. if (pFuncPrivilege == NULL)
  230. {
  231. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoPrivilege, WARN_GUICONSOLE_NO_PRIVILIGE, "VTMSystemControl no privilige");
  232. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_NO_PRIVILIGE, "");
  233. }
  234. DWORD rc = Error_Succeed;
  235. if(!ctx->Req.rebootFunction.Compare("RestartApp", true))
  236. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Framework);
  237. else if (!ctx->Req.rebootFunction.Compare("RestartOS", true))
  238. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_OS);
  239. else if (!ctx->Req.rebootFunction.Compare("ShutdownOS", true))
  240. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Power);
  241. else if (!ctx->Req.rebootFunction.Compare("ExitApp", true))
  242. rc = pFuncPrivilege->Reboot(RebootTrigger_ManualLocal, RebootWayEnum::RebootWay_Unknown);
  243. else
  244. {
  245. LogWarn(SeverityLevelEnum::Severity_Middle, ErrorCodeEnum::Error_NoDefine, WARN_GUICONSOLE_REBOOT_UNKNOWN_REASON,
  246. CSimpleStringA::Format("VTMSystemControl receive unknown param %s, do nothing", ctx->Req.rebootFunction.GetData()).GetData());
  247. return std::pair<DWORD, std::string>(WARN_GUICONSOLE_REBOOT_UNKNOWN_REASON, "");
  248. }
  249. return std::pair<DWORD, std::string>(rc, "");
  250. }
  251. std::pair<DWORD, std::string> CGUIConsoleEntity::VTM_controlEntity(SpReqAnsContext<GUIConsoleService_VTM_controlEntity_Req, GUIConsoleService_VTM_controlEntity_Ans>::Pointer ctx)
  252. {
  253. ErrorCodeEnum ret = ErrorCodeEnum::Error_Succeed;
  254. if (!ctx->Req.operation.Compare("Start", true))
  255. ret = AsyncStartEntity(ctx->Req.entityName, NULL, NULL);
  256. else if (!ctx->Req.operation.Compare("Stop", true))
  257. ret = AsyncStopEntity(ctx->Req.entityName, NULL);
  258. else if (!ctx->Req.operation.Compare("Pause", true))
  259. ret = AsyncPauseEntity(ctx->Req.entityName, NULL);
  260. else if (!ctx->Req.operation.Compare("Continue", true))
  261. ret = AsyncContinueEntity(ctx->Req.entityName, NULL);
  262. else if (!ctx->Req.operation.Compare("Terminate", true))
  263. ret = AsyncTerminateEntity(ctx->Req.entityName, NULL);
  264. if (m_isOpenLogSender)
  265. {
  266. LogInfo msg;
  267. msg.sysError = ret;
  268. msg.userCode = ret;
  269. msg.timeStr = GenerateTimeStr().c_str();
  270. msg.entityName = ctx->Req.entityName;
  271. msg.message = CSimpleString::Format("%s %s %s, ret:%d", ctx->Req.entityName, ctx->Req.operation
  272. , (ret == ErrorCodeEnum::Error_Succeed ? "Success" : "Failed"), ret);
  273. SpSendBroadcast(GetFunction(), eMsg_LogInfo, eMsgSig_LogInfo, msg);
  274. }
  275. return std::pair<DWORD, std::string>(ret, "");
  276. }
  277. CGUIConsoleEntity::CGUIConsoleEntity()
  278. :m_isOpenLogSender(false), m_isOpenEntityMonitor(false), m_isOpenPerformanceSender(false),
  279. m_sendLogType(LogTypeEnum::Log_Warning)
  280. {
  281. }
  282. CGUIConsoleEntity::~CGUIConsoleEntity()
  283. {}
  284. void CGUIConsoleSession::Handle_OpenLogSender(SpReqAnsContext<GUIConsoleService_OpenLogSender_Req, GUIConsoleService_OpenLogSender_Ans>::Pointer ctx)
  285. {
  286. auto ret = m_pEntity->openLogSender(ctx);
  287. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  288. }
  289. void CGUIConsoleSession::Handle_CloseLogSender(SpReqAnsContext<GUIConsoleService_CloseLogSender_Req, GUIConsoleService_CloseLogSender_Ans>::Pointer ctx)
  290. {
  291. auto ret = m_pEntity->closeLogSender();
  292. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  293. }
  294. void CGUIConsoleSession::Handle_OpenEntityMonitor(SpReqAnsContext<GUIConsoleService_OpenEntityMonitor_Req, GUIConsoleService_OpenEntityMonitor_Ans>::Pointer ctx)
  295. {
  296. auto ret = m_pEntity->openEntityMonitor();
  297. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  298. }
  299. void CGUIConsoleSession::Handle_CloseEntityMonitor(SpReqAnsContext<GUIConsoleService_CloseEntityMonitor_Req, GUIConsoleService_CloseEntityMonitor_Ans>::Pointer ctx)
  300. {
  301. auto ret = m_pEntity->closeEntityMonitor();
  302. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  303. }
  304. void CGUIConsoleSession::Handle_OpenPerformanceSender(SpReqAnsContext<GUIConsoleService_OpenPerformanceSender_Req, GUIConsoleService_OpenPerformanceSender_Ans>::Pointer ctx)
  305. {
  306. auto ret = m_pEntity->openPerformanceSender(ctx);
  307. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  308. }
  309. void CGUIConsoleSession::Handle_ClosePerformanceSender(SpReqAnsContext<GUIConsoleService_ClosePerformanceSender_Req, GUIConsoleService_ClosePerformanceSender_Ans>::Pointer ctx)
  310. {
  311. auto ret = m_pEntity->closePerformanceSender();
  312. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  313. }
  314. void CGUIConsoleSession::Handle_VTMSystemControl(SpReqAnsContext<GUIConsoleService_VTMSystemControl_Req, GUIConsoleService_VTMSystemControl_Ans>::Pointer ctx)
  315. {
  316. auto ret = m_pEntity->VTMSystemControl(ctx);
  317. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  318. }
  319. void CGUIConsoleSession::Handle_VTM_controlEntity(SpReqAnsContext<GUIConsoleService_VTM_controlEntity_Req, GUIConsoleService_VTM_controlEntity_Ans>::Pointer ctx)
  320. {
  321. auto ret = m_pEntity->VTM_controlEntity(ctx);
  322. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  323. }
  324. void CGUIConsoleSession::Handle_GetAllEntityList(SpReqAnsContext<GUIConsoleService_GetAllEntityList_Req, GUIConsoleService_GetAllEntityList_Ans>::Pointer ctx)
  325. {
  326. CAutoArray<EntityEntry> Entities;
  327. auto ret = m_pEntity->GetAllEntity(Entities);
  328. if (ret.first != ErrorCodeEnum::Error_Succeed)
  329. {
  330. ctx->Answer((ErrorCodeEnum)ret.first, (ErrorCodeEnum)ret.first);
  331. return;
  332. }
  333. ctx->Ans.entityName.Init(Entities.GetCount());
  334. ctx->Ans.status.Init(Entities.GetCount());
  335. ctx->Ans.processId.Init(Entities.GetCount());
  336. ctx->Ans.versionNo.Init(Entities.GetCount());
  337. ctx->Ans.lastStartTime.Init(Entities.GetCount());
  338. for (int i = 0; i < Entities.GetCount(); ++i) {
  339. ctx->Ans.entityName[i] = Entities[i].Name;
  340. ctx->Ans.status[i] = EntityEntry::GetStateName(Entities[i].State);
  341. ctx->Ans.processId[i] = std::to_string(Entities[i].Pid).c_str();
  342. ctx->Ans.versionNo[i] = m_pEntity->getRunVersion();
  343. ctx->Ans.lastStartTime[i] = Entities[i].lastStartTime.ToTimeString();
  344. }
  345. ctx->Answer(Error_Succeed);
  346. }
  347. CSimpleString CGUIConsoleEntity::getRunVersion()
  348. {
  349. static CSimpleStringA activeVer;
  350. if(activeVer.GetLength() == 0)
  351. GetFunction()->GetRunningVersion(activeVer);
  352. return activeVer;
  353. }
  354. ErrorCodeEnum CGUIConsoleEntity::AsyncStartEntity(const char* entity_name, const char* cmdline, void* pData)
  355. {
  356. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  357. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  358. if (pFuncPrivilege != NULL) {
  359. CSmartPointer<IAsynWaitSp> spWait;
  360. ErrorCodeEnum Error = pFuncPrivilege->StartEntity(entity_name, cmdline, spWait);
  361. if (Error == Error_Succeed) {
  362. callback_entry* entry = new callback_entry();
  363. entry->pRawData = pData;
  364. entry->EntityName = entity_name;
  365. entry->ErrorResult = Error_Unexpect;
  366. entry->op = OP_START_ENTITY;
  367. spWait->SetCallback(this, entry);
  368. }
  369. return Error;
  370. }
  371. else {
  372. return Error_NoPrivilege;
  373. }
  374. }
  375. ErrorCodeEnum CGUIConsoleEntity::AsyncStopEntity(const char* entity_name, void* pData)
  376. {
  377. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  378. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  379. if (pFuncPrivilege != NULL) {
  380. CSmartPointer<IAsynWaitSp> spWait;
  381. ErrorCodeEnum Error = pFuncPrivilege->StopEntity(entity_name, spWait);
  382. if (Error == Error_Succeed) {
  383. callback_entry* entry = new callback_entry();
  384. entry->pRawData = pData;
  385. entry->EntityName = entity_name;
  386. entry->ErrorResult = Error_Unexpect;
  387. entry->op = OP_STOP_ENTITY;
  388. spWait->SetCallback(this, entry);
  389. }
  390. return Error;
  391. }
  392. else {
  393. return Error_NoPrivilege;
  394. }
  395. }
  396. ErrorCodeEnum CGUIConsoleEntity::AsyncPauseEntity(const char* entity_name, void* pData)
  397. {
  398. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  399. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  400. if (pFuncPrivilege != NULL) {
  401. CSmartPointer<IAsynWaitSp> spWait;
  402. ErrorCodeEnum Error = pFuncPrivilege->PauseEntity(entity_name, spWait);
  403. if (Error == Error_Succeed) {
  404. callback_entry* entry = new callback_entry();
  405. entry->pRawData = pData;
  406. entry->EntityName = entity_name;
  407. entry->ErrorResult = Error_Unexpect;
  408. entry->op = OP_PAUSE_ENTITY;
  409. spWait->SetCallback(this, entry);
  410. }
  411. return Error;
  412. }
  413. else {
  414. return Error_NoPrivilege;
  415. }
  416. }
  417. ErrorCodeEnum CGUIConsoleEntity::AsyncContinueEntity(const char* entity_name, void* pData)
  418. {
  419. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  420. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  421. if (pFuncPrivilege != NULL) {
  422. CSmartPointer<IAsynWaitSp> spWait;
  423. ErrorCodeEnum Error = pFuncPrivilege->ContinueEntity(entity_name, spWait);
  424. if (Error == Error_Succeed) {
  425. callback_entry* entry = new callback_entry();
  426. entry->pRawData = pData;
  427. entry->EntityName = entity_name;
  428. entry->ErrorResult = Error_Unexpect;
  429. entry->op = OP_CONTINUE_ENTITY;
  430. spWait->SetCallback(this, entry);
  431. }
  432. return Error;
  433. }
  434. else {
  435. return Error_NoPrivilege;
  436. }
  437. }
  438. ErrorCodeEnum CGUIConsoleEntity::AsyncTerminateEntity(const char* entity_name, void* pData)
  439. {
  440. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  441. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  442. if (pFuncPrivilege != NULL) {
  443. CSmartPointer<IAsynWaitSp> spWait;
  444. ErrorCodeEnum Error = pFuncPrivilege->TerminateEntity(entity_name, spWait);
  445. if (Error == Error_Succeed) {
  446. callback_entry* entry = new callback_entry();
  447. entry->pRawData = pData;
  448. entry->EntityName = entity_name;
  449. entry->ErrorResult = Error_Unexpect;
  450. entry->op = OP_TERMINATE_ENTITY;
  451. spWait->SetCallback(this, entry);
  452. }
  453. return Error;
  454. }
  455. else {
  456. return Error_NoPrivilege;
  457. }
  458. }
  459. ErrorCodeEnum CGUIConsoleEntity::GetEntity(const char* entity_name, EntityEntry& e)
  460. {
  461. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  462. LOG_ASSERT(spFunc != NULL);
  463. CEntityStaticInfo StaticInfo;
  464. CEntityRunInfo RunInfo;
  465. ErrorCodeEnum Error = spFunc->GetEntityStaticInfo(entity_name, StaticInfo);
  466. if (Error == Error_Succeed) {
  467. Error = spFunc->GetEntityRunInfo(entity_name, RunInfo);
  468. if (Error == Error_Succeed) {
  469. e.Name = entity_name;
  470. e.Id = RunInfo.dwEntityInstanceID;
  471. e.ModuleName = _GetFileName(StaticInfo.strSpFileName);
  472. e.State = RunInfo.eState;
  473. e.Type = StaticInfo.bStartedByShell ? 0 : 1;
  474. e.Pid = (int)RunInfo.dwProcessID;
  475. e.DevelopID = (int)StaticInfo.wEntityDevelopID;
  476. e.DebugLevel = RunInfo.eDebugLevel;
  477. e.lastStartTime = RunInfo.tmLastStart;
  478. }
  479. }
  480. return Error;
  481. }
  482. void CGUIConsoleEntity::PostEntityVary(const char* entity_name)
  483. {
  484. EntityEntry ret;
  485. DWORD rc;
  486. if (ErrorCodeEnum::Error_Succeed != (rc = GetEntity(entity_name, ret)))
  487. {
  488. LogWarn(SeverityLevelEnum::Severity_Middle, (ErrorCodeEnum)rc, WARN_GUICONSOLE_GETENTITY_FAILED
  489. , CSimpleStringA::Format("PostEntityVary-GetEntity failed, reason:%d", rc).GetData());
  490. return;
  491. }
  492. EntityStatus cur;
  493. cur.entityName = entity_name;
  494. cur.status = EntityEntry::GetStateName(ret.State);
  495. cur.processId = std::to_string(ret.Pid).c_str();
  496. cur.versionNo = getRunVersion();
  497. cur.lastStartTime = ret.lastStartTime.ToTimeString();
  498. SpSendBroadcast(GetFunction(), eMsg_EntityStatus, eMsgSig_EntityStatus, cur);
  499. }
  500. std::pair<DWORD, std::string> CGUIConsoleEntity::GetAllEntity(CAutoArray<EntityEntry>& Entities)
  501. {
  502. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  503. CAutoArray<CSimpleStringA> strEntityNames;
  504. CAutoArray<WORD> wDevelopIDs;
  505. ErrorCodeEnum Error = spFunc->GetAllRegistedEntity(strEntityNames, wDevelopIDs);
  506. if (Error_Succeed != Error)
  507. {
  508. LogWarn(SeverityLevelEnum::Severity_Middle, Error, WARN_GUICONSOLE_GETALLENTITY_REGISTERED, "GetAllEntity-GetAllRegistedEntity failed");
  509. return std::make_pair(WARN_GUICONSOLE_GETALLENTITY_REGISTERED, "");
  510. }
  511. CSimpleString ErrInfo;
  512. Entities.Init(strEntityNames.GetCount());
  513. for (int i = 0; i < strEntityNames.GetCount(); ++i) {
  514. Error = GetEntity(strEntityNames[i], Entities[i]);
  515. if (Error != Error_Succeed)
  516. ErrInfo += CSimpleString::Format("|%s %d|", strEntityNames[i].GetData(), Error);
  517. }
  518. if (ErrInfo.GetLength() > 0)
  519. {
  520. LogWarn(SeverityLevelEnum::Severity_Middle, Error, WARN_GUICONSOLE_GETALLENTITY_GETENTITY, ErrInfo.GetData());
  521. return std::make_pair(WARN_GUICONSOLE_GETALLENTITY_GETENTITY, "");
  522. }
  523. return std::make_pair(ErrorCodeEnum::Error_Succeed, "");
  524. }
  525. void CGUIConsoleEntity::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  526. {
  527. CSmartPointer<ICallbackListener> spCallback;
  528. CSmartPointer<IReleasable> pData;
  529. pAsynWaitSp->GetCallback(spCallback, pData);
  530. LOG_ASSERT(pData.GetRawPointer() != NULL);
  531. callback_entry* entry = static_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
  532. entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
  533. if(m_isOpenEntityMonitor)
  534. PostEntityVary(entry->EntityName);//if registered, while entity status changed, send to web
  535. }
  536. SP_BEGIN_ENTITY_MAP()
  537. SP_ENTITY(CGUIConsoleEntity)
  538. SP_END_ENTITY_MAP()