Application.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) quickfixengine.org All rights reserved.
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifdef _MSC_VER
  21. #pragma warning( disable : 4503 4355 4786 )
  22. #endif
  23. #include "Application.h"
  24. #include "quickfix/Session.h"
  25. #include <iostream>
  26. #include "ufx_security_interface.h"
  27. void Application::sendMsg(int id, SessionID *sid, Message *msg, const Message *constmsg) {
  28. //std::cout << SignalSyncIsStart(sync) << std::endl;
  29. if (SignalSyncIsStart(sync) == 0) {
  30. return;
  31. }
  32. callbacks[id].app = this;
  33. callbacks[id].id = id;
  34. callbacks[id].sid = sid;
  35. callbacks[id].msg = msg;
  36. callbacks[id].constmsg = constmsg;
  37. try {
  38. SignalSyncSend(sync, id, &callbacks[id]);
  39. SignalSyncDone(sync, id);
  40. } catch (std::exception &e) {
  41. std::cout << e.what() << std::endl;
  42. }
  43. //std::cout << "msg end: " << id << std::endl;
  44. }
  45. void Application::onLogon( const FIX::SessionID& sessionID )
  46. {
  47. SessionID *sid = WarpSessionID(&sessionID);
  48. sendMsg(1, sid, NULL, NULL);
  49. }
  50. void Application::onLogout( const FIX::SessionID& sessionID )
  51. {
  52. SessionID *sid = WarpSessionID(&sessionID);
  53. sendMsg(2, sid, NULL, NULL);
  54. }
  55. void Application::onCreate( const FIX::SessionID& sessionID) {
  56. //std::cout << "Application::onCreate" << std::endl;
  57. sessionId = WarpSessionID(&sessionID);
  58. }
  59. void Application::toAdmin( FIX::Message& message, const FIX::SessionID& sessionID) {
  60. const FIX::Dictionary& session_settings = settings.get(sessionID);
  61. senderCompID = session_settings.getString("SenderCompID");
  62. targetCompID = session_settings.getString("TargetCompID");
  63. if (FIX::MsgType_Logon == message.getHeader().getField(FIX::FIELD::MsgType)) {
  64. message.setField(FIX::EncryptMethod(0));
  65. message.setField(FIX::HeartBtInt(30));
  66. FIX::Username username;
  67. FIX::Password password;
  68. if (session_settings.has("Username")) {
  69. username = session_settings.getString("Username");
  70. }
  71. if (session_settings.has("Password")) {
  72. password = session_settings.getString("Password");
  73. }
  74. message.setField(FIX::Password(password));
  75. message.setField(FIX::Username(username));
  76. message.setField(FIX::ResetSeqNumFlag(FIX::ResetSeqNumFlag_NO));
  77. }
  78. std::cout << message << std::endl;
  79. SessionID *sid = WarpSessionID(&sessionID);
  80. Message *msg = WarpMessage(&message);
  81. std::cout << MessageToStr(msg) << std::endl;
  82. sendMsg(4, sid, msg, NULL);
  83. }
  84. void Application::fromAdmin( const FIX::Message& message, const FIX::SessionID& sessionID)
  85. throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon ) {
  86. SessionID *sid = WarpSessionID(&sessionID);
  87. const Message *msg = WarpConstMessage(&message);
  88. sendMsg(5, sid, NULL, msg);
  89. }
  90. void Application::fromApp(const FIX::Message& message, const FIX::SessionID& sessionID)
  91. throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType ) {
  92. SessionID *sid = WarpSessionID(&sessionID);
  93. const Message *msg = WarpConstMessage(&message);
  94. sendMsg(6, sid, NULL, msg);
  95. }
  96. void Application::toApp( FIX::Message& message, const FIX::SessionID& sessionID ) throw( FIX::DoNotSend )
  97. {
  98. std::cout <<"toApp:" << message << std::endl;
  99. try
  100. {
  101. FIX::PossDupFlag possDupFlag;
  102. message.getHeader().getField( possDupFlag );
  103. if ( possDupFlag ) throw FIX::DoNotSend();
  104. SessionID *sid = WarpSessionID(&sessionID);
  105. Message *msg = WarpMessage(&message);
  106. sendMsg(7, sid, msg, NULL);
  107. } catch (FIX::FieldNotFound&) {
  108. }
  109. }
  110. static void process(void *data) {
  111. MsgCallBack *cb = (MsgCallBack *)data;
  112. //std::cout << "call: " << cb->id << std::endl;
  113. if (cb->id == 1) {
  114. cb->app->IApp->onLogon(cb->app->Trade, cb->sid);
  115. } else if (cb->id == 2) {
  116. cb->app->IApp->onLogout(cb->app->Trade, cb->sid);
  117. } else if (cb->id == 4) {
  118. if (cb->msg == NULL) {
  119. std::cout << "message error: " << cb->id << std::endl;
  120. return;
  121. }
  122. cb->app->IApp->toAdmin(cb->app->Trade, cb->msg, cb->sid);
  123. } else if (cb->id == 5) {
  124. if (cb->constmsg == NULL) {
  125. std::cout << "message error: " << cb->id << std::endl;
  126. return;
  127. }
  128. cb->app->IApp->fromAdmin(cb->app->Trade, cb->constmsg, cb->sid);
  129. } else if (cb->id == 6) {
  130. if (cb->constmsg == NULL) {
  131. std::cout << "message error: " << cb->id << std::endl;
  132. return;
  133. }
  134. cb->app->IApp->fromApp(cb->app->Trade, cb->constmsg, cb->sid);
  135. } else if (cb->id == 7) {
  136. if (cb->msg == NULL) {
  137. std::cout << "message error: " << cb->id << std::endl;
  138. return;
  139. }
  140. cb->app->IApp->toApp(cb->app->Trade, cb->msg, cb->sid);
  141. }
  142. //std::cout << "call end: " << cb->id << std::endl;
  143. }
  144. int Application::run() {
  145. std::cout << "sync start" << std::endl;
  146. SignalSyncStart(sync);
  147. while (true) {
  148. int ret = SignalSyncProcess(sync, process);
  149. if (ret != 1) {
  150. return ret;
  151. }
  152. }
  153. }