SessionState.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_SESSIONSTATE_H
  21. #define FIX_SESSIONSTATE_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "FieldTypes.h"
  26. #include "MessageStore.h"
  27. #include "Log.h"
  28. #include "Mutex.h"
  29. namespace FIX
  30. {
  31. /// Maintains all of state for the Session class.
  32. class SessionState : public MessageStore, public Log
  33. {
  34. typedef std::map < int, Message > Messages;
  35. public:
  36. SessionState()
  37. : m_enabled( true ), m_receivedLogon( false ),
  38. m_sentLogout( false ), m_sentLogon( false ),
  39. m_sentReset( false ), m_receivedReset( false ),
  40. m_initiate( false ), m_logonTimeout( 10 ),
  41. m_logoutTimeout( 2 ), m_testRequest( 0 ),
  42. m_pStore( 0 ), m_pLog( 0 ) {}
  43. bool enabled() const { return m_enabled; }
  44. void enabled( bool value ) { m_enabled = value; }
  45. bool receivedLogon() const { return m_receivedLogon; }
  46. void receivedLogon( bool value ) { m_receivedLogon = value; }
  47. bool sentLogout() const { return m_sentLogout; }
  48. void sentLogout( bool value ) { m_sentLogout = value; }
  49. bool sentLogon() const { return m_sentLogon; }
  50. void sentLogon( bool value ) { m_sentLogon = value; }
  51. bool receivedReset() const { return m_receivedReset; }
  52. void receivedReset( bool value ) { m_receivedReset = value; }
  53. bool sentReset() const { return m_sentReset; }
  54. void sentReset( bool value ) { m_sentReset = value; }
  55. bool initiate() const { return m_initiate; }
  56. void initiate( bool value ) { m_initiate = value; }
  57. int logonTimeout() const { return m_logonTimeout; }
  58. void logonTimeout( int value ) { m_logonTimeout = value; }
  59. int logoutTimeout() const { return m_logoutTimeout; }
  60. void logoutTimeout( int value ) { m_logoutTimeout = value; }
  61. int testRequest() const { return m_testRequest; }
  62. void testRequest( int value ) { m_testRequest = value; }
  63. bool resendRequested() const
  64. { return !(m_resendRange.first == 0 && m_resendRange.second == 0); }
  65. typedef std::pair<int, int> ResendRange;
  66. ResendRange resendRange () const { return m_resendRange; }
  67. void resendRange (int begin, int end)
  68. { m_resendRange = std::make_pair( begin, end ); }
  69. MessageStore* store() { return m_pStore; }
  70. void store( MessageStore* pValue ) { m_pStore = pValue; }
  71. Log* log() { return m_pLog ? m_pLog : &m_nullLog; }
  72. void log( Log* pValue ) { m_pLog = pValue; }
  73. void heartBtInt( const HeartBtInt& value )
  74. { m_heartBtInt = value; }
  75. HeartBtInt& heartBtInt()
  76. { return m_heartBtInt; }
  77. const HeartBtInt& heartBtInt() const
  78. { return m_heartBtInt; }
  79. void lastSentTime( const UtcTimeStamp& value )
  80. { m_lastSentTime = value; }
  81. UtcTimeStamp& lastSentTime()
  82. { return m_lastSentTime; }
  83. const UtcTimeStamp& lastSentTime() const
  84. { return m_lastSentTime; }
  85. void lastReceivedTime( const UtcTimeStamp& value )
  86. { m_lastReceivedTime = value; }
  87. UtcTimeStamp& lastReceivedTime()
  88. { return m_lastReceivedTime; }
  89. const UtcTimeStamp& lastReceivedTime() const
  90. { return m_lastReceivedTime; }
  91. bool shouldSendLogon() const { return initiate() && !sentLogon(); }
  92. bool alreadySentLogon() const { return initiate() && sentLogon(); }
  93. bool logonTimedOut() const
  94. {
  95. UtcTimeStamp now;
  96. return now - lastReceivedTime() >= logonTimeout();
  97. }
  98. bool logoutTimedOut() const
  99. {
  100. UtcTimeStamp now;
  101. return sentLogout() && ( ( now - lastSentTime() ) >= logoutTimeout() );
  102. }
  103. bool withinHeartBeat() const
  104. {
  105. UtcTimeStamp now;
  106. return ( ( now - lastSentTime() ) < heartBtInt() ) &&
  107. ( ( now - lastReceivedTime() ) < heartBtInt() );
  108. }
  109. bool timedOut() const
  110. {
  111. UtcTimeStamp now;
  112. return ( now - lastReceivedTime() ) >= ( 2.4 * ( double ) heartBtInt() );
  113. }
  114. bool needHeartbeat() const
  115. {
  116. UtcTimeStamp now;
  117. return ( ( now - lastSentTime() ) >= heartBtInt() ) && !testRequest();
  118. }
  119. bool needTestRequest() const
  120. {
  121. UtcTimeStamp now;
  122. return ( now - lastReceivedTime() ) >=
  123. ( ( 1.2 * ( ( double ) testRequest() + 1 ) ) * ( double ) heartBtInt() );
  124. }
  125. std::string logoutReason() const
  126. { Locker l( m_mutex ); return m_logoutReason; }
  127. void logoutReason( const std::string& value )
  128. { Locker l( m_mutex ); m_logoutReason = value; }
  129. void queue( int msgSeqNum, const Message& message )
  130. { Locker l( m_mutex ); m_queue[ msgSeqNum ] = message; }
  131. bool retrieve( int msgSeqNum, Message& message )
  132. {
  133. Locker l( m_mutex );
  134. Messages::iterator i = m_queue.find( msgSeqNum );
  135. if ( i != m_queue.end() )
  136. {
  137. message = i->second;
  138. m_queue.erase( i );
  139. return true;
  140. }
  141. return false;
  142. }
  143. void clearQueue()
  144. { Locker l( m_mutex ); m_queue.clear(); }
  145. bool set( int s, const std::string& m ) throw ( IOException )
  146. { Locker l( m_mutex ); return m_pStore->set( s, m ); }
  147. void get( int b, int e, std::vector < std::string > &m ) const
  148. throw ( IOException )
  149. { Locker l( m_mutex ); m_pStore->get( b, e, m ); }
  150. int getNextSenderMsgSeqNum() const throw ( IOException )
  151. { Locker l( m_mutex ); return m_pStore->getNextSenderMsgSeqNum(); }
  152. int getNextTargetMsgSeqNum() const throw ( IOException )
  153. { Locker l( m_mutex ); return m_pStore->getNextTargetMsgSeqNum(); }
  154. void setNextSenderMsgSeqNum( int n ) throw ( IOException )
  155. { Locker l( m_mutex ); m_pStore->setNextSenderMsgSeqNum( n ); }
  156. void setNextTargetMsgSeqNum( int n ) throw ( IOException )
  157. { Locker l( m_mutex ); m_pStore->setNextTargetMsgSeqNum( n ); }
  158. void incrNextSenderMsgSeqNum() throw ( IOException )
  159. { Locker l( m_mutex ); m_pStore->incrNextSenderMsgSeqNum(); }
  160. void incrNextTargetMsgSeqNum() throw ( IOException )
  161. { Locker l( m_mutex ); m_pStore->incrNextTargetMsgSeqNum(); }
  162. UtcTimeStamp getCreationTime() const throw ( IOException )
  163. { Locker l( m_mutex ); return m_pStore->getCreationTime(); }
  164. void reset() throw ( IOException )
  165. { Locker l( m_mutex ); m_pStore->reset(); }
  166. void refresh() throw ( IOException )
  167. { Locker l( m_mutex ); m_pStore->refresh(); }
  168. void clear()
  169. { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->clear(); }
  170. void backup()
  171. { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->backup(); }
  172. void onIncoming( const std::string& string )
  173. { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onIncoming( string ); }
  174. void onOutgoing( const std::string& string )
  175. { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onOutgoing( string ); }
  176. void onEvent( const std::string& string )
  177. { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onEvent( string ); }
  178. private:
  179. bool m_enabled;
  180. bool m_receivedLogon;
  181. bool m_sentLogout;
  182. bool m_sentLogon;
  183. bool m_sentReset;
  184. bool m_receivedReset;
  185. bool m_initiate;
  186. int m_logonTimeout;
  187. int m_logoutTimeout;
  188. int m_testRequest;
  189. ResendRange m_resendRange;
  190. HeartBtInt m_heartBtInt;
  191. UtcTimeStamp m_lastSentTime;
  192. UtcTimeStamp m_lastReceivedTime;
  193. std::string m_logoutReason;
  194. Messages m_queue;
  195. MessageStore* m_pStore;
  196. Log* m_pLog;
  197. NullLog m_nullLog;
  198. mutable Mutex m_mutex;
  199. };
  200. }
  201. #endif //FIX_SESSIONSTATE_H