SessionState.h
Go to the documentation of this file.
/* -*- C++ -*- */
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifndef FIX_SESSIONSTATE_H
#define FIX_SESSIONSTATE_H
#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 4290 )
#endif
#include "FieldTypes.h"
#include "MessageStore.h"
#include "Log.h"
#include "Mutex.h"
namespace FIX
{
class SessionState : public MessageStore, public Log
{
typedef std::map < int, Message > Messages;
public:
SessionState()
: m_enabled( true ), m_receivedLogon( false ),
m_sentLogout( false ), m_sentLogon( false ),
m_sentReset( false ), m_receivedReset( false ),
m_initiate( false ), m_logonTimeout( 10 ),
m_logoutTimeout( 2 ), m_testRequest( 0 ),
m_pStore( 0 ), m_pLog( 0 ) {}
bool enabled() const { return m_enabled; }
void enabled( bool value ) { m_enabled = value; }
bool receivedLogon() const { return m_receivedLogon; }
void receivedLogon( bool value ) { m_receivedLogon = value; }
bool sentLogout() const { return m_sentLogout; }
void sentLogout( bool value ) { m_sentLogout = value; }
bool sentLogon() const { return m_sentLogon; }
void sentLogon( bool value ) { m_sentLogon = value; }
bool receivedReset() const { return m_receivedReset; }
void receivedReset( bool value ) { m_receivedReset = value; }
bool sentReset() const { return m_sentReset; }
void sentReset( bool value ) { m_sentReset = value; }
bool initiate() const { return m_initiate; }
void initiate( bool value ) { m_initiate = value; }
int logonTimeout() const { return m_logonTimeout; }
void logonTimeout( int value ) { m_logonTimeout = value; }
int logoutTimeout() const { return m_logoutTimeout; }
void logoutTimeout( int value ) { m_logoutTimeout = value; }
int testRequest() const { return m_testRequest; }
void testRequest( int value ) { m_testRequest = value; }
bool resendRequested() const
{ return !(m_resendRange.first == 0 && m_resendRange.second == 0); }
typedef std::pair<int, int> ResendRange;
ResendRange resendRange () const { return m_resendRange; }
void resendRange (int begin, int end)
{ m_resendRange = std::make_pair( begin, end ); }
MessageStore* store() { return m_pStore; }
void store( MessageStore* pValue ) { m_pStore = pValue; }
Log* log() { return m_pLog ? m_pLog : &m_nullLog; }
void log( Log* pValue ) { m_pLog = pValue; }
void heartBtInt( const HeartBtInt& value )
{ m_heartBtInt = value; }
HeartBtInt& heartBtInt()
{ return m_heartBtInt; }
const HeartBtInt& heartBtInt() const
{ return m_heartBtInt; }
void lastSentTime( const UtcTimeStamp& value )
{ m_lastSentTime = value; }
UtcTimeStamp& lastSentTime()
{ return m_lastSentTime; }
const UtcTimeStamp& lastSentTime() const
{ return m_lastSentTime; }
void lastReceivedTime( const UtcTimeStamp& value )
{ m_lastReceivedTime = value; }
UtcTimeStamp& lastReceivedTime()
{ return m_lastReceivedTime; }
const UtcTimeStamp& lastReceivedTime() const
{ return m_lastReceivedTime; }
bool shouldSendLogon() const { return initiate() && !sentLogon(); }
bool alreadySentLogon() const { return initiate() && sentLogon(); }
bool logonTimedOut() const
{
UtcTimeStamp now;
return now - lastReceivedTime() >= logonTimeout();
}
bool logoutTimedOut() const
{
UtcTimeStamp now;
return sentLogout() && ( ( now - lastSentTime() ) >= logoutTimeout() );
}
bool withinHeartBeat() const
{
UtcTimeStamp now;
return ( ( now - lastSentTime() ) < heartBtInt() ) &&
( ( now - lastReceivedTime() ) < heartBtInt() );
}
bool timedOut() const
{
UtcTimeStamp now;
return ( now - lastReceivedTime() ) >= ( 2.4 * ( double ) heartBtInt() );
}
bool needHeartbeat() const
{
UtcTimeStamp now;
return ( ( now - lastSentTime() ) >= heartBtInt() ) && !testRequest();
}
bool needTestRequest() const
{
UtcTimeStamp now;
return ( now - lastReceivedTime() ) >=
( ( 1.2 * ( ( double ) testRequest() + 1 ) ) * ( double ) heartBtInt() );
}
std::string logoutReason() const
{ Locker l( m_mutex ); return m_logoutReason; }
void logoutReason( const std::string& value )
{ Locker l( m_mutex ); m_logoutReason = value; }
void queue( int msgSeqNum, const Message& message )
{ Locker l( m_mutex ); m_queue[ msgSeqNum ] = message; }
bool retrieve( int msgSeqNum, Message& message )
{
Locker l( m_mutex );
Messages::iterator i = m_queue.find( msgSeqNum );
if ( i != m_queue.end() )
{
message = i->second;
m_queue.erase( i );
return true;
}
return false;
}
void clearQueue()
{ Locker l( m_mutex ); m_queue.clear(); }
bool set( int s, const std::string& m ) throw ( IOException )
{ Locker l( m_mutex ); return m_pStore->set( s, m ); }
void get( int b, int e, std::vector < std::string > &m ) const
throw ( IOException )
{ Locker l( m_mutex ); m_pStore->get( b, e, m ); }
int getNextSenderMsgSeqNum() const throw ( IOException )
{ Locker l( m_mutex ); return m_pStore->getNextSenderMsgSeqNum(); }
int getNextTargetMsgSeqNum() const throw ( IOException )
{ Locker l( m_mutex ); return m_pStore->getNextTargetMsgSeqNum(); }
void setNextSenderMsgSeqNum( int n ) throw ( IOException )
{ Locker l( m_mutex ); m_pStore->setNextSenderMsgSeqNum( n ); }
void setNextTargetMsgSeqNum( int n ) throw ( IOException )
{ Locker l( m_mutex ); m_pStore->setNextTargetMsgSeqNum( n ); }
void incrNextSenderMsgSeqNum() throw ( IOException )
{ Locker l( m_mutex ); m_pStore->incrNextSenderMsgSeqNum(); }
void incrNextTargetMsgSeqNum() throw ( IOException )
{ Locker l( m_mutex ); m_pStore->incrNextTargetMsgSeqNum(); }
UtcTimeStamp getCreationTime() const throw ( IOException )
{ Locker l( m_mutex ); return m_pStore->getCreationTime(); }
void reset() throw ( IOException )
{ Locker l( m_mutex ); m_pStore->reset(); }
void refresh() throw ( IOException )
{ Locker l( m_mutex ); m_pStore->refresh(); }
void clear()
{ if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->clear(); }
void backup()
{ if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->backup(); }
void onIncoming( const std::string& string )
{ if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onIncoming( string ); }
void onOutgoing( const std::string& string )
{ if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onOutgoing( string ); }
void onEvent( const std::string& string )
{ if ( !m_pLog ) return ; Locker l( m_mutex ); m_pLog->onEvent( string ); }
private:
bool m_enabled;
bool m_receivedLogon;
bool m_sentLogout;
bool m_sentLogon;
bool m_sentReset;
bool m_receivedReset;
bool m_initiate;
int m_logonTimeout;
int m_logoutTimeout;
int m_testRequest;
ResendRange m_resendRange;
HeartBtInt m_heartBtInt;
UtcTimeStamp m_lastSentTime;
UtcTimeStamp m_lastReceivedTime;
std::string m_logoutReason;
Messages m_queue;
MessageStore* m_pStore;
Log* m_pLog;
NullLog m_nullLog;
mutable Mutex m_mutex;
};
}
#endif //FIX_SESSIONSTATE_H
FieldTypes.h
Log.h
MessageStore.h
Mutex.h
FIX::Locker
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:96
FIX::Log
This interface must be implemented to log messages and events.
Definition: Log.h:82
FIX::Log::backup
virtual void backup()=0
FIX::Log::onIncoming
virtual void onIncoming(const std::string &)=0
FIX::Log::onOutgoing
virtual void onOutgoing(const std::string &)=0
FIX::Log::clear
virtual void clear()=0
FIX::Log::onEvent
virtual void onEvent(const std::string &)=0
FIX::Message
Base class for all FIX messages.
Definition: Message.h:118
FIX::MessageStore
This interface must be implemented to store and retrieve messages and sequence numbers.
Definition: MessageStore.h:67
FIX::MessageStore::set
virtual bool set(int, const std::string &)=0
FIX::MessageStore::getNextSenderMsgSeqNum
virtual int getNextSenderMsgSeqNum() const =0
FIX::MessageStore::setNextTargetMsgSeqNum
virtual void setNextTargetMsgSeqNum(int)=0
FIX::MessageStore::incrNextSenderMsgSeqNum
virtual void incrNextSenderMsgSeqNum()=0
FIX::MessageStore::incrNextTargetMsgSeqNum
virtual void incrNextTargetMsgSeqNum()=0
FIX::MessageStore::getNextTargetMsgSeqNum
virtual int getNextTargetMsgSeqNum() const =0
FIX::MessageStore::getCreationTime
virtual UtcTimeStamp getCreationTime() const =0
FIX::MessageStore::reset
virtual void reset()=0
FIX::MessageStore::get
virtual void get(int, int, std::vector< std::string > &) const =0
FIX::MessageStore::setNextSenderMsgSeqNum
virtual void setNextSenderMsgSeqNum(int)=0
FIX::MessageStore::refresh
virtual void refresh()=0
FIX::Mutex
Portable implementation of a mutex.
Definition: Mutex.h:31
FIX::NullLog
Null implementation of Log.
Definition: Log.h:101
FIX::SessionState
Maintains all of state for the Session class.
Definition: SessionState.h:38
FIX::SessionState::m_pLog
Log * m_pLog
Definition: SessionState.h:225
FIX::SessionState::log
Log * log()
Definition: SessionState.h:91
FIX::SessionState::m_lastReceivedTime
UtcTimeStamp m_lastReceivedTime
Definition: SessionState.h:221
FIX::SessionState::m_enabled
bool m_enabled
Definition: SessionState.h:208
FIX::SessionState::m_nullLog
NullLog m_nullLog
Definition: SessionState.h:226
FIX::SessionState::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: SessionState.h:177
FIX::SessionState::lastSentTime
void lastSentTime(const UtcTimeStamp &value)
Definition: SessionState.h:101
FIX::SessionState::testRequest
int testRequest() const
Definition: SessionState.h:77
FIX::SessionState::m_queue
Messages m_queue
Definition: SessionState.h:223
FIX::SessionState::receivedLogon
void receivedLogon(bool value)
Definition: SessionState.h:54
FIX::SessionState::lastSentTime
UtcTimeStamp & lastSentTime()
Definition: SessionState.h:103
FIX::SessionState::sentLogon
bool sentLogon() const
Definition: SessionState.h:59
FIX::SessionState::sentLogon
void sentLogon(bool value)
Definition: SessionState.h:60
FIX::SessionState::logoutTimedOut
bool logoutTimedOut() const
Definition: SessionState.h:122
FIX::SessionState::lastReceivedTime
UtcTimeStamp & lastReceivedTime()
Definition: SessionState.h:110
FIX::SessionState::m_initiate
bool m_initiate
Definition: SessionState.h:214
FIX::SessionState::logoutReason
void logoutReason(const std::string &value)
Definition: SessionState.h:152
FIX::SessionState::onIncoming
void onIncoming(const std::string &string)
Definition: SessionState.h:200
FIX::SessionState::sentLogout
void sentLogout(bool value)
Definition: SessionState.h:57
FIX::SessionState::initiate
void initiate(bool value)
Definition: SessionState.h:69
FIX::SessionState::setNextTargetMsgSeqNum
void setNextTargetMsgSeqNum(int n)
Definition: SessionState.h:183
FIX::SessionState::resendRequested
bool resendRequested() const
Definition: SessionState.h:80
FIX::SessionState::SessionState
SessionState()
Definition: SessionState.h:42
FIX::SessionState::ResendRange
std::pair< int, int > ResendRange
Definition: SessionState.h:83
FIX::SessionState::m_sentLogon
bool m_sentLogon
Definition: SessionState.h:211
FIX::SessionState::m_resendRange
ResendRange m_resendRange
Definition: SessionState.h:218
FIX::SessionState::sentReset
bool sentReset() const
Definition: SessionState.h:65
FIX::SessionState::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int n)
Definition: SessionState.h:181
FIX::SessionState::Messages
std::map< int, Message > Messages
Definition: SessionState.h:39
FIX::SessionState::heartBtInt
HeartBtInt & heartBtInt()
Definition: SessionState.h:96
FIX::SessionState::shouldSendLogon
bool shouldSendLogon() const
Definition: SessionState.h:115
FIX::SessionState::enabled
bool enabled() const
Definition: SessionState.h:50
FIX::SessionState::logoutTimeout
int logoutTimeout() const
Definition: SessionState.h:74
FIX::SessionState::resendRange
void resendRange(int begin, int end)
Definition: SessionState.h:86
FIX::SessionState::sentLogout
bool sentLogout() const
Definition: SessionState.h:56
FIX::SessionState::incrNextSenderMsgSeqNum
void incrNextSenderMsgSeqNum()
Definition: SessionState.h:185
FIX::SessionState::logonTimedOut
bool logonTimedOut() const
Definition: SessionState.h:117
FIX::SessionState::m_lastSentTime
UtcTimeStamp m_lastSentTime
Definition: SessionState.h:220
FIX::SessionState::enabled
void enabled(bool value)
Definition: SessionState.h:51
FIX::SessionState::retrieve
bool retrieve(int msgSeqNum, Message &message)
Definition: SessionState.h:157
FIX::SessionState::m_logoutReason
std::string m_logoutReason
Definition: SessionState.h:222
FIX::SessionState::getNextTargetMsgSeqNum
int getNextTargetMsgSeqNum() const
Definition: SessionState.h:179
FIX::SessionState::lastReceivedTime
void lastReceivedTime(const UtcTimeStamp &value)
Definition: SessionState.h:108
FIX::SessionState::m_sentLogout
bool m_sentLogout
Definition: SessionState.h:210
FIX::SessionState::m_logonTimeout
int m_logonTimeout
Definition: SessionState.h:215
FIX::SessionState::refresh
void refresh()
Definition: SessionState.h:193
FIX::SessionState::receivedLogon
bool receivedLogon() const
Definition: SessionState.h:53
FIX::SessionState::clearQueue
void clearQueue()
Definition: SessionState.h:169
FIX::SessionState::heartBtInt
const HeartBtInt & heartBtInt() const
Definition: SessionState.h:98
FIX::SessionState::lastReceivedTime
const UtcTimeStamp & lastReceivedTime() const
Definition: SessionState.h:112
FIX::SessionState::store
void store(MessageStore *pValue)
Definition: SessionState.h:90
FIX::SessionState::logonTimeout
void logonTimeout(int value)
Definition: SessionState.h:72
FIX::SessionState::logoutReason
std::string logoutReason() const
Definition: SessionState.h:150
FIX::SessionState::withinHeartBeat
bool withinHeartBeat() const
Definition: SessionState.h:127
FIX::SessionState::m_sentReset
bool m_sentReset
Definition: SessionState.h:212
FIX::SessionState::clear
void clear()
Definition: SessionState.h:196
FIX::SessionState::lastSentTime
const UtcTimeStamp & lastSentTime() const
Definition: SessionState.h:105
FIX::SessionState::needTestRequest
bool needTestRequest() const
Definition: SessionState.h:143
FIX::SessionState::m_receivedLogon
bool m_receivedLogon
Definition: SessionState.h:209
FIX::SessionState::incrNextTargetMsgSeqNum
void incrNextTargetMsgSeqNum()
Definition: SessionState.h:187
FIX::SessionState::reset
void reset()
Definition: SessionState.h:191
FIX::SessionState::timedOut
bool timedOut() const
Definition: SessionState.h:133
FIX::SessionState::logonTimeout
int logonTimeout() const
Definition: SessionState.h:71
FIX::SessionState::testRequest
void testRequest(int value)
Definition: SessionState.h:78
FIX::SessionState::needHeartbeat
bool needHeartbeat() const
Definition: SessionState.h:138
FIX::SessionState::m_heartBtInt
HeartBtInt m_heartBtInt
Definition: SessionState.h:219
FIX::SessionState::logoutTimeout
void logoutTimeout(int value)
Definition: SessionState.h:75
FIX::SessionState::onOutgoing
void onOutgoing(const std::string &string)
Definition: SessionState.h:202
FIX::SessionState::set
bool set(int s, const std::string &m)
Definition: SessionState.h:172
FIX::SessionState::initiate
bool initiate() const
Definition: SessionState.h:68
FIX::SessionState::queue
void queue(int msgSeqNum, const Message &message)
Definition: SessionState.h:155
FIX::SessionState::store
MessageStore * store()
Definition: SessionState.h:89
FIX::SessionState::receivedReset
bool receivedReset() const
Definition: SessionState.h:62
FIX::SessionState::m_logoutTimeout
int m_logoutTimeout
Definition: SessionState.h:216
FIX::SessionState::receivedReset
void receivedReset(bool value)
Definition: SessionState.h:63
FIX::SessionState::m_pStore
MessageStore * m_pStore
Definition: SessionState.h:224
FIX::SessionState::getCreationTime
UtcTimeStamp getCreationTime() const
Definition: SessionState.h:189
FIX::SessionState::onEvent
void onEvent(const std::string &string)
Definition: SessionState.h:204
FIX::SessionState::heartBtInt
void heartBtInt(const HeartBtInt &value)
Definition: SessionState.h:94
FIX::SessionState::resendRange
ResendRange resendRange() const
Definition: SessionState.h:85
FIX::SessionState::m_testRequest
int m_testRequest
Definition: SessionState.h:217
FIX::SessionState::m_receivedReset
bool m_receivedReset
Definition: SessionState.h:213
FIX::SessionState::backup
void backup()
Definition: SessionState.h:198
FIX::SessionState::get
void get(int b, int e, std::vector< std::string > &m) const
Definition: SessionState.h:174
FIX::SessionState::alreadySentLogon
bool alreadySentLogon() const
Definition: SessionState.h:116
FIX::SessionState::log
void log(Log *pValue)
Definition: SessionState.h:92
FIX::SessionState::m_mutex
Mutex m_mutex
Definition: SessionState.h:227
FIX::SessionState::sentReset
void sentReset(bool value)
Definition: SessionState.h:66
FIX::UtcTimeStamp
Date and Time represented in UTC.
Definition: FieldTypes.h:583
FIX
Definition: Acceptor.cpp:35
FIX::IOException
IO Error.
Definition: Exceptions.h:239