Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# Session.h

Go to the documentation of this file.

    00001 /* -*- C++ -*- */
    00002
    00003 /****************************************************************************
    00004 ** Copyright (c) quickfixengine.org  All rights reserved.
    00005 **
    00006 ** This file is part of the QuickFIX FIX Engine
    00007 **
    00008 ** This file may be distributed under the terms of the quickfixengine.org
    00009 ** license as defined by quickfixengine.org and appearing in the file
    00010 ** LICENSE included in the packaging of this file.
    00011 **
    00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00014 **
    00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
    00016 **
    00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
    00018 ** not clear to you.
    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(const DateTime& time)
    00109     { return m_sessionTime.isInRange(time); }
    00110   bool isLogonTime(const DateTime& time)
    00111     { return m_logonTime.isInRange(time); }
    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     m_pResponder = pR;
    00195   }
    00196
    00197   bool send( Message& );
    00198   void next();
    00199   void next( const UtcTimeStamp& timeStamp );
    00200   void next( const std::string&, const UtcTimeStamp& timeStamp, bool queued = false );
    00201   void next( const Message&, const UtcTimeStamp& timeStamp, bool queued = false );
    00202   void disconnect();
    00203
    00204   long getExpectedSenderNum() { return m_state.getNextSenderMsgSeqNum(); }
    00205   long getExpectedTargetNum() { return m_state.getNextTargetMsgSeqNum(); }
    00206
    00207   Log* getLog() { return &m_state; }
    00208   const MessageStore* getStore() { return &m_state; }
    00209
    00210 private:
    00211   typedef std::map < SessionID, Session* > Sessions;
    00212   typedef std::set < SessionID > SessionIDs;
    00213
    00214   static bool addSession( Session& );
    00215   static void removeSession( Session& );
    00216
    00217   bool send( const std::string& );
    00218   bool sendRaw( Message&, int msgSeqNum = 0 );
    00219   bool resend( Message& message );
    00220   void persist( const Message&, const std::string& ) throw ( IOException );
    00221
    00222   void insertSendingTime( Header& );
    00223   void insertOrigSendingTime( Header&,
    00224                               const UtcTimeStamp& when = UtcTimeStamp () );
    00225   void fill( Header& );
    00226
    00227   bool isGoodTime( const SendingTime& sendingTime )
    00228   {
    00229     if ( !m_checkLatency ) return true;
    00230     UtcTimeStamp now;
    00231     return labs( now - sendingTime ) <= m_maxLatency;
    00232   }
    00233   bool checkSessionTime( const UtcTimeStamp& timeStamp )
    00234   {
    00235     UtcTimeStamp creationTime = m_state.getCreationTime();
    00236     return m_sessionTime.isInSameRange( timeStamp, creationTime );
    00237   }
    00238   bool isTargetTooHigh( const MsgSeqNum& msgSeqNum )
    00239   { return msgSeqNum > ( m_state.getNextTargetMsgSeqNum() ); }
    00240   bool isTargetTooLow( const MsgSeqNum& msgSeqNum )
    00241   { return msgSeqNum < ( m_state.getNextTargetMsgSeqNum() ); }
    00242   bool isCorrectCompID( const SenderCompID& senderCompID,
    00243                         const TargetCompID& targetCompID )
    00244   {
    00245     if( !m_checkCompId ) return true;
    00246
    00247     return
    00248       m_sessionID.getSenderCompID().getValue() == targetCompID.getValue()
    00249       && m_sessionID.getTargetCompID().getValue() == senderCompID.getValue();
    00250   }
    00251   bool shouldSendReset();
    00252
    00253   bool validLogonState( const MsgType& msgType );
    00254   void fromCallback( const MsgType& msgType, const Message& msg,
    00255                      const SessionID& sessionID );
    00256
    00257   void doBadTime( const Message& msg );
    00258   void doBadCompID( const Message& msg );
    00259   bool doPossDup( const Message& msg );
    00260   bool doTargetTooLow( const Message& msg );
    00261   void doTargetTooHigh( const Message& msg );
    00262   void nextQueued( const UtcTimeStamp& timeStamp );
    00263   bool nextQueued( int num, const UtcTimeStamp& timeStamp );
    00264
    00265   void nextLogon( const Message&, const UtcTimeStamp& timeStamp );
    00266   void nextHeartbeat( const Message&, const UtcTimeStamp& timeStamp );
    00267   void nextTestRequest( const Message&, const UtcTimeStamp& timeStamp );
    00268   void nextLogout( const Message&, const UtcTimeStamp& timeStamp );
    00269   void nextReject( const Message&, const UtcTimeStamp& timeStamp );
    00270   void nextSequenceReset( const Message&, const UtcTimeStamp& timeStamp );
    00271   void nextResendRequest( const Message&, const UtcTimeStamp& timeStamp );
    00272
    00273   void generateLogon();
    00274   void generateLogon( const Message& );
    00275   void generateResendRequest( const BeginString&, const MsgSeqNum& );
    00276   void generateSequenceReset( int, int );
    00277   void generateHeartbeat();
    00278   void generateHeartbeat( const Message& );
    00279   void generateTestRequest( const std::string& );
    00280   void generateReject( const Message&, int err, int field = 0 );
    00281   void generateReject( const Message&, const std::string& );
    00282   void generateBusinessReject( const Message&, int err, int field = 0 );
    00283   void generateLogout( const std::string& text = "" );
    00284
    00285   void populateRejectReason( Message&, int field, const std::string& );
    00286   void populateRejectReason( Message&, const std::string& );
    00287
    00288   bool verify( const Message& msg,
    00289                bool checkTooHigh = true, bool checkTooLow = true );
    00290
    00291   bool set( int s, const Message& m );
    00292   bool get( int s, Message& m ) const;
    00293
    00294   Application& m_application;
    00295   SessionID m_sessionID;
    00296   TimeRange m_sessionTime;
    00297   TimeRange m_logonTime;
    00298
    00299   std::string m_senderDefaultApplVerID;
    00300   std::string m_targetDefaultApplVerID;
    00301   bool m_sendRedundantResendRequests;
    00302   bool m_checkCompId;
    00303   bool m_checkLatency;
    00304   int m_maxLatency;
    00305   bool m_resetOnLogon;
    00306   bool m_resetOnLogout;
    00307   bool m_resetOnDisconnect;
    00308   bool m_refreshOnLogon;
    00309   bool m_millisecondsInTimeStamp;
    00310   bool m_persistMessages;
    00311
    00312   SessionState m_state;
    00313   DataDictionaryProvider m_dataDictionaryProvider;
    00314   MessageStoreFactory& m_messageStoreFactory;
    00315   LogFactory* m_pLogFactory;
    00316   Responder* m_pResponder;
    00317   Mutex m_mutex;
    00318
    00319   static Sessions s_sessions;
    00320   static SessionIDs s_sessionIDs;
    00321   static Sessions s_registered;
    00322   static Mutex s_mutex;
    00323
    00324 };
    00325 }
    00326
    00327 #endif //FIX_SESSION_H