Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
FIX::Acceptor Class Referenceabstract

Base for classes which act as an acceptor for incoming connections. More...

#include <Acceptor.h>

Inheritance diagram for FIX::Acceptor:
Inheritance graph
[legend]
Collaboration diagram for FIX::Acceptor:
Collaboration graph
[legend]

Public Member Functions

 Acceptor (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
 
 Acceptor (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
 
virtual ~Acceptor ()
 
LoggetLog ()
 
void start () throw ( ConfigError, RuntimeError )
 Start acceptor. More...
 
void block () throw ( ConfigError, RuntimeError )
 Block on the acceptor. More...
 
bool poll (double timeout=0.0) throw ( ConfigError, RuntimeError )
 Poll the acceptor. More...
 
void stop (bool force=false)
 Stop acceptor. More...
 
bool isLoggedOn ()
 Check to see if any sessions are currently logged on. More...
 
SessiongetSession (const std::string &msg, Responder &)
 
const std::set< SessionID > & getSessions () const
 
SessiongetSession (const SessionID &sessionID) const
 
const Dictionary *const getSessionSettings (const SessionID &sessionID) const
 
bool has (const SessionID &id)
 
bool isStopped ()
 
ApplicationgetApplication ()
 
MessageStoreFactorygetMessageStoreFactory ()
 

Private Types

typedef std::set< SessionIDSessionIDs
 
typedef std::map< SessionID,
Session * > 
Sessions
 

Private Member Functions

void initialize () throw ( ConfigError )
 
virtual void onConfigure (const SessionSettings &) throw ( ConfigError )
 Implemented to configure acceptor. More...
 
virtual void onInitialize (const SessionSettings &) throw ( RuntimeError )
 Implemented to initialize acceptor. More...
 
virtual void onStart ()=0
 Implemented to start listening for connections. More...
 
virtual bool onPoll (double second)=0
 Implemented to connect and poll for events. More...
 
virtual void onStop ()=0
 Implemented to stop a running acceptor. More...
 

Static Private Member Functions

static THREAD_PROC startThread (void *p)
 

Private Attributes

thread_id m_threadid
 
Sessions m_sessions
 
SessionIDs m_sessionIDs
 
Applicationm_application
 
MessageStoreFactorym_messageStoreFactory
 
SessionSettings m_settings
 
LogFactorym_pLogFactory
 
Logm_pLog
 
NullLog m_nullLog
 
bool m_firstPoll
 
bool m_stop
 

Detailed Description

Base for classes which act as an acceptor for incoming connections.

Most users will not need to implement one of these. The default SocketAcceptor implementation will be used in most cases.

Definition at line 49 of file Acceptor.h.

Member Typedef Documentation

typedef std::set< SessionID > FIX::Acceptor::SessionIDs
private

Definition at line 109 of file Acceptor.h.

typedef std::map< SessionID, Session* > FIX::Acceptor::Sessions
private

Definition at line 110 of file Acceptor.h.

Constructor & Destructor Documentation

FIX::Acceptor::Acceptor ( Application application,
MessageStoreFactory messageStoreFactory,
const SessionSettings settings 
)
throw (ConfigError
)

Definition at line 36 of file Acceptor.cpp.

40  : m_threadid( 0 ),
41  m_application( application ),
42  m_messageStoreFactory( messageStoreFactory ),
43  m_settings( settings ),
44  m_pLogFactory( 0 ),
45  m_pLog( 0 ),
46  m_firstPoll( true ),
47  m_stop( true )
48 {
49  initialize();
50 }
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
Log * m_pLog
Definition: Acceptor.h:119
Application & m_application
Definition: Acceptor.h:115
void initialize()
Definition: Acceptor.cpp:69
bool m_firstPoll
Definition: Acceptor.h:121
SessionSettings m_settings
Definition: Acceptor.h:117
thread_id m_threadid
Definition: Acceptor.h:112
FIX::Acceptor::Acceptor ( Application application,
MessageStoreFactory messageStoreFactory,
const SessionSettings settings,
LogFactory logFactory 
)
throw (ConfigError
)

Definition at line 52 of file Acceptor.cpp.

57 : m_threadid( 0 ),
58  m_application( application ),
59  m_messageStoreFactory( messageStoreFactory ),
60  m_settings( settings ),
61  m_pLogFactory( &logFactory ),
62  m_pLog( logFactory.create() ),
63  m_firstPoll( true ),
64  m_stop( true )
65 {
66  initialize();
67 }
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
Log * m_pLog
Definition: Acceptor.h:119
Application & m_application
Definition: Acceptor.h:115
void initialize()
Definition: Acceptor.cpp:69
bool m_firstPoll
Definition: Acceptor.h:121
SessionSettings m_settings
Definition: Acceptor.h:117
thread_id m_threadid
Definition: Acceptor.h:112
FIX::Acceptor::~Acceptor ( )
virtual

Definition at line 93 of file Acceptor.cpp.

References FIX::LogFactory::destroy(), m_pLog, m_pLogFactory, and m_sessions.

94 {
95  Sessions::iterator i;
96  for ( i = m_sessions.begin(); i != m_sessions.end(); ++i )
97  delete i->second;
98 
99  if( m_pLogFactory && m_pLog )
101 }
virtual void destroy(Log *)=0
Sessions m_sessions
Definition: Acceptor.h:113
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
Log * m_pLog
Definition: Acceptor.h:119

Member Function Documentation

void FIX::Acceptor::block ( )
throw ( ConfigError,
RuntimeError
)

Block on the acceptor.

Definition at line 170 of file Acceptor.cpp.

References m_settings, m_stop, onConfigure(), onInitialize(), and startThread().

171 {
172  m_stop = false;
175 
176  startThread(this);
177 }
static THREAD_PROC startThread(void *p)
Definition: Acceptor.cpp:242
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition: Acceptor.h:97
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:99
SessionSettings m_settings
Definition: Acceptor.h:117
Application& FIX::Acceptor::getApplication ( )
inline

Definition at line 89 of file Acceptor.h.

References m_application.

89 { return m_application; }
Application & m_application
Definition: Acceptor.h:115
Log* FIX::Acceptor::getLog ( )
inline

Definition at line 59 of file Acceptor.h.

References m_nullLog, and m_pLog.

Referenced by FIX::SocketAcceptor::onConnect(), and FIX::SocketConnection::read().

60  {
61  if( m_pLog ) return m_pLog;
62  return &m_nullLog;
63  }
Log * m_pLog
Definition: Acceptor.h:119
NullLog m_nullLog
Definition: Acceptor.h:120
MessageStoreFactory& FIX::Acceptor::getMessageStoreFactory ( )
inline

Definition at line 90 of file Acceptor.h.

References m_messageStoreFactory.

91  { return m_messageStoreFactory; }
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
Session * FIX::Acceptor::getSession ( const std::string &  msg,
Responder responder 
)

Definition at line 104 of file Acceptor.cpp.

References FIX::FIELD::BeginString, FIX::FieldMap::getField(), FIX::Message::getHeader(), FIX::FIELD::MsgType, FIX::FIELD::SenderCompID, FIX::Message::setStringHeader(), and FIX::FIELD::TargetCompID.

Referenced by FIX::SocketConnection::read().

105 {
106  Message message;
107  if ( !message.setStringHeader( msg ) )
108  return 0;
109 
110  BeginString beginString;
111  SenderCompID clSenderCompID;
112  TargetCompID clTargetCompID;
113  MsgType msgType;
114  try
115  {
116  message.getHeader().getField( beginString );
117  message.getHeader().getField( clSenderCompID );
118  message.getHeader().getField( clTargetCompID );
119  message.getHeader().getField( msgType );
120  if ( msgType != "A" ) return 0;
121 
122  SenderCompID senderCompID( clTargetCompID );
123  TargetCompID targetCompID( clSenderCompID );
124  SessionID sessionID( beginString, senderCompID, targetCompID );
125 
126  Sessions::iterator i = m_sessions.find( sessionID );
127  if ( i != m_sessions.end() )
128  {
129  i->second->setResponder( &responder );
130  return i->second;
131  }
132  }
133  catch ( FieldNotFound& ) {}
134  return 0;
135 }
Sessions m_sessions
Definition: Acceptor.h:113
const int TargetCompID
const int SenderCompID
const int BeginString
const int MsgType
Session * FIX::Acceptor::getSession ( const SessionID sessionID) const

Definition at line 137 of file Acceptor.cpp.

References m_sessions.

138 {
139  Sessions::const_iterator i = m_sessions.find( sessionID );
140  if( i != m_sessions.end() )
141  return i->second;
142  else
143  return 0;
144 }
Sessions m_sessions
Definition: Acceptor.h:113
const std::set<SessionID>& FIX::Acceptor::getSessions ( ) const
inline

Definition at line 80 of file Acceptor.h.

References m_sessionIDs.

80 { return m_sessionIDs; }
SessionIDs m_sessionIDs
Definition: Acceptor.h:114
const Dictionary *const FIX::Acceptor::getSessionSettings ( const SessionID sessionID) const

Definition at line 146 of file Acceptor.cpp.

References FIX::SessionSettings::get(), and m_settings.

147 {
148  try
149  {
150  return &m_settings.get( sessionID );
151  }
152  catch( ConfigError& )
153  {
154  return 0;
155  }
156 }
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
SessionSettings m_settings
Definition: Acceptor.h:117
bool FIX::Acceptor::has ( const SessionID id)
inline

Definition at line 84 of file Acceptor.h.

References m_sessions.

85  { return m_sessions.find( id ) != m_sessions.end(); }
Sessions m_sessions
Definition: Acceptor.h:113
void FIX::Acceptor::initialize ( )
throw (ConfigError
)
private

Definition at line 69 of file Acceptor.cpp.

References FIX::CONNECTION_TYPE, FIX::SessionFactory::create(), FIX::SessionSettings::get(), FIX::SessionSettings::getSessions(), FIX::Dictionary::getString(), m_application, m_messageStoreFactory, m_pLogFactory, m_sessionIDs, m_sessions, and m_settings.

70 {
71  std::set < SessionID > sessions = m_settings.getSessions();
72  std::set < SessionID > ::iterator i;
73 
74  if ( !sessions.size() )
75  throw ConfigError( "No sessions defined" );
76 
77  SessionFactory factory( m_application, m_messageStoreFactory,
78  m_pLogFactory );
79 
80  for ( i = sessions.begin(); i != sessions.end(); ++i )
81  {
82  if ( m_settings.get( *i ).getString( CONNECTION_TYPE ) == "acceptor" )
83  {
84  m_sessionIDs.insert( *i );
85  m_sessions[ *i ] = factory.create( *i, m_settings.get( *i ) );
86  }
87  }
88 
89  if ( !m_sessions.size() )
90  throw ConfigError( "No sessions defined for acceptor" );
91 }
const char CONNECTION_TYPE[]
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Sessions m_sessions
Definition: Acceptor.h:113
SessionIDs m_sessionIDs
Definition: Acceptor.h:114
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
Application & m_application
Definition: Acceptor.h:115
std::set< SessionID > getSessions() const
SessionSettings m_settings
Definition: Acceptor.h:117
bool FIX::Acceptor::isLoggedOn ( )

Check to see if any sessions are currently logged on.

Definition at line 230 of file Acceptor.cpp.

References m_sessions.

Referenced by FIX::SocketAcceptor::onPoll(), FIX::SocketAcceptor::onStart(), FIX::ThreadedSocketAcceptor::onStop(), and stop().

231 {
232  Sessions sessions = m_sessions;
233  Sessions::iterator i = sessions.begin();
234  for ( ; i != sessions.end(); ++i )
235  {
236  if( i->second->isLoggedOn() )
237  return true;
238  }
239  return false;
240 }
Sessions m_sessions
Definition: Acceptor.h:113
std::map< SessionID, Session * > Sessions
Definition: Acceptor.h:110
bool FIX::Acceptor::isStopped ( )
inline

Definition at line 87 of file Acceptor.h.

References m_stop.

Referenced by FIX::SocketAcceptor::onPoll(), FIX::SocketAcceptor::onStart(), and stop().

87 { return m_stop; }
virtual void FIX::Acceptor::onConfigure ( const SessionSettings )
throw (ConfigError
)
inlineprivatevirtual

Implemented to configure acceptor.

Reimplemented in FIX::ThreadedSocketAcceptor, and FIX::SocketAcceptor.

Definition at line 97 of file Acceptor.h.

Referenced by block(), and start().

97 {};
virtual void FIX::Acceptor::onInitialize ( const SessionSettings )
throw (RuntimeError
)
inlineprivatevirtual

Implemented to initialize acceptor.

Reimplemented in FIX::ThreadedSocketAcceptor, and FIX::SocketAcceptor.

Definition at line 99 of file Acceptor.h.

Referenced by block(), and start().

99 {};
virtual bool FIX::Acceptor::onPoll ( double  second)
privatepure virtual

Implemented to connect and poll for events.

Implemented in FIX::ThreadedSocketAcceptor, and FIX::SocketAcceptor.

virtual void FIX::Acceptor::onStart ( )
privatepure virtual

Implemented to start listening for connections.

Implemented in FIX::ThreadedSocketAcceptor, and FIX::SocketAcceptor.

Referenced by startThread().

virtual void FIX::Acceptor::onStop ( )
privatepure virtual

Implemented to stop a running acceptor.

Implemented in FIX::ThreadedSocketAcceptor, and FIX::SocketAcceptor.

Referenced by stop().

bool FIX::Acceptor::poll ( double  timeout = 0.0)
throw ( ConfigError,
RuntimeError
)

Poll the acceptor.

Definition at line 179 of file Acceptor.cpp.

180 {
181  if( m_firstPoll )
182  {
183  m_stop = false;
186  m_firstPoll = false;
187  }
188 
189  return onPoll( timeout );
190 }
virtual bool onPoll(double second)=0
Implemented to connect and poll for events.
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition: Acceptor.h:97
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:99
bool m_firstPoll
Definition: Acceptor.h:121
SessionSettings m_settings
Definition: Acceptor.h:117
void FIX::Acceptor::start ( )
throw ( ConfigError,
RuntimeError
)

Start acceptor.

Definition at line 158 of file Acceptor.cpp.

References m_settings, m_stop, m_threadid, onConfigure(), onInitialize(), FIX::HttpServer::startGlobal(), startThread(), and FIX::thread_spawn().

Referenced by FIX::SocketAcceptor::onPoll(), FIX::SocketAcceptor::onStart(), and FIX::ThreadedSocketAcceptor::onStop().

159 {
160  m_stop = false;
163 
165 
166  if( !thread_spawn( &startThread, this, m_threadid ) )
167  throw RuntimeError("Unable to spawn thread");
168 }
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:37
static THREAD_PROC startThread(void *p)
Definition: Acceptor.cpp:242
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition: Acceptor.h:97
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:393
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:99
SessionSettings m_settings
Definition: Acceptor.h:117
thread_id m_threadid
Definition: Acceptor.h:112
THREAD_PROC FIX::Acceptor::startThread ( void *  p)
staticprivate

Definition at line 242 of file Acceptor.cpp.

References onStart().

Referenced by block(), and start().

243 {
244  Acceptor * pAcceptor = static_cast < Acceptor* > ( p );
245  pAcceptor->onStart();
246  return 0;
247 }
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Acceptor.cpp:36
void FIX::Acceptor::stop ( bool  force = false)

Stop acceptor.

Definition at line 192 of file Acceptor.cpp.

References FIX::Session::getSessionID(), FIX::Session::isEnabled(), isLoggedOn(), isStopped(), FIX::Session::logout(), FIX::Session::lookupSession(), m_sessions, m_stop, m_threadid, onStop(), FIX::process_sleep(), FIX::HttpServer::stopGlobal(), FIX::thread_join(), and FIX::Session::unregisterSession().

193 {
194  if( isStopped() ) return;
195 
197 
198  std::vector<Session*> enabledSessions;
199 
200  Sessions sessions = m_sessions;
201  Sessions::iterator i = sessions.begin();
202  for ( ; i != sessions.end(); ++i )
203  {
204  Session* pSession = Session::lookupSession(i->first);
205  if( pSession && pSession->isEnabled() )
206  {
207  enabledSessions.push_back( pSession );
208  pSession->logout();
209  Session::unregisterSession( pSession->getSessionID() );
210  }
211  }
212 
213  if( !force )
214  {
215  for ( int second = 1; second <= 10 && isLoggedOn(); ++second )
216  process_sleep( 1 );
217  }
218 
219  m_stop = true;
220  onStop();
221  if( m_threadid )
223  m_threadid = 0;
224 
225  std::vector<Session*>::iterator session = enabledSessions.begin();
226  for( ; session != enabledSessions.end(); ++session )
227  (*session)->logon();
228 }
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Acceptor.cpp:230
void thread_join(thread_id thread)
Definition: Utility.cpp:414
bool isStopped()
Definition: Acceptor.h:87
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1403
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1454
void process_sleep(double s)
Definition: Utility.cpp:443
Sessions m_sessions
Definition: Acceptor.h:113
virtual void onStop()=0
Implemented to stop a running acceptor.
std::map< SessionID, Session * > Sessions
Definition: Acceptor.h:110
static void stopGlobal()
Definition: HttpServer.cpp:53
thread_id m_threadid
Definition: Acceptor.h:112

Member Data Documentation

Application& FIX::Acceptor::m_application
private

Definition at line 115 of file Acceptor.h.

Referenced by getApplication(), and initialize().

bool FIX::Acceptor::m_firstPoll
private

Definition at line 121 of file Acceptor.h.

MessageStoreFactory& FIX::Acceptor::m_messageStoreFactory
private

Definition at line 116 of file Acceptor.h.

Referenced by getMessageStoreFactory(), and initialize().

NullLog FIX::Acceptor::m_nullLog
private

Definition at line 120 of file Acceptor.h.

Referenced by getLog().

Log* FIX::Acceptor::m_pLog
private

Definition at line 119 of file Acceptor.h.

Referenced by getLog(), and ~Acceptor().

LogFactory* FIX::Acceptor::m_pLogFactory
private

Definition at line 118 of file Acceptor.h.

Referenced by initialize(), and ~Acceptor().

SessionIDs FIX::Acceptor::m_sessionIDs
private

Definition at line 114 of file Acceptor.h.

Referenced by getSessions(), and initialize().

Sessions FIX::Acceptor::m_sessions
private

Definition at line 113 of file Acceptor.h.

Referenced by getSession(), has(), initialize(), isLoggedOn(), stop(), and ~Acceptor().

SessionSettings FIX::Acceptor::m_settings
private

Definition at line 117 of file Acceptor.h.

Referenced by block(), getSessionSettings(), initialize(), and start().

bool FIX::Acceptor::m_stop
private

Definition at line 122 of file Acceptor.h.

Referenced by block(), isStopped(), start(), and stop().

thread_id FIX::Acceptor::m_threadid
private

Definition at line 112 of file Acceptor.h.

Referenced by start(), and stop().


The documentation for this class was generated from the following files:

Generated on Sat Mar 29 2014 15:13:33 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001