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
34namespace FIX
35{
37class SessionState : public MessageStore, public Log
38{
39 typedef std::map < int, Message > Messages;
40
41public:
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
86 void resendRange (int begin, int end)
87 { m_resendRange = std::make_pair( begin, end ); }
88
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; }
98 const HeartBtInt& heartBtInt() const
99 { return m_heartBtInt; }
100
101 void lastSentTime( const UtcTimeStamp& value )
102 { m_lastSentTime = value; }
106 { return m_lastSentTime; }
107
108 void lastReceivedTime( const UtcTimeStamp& value )
109 { m_lastReceivedTime = value; }
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 {
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 {
141 return ( ( now - lastSentTime() ) >= heartBtInt() ) && !testRequest();
142 }
143 bool needTestRequest() const
144 {
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 )
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 }
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 ); }
192 { Locker l( m_mutex ); m_pStore->reset(); }
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
207private:
222 std::string m_logoutReason;
227 mutable Mutex m_mutex;
228};
229}
230
231#endif //FIX_SESSIONSTATE_H
Locks/Unlocks a mutex using RAII.
Definition Mutex.h:96
This interface must be implemented to log messages and events.
Definition Log.h:82
virtual void backup()=0
virtual void onIncoming(const std::string &)=0
virtual void onOutgoing(const std::string &)=0
virtual void clear()=0
virtual void onEvent(const std::string &)=0
Base class for all FIX messages.
Definition Message.h:118
This interface must be implemented to store and retrieve messages and sequence numbers.
virtual bool set(int, const std::string &)=0
virtual int getNextSenderMsgSeqNum() const =0
virtual void setNextTargetMsgSeqNum(int)=0
virtual void incrNextSenderMsgSeqNum()=0
virtual void incrNextTargetMsgSeqNum()=0
virtual int getNextTargetMsgSeqNum() const =0
virtual UtcTimeStamp getCreationTime() const =0
virtual void reset()=0
virtual void get(int, int, std::vector< std::string > &) const =0
virtual void setNextSenderMsgSeqNum(int)=0
virtual void refresh()=0
Portable implementation of a mutex.
Definition Mutex.h:31
Null implementation of Log.
Definition Log.h:101
Maintains all of state for the Session class.
UtcTimeStamp m_lastReceivedTime
MessageStore * store()
UtcTimeStamp & lastSentTime()
const UtcTimeStamp & lastReceivedTime() const
int getNextSenderMsgSeqNum() const
void lastSentTime(const UtcTimeStamp &value)
int testRequest() const
void receivedLogon(bool value)
bool sentLogon() const
void sentLogon(bool value)
bool logoutTimedOut() const
void logoutReason(const std::string &value)
void onIncoming(const std::string &string)
void sentLogout(bool value)
void initiate(bool value)
void setNextTargetMsgSeqNum(int n)
bool resendRequested() const
std::pair< int, int > ResendRange
ResendRange m_resendRange
bool sentReset() const
void setNextSenderMsgSeqNum(int n)
const HeartBtInt & heartBtInt() const
std::map< int, Message > Messages
bool shouldSendLogon() const
bool enabled() const
int logoutTimeout() const
void resendRange(int begin, int end)
bool sentLogout() const
HeartBtInt & heartBtInt()
void incrNextSenderMsgSeqNum()
bool logonTimedOut() const
UtcTimeStamp m_lastSentTime
void enabled(bool value)
bool retrieve(int msgSeqNum, Message &message)
std::string m_logoutReason
int getNextTargetMsgSeqNum() const
void lastReceivedTime(const UtcTimeStamp &value)
bool receivedLogon() const
void store(MessageStore *pValue)
void logonTimeout(int value)
std::string logoutReason() const
bool withinHeartBeat() const
bool needTestRequest() const
void incrNextTargetMsgSeqNum()
bool timedOut() const
int logonTimeout() const
void testRequest(int value)
bool needHeartbeat() const
HeartBtInt m_heartBtInt
void logoutTimeout(int value)
void onOutgoing(const std::string &string)
bool set(int s, const std::string &m)
const UtcTimeStamp & lastSentTime() const
bool initiate() const
void queue(int msgSeqNum, const Message &message)
bool receivedReset() const
void receivedReset(bool value)
MessageStore * m_pStore
UtcTimeStamp getCreationTime() const
void onEvent(const std::string &string)
void heartBtInt(const HeartBtInt &value)
ResendRange resendRange() const
UtcTimeStamp & lastReceivedTime()
void get(int b, int e, std::vector< std::string > &m) const
bool alreadySentLogon() const
void log(Log *pValue)
void sentReset(bool value)
Date and Time represented in UTC.
Definition FieldTypes.h:583

Generated on Thu May 22 2025 08:23:50 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001