SessionState.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FIX_SESSIONSTATE_H
00023 #define FIX_SESSIONSTATE_H
00024
00025 #ifdef _MSC_VER
00026 #pragma warning( disable : 4503 4355 4786 4290 )
00027 #endif
00028
00029 #include "FieldTypes.h"
00030 #include "MessageStore.h"
00031 #include "Log.h"
00032 #include "Mutex.h"
00033
00034 namespace FIX
00035 {
00037 class SessionState : public MessageStore, public Log
00038 {
00039 typedef std::map < int, Message > Messages;
00040
00041 public:
00042 SessionState()
00043 : m_enabled( true ), m_receivedLogon( false ),
00044 m_sentLogout( false ), m_sentLogon( false ),
00045 m_sentReset( false ), m_receivedReset( false ),
00046 m_initiate( false ), m_logonTimeout( 10 ),
00047 m_logoutTimeout( 2 ), m_testRequest( 0 ),
00048 m_pStore( 0 ), m_pLog( 0 ) {}
00049
00050 bool enabled() const { return m_enabled; }
00051 void enabled( bool value ) { m_enabled = value; }
00052
00053 bool receivedLogon() const { return m_receivedLogon; }
00054 void receivedLogon( bool value ) { m_receivedLogon = value; }
00055
00056 bool sentLogout() const { return m_sentLogout; }
00057 void sentLogout( bool value ) { m_sentLogout = value; }
00058
00059 bool sentLogon() const { return m_sentLogon; }
00060 void sentLogon( bool value ) { m_sentLogon = value; }
00061
00062 bool receivedReset() const { return m_receivedReset; }
00063 void receivedReset( bool value ) { m_receivedReset = value; }
00064
00065 bool sentReset() const { return m_sentReset; }
00066 void sentReset( bool value ) { m_sentReset = value; }
00067
00068 bool initiate() const { return m_initiate; }
00069 void initiate( bool value ) { m_initiate = value; }
00070
00071 int logonTimeout() const { return m_logonTimeout; }
00072 void logonTimeout( int value ) { m_logonTimeout = value; }
00073
00074 int logoutTimeout() const { return m_logoutTimeout; }
00075 void logoutTimeout( int value ) { m_logoutTimeout = value; }
00076
00077 int testRequest() const { return m_testRequest; }
00078 void testRequest( int value ) { m_testRequest = value; }
00079
00080 bool resendRequested() const
00081 { return !(m_resendRange.first == 0 && m_resendRange.second == 0); }
00082
00083 typedef std::pair<int, int> ResendRange;
00084
00085 ResendRange resendRange () const { return m_resendRange; }
00086 void resendRange (int begin, int end)
00087 { m_resendRange = std::make_pair( begin, end ); }
00088
00089 MessageStore* store() { return m_pStore; }
00090 void store( MessageStore* pValue ) { m_pStore = pValue; }
00091 Log* log() { return m_pLog; }
00092 void log( Log* pValue ) { m_pLog = pValue; }
00093
00094 void heartBtInt( const HeartBtInt& value )
00095 { m_heartBtInt = value; }
00096 HeartBtInt& heartBtInt()
00097 { return m_heartBtInt; }
00098 const HeartBtInt& heartBtInt() const
00099 { return m_heartBtInt; }
00100
00101 void lastSentTime( const UtcTimeStamp& value )
00102 { m_lastSentTime = value; }
00103 UtcTimeStamp& lastSentTime()
00104 { return m_lastSentTime; }
00105 const UtcTimeStamp& lastSentTime() const
00106 { return m_lastSentTime; }
00107
00108 void lastReceivedTime( const UtcTimeStamp& value )
00109 { m_lastReceivedTime = value; }
00110 UtcTimeStamp& lastReceivedTime()
00111 { return m_lastReceivedTime; }
00112 const UtcTimeStamp& lastReceivedTime() const
00113 { return m_lastReceivedTime; }
00114
00115 bool shouldSendLogon() const { return initiate() && !sentLogon(); }
00116 bool alreadySentLogon() const { return initiate() && sentLogon(); }
00117 bool logonTimedOut() const
00118 {
00119 UtcTimeStamp now;
00120 return now - lastReceivedTime() >= logonTimeout();
00121 }
00122 bool logoutTimedOut() const
00123 {
00124 UtcTimeStamp now;
00125 return sentLogout() && ( ( now - lastSentTime() ) >= logoutTimeout() );
00126 }
00127 bool withinHeartBeat() const
00128 {
00129 UtcTimeStamp now;
00130 return ( ( now - lastSentTime() ) < heartBtInt() ) &&
00131 ( ( now - lastReceivedTime() ) < heartBtInt() );
00132 }
00133 bool timedOut() const
00134 {
00135 UtcTimeStamp now;
00136 return ( now - lastReceivedTime() ) >= ( 2.4 * ( double ) heartBtInt() );
00137 }
00138 bool needHeartbeat() const
00139 {
00140 UtcTimeStamp now;
00141 return ( ( now - lastSentTime() ) >= heartBtInt() ) && !testRequest();
00142 }
00143 bool needTestRequest() const
00144 {
00145 UtcTimeStamp now;
00146 return ( now - lastReceivedTime() ) >=
00147 ( ( 1.2 * ( ( double ) testRequest() + 1 ) ) * ( double ) heartBtInt() );
00148 }
00149
00150 std::string logoutReason() const
00151 { Locker l( m_mutex ); return m_logoutReason; }
00152 void logoutReason( const std::string& value )
00153 { Locker l( m_mutex ); m_logoutReason = value; }
00154
00155 void queue( int msgSeqNum, const Message& message )
00156 { Locker l( m_mutex ); m_queue[ msgSeqNum ] = message; }
00157 bool retrieve( int msgSeqNum, Message& message )
00158 {
00159 Locker l( m_mutex );
00160 Messages::iterator i = m_queue.find( msgSeqNum );
00161 if ( i != m_queue.end() )
00162 {
00163 message = i->second;
00164 m_queue.erase( i );
00165 return true;
00166 }
00167 return false;
00168 }
00169 void clearQueue()
00170 { Locker l( m_mutex ); m_queue.clear(); }
00171
00172 bool set( int s, const std::string& m ) throw ( IOException )
00173 { Locker l( m_mutex ); return m_pStore->set( s, m ); }
00174 void get( int b, int e, std::vector < std::string > &m ) const
00175 throw ( IOException )
00176 { Locker l( m_mutex ); m_pStore->get( b, e, m ); }
00177 int getNextSenderMsgSeqNum() const throw ( IOException )
00178 { Locker l( m_mutex ); return m_pStore->getNextSenderMsgSeqNum(); }
00179 int getNextTargetMsgSeqNum() const throw ( IOException )
00180 { Locker l( m_mutex ); return m_pStore->getNextTargetMsgSeqNum(); }
00181 void setNextSenderMsgSeqNum( int n ) throw ( IOException )
00182 { Locker l( m_mutex ); m_pStore->setNextSenderMsgSeqNum( n ); }
00183 void setNextTargetMsgSeqNum( int n ) throw ( IOException )
00184 { Locker l( m_mutex ); m_pStore->setNextTargetMsgSeqNum( n ); }
00185 void incrNextSenderMsgSeqNum() throw ( IOException )
00186 { Locker l( m_mutex ); m_pStore->incrNextSenderMsgSeqNum(); }
00187 void incrNextTargetMsgSeqNum() throw ( IOException )
00188 { Locker l( m_mutex ); m_pStore->incrNextTargetMsgSeqNum(); }
00189 UtcTimeStamp getCreationTime() const throw ( IOException )
00190 { Locker l( m_mutex ); return m_pStore->getCreationTime(); }
00191 void reset() throw ( IOException )
00192 { Locker l( m_mutex ); m_pStore->reset(); }
00193 void refresh() throw ( IOException )
00194 { Locker l( m_mutex ); m_pStore->refresh(); }
00195
00196 void clear()
00197 { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->clear(); }
00198 void backup()
00199 { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->backup(); }
00200 void onIncoming( const std::string& string )
00201 { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onIncoming( string ); }
00202 void onOutgoing( const std::string& string )
00203 { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onOutgoing( string ); }
00204 void onEvent( const std::string& string )
00205 { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onEvent( string ); }
00206
00207 private:
00208 bool m_enabled;
00209 bool m_receivedLogon;
00210 bool m_sentLogout;
00211 bool m_sentLogon;
00212 bool m_sentReset;
00213 bool m_receivedReset;
00214 bool m_initiate;
00215 int m_logonTimeout;
00216 int m_logoutTimeout;
00217 int m_testRequest;
00218 ResendRange m_resendRange;
00219 HeartBtInt m_heartBtInt;
00220 UtcTimeStamp m_lastSentTime;
00221 UtcTimeStamp m_lastReceivedTime;
00222 std::string m_logoutReason;
00223 Messages m_queue;
00224 MessageStore* m_pStore;
00225 Log* m_pLog;
00226 mutable Mutex m_mutex;
00227 };
00228 }
00229
00230 #endif //FIX_SESSIONSTATE_H