SpModule.cpp 5.6 KB

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