Initiator.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_INITIATOR_H
  21. #define FIX_INITIATOR_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "Application.h"
  26. #include "MessageStore.h"
  27. #include "Log.h"
  28. #include "Responder.h"
  29. #include "SessionSettings.h"
  30. #include "Exceptions.h"
  31. #include "Mutex.h"
  32. #include "Session.h"
  33. #include <set>
  34. #include <map>
  35. #include <string>
  36. namespace FIX
  37. {
  38. class Client;
  39. /**
  40. * Base for classes which act as an initiator for establishing connections.
  41. *
  42. * Most users will not need to implement one of these. The default
  43. * SocketInitiator implementation will be used in most cases.
  44. */
  45. class Initiator
  46. {
  47. public:
  48. Initiator( Application&, MessageStoreFactory&,
  49. const SessionSettings& ) throw( ConfigError );
  50. Initiator( Application&, MessageStoreFactory&,
  51. const SessionSettings&, LogFactory& ) throw( ConfigError );
  52. virtual ~Initiator();
  53. /// Start initiator.
  54. void start() throw ( ConfigError, RuntimeError );
  55. /// Block on the initiator
  56. void block() throw ( ConfigError, RuntimeError );
  57. /// Poll the initiator
  58. bool poll( double timeout = 0.0 ) throw ( ConfigError, RuntimeError );
  59. /// Stop initiator.
  60. void stop( bool force = false );
  61. /// Check to see if any sessions are currently logged on
  62. bool isLoggedOn();
  63. Session* getSession( const SessionID& sessionID, Responder& );
  64. const std::set<SessionID>& getSessions() const { return m_sessionIDs; }
  65. Session* getSession( const SessionID& sessionID ) const;
  66. const Dictionary* const getSessionSettings( const SessionID& sessionID ) const;
  67. bool has( const SessionID& id )
  68. { return m_sessions.find( id ) != m_sessions.end(); }
  69. bool isStopped() { return m_stop; }
  70. public:
  71. Application& getApplication() { return m_application; }
  72. MessageStoreFactory& getMessageStoreFactory()
  73. { return m_messageStoreFactory; }
  74. Log* getLog()
  75. {
  76. if( m_pLog ) return m_pLog;
  77. return &m_nullLog;
  78. }
  79. protected:
  80. void setPending( const SessionID& );
  81. void setConnected( const SessionID& );
  82. void setDisconnected( const SessionID& );
  83. bool isPending( const SessionID& );
  84. bool isConnected( const SessionID& );
  85. bool isDisconnected( const SessionID& );
  86. void connect();
  87. private:
  88. void initialize() throw ( ConfigError );
  89. /// Implemented to configure acceptor
  90. virtual void onConfigure( const SessionSettings& ) throw ( ConfigError ) {};
  91. /// Implemented to initialize initiator
  92. virtual void onInitialize( const SessionSettings& ) throw ( RuntimeError ) {};
  93. /// Implemented to start connecting to targets.
  94. virtual void onStart() = 0;
  95. /// Implemented to connect and poll for events.
  96. virtual bool onPoll( double timeout ) = 0;
  97. /// Implemented to stop a running initiator.
  98. virtual void onStop() = 0;
  99. /// Implemented to connect a session to its target.
  100. virtual void doConnect( const SessionID&, const Dictionary& ) = 0;
  101. static THREAD_PROC startThread( void* p );
  102. typedef std::set < SessionID > SessionIDs;
  103. typedef std::map < SessionID, int > SessionState;
  104. typedef std::map < SessionID, Session* > Sessions;
  105. Sessions m_sessions;
  106. SessionIDs m_sessionIDs;
  107. SessionIDs m_pending;
  108. SessionIDs m_connected;
  109. SessionIDs m_disconnected;
  110. SessionState m_sessionState;
  111. thread_id m_threadid;
  112. Application& m_application;
  113. MessageStoreFactory& m_messageStoreFactory;
  114. SessionSettings m_settings;
  115. LogFactory* m_pLogFactory;
  116. Log* m_pLog;
  117. NullLog m_nullLog;
  118. bool m_firstPoll;
  119. bool m_stop;
  120. Mutex m_mutex;
  121. };
  122. /*! @} */
  123. }
  124. #endif // FIX_INITIATOR_H