Session.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FIX_SESSION_H
00023 #define FIX_SESSION_H
00024
00025 #ifdef _MSC_VER
00026 #pragma warning( disable : 4503 4355 4786 4290 )
00027 #endif
00028
00029 #include "SessionState.h"
00030 #include "TimeRange.h"
00031 #include "SessionID.h"
00032 #include "Responder.h"
00033 #include "Fields.h"
00034 #include "DataDictionaryProvider.h"
00035 #include "Application.h"
00036 #include "Mutex.h"
00037 #include "Log.h"
00038 #include <utility>
00039 #include <map>
00040 #include <queue>
00041
00042 namespace FIX
00043 {
00045 class Session
00046 {
00047 public:
00048 Session( Application&, MessageStoreFactory&,
00049 const SessionID&,
00050 const DataDictionaryProvider&,
00051 const TimeRange&,
00052 int heartBtInt, LogFactory* pLogFactory );
00053 ~Session();
00054
00055 void logon()
00056 { m_state.enabled( true ); m_state.logoutReason( "" ); }
00057 void logout( const std::string& reason = "" )
00058 { m_state.enabled( false ); m_state.logoutReason( reason ); }
00059 bool isEnabled()
00060 { return m_state.enabled(); }
00061
00062 bool sentLogon() { return m_state.sentLogon(); }
00063 bool sentLogout() { return m_state.sentLogout(); }
00064 bool receivedLogon() { return m_state.receivedLogon(); }
00065 bool isLoggedOn() { return receivedLogon() && sentLogon(); }
00066 void reset() throw( IOException )
00067 { generateLogout(); disconnect(); m_state.reset(); }
00068 void refresh() throw( IOException )
00069 { m_state.refresh(); }
00070 void setNextSenderMsgSeqNum( int num ) throw( IOException )
00071 { m_state.setNextSenderMsgSeqNum( num ); }
00072 void setNextTargetMsgSeqNum( int num ) throw( IOException )
00073 { m_state.setNextTargetMsgSeqNum( num ); }
00074
00075 const SessionID& getSessionID() const
00076 { return m_sessionID; }
00077 void setDataDictionaryProvider( const DataDictionaryProvider& dataDictionaryProvider )
00078 { m_dataDictionaryProvider = dataDictionaryProvider; }
00079 const DataDictionaryProvider& getDataDictionaryProvider() const
00080 { return m_dataDictionaryProvider; }
00081
00082 static bool sendToTarget( Message& message,
00083 const std::string& qualifier = "" )
00084 throw( SessionNotFound );
00085 static bool sendToTarget( Message& message, const SessionID& sessionID )
00086 throw( SessionNotFound );
00087 static bool sendToTarget( Message&,
00088 const SenderCompID& senderCompID,
00089 const TargetCompID& targetCompID,
00090 const std::string& qualifier = "" )
00091 throw( SessionNotFound );
00092 static bool sendToTarget( Message& message,
00093 const std::string& senderCompID,
00094 const std::string& targetCompID,
00095 const std::string& qualifier = "" )
00096 throw( SessionNotFound );
00097
00098 static std::set<SessionID> getSessions();
00099 static bool doesSessionExist( const SessionID& );
00100 static Session* lookupSession( const SessionID& );
00101 static Session* lookupSession( const std::string&, bool reverse = false );
00102 static bool isSessionRegistered( const SessionID& );
00103 static Session* registerSession( const SessionID& );
00104 static void unregisterSession( const SessionID& );
00105
00106 static int numSessions();
00107
00108 bool isSessionTime()
00109 { return m_sessionTime.isInRange(); }
00110 bool isLogonTime()
00111 { return m_logonTime.isInRange(); }
00112 bool isInitiator()
00113 { return m_state.initiate(); }
00114 bool isAcceptor()
00115 { return !m_state.initiate(); }
00116
00117 const TimeRange& getLogonTime()
00118 { return m_logonTime; }
00119 void setLogonTime( const TimeRange& value )
00120 { m_logonTime = value; }
00121
00122 const std::string& getSenderDefaultApplVerID()
00123 { return m_senderDefaultApplVerID; }
00124 void setSenderDefaultApplVerID( const std::string& senderDefaultApplVerID )
00125 { m_senderDefaultApplVerID = senderDefaultApplVerID; }
00126
00127 const std::string& getTargetDefaultApplVerID()
00128 { return m_targetDefaultApplVerID; }
00129 void setTargetDefaultApplVerID( const std::string& targetDefaultApplVerID )
00130 { m_targetDefaultApplVerID = targetDefaultApplVerID; }
00131
00132 bool getSendRedundantResendRequests()
00133 { return m_sendRedundantResendRequests; }
00134 void setSendRedundantResendRequests ( bool value )
00135 { m_sendRedundantResendRequests = value; }
00136
00137 bool getCheckCompId()
00138 { return m_checkCompId; }
00139 void setCheckCompId ( bool value )
00140 { m_checkCompId = value; }
00141
00142 bool getCheckLatency()
00143 { return m_checkLatency; }
00144 void setCheckLatency ( bool value )
00145 { m_checkLatency = value; }
00146
00147 int getMaxLatency()
00148 { return m_maxLatency; }
00149 void setMaxLatency ( int value )
00150 { m_maxLatency = value; }
00151
00152 int getLogonTimeout()
00153 { return m_state.logonTimeout(); }
00154 void setLogonTimeout ( int value )
00155 { m_state.logonTimeout( value ); }
00156
00157 int getLogoutTimeout()
00158 { return m_state.logoutTimeout(); }
00159 void setLogoutTimeout ( int value )
00160 { m_state.logoutTimeout( value ); }
00161
00162 bool getResetOnLogon()
00163 { return m_resetOnLogon; }
00164 void setResetOnLogon ( bool value )
00165 { m_resetOnLogon = value; }
00166
00167 bool getResetOnLogout()
00168 { return m_resetOnLogout; }
00169 void setResetOnLogout ( bool value )
00170 { m_resetOnLogout = value; }
00171
00172 bool getResetOnDisconnect()
00173 { return m_resetOnDisconnect; }
00174 void setResetOnDisconnect( bool value )
00175 { m_resetOnDisconnect = value; }
00176
00177 bool getRefreshOnLogon()
00178 { return m_refreshOnLogon; }
00179 void setRefreshOnLogon( bool value )
00180 { m_refreshOnLogon = value; }
00181
00182 bool getMillisecondsInTimeStamp()
00183 { return m_millisecondsInTimeStamp; }
00184 void setMillisecondsInTimeStamp ( bool value )
00185 { m_millisecondsInTimeStamp = value; }
00186
00187 bool getPersistMessages()
00188 { return m_persistMessages; }
00189 void setPersistMessages ( bool value )
00190 { m_persistMessages = value; }
00191
00192 void setResponder( Responder* pR )
00193 {
00194 if ( !checkSessionTime() )
00195 reset();
00196 m_pResponder = pR;
00197 }
00198
00199 bool send( Message& );
00200 void next();
00201 void next( const std::string&, bool queued = false );
00202 void next( const Message&, bool queued = false );
00203 void disconnect();
00204
00205 long getExpectedSenderNum() { return m_state.getNextSenderMsgSeqNum(); }
00206 long getExpectedTargetNum() { return m_state.getNextTargetMsgSeqNum(); }
00207
00208 Log* getLog() { return &m_state; }
00209 const MessageStore* getStore() { return &m_state; }
00210
00211 private:
00212 typedef std::map < SessionID, Session* > Sessions;
00213 typedef std::set < SessionID > SessionIDs;
00214
00215 static bool addSession( Session& );
00216 static void removeSession( Session& );
00217
00218 bool send( const std::string& );
00219 bool sendRaw( Message&, int msgSeqNum = 0 );
00220 bool resend( Message& message );
00221 void persist( const Message&, const std::string& ) throw ( IOException );
00222
00223 void insertSendingTime( Header& );
00224 void insertOrigSendingTime( Header&,
00225 const UtcTimeStamp& when = UtcTimeStamp () );
00226 void fill( Header& );
00227
00228 bool isGoodTime( const SendingTime& sendingTime )
00229 {
00230 if ( !m_checkLatency ) return true;
00231 UtcTimeStamp now;
00232 return labs( now - sendingTime ) <= m_maxLatency;
00233 }
00234 bool checkSessionTime()
00235 {
00236 UtcTimeStamp creationTime = m_state.getCreationTime();
00237 if( m_sessionTime.useLocalTime() )
00238 {
00239 return m_sessionTime.isInSameRange
00240 ( LocalTimeStamp(), LocalTimeStamp(creationTime.getTimeT()) );
00241 }
00242 else
00243 {
00244 return m_sessionTime.isInSameRange( UtcTimeStamp(), creationTime );
00245 }
00246 }
00247 bool isTargetTooHigh( const MsgSeqNum& msgSeqNum )
00248 { return msgSeqNum > ( m_state.getNextTargetMsgSeqNum() ); }
00249 bool isTargetTooLow( const MsgSeqNum& msgSeqNum )
00250 { return msgSeqNum < ( m_state.getNextTargetMsgSeqNum() ); }
00251 bool isCorrectCompID( const SenderCompID& senderCompID,
00252 const TargetCompID& targetCompID )
00253 {
00254 if( !m_checkCompId ) return true;
00255
00256 return
00257 m_sessionID.getSenderCompID().getValue() == targetCompID.getValue()
00258 && m_sessionID.getTargetCompID().getValue() == senderCompID.getValue();
00259 }
00260 bool shouldSendReset();
00261
00262 bool validLogonState( const MsgType& msgType );
00263 void fromCallback( const MsgType& msgType, const Message& msg,
00264 const SessionID& sessionID );
00265
00266 void doBadTime( const Message& msg );
00267 void doBadCompID( const Message& msg );
00268 bool doPossDup( const Message& msg );
00269 bool doTargetTooLow( const Message& msg );
00270 void doTargetTooHigh( const Message& msg );
00271 void nextQueued();
00272 bool nextQueued( int num );
00273
00274 void nextLogon( const Message& );
00275 void nextHeartbeat( const Message& );
00276 void nextTestRequest( const Message& );
00277 void nextLogout( const Message& );
00278 void nextReject( const Message& );
00279 void nextSequenceReset( const Message& );
00280 void nextResendRequest( const Message& );
00281
00282 void generateLogon();
00283 void generateLogon( const Message& );
00284 void generateResendRequest( const BeginString&, const MsgSeqNum& );
00285 void generateSequenceReset( int, int );
00286 void generateHeartbeat();
00287 void generateHeartbeat( const Message& );
00288 void generateTestRequest( const std::string& );
00289 void generateReject( const Message&, int err, int field = 0 );
00290 void generateReject( const Message&, const std::string& );
00291 void generateBusinessReject( const Message&, int err, int field = 0 );
00292 void generateLogout( const std::string& text = "" );
00293
00294 void populateRejectReason( Message&, int field, const std::string& );
00295 void populateRejectReason( Message&, const std::string& );
00296
00297 bool verify( const Message& msg,
00298 bool checkTooHigh = true, bool checkTooLow = true );
00299
00300 bool set( int s, const Message& m );
00301 bool get( int s, Message& m ) const;
00302
00303 Application& m_application;
00304 SessionID m_sessionID;
00305 TimeRange m_sessionTime;
00306 TimeRange m_logonTime;
00307
00308 std::string m_senderDefaultApplVerID;
00309 std::string m_targetDefaultApplVerID;
00310 bool m_sendRedundantResendRequests;
00311 bool m_checkCompId;
00312 bool m_checkLatency;
00313 int m_maxLatency;
00314 bool m_resetOnLogon;
00315 bool m_resetOnLogout;
00316 bool m_resetOnDisconnect;
00317 bool m_refreshOnLogon;
00318 bool m_millisecondsInTimeStamp;
00319 bool m_persistMessages;
00320
00321 SessionState m_state;
00322 DataDictionaryProvider m_dataDictionaryProvider;
00323 MessageStoreFactory& m_messageStoreFactory;
00324 LogFactory* m_pLogFactory;
00325 Responder* m_pResponder;
00326 Mutex m_mutex;
00327
00328 static Sessions s_sessions;
00329 static SessionIDs s_sessionIDs;
00330 static Sessions s_registered;
00331 static Mutex s_mutex;
00332
00333 };
00334 }
00335
00336 #endif //FIX_SESSION_H