123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include "DetectorLib.h"
- #include "SysUtils.h"
- #if defined(LINUX)
- #include <dlfcn.h>
- #endif
- #define DET_LIB_INFO_LEN 1000
- NAMESPACE_MAS_BEGIN
- DetectorLib::DetectorLib(const std::string & path)
- : m_path(path),
- m_detKey(""),
- m_sohd(INVALID_DLLHD),
- m_initialize(nullptr),
- m_dispose(nullptr),
- m_buildDetector(nullptr),
- m_destroyDetector(nullptr),
- m_getInformation(nullptr) {}
- DetectorLib::~DetectorLib()
- {
- if (m_sohd != INVALID_DLLHD)
- {
- if (m_dispose)
- {
- m_dispose();
- }
- tzc::SysUtils::FreeLibrary(m_sohd);
- m_sohd = INVALID_DLLHD;
- }
- }
- TZ_INT DetectorLib::Initialize(DetectorInfo & jinfo)
- {
- m_sohd = tzc::SysUtils::LoadLibrary(m_path);
- if (m_sohd == INVALID_DLLHD)
- {
- TZLogError("Load DLL %s failed, dlerror: %s!!", m_path.c_str(), dlerror());
- return MEC_FAILED;
- }
- m_initialize = (DET_Initialize)tzc::SysUtils::GetSymbol(m_sohd, INITIALIZE_DETECTOR_SYMBOL);
- if (!m_initialize)
- {
- TZLogError("Get INITIALIZE_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
- return MEC_FAILED;
- }
- m_dispose = (DET_Dispose)tzc::SysUtils::GetSymbol(m_sohd, DISPOSE_DETECTOR_SYMBOL);
- if (!m_dispose)
- {
- TZLogError("Get DISPOSE_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
- return MEC_FAILED;
- }
- m_buildDetector = (DET_BuildDetector)tzc::SysUtils::GetSymbol(m_sohd, BUILD_DETECTOR_SYMBOL);
- if (!m_buildDetector)
- {
- TZLogError("Get BUILD_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
- return MEC_FAILED;
- }
- m_destroyDetector = (DET_DestroyDetector)tzc::SysUtils::GetSymbol(m_sohd, DESTROY_DETECTOR_SYMBOL);
- if (!m_destroyDetector)
- {
- TZLogError("Get DESTROY_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
- return MEC_FAILED;
- }
- m_getInformation = (DET_GetInformation)tzc::SysUtils::GetSymbol(m_sohd, INFO_DETECTOR_SYMBOL);
- if (!m_getInformation)
- {
- TZLogError("Get INFO_DETECTOR_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);
- DetectorInfo::fromJson(sinfo, jinfo);
- m_detKey = jinfo.Key;
- return m_initialize();
- }
- Detector DetectorLib::BuildDetector()
- {
- if (!m_buildDetector)
- {
- TZLogWarn("m_buildDetector is NULL, maybe it has not been initialized!!!");
- return nullptr;
- }
- return m_buildDetector();
- }
- TZ_INT DetectorLib::DestroyDetector(Detector * pdet)
- {
- if (!m_destroyDetector)
- {
- TZLogWarn("m_destroyDetector is NULL, maybe it has not been initialized!!!");
- return MEC_NULL_OBJ;
- }
- return m_destroyDetector(pdet);
- }
- TZ_INT DetectorLib::GetInformation(std::string & info)
- {
- if (!m_getInformation)
- {
- TZLogWarn("m_getInformation is NULL, maybe it has not been initialized!!!");
- return MEC_NULL_OBJ;
- }
- TZ_CHAR cinfo[DET_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 DetectorLib::GetDetLibKey(std::string & detKey)
- {
- detKey = m_detKey;
- }
- NAMESPACE_MAS_END
|