SpModule.cpp 4.9 KB

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