StrategyLib.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "StrategyLib.h"
  2. #include "SysUtils.h"
  3. #if defined(LINUX)
  4. #include <dlfcn.h>
  5. #endif
  6. #define STRA_LIB_INFO_LEN 1000
  7. NAMESPACE_MAS_BEGIN
  8. StrategyLib::StrategyLib(const std::string & path) :
  9. m_path(path), m_straKey(""),
  10. m_sohd(INVALID_DLLHD),
  11. m_initialize(0), m_dispose(0),
  12. m_buildStrategy(0), m_destroyStrategy(0),
  13. m_getInformation(0)
  14. {
  15. }
  16. StrategyLib::~StrategyLib()
  17. {
  18. if (m_sohd != INVALID_DLLHD)
  19. {
  20. if (m_dispose)
  21. {
  22. m_dispose();
  23. }
  24. tzc::SysUtils::FreeLibrary(m_sohd);
  25. m_sohd = INVALID_DLLHD;
  26. }
  27. }
  28. TZ_INT StrategyLib::Initialize(StrategyInfo & straInfo)
  29. {
  30. m_sohd = tzc::SysUtils::LoadLibrary(m_path);
  31. if (INVALID_DLLHD == m_sohd)
  32. {
  33. TZLogError("load dll %s failed, dlerr = %s!!",
  34. m_path.c_str(), dlerror());
  35. return MEC_FAILED;
  36. }
  37. m_initialize =
  38. (STRA_Initialize)tzc::SysUtils::GetSymbol(m_sohd, INITIALIZE_STRATEGY_SYMBOL);
  39. if (!m_initialize)
  40. {
  41. TZLogError("get INITIALIZE_STRATEGY_SYMBOL failed, invalid dll %s!!",
  42. m_path.c_str());
  43. return MEC_FAILED;
  44. }
  45. m_dispose =
  46. (STRA_Dispose)tzc::SysUtils::GetSymbol(m_sohd, DISPOSE_STRATEGY_SYMBOL);
  47. if (!m_dispose)
  48. {
  49. TZLogError("get DISPOSE_STRATEGY_SYMBOL failed, invalid dll %s!!",
  50. m_path.c_str());
  51. return MEC_FAILED;
  52. }
  53. m_buildStrategy =
  54. (STRA_BuildStrategy)tzc::SysUtils::GetSymbol(m_sohd, BUILD_STRATEGY_SYMBOL);
  55. if (!m_buildStrategy)
  56. {
  57. TZLogError("get BUILD_STRATEGY_SYMBOL failed, invalid dll %s!!",
  58. m_path.c_str());
  59. return MEC_FAILED;
  60. }
  61. m_destroyStrategy =
  62. (STRA_DestroyStrategy)tzc::SysUtils::GetSymbol(m_sohd, DESTROY_STRATEGY_SYMBOL);
  63. if (!m_destroyStrategy)
  64. {
  65. TZLogError("get DESTROY_STRATEGY_SYMBOL failed, invalid dll %s!!",
  66. m_path.c_str());
  67. return MEC_FAILED;
  68. }
  69. m_getInformation =
  70. (STRA_GetInformation)tzc::SysUtils::GetSymbol(m_sohd, INFO_STRATEGY_SYMBOL);
  71. if (!m_getInformation)
  72. {
  73. TZLogError("get INFO_STRATEGY_SYMBOL failed, invalid dll %s!!",
  74. m_path.c_str());
  75. return MEC_FAILED;
  76. }
  77. TZLogInfo("initialize SONode of dll %s succeed~~~", m_path.c_str());
  78. std::string sinfo;
  79. this->GetInformation(sinfo);
  80. StrategyInfo::fromJson(sinfo, straInfo);
  81. m_straKey = straInfo.Key;
  82. return m_initialize();
  83. }
  84. Strategy * StrategyLib::BuildStrategy()
  85. {
  86. if (!m_buildStrategy)
  87. {
  88. TZLogWarn("m_buildDetector is NULL, maybe have not been initialized!!!");
  89. return nullptr;
  90. }
  91. return m_buildStrategy();
  92. }
  93. TZ_INT StrategyLib::DestroyStrategy(Strategy * pstra)
  94. {
  95. if (!m_destroyStrategy)
  96. {
  97. TZLogWarn("m_destroyDetector is NULL, maybe have not been initialized!!!");
  98. return MEC_NULL_OBJ;
  99. }
  100. return m_destroyStrategy(pstra);
  101. }
  102. TZ_INT StrategyLib::GetInformation(std::string & info)
  103. {
  104. if (!m_getInformation)
  105. {
  106. TZLogWarn("m_getInformation is NULL, maybe have not been initialized!!!");
  107. return MEC_NULL_OBJ;
  108. }
  109. TZ_CHAR cinfo[STRA_LIB_INFO_LEN << 10];
  110. TZ_INT irst = m_getInformation(cinfo);
  111. if (irst)
  112. {
  113. TZLogWarn("%s m_getInformation failed!!!", m_path.c_str());
  114. return irst;
  115. }
  116. info = cinfo;
  117. return MEC_OK;
  118. }
  119. void StrategyLib::GetStraLibKey(std::string & straKey)
  120. {
  121. straKey = m_straKey;
  122. }
  123. NAMESPACE_MAS_END