DetectorLib.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "DetectorLib.h"
  2. #include "SysUtils.h"
  3. #if defined(LINUX)
  4. #include <dlfcn.h>
  5. #endif
  6. #define DET_LIB_INFO_LEN 1000
  7. NAMESPACE_MAS_BEGIN
  8. DetectorLib::DetectorLib(const std::string & path)
  9. : m_path(path),
  10. m_detKey(""),
  11. m_sohd(INVALID_DLLHD),
  12. m_initialize(nullptr),
  13. m_dispose(nullptr),
  14. m_buildDetector(nullptr),
  15. m_destroyDetector(nullptr),
  16. m_getInformation(nullptr) {}
  17. DetectorLib::~DetectorLib()
  18. {
  19. if (m_sohd != INVALID_DLLHD)
  20. {
  21. if (m_dispose)
  22. {
  23. m_dispose();
  24. }
  25. tzc::SysUtils::FreeLibrary(m_sohd);
  26. m_sohd = INVALID_DLLHD;
  27. }
  28. }
  29. TZ_INT DetectorLib::Initialize(DetectorInfo & jinfo)
  30. {
  31. m_sohd = tzc::SysUtils::LoadLibrary(m_path);
  32. if (m_sohd == INVALID_DLLHD)
  33. {
  34. TZLogError("Load DLL %s failed, dlerror: %s!!", m_path.c_str(), dlerror());
  35. return MEC_FAILED;
  36. }
  37. m_initialize = (DET_Initialize)tzc::SysUtils::GetSymbol(m_sohd, INITIALIZE_DETECTOR_SYMBOL);
  38. if (!m_initialize)
  39. {
  40. TZLogError("Get INITIALIZE_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
  41. return MEC_FAILED;
  42. }
  43. m_dispose = (DET_Dispose)tzc::SysUtils::GetSymbol(m_sohd, DISPOSE_DETECTOR_SYMBOL);
  44. if (!m_dispose)
  45. {
  46. TZLogError("Get DISPOSE_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
  47. return MEC_FAILED;
  48. }
  49. m_buildDetector = (DET_BuildDetector)tzc::SysUtils::GetSymbol(m_sohd, BUILD_DETECTOR_SYMBOL);
  50. if (!m_buildDetector)
  51. {
  52. TZLogError("Get BUILD_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
  53. return MEC_FAILED;
  54. }
  55. m_destroyDetector = (DET_DestroyDetector)tzc::SysUtils::GetSymbol(m_sohd, DESTROY_DETECTOR_SYMBOL);
  56. if (!m_destroyDetector)
  57. {
  58. TZLogError("Get DESTROY_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
  59. return MEC_FAILED;
  60. }
  61. m_getInformation = (DET_GetInformation)tzc::SysUtils::GetSymbol(m_sohd, INFO_DETECTOR_SYMBOL);
  62. if (!m_getInformation)
  63. {
  64. TZLogError("Get INFO_DETECTOR_SYMBOL failed, invalid DLL %s!!", m_path.c_str());
  65. return MEC_FAILED;
  66. }
  67. TZLogInfo("Initialize SONode of DLL %s succeed~~~", m_path.c_str());
  68. std::string sinfo;
  69. this->GetInformation(sinfo);
  70. DetectorInfo::fromJson(sinfo, jinfo);
  71. m_detKey = jinfo.Key;
  72. return m_initialize();
  73. }
  74. Detector DetectorLib::BuildDetector()
  75. {
  76. if (!m_buildDetector)
  77. {
  78. TZLogWarn("m_buildDetector is NULL, maybe it has not been initialized!!!");
  79. return nullptr;
  80. }
  81. return m_buildDetector();
  82. }
  83. TZ_INT DetectorLib::DestroyDetector(Detector * pdet)
  84. {
  85. if (!m_destroyDetector)
  86. {
  87. TZLogWarn("m_destroyDetector is NULL, maybe it has not been initialized!!!");
  88. return MEC_NULL_OBJ;
  89. }
  90. return m_destroyDetector(pdet);
  91. }
  92. TZ_INT DetectorLib::GetInformation(std::string & info)
  93. {
  94. if (!m_getInformation)
  95. {
  96. TZLogWarn("m_getInformation is NULL, maybe it has not been initialized!!!");
  97. return MEC_NULL_OBJ;
  98. }
  99. TZ_CHAR cinfo[DET_LIB_INFO_LEN + 10];
  100. TZ_INT irst = m_getInformation(cinfo);
  101. if (irst)
  102. {
  103. TZLogWarn("%s m_getInformation failed!!", m_path.c_str());
  104. return irst;
  105. }
  106. info = cinfo;
  107. return MEC_OK;
  108. }
  109. void DetectorLib::GetDetLibKey(std::string & detKey)
  110. {
  111. detKey = m_detKey;
  112. }
  113. NAMESPACE_MAS_END