123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpEntity.h"
- #include "SpEntityPrivilege.h"
- #include "SpModule.h"
- #include "sp_dbg_export.h"
- #include "sp_log.h"
- #include "sp_def.h"
- #include "sp_env.h"
- #include "strutil.h"
- #ifdef RVC_OS_WIN
- #include "sp_checkEntity.h"
- #endif //RVC_OS_WIN
- #include <winpr/thread.h>
- #include <RVCEventCode.h>
- SpModule::SpModule( sp_mod_t *mod, sp_cfg_shell_module_t *cfg_mod )
- : m_arrEntity(NULL)
- , m_iom(NULL)
- , m_mod(mod),
- m_anonymous_log(NULL), m_stub(NULL), m_cfg_mod(cfg_mod)
- {
- m_dwEntityTls = TlsAlloc();
- }
- SpModule::~SpModule()
- {
- TlsFree(m_dwEntityTls);
- }
- void SpModule::SetThreadEntity(SpEntity* pEntity)
- {
- TlsSetValue(m_dwEntityTls, pEntity);
- }
- SpEntity* SpModule::GetThreadEntity()
- {
- return (SpEntity*)TlsGetValue(m_dwEntityTls);
- }
- void SpModule::LogMessage( const LogTypeEnum LogType, const SeverityLevelEnum Level, DWORD dwSysErrorCode, DWORD dwUserErrorCode, const char *pMessage )
- {
- #ifdef RVC_OS_WIN
- SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
- #else
- SpEntity* pEntity = GetThreadEntity();
- #endif //RVC_OS_WIN
- if (!pEntity) {
- if (m_arrEntity->nelts == 1)
- pEntity = ARRAY_IDX(m_arrEntity, 0, SpEntity*);
- }
- if (pEntity) {
- pEntity->LogMessage(LogType, Level, dwSysErrorCode, dwUserErrorCode, pMessage);
- } else {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("use anonymous name to log!");
- sp_log_client_logEx(m_anonymous_log, LogType, Level, (int)dwSysErrorCode, (int)dwUserErrorCode, 0, 0
- , NULL == pMessage ? "" : pMessage, NULL == pMessage ? 0 : strlen(pMessage));
- }
- }
- void SpModule::LogMessage(const LogTypeEnum LogType, const SeverityLevelEnum Level, DWORD dwSysErrorCode, DWORD dwUserErrorCode, const char* pMessage, const linkContext& t_context)
- {
- #ifdef RVC_OS_WIN
- SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
- #else
- SpEntity* pEntity = GetThreadEntity();
- #endif //RVC_OS_WIN
- if (!pEntity) {
- if (m_arrEntity->nelts == 1)
- pEntity = ARRAY_IDX(m_arrEntity, 0, SpEntity*);
- }
- if (pEntity) {
- pEntity->LogMessage(LogType, Level, dwSysErrorCode, dwUserErrorCode, pMessage, t_context);
- }
- else {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("use anonymous name to log!");
- sp_log_client_logEx(m_anonymous_log, LogType, Level, (int)dwSysErrorCode, (int)dwUserErrorCode, 0, 0
- , NULL == pMessage ? "" : pMessage, NULL == pMessage ? 0 : strlen(pMessage));
- }
- }
- ErrorCodeEnum SpModule::Init( const char *url )
- {
- int rc;
- m_arrEntity = array_make(0, sizeof(SpEntity*));
- /** create bus endpt*/
- //sp_dbg_debug("to create iom instance...");
- rc = sp_iom_create(url, m_mod->cfg->idx, &m_iom);
- if (rc != 0) {
- Sleep(500);
- if (0 != (rc = sp_iom_create(url, m_mod->cfg->idx, &m_iom)))
- {
- DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode(RTAERR_SPHOST_IOM_FAILED)("creat iom instance failed!");
- return Error_Param;
- }
- }
- rc = sp_log_client_create(NULL, m_iom, &m_anonymous_log);
- if (rc != 0) {
- DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("creat log client instance failed!");
- return Error_Unexpect;
- }
- sp_mod_stub_cb cb;
- cb.on_module_init = &SpModule::__on_module_init;
- cb.on_module_term = &SpModule::__on_module_term;
- cb.user_data = this;
- //sp_dbg_debug("to create module sub instance...");
- rc = sp_mod_stub_create(&cb, m_iom, &m_stub);
- if (rc != 0) {
- DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("creat module stub instance failed!");
- return Error_Unexpect;
- }
- return Error_Succeed;
- }
- VOID SpModule::Term()
- {
-
- sp_mod_stub_destroy(m_stub);
- RemoveEntityBase(NULL); // remove all
- if (m_anonymous_log) {
- sp_log_client_destroy(m_anonymous_log);
- m_anonymous_log = NULL;
- }
- if (m_iom) {
- sp_iom_destroy(m_iom);
- m_iom = NULL;
- }
- array_free(m_arrEntity);
- }
- ErrorCodeEnum SpModule::Run()
- {
- sp_iom_run(m_iom);
- return Error_Succeed;
- }
- int SpModule::on_module_init()
- {
- #ifdef RVC_OS_WIN
- EntryRoutine curPfMain = (EntryRoutine)getEntityResource()->m_pfMain;
- if (NULL != curPfMain)
- return curPfMain();
- #else
- if (SpModule::s_pfMain) {
- return SpModule::s_pfMain();
- }
- #endif //RVC_OS_WIN
- return Error_NotInit; // dont allow use null routine
- }
- int SpModule::on_module_term()
- {
- #ifdef RVC_OS_WIN
- EntryRoutine curPfExit = (EntryRoutine)getEntityResource()->m_pfExit;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("on_module_term");
- #endif //RVC_OS_WIN
- if (SpModule::s_pfExit)
- return SpModule::s_pfExit();
- #ifdef RVC_OS_WIN
- if (NULL != curPfExit)
- return curPfExit();
- #endif //RVC_OS_WIN
- return Error_Succeed; // allow use null routine
- }
- int SpModule::__on_module_init(sp_mod_stub_t *stub, void *user_data)
- {
- SpModule *pThis = (SpModule *)user_data;
- return pThis->on_module_init();
- }
- int SpModule::__on_module_term(sp_mod_stub_t *stub, void *user_data)
- {
- SpModule *pThis = (SpModule *)user_data;
- return pThis->on_module_term();
- }
- ErrorCodeEnum SpModule::AddEntityBase(CEntityBase *pEntity)
- {
- sp_env_t *env = sp_get_env();
- const char *lpEntityName = pEntity->GetEntityName();
- SpEntity *pSpEntity = FindEntity(lpEntityName);
- if (pSpEntity) {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("entity already exist!!!");
- return Error_AlreadyExist;
- }
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, lpEntityName);
- if (!ent || ent->mod->cfg->idx != m_mod->cfg->idx) {
- DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("entity %s does not exist in current module!", lpEntityName);
- return Error_Bug;
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Initialize entity %s, Version: %s ...", lpEntityName, pEntity->GetEntityVersion());
- sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, ent->cfg->idx);
- sp_cfg_refresh_ent_version(cfg_ent, pEntity->GetEntityVersion());
- if (cfg_ent->privilege) {
- pSpEntity = new SpEntityPrivilege(this, ent, cfg_ent, pEntity);
- } else {
- pSpEntity = new SpEntity(this, ent, cfg_ent, pEntity);
- }
- ErrorCodeEnum Error = pSpEntity->Init();
- if (Error == Error_Succeed) {
- ARRAY_PUSH(m_arrEntity, SpEntity*) = pSpEntity;
- pEntity->m_pEntityFunction = pSpEntity;
- } else {
- delete pSpEntity;
- pSpEntity = NULL;
- }
- return Error;
- }
- ErrorCodeEnum SpModule::RemoveEntityBase(CEntityBase *pEntity)
- {
- ErrorCodeEnum Error = Error_Unexpect;
- const char *lpEntityName = pEntity ? pEntity->GetEntityName() : NULL;
- for (int i = 0; i < m_arrEntity->nelts; ++i) {
- SpEntity *pSpEntity = ARRAY_IDX(m_arrEntity, i, SpEntity*);
- if (lpEntityName == NULL || _stricmp(lpEntityName, pSpEntity->get_ent()->cfg->name) == 0) {
- if (i != m_arrEntity->nelts-1) {
- ARRAY_IDX(m_arrEntity,i,SpEntity*) = ARRAY_IDX(m_arrEntity,m_arrEntity->nelts-1,SpEntity*);
- }
- array_pop(m_arrEntity);
- pSpEntity->Term();
- delete pSpEntity;
- pSpEntity = NULL;
- Error = Error_Succeed;
- break;
- }
- }
- return Error;
- }
- ErrorCodeEnum SpModule::GetEntityBase(const char *pszEntityName,CSmartPointer<CEntityBase> &pEntity)
- {
- SpEntity *pSpEntity = FindEntity(pszEntityName);
- if (pSpEntity) {
- pEntity = CSmartPointer<CEntityBase>(pSpEntity->GetEntityBase());
- return Error_Succeed;
- }
- return Error_NotExist;
- }
- int SpModule::GetEntityCount()
- {
- return m_arrEntity->nelts;
- }
- SpEntity *SpModule::FindEntity(const char *lpName)
- {
- if (lpName) {
- for (int i = 0; i < m_arrEntity->nelts; ++i) {
- SpEntity *t = ARRAY_IDX(m_arrEntity, i, SpEntity *);
- if (_stricmp(t->get_ent()->cfg->name, lpName) == 0)
- return t;
- }
- }
- return NULL;
- }
- EntryRoutine SpModule::s_pfExit = NULL;
- EntryRoutine SpModule::s_pfMain = NULL;
|