PluginInstance.cpp 3.4 KB

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