Session.h
Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /****************************************************************************
00004 ** Copyright (c) 2001-2014
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   virtual ~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 size_t numSessions();
00107 
00108   bool isSessionTime(const UtcTimeStamp& time)
00109     { return m_sessionTime.isInRange(time); }
00110   bool isLogonTime(const UtcTimeStamp& 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   bool getValidateLengthAndChecksum()
00193     { return m_validateLengthAndChecksum; }
00194   void setValidateLengthAndChecksum ( bool value )
00195     { m_validateLengthAndChecksum = value; }
00196 
00197   void setResponder( Responder* pR )
00198   {
00199     if( !checkSessionTime(UtcTimeStamp()) )
00200       reset();
00201     m_pResponder = pR;
00202   }
00203 
00204   bool send( Message& );
00205   void next();
00206   void next( const UtcTimeStamp& timeStamp );
00207   void next( const std::string&, const UtcTimeStamp& timeStamp, bool queued = false );
00208   void next( const Message&, const UtcTimeStamp& timeStamp, bool queued = false );
00209   void disconnect();
00210 
00211   int getExpectedSenderNum() { return m_state.getNextSenderMsgSeqNum(); }
00212   int getExpectedTargetNum() { return m_state.getNextTargetMsgSeqNum(); }
00213 
00214   Log* getLog() { return &m_state; }
00215   const MessageStore* getStore() { return &m_state; }
00216 
00217 private:
00218   typedef std::map < SessionID, Session* > Sessions;
00219   typedef std::set < SessionID > SessionIDs;
00220 
00221   static bool addSession( Session& );
00222   static void removeSession( Session& );
00223 
00224   bool send( const std::string& );
00225   bool sendRaw( Message&, int msgSeqNum = 0 );
00226   bool resend( Message& message );
00227   void persist( const Message&, const std::string& ) throw ( IOException );
00228 
00229   void insertSendingTime( Header& );
00230   void insertOrigSendingTime( Header&,
00231                               const UtcTimeStamp& when = UtcTimeStamp () );
00232   void fill( Header& );
00233 
00234   bool isGoodTime( const SendingTime& sendingTime )
00235   {
00236     if ( !m_checkLatency ) return true;
00237     UtcTimeStamp now;
00238     return labs( now - sendingTime ) <= m_maxLatency;
00239   }
00240   bool checkSessionTime( const UtcTimeStamp& timeStamp )
00241   {
00242     UtcTimeStamp creationTime = m_state.getCreationTime();
00243     return m_sessionTime.isInSameRange( timeStamp, creationTime );
00244   }
00245   bool isTargetTooHigh( const MsgSeqNum& msgSeqNum )
00246   { return msgSeqNum > ( m_state.getNextTargetMsgSeqNum() ); }
00247   bool isTargetTooLow( const MsgSeqNum& msgSeqNum )
00248   { return msgSeqNum < ( m_state.getNextTargetMsgSeqNum() ); }
00249   bool isCorrectCompID( const SenderCompID& senderCompID,
00250                         const TargetCompID& targetCompID )
00251   {
00252     if( !m_checkCompId ) return true;
00253 
00254     return
00255       m_sessionID.getSenderCompID().getValue() == targetCompID.getValue()
00256       && m_sessionID.getTargetCompID().getValue() == senderCompID.getValue();
00257   }
00258   bool shouldSendReset();
00259 
00260   bool validLogonState( const MsgType& msgType );
00261   void fromCallback( const MsgType& msgType, const Message& msg,
00262                      const SessionID& sessionID );
00263 
00264   void doBadTime( const Message& msg );
00265   void doBadCompID( const Message& msg );
00266   bool doPossDup( const Message& msg );
00267   bool doTargetTooLow( const Message& msg );
00268   void doTargetTooHigh( const Message& msg );
00269   void nextQueued( const UtcTimeStamp& timeStamp );
00270   bool nextQueued( int num, const UtcTimeStamp& timeStamp );
00271 
00272   void nextLogon( const Message&, const UtcTimeStamp& timeStamp );
00273   void nextHeartbeat( const Message&, const UtcTimeStamp& timeStamp );
00274   void nextTestRequest( const Message&, const UtcTimeStamp& timeStamp );
00275   void nextLogout( const Message&, const UtcTimeStamp& timeStamp );
00276   void nextReject( const Message&, const UtcTimeStamp& timeStamp );
00277   void nextSequenceReset( const Message&, const UtcTimeStamp& timeStamp );
00278   void nextResendRequest( const Message&, const UtcTimeStamp& timeStamp );
00279 
00280   void generateLogon();
00281   void generateLogon( const Message& );
00282   void generateResendRequest( const BeginString&, const MsgSeqNum& );
00283   void generateSequenceReset( int, int );
00284   void generateHeartbeat();
00285   void generateHeartbeat( const Message& );
00286   void generateTestRequest( const std::string& );
00287   void generateReject( const Message&, int err, int field = 0 );
00288   void generateReject( const Message&, const std::string& );
00289   void generateBusinessReject( const Message&, int err, int field = 0 );
00290   void generateLogout( const std::string& text = "" );
00291 
00292   void populateRejectReason( Message&, int field, const std::string& );
00293   void populateRejectReason( Message&, const std::string& );
00294 
00295   bool verify( const Message& msg,
00296                bool checkTooHigh = true, bool checkTooLow = true );
00297 
00298   bool set( int s, const Message& m );
00299   bool get( int s, Message& m ) const;
00300 
00301   Application& m_application;
00302   SessionID m_sessionID;
00303   TimeRange m_sessionTime;
00304   TimeRange m_logonTime;
00305 
00306   std::string m_senderDefaultApplVerID;
00307   std::string m_targetDefaultApplVerID;
00308   bool m_sendRedundantResendRequests;
00309   bool m_checkCompId;
00310   bool m_checkLatency;
00311   int m_maxLatency;
00312   bool m_resetOnLogon;
00313   bool m_resetOnLogout;
00314   bool m_resetOnDisconnect;
00315   bool m_refreshOnLogon;
00316   bool m_millisecondsInTimeStamp;
00317   bool m_persistMessages;
00318   bool m_validateLengthAndChecksum;
00319 
00320   SessionState m_state;
00321   DataDictionaryProvider m_dataDictionaryProvider;
00322   MessageStoreFactory& m_messageStoreFactory;
00323   LogFactory* m_pLogFactory;
00324   Responder* m_pResponder;
00325   Mutex m_mutex;
00326 
00327   static Sessions s_sessions;
00328   static SessionIDs s_sessionIDs;
00329   static Sessions s_registered;
00330   static Mutex s_mutex;
00331 
00332 };
00333 }
00334 
00335 #endif //FIX_SESSION_H

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001