SessionState.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef FIX_SESSIONSTATE_H
23 #define FIX_SESSIONSTATE_H
24 
25 #ifdef _MSC_VER
26 #pragma warning( disable : 4503 4355 4786 4290 )
27 #endif
28 
29 #include "FieldTypes.h"
30 #include "MessageStore.h"
31 #include "Log.h"
32 #include "Mutex.h"
33 
34 namespace FIX
35 {
37 class SessionState : public MessageStore, public Log
38 {
39  typedef std::map < int, Message > Messages;
40 
41 public:
42  SessionState()
43 : m_enabled( true ), m_receivedLogon( false ),
44  m_sentLogout( false ), m_sentLogon( false ),
45  m_sentReset( false ), m_receivedReset( false ),
46  m_initiate( false ), m_logonTimeout( 10 ),
47  m_logoutTimeout( 2 ), m_testRequest( 0 ),
48  m_pStore( 0 ), m_pLog( 0 ) {}
49 
50  bool enabled() const { return m_enabled; }
51  void enabled( bool value ) { m_enabled = value; }
52 
53  bool receivedLogon() const { return m_receivedLogon; }
54  void receivedLogon( bool value ) { m_receivedLogon = value; }
55 
56  bool sentLogout() const { return m_sentLogout; }
57  void sentLogout( bool value ) { m_sentLogout = value; }
58 
59  bool sentLogon() const { return m_sentLogon; }
60  void sentLogon( bool value ) { m_sentLogon = value; }
61 
62  bool receivedReset() const { return m_receivedReset; }
63  void receivedReset( bool value ) { m_receivedReset = value; }
64 
65  bool sentReset() const { return m_sentReset; }
66  void sentReset( bool value ) { m_sentReset = value; }
67 
68  bool initiate() const { return m_initiate; }
69  void initiate( bool value ) { m_initiate = value; }
70 
71  int logonTimeout() const { return m_logonTimeout; }
72  void logonTimeout( int value ) { m_logonTimeout = value; }
73 
74  int logoutTimeout() const { return m_logoutTimeout; }
75  void logoutTimeout( int value ) { m_logoutTimeout = value; }
76 
77  int testRequest() const { return m_testRequest; }
78  void testRequest( int value ) { m_testRequest = value; }
79 
80  bool resendRequested() const
81  { return !(m_resendRange.first == 0 && m_resendRange.second == 0); }
82 
83  typedef std::pair<int, int> ResendRange;
84 
85  ResendRange resendRange () const { return m_resendRange; }
86  void resendRange (int begin, int end)
87  { m_resendRange = std::make_pair( begin, end ); }
88 
89  MessageStore* store() { return m_pStore; }
90  void store( MessageStore* pValue ) { m_pStore = pValue; }
91  Log* log() { return m_pLog ? m_pLog : &m_nullLog; }
92  void log( Log* pValue ) { m_pLog = pValue; }
93 
94  void heartBtInt( const HeartBtInt& value )
95  { m_heartBtInt = value; }
96  HeartBtInt& heartBtInt()
97  { return m_heartBtInt; }
98  const HeartBtInt& heartBtInt() const
99  { return m_heartBtInt; }
100 
101  void lastSentTime( const UtcTimeStamp& value )
102  { m_lastSentTime = value; }
104  { return m_lastSentTime; }
105  const UtcTimeStamp& lastSentTime() const
106  { return m_lastSentTime; }
107 
108  void lastReceivedTime( const UtcTimeStamp& value )
109  { m_lastReceivedTime = value; }
111  { return m_lastReceivedTime; }
113  { return m_lastReceivedTime; }
114 
115  bool shouldSendLogon() const { return initiate() && !sentLogon(); }
116  bool alreadySentLogon() const { return initiate() && sentLogon(); }
117  bool logonTimedOut() const
118  {
120  return now - lastReceivedTime() >= logonTimeout();
121  }
122  bool logoutTimedOut() const
123  {
125  return sentLogout() && ( ( now - lastSentTime() ) >= logoutTimeout() );
126  }
127  bool withinHeartBeat() const
128  {
129  UtcTimeStamp now;
130  return ( ( now - lastSentTime() ) < heartBtInt() ) &&
131  ( ( now - lastReceivedTime() ) < heartBtInt() );
132  }
133  bool timedOut() const
134  {
136  return ( now - lastReceivedTime() ) >= ( 2.4 * ( double ) heartBtInt() );
137  }
138  bool needHeartbeat() const
139  {
140  UtcTimeStamp now;
141  return ( ( now - lastSentTime() ) >= heartBtInt() ) && !testRequest();
142  }
143  bool needTestRequest() const
144  {
145  UtcTimeStamp now;
146  return ( now - lastReceivedTime() ) >=
147  ( ( 1.2 * ( ( double ) testRequest() + 1 ) ) * ( double ) heartBtInt() );
148  }
149 
150  std::string logoutReason() const
151  { Locker l( m_mutex ); return m_logoutReason; }
152  void logoutReason( const std::string& value )
153  { Locker l( m_mutex ); m_logoutReason = value; }
154 
155  void queue( int msgSeqNum, const Message& message )
156  { Locker l( m_mutex ); m_queue[ msgSeqNum ] = message; }
157  bool retrieve( int msgSeqNum, Message& message )
158  {
159  Locker l( m_mutex );
160  Messages::iterator i = m_queue.find( msgSeqNum );
161  if ( i != m_queue.end() )
162  {
163  message = i->second;
164  m_queue.erase( i );
165  return true;
166  }
167  return false;
168  }
169  void clearQueue()
170  { Locker l( m_mutex ); m_queue.clear(); }
171 
172  bool set( int s, const std::string& m ) throw ( IOException )
173  { Locker l( m_mutex ); return m_pStore->set( s, m ); }
174  void get( int b, int e, std::vector < std::string > &m ) const
175  throw ( IOException )
176  { Locker l( m_mutex ); m_pStore->get( b, e, m ); }
177  int getNextSenderMsgSeqNum() const throw ( IOException )
178  { Locker l( m_mutex ); return m_pStore->getNextSenderMsgSeqNum(); }
179  int getNextTargetMsgSeqNum() const throw ( IOException )
180  { Locker l( m_mutex ); return m_pStore->getNextTargetMsgSeqNum(); }
181  void setNextSenderMsgSeqNum( int n ) throw ( IOException )
182  { Locker l( m_mutex ); m_pStore->setNextSenderMsgSeqNum( n ); }
183  void setNextTargetMsgSeqNum( int n ) throw ( IOException )
185  void incrNextSenderMsgSeqNum() throw ( IOException )
187  void incrNextTargetMsgSeqNum() throw ( IOException )
190  { Locker l( m_mutex ); return m_pStore->getCreationTime(); }
191  void reset() throw ( IOException )
192  { Locker l( m_mutex ); m_pStore->reset(); }
193  void refresh() throw ( IOException )
194  { Locker l( m_mutex ); m_pStore->refresh(); }
195 
196  void clear()
197  { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->clear(); }
198  void backup()
199  { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->backup(); }
200  void onIncoming( const std::string& string )
201  { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onIncoming( string ); }
202  void onOutgoing( const std::string& string )
203  { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onOutgoing( string ); }
204  void onEvent( const std::string& string )
205  { if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onEvent( string ); }
206 
207 private:
208  bool m_enabled;
209  bool m_receivedLogon;
210  bool m_sentLogout;
212  bool m_sentReset;
214  bool m_initiate;
216  int m_logoutTimeout;
219  HeartBtInt m_heartBtInt;
222  std::string m_logoutReason;
227  mutable Mutex m_mutex;
228 };
229 }
230 
231 #endif //FIX_SESSIONSTATE_H
FIX::SessionState::reset
void reset()
Definition: SessionState.h:225
FIX::SessionState::alreadySentLogon
bool alreadySentLogon() const
Definition: SessionState.h:150
FIX::SessionState::setNextTargetMsgSeqNum
void setNextTargetMsgSeqNum(int n)
Definition: SessionState.h:217
FIX::SessionState::withinHeartBeat
bool withinHeartBeat() const
Definition: SessionState.h:161
FIX::SessionState::m_queue
Messages m_queue
Definition: SessionState.h:257
FIX::SessionState::logonTimeout
int logonTimeout() const
Definition: SessionState.h:105
FIX::SessionState::getNextTargetMsgSeqNum
int getNextTargetMsgSeqNum() const
Definition: SessionState.h:213
FIX::SessionState::testRequest
int testRequest() const
Definition: SessionState.h:111
FIX::SessionState::timedOut
bool timedOut() const
Definition: SessionState.h:167
FIX::SessionState::queue
void queue(int msgSeqNum, const Message &message)
Definition: SessionState.h:189
FIX::UtcTimeStamp
Date and Time represented in UTC.
Definition: FieldTypes.h:599
FIX::SessionState::m_lastSentTime
UtcTimeStamp m_lastSentTime
Definition: SessionState.h:254
FIX::SessionState::m_logoutReason
std::string m_logoutReason
Definition: SessionState.h:256
FIX::SessionState::needTestRequest
bool needTestRequest() const
Definition: SessionState.h:177
FIX::Mutex
Portable implementation of a mutex.
Definition: Mutex.h:47
FIX::SessionState::receivedReset
bool receivedReset() const
Definition: SessionState.h:96
FIX::SessionState::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: SessionState.h:211
FIX::SessionState::backup
void backup()
Definition: SessionState.h:232
FIX::SessionState::Messages
std::map< int, Message > Messages
Definition: SessionState.h:73
FIX::SessionState::clearQueue
void clearQueue()
Definition: SessionState.h:203
FIX::SessionState::ResendRange
std::pair< int, int > ResendRange
Definition: SessionState.h:117
FIX::SessionState::onOutgoing
void onOutgoing(const std::string &string)
Definition: SessionState.h:236
FIX::SessionState::m_logoutTimeout
int m_logoutTimeout
Definition: SessionState.h:250
FIX::MessageStore::getCreationTime
virtual UtcTimeStamp getCreationTime() const =0
FIX::SessionState::log
Log * log()
Definition: SessionState.h:125
FIX::SessionState::SessionState
SessionState()
Definition: SessionState.h:76
FIX::SessionState::lastReceivedTime
const UtcTimeStamp & lastReceivedTime() const
Definition: SessionState.h:146
FIX::SessionState::sentLogout
bool sentLogout() const
Definition: SessionState.h:90
FIX::MessageStore::incrNextSenderMsgSeqNum
virtual void incrNextSenderMsgSeqNum()=0
FIX::SessionState::m_receivedLogon
bool m_receivedLogon
Definition: SessionState.h:243
FIX::SessionState::retrieve
bool retrieve(int msgSeqNum, Message &message)
Definition: SessionState.h:191
FIX::IOException
IO Error.
Definition: Exceptions.h:255
FIX::MessageStore::incrNextTargetMsgSeqNum
virtual void incrNextTargetMsgSeqNum()=0
FIX::SessionState::get
void get(int b, int e, std::vector< std::string > &m) const
Definition: SessionState.h:208
FIX::SessionState::m_nullLog
NullLog m_nullLog
Definition: SessionState.h:260
FIX::SessionState::m_pLog
Log * m_pLog
Definition: SessionState.h:259
FIX::Log::backup
virtual void backup()=0
FIX::SessionState::enabled
bool enabled() const
Definition: SessionState.h:84
FIX::SessionState::initiate
bool initiate() const
Definition: SessionState.h:102
Mutex.h
FIX::SessionState::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int n)
Definition: SessionState.h:215
FIX::MessageStore::reset
virtual void reset()=0
FIX::MessageStore::setNextTargetMsgSeqNum
virtual void setNextTargetMsgSeqNum(int)=0
FIX::Log::onEvent
virtual void onEvent(const std::string &)=0
FIX::MessageStore::refresh
virtual void refresh()=0
FIX::MessageStore
This interface must be implemented to store and retrieve messages and sequence numbers.
Definition: MessageStore.h:83
FIX::SessionState::m_lastReceivedTime
UtcTimeStamp m_lastReceivedTime
Definition: SessionState.h:255
FIX::SessionState::m_heartBtInt
HeartBtInt m_heartBtInt
Definition: SessionState.h:253
FIX::SessionState::m_initiate
bool m_initiate
Definition: SessionState.h:248
FIX::SessionState::clear
void clear()
Definition: SessionState.h:230
FIX::SessionState::m_sentLogon
bool m_sentLogon
Definition: SessionState.h:245
FIX::SessionState::getCreationTime
UtcTimeStamp getCreationTime() const
Definition: SessionState.h:223
FIX::SessionState::sentLogon
bool sentLogon() const
Definition: SessionState.h:93
FIX::SessionState::onIncoming
void onIncoming(const std::string &string)
Definition: SessionState.h:234
FIX::SessionState::m_resendRange
ResendRange m_resendRange
Definition: SessionState.h:252
FIX::MessageStore::set
virtual bool set(int, const std::string &)=0
FIX::SessionState::logoutTimeout
int logoutTimeout() const
Definition: SessionState.h:108
FIX::SessionState::logoutReason
std::string logoutReason() const
Definition: SessionState.h:184
FIX::SessionState::lastReceivedTime
UtcTimeStamp & lastReceivedTime()
Definition: SessionState.h:144
FIX::SessionState::receivedLogon
bool receivedLogon() const
Definition: SessionState.h:87
FIX::Message
Base class for all FIX messages.
Definition: Message.h:134
FIX::SessionState::shouldSendLogon
bool shouldSendLogon() const
Definition: SessionState.h:149
FIX::SessionState::resendRequested
bool resendRequested() const
Definition: SessionState.h:114
FIX
Definition: Acceptor.cpp:34
FIX::SessionState::store
MessageStore * store()
Definition: SessionState.h:123
FIX::SessionState::m_testRequest
int m_testRequest
Definition: SessionState.h:251
FIX::SessionState::logoutTimedOut
bool logoutTimedOut() const
Definition: SessionState.h:156
FIX::MessageStore::get
virtual void get(int, int, std::vector< std::string > &) const =0
FIX::Log::clear
virtual void clear()=0
FIX::SessionState::m_enabled
bool m_enabled
Definition: SessionState.h:242
FIX::SessionState::sentReset
bool sentReset() const
Definition: SessionState.h:99
FIX::MessageStore::getNextSenderMsgSeqNum
virtual int getNextSenderMsgSeqNum() const =0
FIX::NullLog
Null implementation of Log.
Definition: Log.h:117
FIX::SessionState::m_pStore
MessageStore * m_pStore
Definition: SessionState.h:258
FIX::SessionState::needHeartbeat
bool needHeartbeat() const
Definition: SessionState.h:172
FIX::SessionState::m_sentReset
bool m_sentReset
Definition: SessionState.h:246
FIX::Locker
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:112
FieldTypes.h
FIX::SessionState::onEvent
void onEvent(const std::string &string)
Definition: SessionState.h:238
FIX::SessionState::refresh
void refresh()
Definition: SessionState.h:227
FIX::SessionState::heartBtInt
HeartBtInt & heartBtInt()
Definition: SessionState.h:130
FIX::SessionState::lastSentTime
UtcTimeStamp & lastSentTime()
Definition: SessionState.h:137
FIX::SessionState::m_receivedReset
bool m_receivedReset
Definition: SessionState.h:247
FIX::Log::onIncoming
virtual void onIncoming(const std::string &)=0
FIX::SessionState::set
bool set(int s, const std::string &m)
Definition: SessionState.h:206
FIX::Log::onOutgoing
virtual void onOutgoing(const std::string &)=0
FIX::SessionState::incrNextSenderMsgSeqNum
void incrNextSenderMsgSeqNum()
Definition: SessionState.h:219
FIX::SessionState::incrNextTargetMsgSeqNum
void incrNextTargetMsgSeqNum()
Definition: SessionState.h:221
FIX::Log
This interface must be implemented to log messages and events.
Definition: Log.h:98
FIX::SessionState::resendRange
ResendRange resendRange() const
Definition: SessionState.h:119
FIX::SessionState::logonTimedOut
bool logonTimedOut() const
Definition: SessionState.h:151
FIX::SessionState::m_logonTimeout
int m_logonTimeout
Definition: SessionState.h:249
Log.h
FIX::MessageStore::getNextTargetMsgSeqNum
virtual int getNextTargetMsgSeqNum() const =0
FIX::SessionState::m_mutex
Mutex m_mutex
Definition: SessionState.h:261
MessageStore.h
FIX::SessionState::m_sentLogout
bool m_sentLogout
Definition: SessionState.h:244
FIX::MessageStore::setNextSenderMsgSeqNum
virtual void setNextSenderMsgSeqNum(int)=0

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001