SpModule.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpEntity.h"
  4. #include "SpEntityPrivilege.h"
  5. #include "SpModule.h"
  6. #include "sp_dbg_export.h"
  7. #include "sp_log.h"
  8. #include "sp_def.h"
  9. #include "sp_env.h"
  10. #include "strutil.h"
  11. #ifdef RVC_OS_WIN
  12. #include "sp_checkEntity.h"
  13. #endif //RVC_OS_WIN
  14. #include <winpr/thread.h>
  15. #include <RVCEventCode.h>
  16. SpModule::SpModule( sp_mod_t *mod, sp_cfg_shell_module_t *cfg_mod )
  17. : m_arrEntity(NULL)
  18. , m_iom(NULL)
  19. , m_mod(mod),
  20. m_anonymous_log(NULL), m_stub(NULL), m_cfg_mod(cfg_mod)
  21. {
  22. m_dwEntityTls = TlsAlloc();
  23. }
  24. SpModule::~SpModule()
  25. {
  26. TlsFree(m_dwEntityTls);
  27. }
  28. void SpModule::SetThreadEntity(SpEntity* pEntity)
  29. {
  30. TlsSetValue(m_dwEntityTls, pEntity);
  31. }
  32. SpEntity* SpModule::GetThreadEntity()
  33. {
  34. return (SpEntity*)TlsGetValue(m_dwEntityTls);
  35. }
  36. void SpModule::LogMessage( const LogTypeEnum LogType, const SeverityLevelEnum Level, DWORD dwSysErrorCode, DWORD dwUserErrorCode, const char *pMessage )
  37. {
  38. #ifdef RVC_OS_WIN
  39. SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
  40. #else
  41. SpEntity* pEntity = GetThreadEntity();
  42. #endif //RVC_OS_WIN
  43. if (!pEntity) {
  44. if (m_arrEntity->nelts == 1)
  45. pEntity = ARRAY_IDX(m_arrEntity, 0, SpEntity*);
  46. }
  47. if (pEntity) {
  48. pEntity->LogMessage(LogType, Level, dwSysErrorCode, dwUserErrorCode, pMessage);
  49. } else {
  50. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("use anonymous name to log!");
  51. sp_log_client_logEx(m_anonymous_log, LogType, Level, (int)dwSysErrorCode, (int)dwUserErrorCode, 0, 0
  52. , NULL == pMessage ? "" : pMessage, NULL == pMessage ? 0 : strlen(pMessage));
  53. }
  54. }
  55. void SpModule::LogMessage(const LogTypeEnum LogType, const SeverityLevelEnum Level, DWORD dwSysErrorCode, DWORD dwUserErrorCode, const char* pMessage, const linkContext& t_context)
  56. {
  57. #ifdef RVC_OS_WIN
  58. SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
  59. #else
  60. SpEntity* pEntity = GetThreadEntity();
  61. #endif //RVC_OS_WIN
  62. if (!pEntity) {
  63. if (m_arrEntity->nelts == 1)
  64. pEntity = ARRAY_IDX(m_arrEntity, 0, SpEntity*);
  65. }
  66. if (pEntity) {
  67. pEntity->LogMessage(LogType, Level, dwSysErrorCode, dwUserErrorCode, pMessage, t_context);
  68. }
  69. else {
  70. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("use anonymous name to log!");
  71. sp_log_client_logEx(m_anonymous_log, LogType, Level, (int)dwSysErrorCode, (int)dwUserErrorCode, 0, 0
  72. , NULL == pMessage ? "" : pMessage, NULL == pMessage ? 0 : strlen(pMessage));
  73. }
  74. }
  75. ErrorCodeEnum SpModule::Init( const char *url )
  76. {
  77. int rc;
  78. m_arrEntity = array_make(0, sizeof(SpEntity*));
  79. /** create bus endpt*/
  80. //sp_dbg_debug("to create iom instance...");
  81. rc = sp_iom_create(url, m_mod->cfg->idx, &m_iom);
  82. if (rc != 0) {
  83. Sleep(500);
  84. if (0 != (rc = sp_iom_create(url, m_mod->cfg->idx, &m_iom)))
  85. {
  86. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode(RTAERR_SPHOST_IOM_FAILED)("creat iom instance failed!");
  87. return Error_Param;
  88. }
  89. }
  90. rc = sp_log_client_create(NULL, m_iom, &m_anonymous_log);
  91. if (rc != 0) {
  92. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("creat log client instance failed!");
  93. return Error_Unexpect;
  94. }
  95. sp_mod_stub_cb cb;
  96. cb.on_module_init = &SpModule::__on_module_init;
  97. cb.on_module_term = &SpModule::__on_module_term;
  98. cb.user_data = this;
  99. //sp_dbg_debug("to create module sub instance...");
  100. rc = sp_mod_stub_create(&cb, m_iom, &m_stub);
  101. if (rc != 0) {
  102. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("creat module stub instance failed!");
  103. return Error_Unexpect;
  104. }
  105. return Error_Succeed;
  106. }
  107. VOID SpModule::Term()
  108. {
  109. sp_mod_stub_destroy(m_stub);
  110. RemoveEntityBase(NULL); // remove all
  111. if (m_anonymous_log) {
  112. sp_log_client_destroy(m_anonymous_log);
  113. m_anonymous_log = NULL;
  114. }
  115. if (m_iom) {
  116. sp_iom_destroy(m_iom);
  117. m_iom = NULL;
  118. }
  119. array_free(m_arrEntity);
  120. }
  121. ErrorCodeEnum SpModule::Run()
  122. {
  123. sp_iom_run(m_iom);
  124. return Error_Succeed;
  125. }
  126. int SpModule::on_module_init()
  127. {
  128. #ifdef RVC_OS_WIN
  129. EntryRoutine curPfMain = (EntryRoutine)getEntityResource()->m_pfMain;
  130. if (NULL != curPfMain)
  131. return curPfMain();
  132. #else
  133. if (SpModule::s_pfMain) {
  134. return SpModule::s_pfMain();
  135. }
  136. #endif //RVC_OS_WIN
  137. return Error_NotInit; // dont allow use null routine
  138. }
  139. int SpModule::on_module_term()
  140. {
  141. #ifdef RVC_OS_WIN
  142. EntryRoutine curPfExit = (EntryRoutine)getEntityResource()->m_pfExit;
  143. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("on_module_term");
  144. #endif //RVC_OS_WIN
  145. if (SpModule::s_pfExit)
  146. return SpModule::s_pfExit();
  147. #ifdef RVC_OS_WIN
  148. if (NULL != curPfExit)
  149. return curPfExit();
  150. #endif //RVC_OS_WIN
  151. return Error_Succeed; // allow use null routine
  152. }
  153. int SpModule::__on_module_init(sp_mod_stub_t *stub, void *user_data)
  154. {
  155. SpModule *pThis = (SpModule *)user_data;
  156. return pThis->on_module_init();
  157. }
  158. int SpModule::__on_module_term(sp_mod_stub_t *stub, void *user_data)
  159. {
  160. SpModule *pThis = (SpModule *)user_data;
  161. return pThis->on_module_term();
  162. }
  163. ErrorCodeEnum SpModule::AddEntityBase(CEntityBase *pEntity)
  164. {
  165. sp_env_t *env = sp_get_env();
  166. const char *lpEntityName = pEntity->GetEntityName();
  167. SpEntity *pSpEntity = FindEntity(lpEntityName);
  168. if (pSpEntity) {
  169. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("entity already exist!!!");
  170. return Error_AlreadyExist;
  171. }
  172. sp_entity_t *ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, lpEntityName);
  173. if (!ent || ent->mod->cfg->idx != m_mod->cfg->idx) {
  174. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("entity %s does not exist in current module!", lpEntityName);
  175. return Error_Bug;
  176. }
  177. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Initialize entity %s, Version: %s ...", lpEntityName, pEntity->GetEntityVersion());
  178. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, ent->cfg->idx);
  179. sp_cfg_refresh_ent_version(cfg_ent, pEntity->GetEntityVersion());
  180. if (cfg_ent->privilege) {
  181. pSpEntity = new SpEntityPrivilege(this, ent, cfg_ent, pEntity);
  182. } else {
  183. pSpEntity = new SpEntity(this, ent, cfg_ent, pEntity);
  184. }
  185. ErrorCodeEnum Error = pSpEntity->Init();
  186. if (Error == Error_Succeed) {
  187. ARRAY_PUSH(m_arrEntity, SpEntity*) = pSpEntity;
  188. pEntity->m_pEntityFunction = pSpEntity;
  189. } else {
  190. delete pSpEntity;
  191. pSpEntity = NULL;
  192. }
  193. return Error;
  194. }
  195. ErrorCodeEnum SpModule::RemoveEntityBase(CEntityBase *pEntity)
  196. {
  197. ErrorCodeEnum Error = Error_Unexpect;
  198. const char *lpEntityName = pEntity ? pEntity->GetEntityName() : NULL;
  199. for (int i = 0; i < m_arrEntity->nelts; ++i) {
  200. SpEntity *pSpEntity = ARRAY_IDX(m_arrEntity, i, SpEntity*);
  201. if (lpEntityName == NULL || _stricmp(lpEntityName, pSpEntity->get_ent()->cfg->name) == 0) {
  202. if (i != m_arrEntity->nelts-1) {
  203. ARRAY_IDX(m_arrEntity,i,SpEntity*) = ARRAY_IDX(m_arrEntity,m_arrEntity->nelts-1,SpEntity*);
  204. }
  205. array_pop(m_arrEntity);
  206. pSpEntity->Term();
  207. delete pSpEntity;
  208. pSpEntity = NULL;
  209. Error = Error_Succeed;
  210. break;
  211. }
  212. }
  213. return Error;
  214. }
  215. ErrorCodeEnum SpModule::GetEntityBase(const char *pszEntityName,CSmartPointer<CEntityBase> &pEntity)
  216. {
  217. SpEntity *pSpEntity = FindEntity(pszEntityName);
  218. if (pSpEntity) {
  219. pEntity = CSmartPointer<CEntityBase>(pSpEntity->GetEntityBase());
  220. return Error_Succeed;
  221. }
  222. return Error_NotExist;
  223. }
  224. int SpModule::GetEntityCount()
  225. {
  226. return m_arrEntity->nelts;
  227. }
  228. SpEntity *SpModule::FindEntity(const char *lpName)
  229. {
  230. if (lpName) {
  231. for (int i = 0; i < m_arrEntity->nelts; ++i) {
  232. SpEntity *t = ARRAY_IDX(m_arrEntity, i, SpEntity *);
  233. if (_stricmp(t->get_ent()->cfg->name, lpName) == 0)
  234. return t;
  235. }
  236. }
  237. return NULL;
  238. }
  239. EntryRoutine SpModule::s_pfExit = NULL;
  240. EntryRoutine SpModule::s_pfMain = NULL;