Application.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  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. #ifndef FIX_APPLICATION_H
  21. #define FIX_APPLICATION_H
  22. #include "Message.h"
  23. #include "SessionID.h"
  24. #include "Mutex.h"
  25. namespace FIX
  26. {
  27. /**
  28. * This interface must be implemented to define what your %FIX application
  29. * does.
  30. *
  31. * These methods notify your application about events that happen on
  32. * active %FIX sessions. There is no guarantee how many threads will be calling
  33. * these functions. If the application is sharing resources among multiple sessions,
  34. * you must synchronize those resources. You can also use the SynchronizedApplication
  35. * class to automatically synchronize all function calls into your application.
  36. * The various MessageCracker classes can be used to parse the generic message
  37. * structure into specific %FIX messages.
  38. */
  39. class Application
  40. {
  41. public:
  42. virtual ~Application() {};
  43. /// Notification of a session begin created
  44. virtual void onCreate( const SessionID& ) = 0;
  45. /// Notification of a session successfully logging on
  46. virtual void onLogon( const SessionID& ) = 0;
  47. /// Notification of a session logging off or disconnecting
  48. virtual void onLogout( const SessionID& ) = 0;
  49. /// Notification of admin message being sent to target
  50. virtual void toAdmin( Message&, const SessionID& ) = 0;
  51. /// Notification of app message being sent to target
  52. virtual void toApp( Message&, const SessionID& )
  53. throw( DoNotSend ) = 0;
  54. /// Notification of admin message being received from target
  55. virtual void fromAdmin( const Message&, const SessionID& )
  56. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon ) = 0;
  57. /// Notification of app message being received from target
  58. virtual void fromApp( const Message&, const SessionID& )
  59. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType ) = 0;
  60. };
  61. /**
  62. * This is a special implementation of the Application interface that takes
  63. * in another Application interface and synchronizes all of its callbacks. This
  64. * will guarantee that only one thread will access the applications code at a time.
  65. *
  66. * This class is a great convenience for writing applications where you
  67. * don't want to worry about synchronization. There is of course a tradeoff
  68. * in that you may be synchronizing more than you need to. There is also a very
  69. * minor performance penalty due to the extra virtual table lookup.
  70. */
  71. class SynchronizedApplication : public Application
  72. {
  73. public:
  74. SynchronizedApplication( Application& app ) : m_app( app ) {}
  75. void onCreate( const SessionID& sessionID )
  76. { Locker l( m_mutex ); app().onCreate( sessionID ); }
  77. void onLogon( const SessionID& sessionID )
  78. { Locker l( m_mutex ); app().onLogon( sessionID ); }
  79. void onLogout( const SessionID& sessionID )
  80. { Locker l( m_mutex ); app().onLogout( sessionID ); }
  81. void toAdmin( Message& message, const SessionID& sessionID )
  82. { Locker l( m_mutex ); app().toAdmin( message, sessionID ); }
  83. void toApp( Message& message, const SessionID& sessionID )
  84. throw( DoNotSend )
  85. { Locker l( m_mutex ); app().toApp( message, sessionID ); }
  86. void fromAdmin( const Message& message, const SessionID& sessionID )
  87. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon )
  88. { Locker l( m_mutex ); app().fromAdmin( message, sessionID ); }
  89. void fromApp( const Message& message, const SessionID& sessionID )
  90. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType )
  91. { Locker l( m_mutex ); app().fromApp( message, sessionID ); }
  92. Mutex m_mutex;
  93. Application& app() { return m_app; }
  94. Application& m_app;
  95. };
  96. /**
  97. * An empty implementation of an Application. This can be used if you
  98. * do not want to provide an implementation for all the callback methods.
  99. * It is also useful for unit tests writing unit tests where the callback
  100. * values of some or all methods are not of interest.
  101. */
  102. class NullApplication : public Application
  103. {
  104. void onCreate( const SessionID& ) {}
  105. void onLogon( const SessionID& ) {}
  106. void onLogout( const SessionID& ) {}
  107. void toAdmin( Message&, const SessionID& ) {}
  108. void toApp( Message&, const SessionID& )
  109. throw( DoNotSend ) {}
  110. void fromAdmin( const Message&, const SessionID& )
  111. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon ) {}
  112. void fromApp( const Message&, const SessionID& )
  113. throw( FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType ) {}
  114. };
  115. /*! @} */
  116. }
  117. #endif //FIX_APPLICATION_H