Session.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_SESSION_H
23#define FIX_SESSION_H
24
25#ifdef _MSC_VER
26#pragma warning( disable : 4503 4355 4786 4290 )
27#endif
28
29#include "SessionState.h"
30#include "TimeRange.h"
31#include "SessionID.h"
32#include "Responder.h"
33#include "Fields.h"
35#include "Application.h"
36#include "Mutex.h"
37#include "Log.h"
38#include <utility>
39#include <map>
40#include <queue>
41
42namespace FIX
43{
46{
47public:
49 const SessionID&,
51 const TimeRange&,
52 int heartBtInt, LogFactory* pLogFactory );
53 virtual ~Session();
54
55 void logon()
56 { m_state.enabled( true ); m_state.logoutReason( "" ); }
57 void logout( const std::string& reason = "" )
58 { m_state.enabled( false ); m_state.logoutReason( reason ); }
59 bool isEnabled()
60 { return m_state.enabled(); }
61
62 bool sentLogon() { return m_state.sentLogon(); }
63 bool sentLogout() { return m_state.sentLogout(); }
64 bool receivedLogon() { return m_state.receivedLogon(); }
65 bool isLoggedOn() { return receivedLogon() && sentLogon(); }
66 void reset() throw( IOException )
67 { generateLogout(); disconnect(); m_state.reset(); }
68 void refresh() throw( IOException )
69 { m_state.refresh(); }
70 void setNextSenderMsgSeqNum( int num ) throw( IOException )
71 { m_state.setNextSenderMsgSeqNum( num ); }
72 void setNextTargetMsgSeqNum( int num ) throw( IOException )
73 { m_state.setNextTargetMsgSeqNum( num ); }
74
75 const SessionID& getSessionID() const
76 { return m_sessionID; }
77 void setDataDictionaryProvider( const DataDictionaryProvider& dataDictionaryProvider )
78 { m_dataDictionaryProvider = dataDictionaryProvider; }
81
82 static bool sendToTarget( Message& message,
83 const std::string& qualifier = "" )
84 throw( SessionNotFound );
85 static bool sendToTarget( Message& message, const SessionID& sessionID )
86 throw( SessionNotFound );
87 static bool sendToTarget( Message&,
88 const SenderCompID& senderCompID,
89 const TargetCompID& targetCompID,
90 const std::string& qualifier = "" )
91 throw( SessionNotFound );
92 static bool sendToTarget( Message& message,
93 const std::string& senderCompID,
94 const std::string& targetCompID,
95 const std::string& qualifier = "" )
96 throw( SessionNotFound );
97
98 static std::set<SessionID> getSessions();
99 static bool doesSessionExist( const SessionID& );
100 static Session* lookupSession( const SessionID& );
101 static Session* lookupSession( const std::string&, bool reverse = false );
102 static bool isSessionRegistered( const SessionID& );
103 static Session* registerSession( const SessionID& );
104 static void unregisterSession( const SessionID& );
105
106 static size_t numSessions();
107
108 bool isSessionTime(const UtcTimeStamp& time)
109 { return m_sessionTime.isInRange(time); }
110 bool isLogonTime(const UtcTimeStamp& time)
111 { return m_logonTime.isInRange(time); }
113 { return m_state.initiate(); }
115 { return !m_state.initiate(); }
116
118 { return m_logonTime; }
119 void setLogonTime( const TimeRange& value )
120 { m_logonTime = value; }
121
122 const std::string& getSenderDefaultApplVerID()
123 { return m_senderDefaultApplVerID; }
124 void setSenderDefaultApplVerID( const std::string& senderDefaultApplVerID )
125 { m_senderDefaultApplVerID = senderDefaultApplVerID; }
126
127 const std::string& getTargetDefaultApplVerID()
128 { return m_targetDefaultApplVerID; }
129 void setTargetDefaultApplVerID( const std::string& targetDefaultApplVerID )
130 { m_targetDefaultApplVerID = targetDefaultApplVerID; }
131
136
138 { return m_checkCompId; }
139 void setCheckCompId ( bool value )
140 { m_checkCompId = value; }
141
143 { return m_checkLatency; }
144 void setCheckLatency ( bool value )
145 { m_checkLatency = value; }
146
148 { return m_maxLatency; }
149 void setMaxLatency ( int value )
150 { m_maxLatency = value; }
151
153 { return m_state.logonTimeout(); }
154 void setLogonTimeout ( int value )
155 { m_state.logonTimeout( value ); }
156
158 { return m_state.logoutTimeout(); }
159 void setLogoutTimeout ( int value )
160 { m_state.logoutTimeout( value ); }
161
163 { return m_resetOnLogon; }
164 void setResetOnLogon ( bool value )
165 { m_resetOnLogon = value; }
166
168 { return m_resetOnLogout; }
169 void setResetOnLogout ( bool value )
170 { m_resetOnLogout = value; }
171
174 void setResetOnDisconnect( bool value )
175 { m_resetOnDisconnect = value; }
176
178 { return m_refreshOnLogon; }
179 void setRefreshOnLogon( bool value )
180 { m_refreshOnLogon = value; }
181
184 void setMillisecondsInTimeStamp ( bool value )
185 { if (value)
187 else
189 }
192 void setTimestampPrecision(int precision)
193 {
194 if (precision < 0 || precision > 9)
195 return;
196
197 m_timestampPrecision = precision;
198 }
199
201 { return m_persistMessages; }
202 void setPersistMessages ( bool value )
203 { m_persistMessages = value; }
204
208 { m_validateLengthAndChecksum = value; }
209
211 {
213 reset();
214 m_pResponder = pR;
215 }
216
217 bool send( Message& );
218 void next();
219 void next( const UtcTimeStamp& timeStamp );
220 void next( const std::string&, const UtcTimeStamp& timeStamp, bool queued = false );
221 void next( const Message&, const UtcTimeStamp& timeStamp, bool queued = false );
222 void disconnect();
223
224 int getExpectedSenderNum() { return m_state.getNextSenderMsgSeqNum(); }
225 int getExpectedTargetNum() { return m_state.getNextTargetMsgSeqNum(); }
226
227 Log* getLog() { return &m_state; }
228 const MessageStore* getStore() { return &m_state; }
229
230 const HeartBtInt& getHeartBtInt() const { return m_state.heartBtInt(); }
231
232private:
233 typedef std::map < SessionID, Session* > Sessions;
234 typedef std::set < SessionID > SessionIDs;
235
236 static bool addSession( Session& );
237 static void removeSession( Session& );
238
239 bool send( const std::string& );
240 bool sendRaw( Message&, int msgSeqNum = 0 );
241 bool resend( Message& message );
242 void persist( const Message&, const std::string& ) throw ( IOException );
243
244 void insertSendingTime( Header& );
246 const UtcTimeStamp& when = UtcTimeStamp () );
247 void fill( Header& );
248
249 bool isGoodTime( const SendingTime& sendingTime )
250 {
251 if ( !m_checkLatency ) return true;
252 UtcTimeStamp now;
253 return labs( now - sendingTime ) <= m_maxLatency;
254 }
255 bool checkSessionTime( const UtcTimeStamp& timeStamp )
256 {
257 UtcTimeStamp creationTime = m_state.getCreationTime();
258 return m_sessionTime.isInSameRange( timeStamp, creationTime );
259 }
260 bool isTargetTooHigh( const MsgSeqNum& msgSeqNum )
261 { return msgSeqNum > ( m_state.getNextTargetMsgSeqNum() ); }
262 bool isTargetTooLow( const MsgSeqNum& msgSeqNum )
263 { return msgSeqNum < ( m_state.getNextTargetMsgSeqNum() ); }
264 bool isCorrectCompID( const SenderCompID& senderCompID,
265 const TargetCompID& targetCompID )
266 {
267 if( !m_checkCompId ) return true;
268
269 return
270 m_sessionID.getSenderCompID().getValue() == targetCompID.getValue()
271 && m_sessionID.getTargetCompID().getValue() == senderCompID.getValue();
272 }
273 bool shouldSendReset();
274
275 bool validLogonState( const MsgType& msgType );
276 void fromCallback( const MsgType& msgType, const Message& msg,
277 const SessionID& sessionID );
278
279 void doBadTime( const Message& msg );
280 void doBadCompID( const Message& msg );
281 bool doPossDup( const Message& msg );
282 bool doTargetTooLow( const Message& msg );
283 void doTargetTooHigh( const Message& msg );
284 void nextQueued( const UtcTimeStamp& timeStamp );
285 bool nextQueued( int num, const UtcTimeStamp& timeStamp );
286
287 void nextLogon( const Message&, const UtcTimeStamp& timeStamp );
288 void nextHeartbeat( const Message&, const UtcTimeStamp& timeStamp );
289 void nextTestRequest( const Message&, const UtcTimeStamp& timeStamp );
290 void nextLogout( const Message&, const UtcTimeStamp& timeStamp );
291 void nextReject( const Message&, const UtcTimeStamp& timeStamp );
292 void nextSequenceReset( const Message&, const UtcTimeStamp& timeStamp );
293 void nextResendRequest( const Message&, const UtcTimeStamp& timeStamp );
294
295 void generateLogon();
296 void generateLogon( const Message& );
297 void generateResendRequest( const BeginString&, const MsgSeqNum& );
298 void generateSequenceReset( int, int );
299 void generateHeartbeat();
300 void generateHeartbeat( const Message& );
301 void generateTestRequest( const std::string& );
302 void generateReject( const Message&, int err, int field = 0 );
303 void generateReject( const Message&, const std::string& );
304 void generateBusinessReject( const Message&, int err, int field = 0 );
305 void generateLogout( const std::string& text = "" );
306
307 void populateRejectReason( Message&, int field, const std::string& );
308 void populateRejectReason( Message&, const std::string& );
309
310 bool verify( const Message& msg,
311 bool checkTooHigh = true, bool checkTooLow = true );
312
313 bool set( int s, const Message& m );
314 bool get( int s, Message& m ) const;
315
316 Message * newMessage(const std::string & msgType) const;
317
322
336
343
348};
349}
350
351#endif //FIX_SESSION_H
This interface must be implemented to define what your FIX application does.
Definition Application.h:44
Queries for DataDictionary based on appropriate version of FIX.
This interface must be implemented to create a Log.
Definition Log.h:43
This interface must be implemented to log messages and events.
Definition Log.h:82
Base class for all FIX messages.
Definition Message.h:118
This interface must be implemented to create a MessageStore.
This interface must be implemented to store and retrieve messages and sequence numbers.
Portable implementation of a mutex.
Definition Mutex.h:31
Interface implements sending on and disconnecting a transport.
Definition Responder.h:35
Maintains the state and implements the logic of a FIX session.
Definition Session.h:46
MessageStoreFactory & m_messageStoreFactory
Definition Session.h:339
void fromCallback(const MsgType &msgType, const Message &msg, const SessionID &sessionID)
Definition Session.cpp:1137
bool m_resetOnLogout
Definition Session.h:330
bool getMillisecondsInTimeStamp()
Definition Session.h:182
int m_maxLatency
Definition Session.h:328
static Session * registerSession(const SessionID &)
Definition Session.cpp:1537
void nextQueued(const UtcTimeStamp &timeStamp)
Definition Session.cpp:1238
void setResetOnLogon(bool value)
Definition Session.h:164
LogFactory * m_pLogFactory
Definition Session.h:340
bool isTargetTooHigh(const MsgSeqNum &msgSeqNum)
Definition Session.h:260
void setSendRedundantResendRequests(bool value)
Definition Session.h:134
bool isGoodTime(const SendingTime &sendingTime)
Definition Session.h:249
bool m_checkLatency
Definition Session.h:327
int getTimestampPrecision()
Definition Session.h:190
void nextHeartbeat(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:280
void insertSendingTime(Header &)
Definition Session.cpp:91
bool getCheckCompId()
Definition Session.h:137
static Mutex s_mutex
Definition Session.h:347
void setResetOnLogout(bool value)
Definition Session.h:169
void nextSequenceReset(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:319
Application & m_application
Definition Session.h:318
void nextLogout(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:295
void populateRejectReason(Message &, int field, const std::string &)
Definition Session.cpp:1009
void setMillisecondsInTimeStamp(bool value)
Definition Session.h:184
bool getSendRedundantResendRequests()
Definition Session.h:132
bool isTargetTooLow(const MsgSeqNum &msgSeqNum)
Definition Session.h:262
SessionState m_state
Definition Session.h:337
void setSenderDefaultApplVerID(const std::string &senderDefaultApplVerID)
Definition Session.h:124
bool get(int s, Message &m) const
bool m_refreshOnLogon
Definition Session.h:332
static void unregisterSession(const SessionID &)
Definition Session.cpp:1547
bool m_persistMessages
Definition Session.h:334
bool getResetOnLogout()
Definition Session.h:167
void fill(Header &)
Definition Session.cpp:114
bool send(Message &)
Definition Session.cpp:523
const HeartBtInt & getHeartBtInt() const
Definition Session.h:230
bool resend(Message &message)
Definition Session.cpp:643
void insertOrigSendingTime(Header &, const UtcTimeStamp &when=UtcTimeStamp())
Definition Session.cpp:103
bool isCorrectCompID(const SenderCompID &senderCompID, const TargetCompID &targetCompID)
Definition Session.h:264
const MessageStore * getStore()
Definition Session.h:228
void logout(const std::string &reason="")
Definition Session.h:57
void generateHeartbeat()
Definition Session.cpp:764
int getMaxLatency()
Definition Session.h:147
Responder * m_pResponder
Definition Session.h:341
DataDictionaryProvider m_dataDictionaryProvider
Definition Session.h:338
void setMaxLatency(int value)
Definition Session.h:149
bool checkSessionTime(const UtcTimeStamp &timeStamp)
Definition Session.h:255
static bool doesSessionExist(const SessionID &)
Definition Session.cpp:1490
void setResponder(Responder *pR)
Definition Session.h:210
bool m_sendRedundantResendRequests
Definition Session.h:325
void setTargetDefaultApplVerID(const std::string &targetDefaultApplVerID)
Definition Session.h:129
bool validLogonState(const MsgType &msgType)
Definition Session.cpp:1117
bool getPersistMessages()
Definition Session.h:200
static void removeSession(Session &)
Definition Session.cpp:1573
SessionID m_sessionID
Definition Session.h:319
bool doTargetTooLow(const Message &msg)
Definition Session.cpp:1186
void reset()
Definition Session.h:66
void generateLogon()
Definition Session.cpp:673
Mutex m_mutex
Definition Session.h:342
void setNextSenderMsgSeqNum(int num)
Definition Session.h:70
bool isEnabled()
Definition Session.h:59
void generateResendRequest(const BeginString &, const MsgSeqNum &)
Definition Session.cpp:718
int getExpectedTargetNum()
Definition Session.h:225
const TimeRange & getLogonTime()
Definition Session.h:117
bool sentLogout()
Definition Session.h:63
bool verify(const Message &msg, bool checkTooHigh=true, bool checkTooLow=true)
Definition Session.cpp:1034
void nextReject(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:312
int getLogonTimeout()
Definition Session.h:152
const DataDictionaryProvider & getDataDictionaryProvider() const
Definition Session.h:79
bool isSessionTime(const UtcTimeStamp &time)
Definition Session.h:108
static Session * lookupSession(const SessionID &)
Definition Session.cpp:1496
bool getValidateLengthAndChecksum()
Definition Session.h:205
void generateBusinessReject(const Message &, int err, int field=0)
Definition Session.cpp:931
void setCheckLatency(bool value)
Definition Session.h:144
void nextTestRequest(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:287
bool shouldSendReset()
Definition Session.cpp:1106
int getLogoutTimeout()
Definition Session.h:157
void nextLogon(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:202
void setRefreshOnLogon(bool value)
Definition Session.h:179
bool isAcceptor()
Definition Session.h:114
void setTimestampPrecision(int precision)
Definition Session.h:192
void setResetOnDisconnect(bool value)
Definition Session.h:174
bool isInitiator()
Definition Session.h:112
std::string m_targetDefaultApplVerID
Definition Session.h:324
void refresh()
Definition Session.h:68
void nextResendRequest(const Message &, const UtcTimeStamp &timeStamp)
Definition Session.cpp:344
void generateTestRequest(const std::string &)
Definition Session.cpp:792
const SessionID & getSessionID() const
Definition Session.h:75
bool getResetOnLogon()
Definition Session.h:162
std::string m_senderDefaultApplVerID
Definition Session.h:323
Log * getLog()
Definition Session.h:227
void next()
Definition Session.cpp:125
bool doPossDup(const Message &msg)
Definition Session.cpp:1158
int m_timestampPrecision
Definition Session.h:333
std::set< SessionID > SessionIDs
Definition Session.h:234
void disconnect()
Definition Session.cpp:613
static Sessions s_registered
Definition Session.h:346
void doBadTime(const Message &msg)
Definition Session.cpp:1146
static SessionIDs s_sessionIDs
Definition Session.h:345
void doTargetTooHigh(const Message &msg)
Definition Session.cpp:1206
bool getResetOnDisconnect()
Definition Session.h:172
void logon()
Definition Session.h:55
bool sentLogon()
Definition Session.h:62
static bool addSession(Session &)
Definition Session.cpp:1559
void generateLogout(const std::string &text="")
Definition Session.cpp:996
bool isLogonTime(const UtcTimeStamp &time)
Definition Session.h:110
void generateReject(const Message &, int err, int field=0)
Definition Session.cpp:805
bool m_resetOnDisconnect
Definition Session.h:331
int getExpectedSenderNum()
Definition Session.h:224
bool sendRaw(Message &, int msgSeqNum=0)
Definition Session.cpp:530
void setValidateLengthAndChecksum(bool value)
Definition Session.h:207
void setLogonTime(const TimeRange &value)
Definition Session.h:119
void doBadCompID(const Message &msg)
Definition Session.cpp:1152
bool m_validateLengthAndChecksum
Definition Session.h:335
void setPersistMessages(bool value)
Definition Session.h:202
std::map< SessionID, Session * > Sessions
Definition Session.h:233
bool m_resetOnLogon
Definition Session.h:329
const std::string & getSenderDefaultApplVerID()
Definition Session.h:122
virtual ~Session()
Definition Session.cpp:83
void setCheckCompId(bool value)
Definition Session.h:139
void setDataDictionaryProvider(const DataDictionaryProvider &dataDictionaryProvider)
Definition Session.h:77
TimeRange m_sessionTime
Definition Session.h:320
bool receivedLogon()
Definition Session.h:64
bool m_checkCompId
Definition Session.h:326
static std::set< SessionID > getSessions()
Definition Session.cpp:1485
static Sessions s_sessions
Definition Session.h:344
static bool isSessionRegistered(const SessionID &)
Definition Session.cpp:1531
Message * newMessage(const std::string &msgType) const
Definition Session.cpp:492
void generateSequenceReset(int, int)
Definition Session.cpp:743
static bool sendToTarget(Message &message, const std::string &qualifier="")
Definition Session.cpp:1444
Session(Application &, MessageStoreFactory &, const SessionID &, const DataDictionaryProvider &, const TimeRange &, int heartBtInt, LogFactory *pLogFactory)
Definition Session.cpp:41
void setNextTargetMsgSeqNum(int num)
Definition Session.h:72
bool isLoggedOn()
Definition Session.h:65
bool getCheckLatency()
Definition Session.h:142
void setLogonTimeout(int value)
Definition Session.h:154
static size_t numSessions()
Definition Session.cpp:1553
bool getRefreshOnLogon()
Definition Session.h:177
bool set(int s, const Message &m)
TimeRange m_logonTime
Definition Session.h:321
const std::string & getTargetDefaultApplVerID()
Definition Session.h:127
void persist(const Message &, const std::string &)
Definition Session.cpp:663
void setLogoutTimeout(int value)
Definition Session.h:159
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition SessionID.h:31
Maintains all of state for the Session class.
Keeps track of when session is active.
Definition TimeRange.h:35
Date and Time represented in UTC.
Definition FieldTypes.h:583
void reverse(I begin, I end)
Definition pugixml.cpp:6009
Session cannot be found for specified action.
Definition Exceptions.h:232

Generated on for QuickFIX by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2001