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:
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; }
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  {
119  UtcTimeStamp now;
120  return now - lastReceivedTime() >= logonTimeout();
121  }
122  bool logoutTimedOut() const
123  {
124  UtcTimeStamp now;
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  {
135  UtcTimeStamp now;
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 )
183  void setNextTargetMsgSeqNum( int n ) 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;
222  std::string m_logoutReason;
227  mutable Mutex m_mutex;
228 };
229 }
230 
231 #endif //FIX_SESSIONSTATE_H
void sentLogout(bool value)
Definition: SessionState.h:57
int getNextTargetMsgSeqNum() const
Definition: SessionState.h:179
virtual void setNextSenderMsgSeqNum(int)=0
void lastReceivedTime(const UtcTimeStamp &value)
Definition: SessionState.h:108
bool enabled() const
Definition: SessionState.h:50
const UtcTimeStamp & lastReceivedTime() const
Definition: SessionState.h:112
void onOutgoing(const std::string &string)
Definition: SessionState.h:202
UtcTimeStamp getCreationTime() const
Definition: SessionState.h:189
const int HeartBtInt
void logoutReason(const std::string &value)
Definition: SessionState.h:152
int logonTimeout() const
Definition: SessionState.h:71
std::pair< int, int > ResendRange
Definition: SessionState.h:83
int getNextSenderMsgSeqNum() const
Definition: SessionState.h:177
virtual bool set(int, const std::string &)=0
void incrNextTargetMsgSeqNum()
Definition: SessionState.h:187
bool sentLogout() const
Definition: SessionState.h:56
virtual void onOutgoing(const std::string &)=0
bool set(int s, const std::string &m)
Definition: SessionState.h:172
void setNextTargetMsgSeqNum(int n)
Definition: SessionState.h:183
bool withinHeartBeat() const
Definition: SessionState.h:127
bool retrieve(int msgSeqNum, Message &message)
Definition: SessionState.h:157
UtcTimeStamp m_lastSentTime
Definition: SessionState.h:220
HeartBtInt m_heartBtInt
Definition: SessionState.h:219
bool receivedLogon() const
Definition: SessionState.h:53
bool sentLogon() const
Definition: SessionState.h:59
void sentReset(bool value)
Definition: SessionState.h:66
bool sentReset() const
Definition: SessionState.h:65
void initiate(bool value)
Definition: SessionState.h:69
bool receivedReset() const
Definition: SessionState.h:62
void queue(int msgSeqNum, const Message &message)
Definition: SessionState.h:155
virtual void backup()=0
Null implementation of Log.
Definition: Log.h:100
void receivedReset(bool value)
Definition: SessionState.h:63
void lastSentTime(const UtcTimeStamp &value)
Definition: SessionState.h:101
int logoutTimeout() const
Definition: SessionState.h:74
void sentLogon(bool value)
Definition: SessionState.h:60
virtual int getNextTargetMsgSeqNum() const =0
void resendRange(int begin, int end)
Definition: SessionState.h:86
This interface must be implemented to log messages and events.
Definition: Log.h:81
std::map< int, Message > Messages
Definition: SessionState.h:39
virtual UtcTimeStamp getCreationTime() const =0
void log(Log *pValue)
Definition: SessionState.h:92
MessageStore * m_pStore
Definition: SessionState.h:224
virtual void onEvent(const std::string &)=0
virtual void incrNextTargetMsgSeqNum()=0
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:95
bool initiate() const
Definition: SessionState.h:68
std::string m_logoutReason
Definition: SessionState.h:222
virtual void clear()=0
void enabled(bool value)
Definition: SessionState.h:51
void logoutTimeout(int value)
Definition: SessionState.h:75
bool logoutTimedOut() const
Definition: SessionState.h:122
Base class for all FIX messages.
Definition: Message.h:67
This interface must be implemented to store and retrieve messages and sequence numbers.
Definition: MessageStore.h:66
bool resendRequested() const
Definition: SessionState.h:80
void incrNextSenderMsgSeqNum()
Definition: SessionState.h:185
Portable implementation of a mutex.
Definition: Mutex.h:30
const HeartBtInt & heartBtInt() const
Definition: SessionState.h:98
void receivedLogon(bool value)
Definition: SessionState.h:54
void onIncoming(const std::string &string)
Definition: SessionState.h:200
void onEvent(const std::string &string)
Definition: SessionState.h:204
bool alreadySentLogon() const
Definition: SessionState.h:116
virtual void onIncoming(const std::string &)=0
Date and Time represented in UTC.
Definition: FieldTypes.h:399
Maintains all of state for the Session class.
Definition: SessionState.h:37
HeartBtInt & heartBtInt()
Definition: SessionState.h:96
void logonTimeout(int value)
Definition: SessionState.h:72
UtcTimeStamp & lastReceivedTime()
Definition: SessionState.h:110
UtcTimeStamp m_lastReceivedTime
Definition: SessionState.h:221
virtual void get(int, int, std::vector< std::string > &) const =0
void store(MessageStore *pValue)
Definition: SessionState.h:90
virtual int getNextSenderMsgSeqNum() const =0
bool logonTimedOut() const
Definition: SessionState.h:117
int testRequest() const
Definition: SessionState.h:77
virtual void incrNextSenderMsgSeqNum()=0
bool needHeartbeat() const
Definition: SessionState.h:138
virtual void reset()=0
ResendRange m_resendRange
Definition: SessionState.h:218
const UtcTimeStamp & lastSentTime() const
Definition: SessionState.h:105
bool needTestRequest() const
Definition: SessionState.h:143
std::string logoutReason() const
Definition: SessionState.h:150
MessageStore * store()
Definition: SessionState.h:89
void setNextSenderMsgSeqNum(int n)
Definition: SessionState.h:181
bool timedOut() const
Definition: SessionState.h:133
IO Error.
Definition: Exceptions.h:238
ResendRange resendRange() const
Definition: SessionState.h:85
void heartBtInt(const HeartBtInt &value)
Definition: SessionState.h:94
virtual void refresh()=0
virtual void setNextTargetMsgSeqNum(int)=0
void testRequest(int value)
Definition: SessionState.h:78
bool shouldSendLogon() const
Definition: SessionState.h:115
UtcTimeStamp & lastSentTime()
Definition: SessionState.h:103

Generated on Sat Mar 29 2014 15:13:32 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001