123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include "StrategyLib.h"
- #include "SysUtils.h"
- #if defined(LINUX)
- #include <dlfcn.h>
- #endif
- #define STRA_LIB_INFO_LEN 1000
- NAMESPACE_MAS_BEGIN
- StrategyLib::StrategyLib(const std::string & path) :
- m_path(path), m_straKey(""),
- m_sohd(INVALID_DLLHD),
- m_initialize(0), m_dispose(0),
- m_buildStrategy(0), m_destroyStrategy(0),
- m_getInformation(0)
- {
- }
- StrategyLib::~StrategyLib()
- {
- if (m_sohd != INVALID_DLLHD)
- {
- if (m_dispose)
- {
- m_dispose();
- }
- tzc::SysUtils::FreeLibrary(m_sohd);
- m_sohd = INVALID_DLLHD;
- }
- }
- TZ_INT StrategyLib::Initialize(StrategyInfo & straInfo)
- {
- m_sohd = tzc::SysUtils::LoadLibrary(m_path);
-
- if (INVALID_DLLHD == m_sohd)
- {
- TZLogError("load dll %s failed, dlerr = %s!!",
- m_path.c_str(), dlerror());
- return MEC_FAILED;
- }
- m_initialize =
- (STRA_Initialize)tzc::SysUtils::GetSymbol(m_sohd, INITIALIZE_STRATEGY_SYMBOL);
- if (!m_initialize)
- {
- TZLogError("get INITIALIZE_STRATEGY_SYMBOL failed, invalid dll %s!!",
- m_path.c_str());
- return MEC_FAILED;
- }
- m_dispose =
- (STRA_Dispose)tzc::SysUtils::GetSymbol(m_sohd, DISPOSE_STRATEGY_SYMBOL);
- if (!m_dispose)
- {
- TZLogError("get DISPOSE_STRATEGY_SYMBOL failed, invalid dll %s!!",
- m_path.c_str());
- return MEC_FAILED;
- }
- m_buildStrategy =
- (STRA_BuildStrategy)tzc::SysUtils::GetSymbol(m_sohd, BUILD_STRATEGY_SYMBOL);
- if (!m_buildStrategy)
- {
- TZLogError("get BUILD_STRATEGY_SYMBOL failed, invalid dll %s!!",
- m_path.c_str());
- return MEC_FAILED;
- }
- m_destroyStrategy =
- (STRA_DestroyStrategy)tzc::SysUtils::GetSymbol(m_sohd, DESTROY_STRATEGY_SYMBOL);
- if (!m_destroyStrategy)
- {
- TZLogError("get DESTROY_STRATEGY_SYMBOL failed, invalid dll %s!!",
- m_path.c_str());
- return MEC_FAILED;
- }
- m_getInformation =
- (STRA_GetInformation)tzc::SysUtils::GetSymbol(m_sohd, INFO_STRATEGY_SYMBOL);
- if (!m_getInformation)
- {
- TZLogError("get INFO_STRATEGY_SYMBOL failed, invalid dll %s!!",
- m_path.c_str());
- return MEC_FAILED;
- }
- TZLogInfo("initialize SONode of dll %s succeed~~~", m_path.c_str());
- std::string sinfo;
- this->GetInformation(sinfo);
-
- StrategyInfo::fromJson(sinfo, straInfo);
- m_straKey = straInfo.Key;
- return m_initialize();
- }
- Strategy * StrategyLib::BuildStrategy()
- {
- if (!m_buildStrategy)
- {
- TZLogWarn("m_buildDetector is NULL, maybe have not been initialized!!!");
- return nullptr;
- }
- return m_buildStrategy();
- }
- TZ_INT StrategyLib::DestroyStrategy(Strategy * pstra)
- {
- if (!m_destroyStrategy)
- {
- TZLogWarn("m_destroyDetector is NULL, maybe have not been initialized!!!");
- return MEC_NULL_OBJ;
- }
- return m_destroyStrategy(pstra);
- }
- TZ_INT StrategyLib::GetInformation(std::string & info)
- {
- if (!m_getInformation)
- {
- TZLogWarn("m_getInformation is NULL, maybe have not been initialized!!!");
- return MEC_NULL_OBJ;
- }
- TZ_CHAR cinfo[STRA_LIB_INFO_LEN << 10];
- TZ_INT irst = m_getInformation(cinfo);
- if (irst)
- {
- TZLogWarn("%s m_getInformation failed!!!", m_path.c_str());
- return irst;
- }
- info = cinfo;
- return MEC_OK;
- }
- void StrategyLib::GetStraLibKey(std::string & straKey)
- {
- straKey = m_straKey;
- }
- NAMESPACE_MAS_END
|