Acceptor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_ACCEPTOR_H
  21. #define FIX_ACCEPTOR_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 <map>
  32. #include <string>
  33. namespace FIX
  34. {
  35. class Client;
  36. class Session;
  37. /**
  38. * Base for classes which act as an acceptor for incoming connections.
  39. *
  40. * Most users will not need to implement one of these. The default
  41. * SocketAcceptor implementation will be used in most cases.
  42. */
  43. class Acceptor
  44. {
  45. public:
  46. Acceptor( Application&, MessageStoreFactory&,
  47. const SessionSettings& ) throw( ConfigError );
  48. Acceptor( Application&, MessageStoreFactory&,
  49. const SessionSettings&, LogFactory& ) throw( ConfigError );
  50. virtual ~Acceptor();
  51. Log* getLog()
  52. {
  53. if( m_pLog ) return m_pLog;
  54. return &m_nullLog;
  55. }
  56. /// Start acceptor.
  57. void start() throw ( ConfigError, RuntimeError );
  58. /// Block on the acceptor
  59. void block() throw ( ConfigError, RuntimeError );
  60. /// Poll the acceptor
  61. bool poll( double timeout = 0.0 ) throw ( ConfigError, RuntimeError );
  62. /// Stop acceptor.
  63. void stop( bool force = false );
  64. /// Check to see if any sessions are currently logged on
  65. bool isLoggedOn();
  66. Session* getSession( const std::string& msg, Responder& );
  67. const std::set<SessionID>& getSessions() const { return m_sessionIDs; }
  68. Session* getSession( const SessionID& sessionID ) const;
  69. const Dictionary* const getSessionSettings( const SessionID& sessionID ) const;
  70. bool has( const SessionID& id )
  71. { return m_sessions.find( id ) != m_sessions.end(); }
  72. bool isStopped() { return m_stop; }
  73. Application& getApplication() { return m_application; }
  74. MessageStoreFactory& getMessageStoreFactory()
  75. { return m_messageStoreFactory; }
  76. private:
  77. void initialize() throw ( ConfigError );
  78. /// Implemented to configure acceptor
  79. virtual void onConfigure( const SessionSettings& ) throw ( ConfigError ) {};
  80. /// Implemented to initialize acceptor
  81. virtual void onInitialize( const SessionSettings& ) throw ( RuntimeError ) {};
  82. /// Implemented to start listening for connections.
  83. virtual void onStart() = 0;
  84. /// Implemented to connect and poll for events.
  85. virtual bool onPoll( double second ) = 0;
  86. /// Implemented to stop a running acceptor.
  87. virtual void onStop() = 0;
  88. static THREAD_PROC startThread( void* p );
  89. typedef std::set < SessionID > SessionIDs;
  90. typedef std::map < SessionID, Session* > Sessions;
  91. thread_id m_threadid;
  92. Sessions m_sessions;
  93. SessionIDs m_sessionIDs;
  94. Application& m_application;
  95. MessageStoreFactory& m_messageStoreFactory;
  96. SessionSettings m_settings;
  97. LogFactory* m_pLogFactory;
  98. Log* m_pLog;
  99. NullLog m_nullLog;
  100. bool m_firstPoll;
  101. bool m_stop;
  102. };
  103. /*! @} */
  104. }
  105. #endif // FIX_ACCEPTOR_H