PluginInstance.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "pch.h"
  2. #include "PluginInstance.h"
  3. CPluginInstance::CPluginInstance()
  4. : m_enable(false)
  5. {
  6. }
  7. CPluginInstance::~CPluginInstance()
  8. {
  9. Stop();
  10. }
  11. void CPluginInstance::Release()
  12. {
  13. delete this;
  14. }
  15. MTAPIRES CPluginInstance::Start(IMTServerAPI * server)
  16. {
  17. MTAPIRES ret = MT_RET_OK;
  18. if (!server) return MT_RET_ERR_PARAMS;
  19. m_api = server;
  20. if ((m_config = m_api->PluginCreate()) == nullptr)
  21. return MT_RET_ERR_MEM;
  22. ret = m_api->About(m_info);
  23. if (ret != MT_RET_OK)
  24. m_api->LoggerOut(MTLogOK, L"Server info failed [%d]", ret);
  25. if ((ret = m_api->PluginSubscribe(this)) != MT_RET_OK)
  26. {
  27. m_api->LoggerOut(MTLogAtt, L"Plugin subscribe failed [%d]", ret);
  28. return ret;
  29. }
  30. if (ret = m_api->OrderSubscribe(this) != MT_RET_OK)
  31. {
  32. m_api->LoggerOut(MTLogAtt, L"Order subscribe failed [%d]", ret);
  33. return ret;
  34. }
  35. if (ret = m_api->DealSubscribe(this) != MT_RET_OK)
  36. {
  37. m_api->LoggerOut(MTLogAtt, L"Deal subscribe failed [%d]", ret);
  38. return ret;
  39. }
  40. if ((ret = LoadParam()) != MT_RET_OK)
  41. {
  42. m_api->LoggerOut(MTLogAtt, L"Load param failed [%d]", ret);
  43. return ret;
  44. }
  45. return MT_RET_OK;
  46. }
  47. MTAPIRES CPluginInstance::Stop()
  48. {
  49. MTAPIRES ret = MT_RET_OK;
  50. if (m_api == nullptr)
  51. return MT_RET_OK;
  52. if (m_config != nullptr)
  53. {
  54. m_config->Release();
  55. m_config = nullptr;
  56. }
  57. if ((ret = m_api->PluginUnsubscribe(this)) != MT_RET_OK && ret != MT_RET_ERR_NOTFOUND)
  58. m_api->LoggerOut(MTLogErr, L"failed to unsubscribe from plugin config updates [%s (%u)]",
  59. SMTFormat::FormatError(ret), ret);
  60. if ((ret = m_api->OrderUnsubscribe(this)) != MT_RET_OK && ret != MT_RET_ERR_NOTFOUND)
  61. m_api->LoggerOut(MTLogErr, L"failed to unsubscribe order [%s (%u)]",
  62. SMTFormat::FormatError(ret), ret);
  63. if ((ret = m_api->DealUnsubscribe(this)) != MT_RET_OK && ret != MT_RET_ERR_NOTFOUND)
  64. m_api->LoggerOut(MTLogErr, L"failed to unsubscribe deal [%s (%u)]",
  65. SMTFormat::FormatError(ret), ret);
  66. m_api = nullptr;
  67. m_enable = false;
  68. return MT_RET_OK;
  69. }
  70. void CPluginInstance::OnPluginUpdate(const IMTConPlugin * plugin)
  71. {
  72. LoadParam();
  73. }
  74. void CPluginInstance::OnDealPerform(const IMTDeal * deal, IMTAccount * account, IMTPosition * position)
  75. {
  76. // positionΪnullptrʱ£¬
  77. if (position == nullptr
  78. || deal == nullptr
  79. || account == nullptr)
  80. return;
  81. std::lock_guard<decltype(m_lock)> lk(m_lock);
  82. if (m_enable) return;
  83. }
  84. MTAPIRES CPluginInstance::LoadParam()
  85. {
  86. MTAPIRES res = MT_RET_OK;
  87. IMTConParam* param = NULL;
  88. CMTStr128 tmp;
  89. if (!m_api || !m_config) return MT_RET_ERR_PARAMS;
  90. if ((res = m_api->PluginCurrent(m_config)) != MT_RET_OK)
  91. {
  92. m_api->LoggerOut(MTLogErr, L"failed to get current plugin configuration [%s (%u)]",
  93. SMTFormat::FormatError(res), res);
  94. return res;
  95. }
  96. if ((param = m_api->PluginParamCreate()) == NULL)
  97. {
  98. m_api->LoggerOut(MTLogErr, L"failed to create plugin parameter object");
  99. return MT_RET_ERR_MEM;
  100. }
  101. ScopeGuard param_release([param]()
  102. {
  103. param->Release();
  104. });
  105. std::lock_guard<decltype(m_lock)> lk(m_lock);
  106. if ((res = m_config->ParameterGet(L"Trader", param)) != MT_RET_OK || param->Type() != IMTConParam::TYPE_INT)
  107. {
  108. return(MT_RET_ERR_PARAMS);
  109. }
  110. m_trader = param->ValueInt();
  111. if ((res = m_config->ParameterGet(L"Step", param)) != MT_RET_OK || param->Type() != IMTConParam::TYPE_INT)
  112. {
  113. return(MT_RET_ERR_PARAMS);
  114. }
  115. m_step = param->ValueInt();
  116. if ((res = m_config->ParameterGet(L"Tolerance", param)) != MT_RET_OK || param->Type() != IMTConParam::TYPE_INT)
  117. {
  118. return(MT_RET_ERR_PARAMS);
  119. }
  120. m_tolerance = param->ValueInt();
  121. if ((res = m_config->ParameterGet(L"Groups", param)) != MT_RET_OK || param->Type() != IMTConParam::TYPE_STRING)
  122. {
  123. return(MT_RET_ERR_PARAMS);
  124. }
  125. m_groups = param->ValueString();
  126. if ((res = m_config->ParameterGet(L"Logins", param)) != MT_RET_OK || param->Type() != IMTConParam::TYPE_STRING)
  127. {
  128. return(MT_RET_ERR_PARAMS);
  129. }
  130. m_logins = param->ValueString();
  131. m_enable = true;
  132. return MT_RET_OK;
  133. }