Acceptor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "Acceptor.h"
27 #include "Utility.h"
28 #include "Session.h"
29 #include "SessionFactory.h"
30 #include "HttpServer.h"
31 #include <algorithm>
32 #include <fstream>
33 
34 namespace FIX
35 {
37  MessageStoreFactory& messageStoreFactory,
38  const SessionSettings& settings )
39 throw( ConfigError )
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 }
51 
53  MessageStoreFactory& messageStoreFactory,
54  const SessionSettings& settings,
55  LogFactory& logFactory )
56 throw( ConfigError )
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 }
68 
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 
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 }
92 
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 }
102 
104 ( const std::string& msg, Responder& responder )
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 }
136 
137 Session* Acceptor::getSession( const SessionID& sessionID ) const
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 }
145 
146 const Dictionary* const Acceptor::getSessionSettings( const SessionID& sessionID ) const
147 {
148  try
149  {
150  return &m_settings.get( sessionID );
151  }
152  catch( ConfigError& )
153  {
154  return 0;
155  }
156 }
157 
159 {
160  m_stop = false;
163 
165 
166  if( !thread_spawn( &startThread, this, m_threadid ) )
167  throw RuntimeError("Unable to spawn thread");
168 }
169 
171 {
172  m_stop = false;
175 
176  startThread(this);
177 }
178 
179 bool Acceptor::poll( double timeout ) throw ( ConfigError, RuntimeError )
180 {
181  if( m_firstPoll )
182  {
183  m_stop = false;
184  onConfigure( m_settings );
185  onInitialize( m_settings );
186  m_firstPoll = false;
187  }
188 
189  return onPoll( timeout );
190 }
191 
192 void Acceptor::stop( bool force )
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();
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 }
229 
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 }
241 
243 {
244  Acceptor * pAcceptor = static_cast < Acceptor* > ( p );
245  pAcceptor->onStart();
246  return 0;
247 }
248 }
const Header & getHeader() const
Getter for the message header.
Definition: Message.h:192
bool isEnabled()
Definition: Session.h:59
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Acceptor.cpp:230
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:37
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
const char CONNECTION_TYPE[]
Maintains the state and implements the logic of a FIX session.
Definition: Session.h:45
Interface implements sending on and disconnecting a transport.
Definition: Responder.h:34
FieldBase & getField(FieldBase &field) const
Get a field without type checking.
Definition: FieldMap.h:112
bool setStringHeader(const std::string &string)
Set a messages header from a string This is an optimization that can be used to get useful informatio...
Definition: Message.cpp:371
void process_sleep(double s)
Definition: Utility.cpp:443
Session * getSession(const std::string &msg, Responder &)
Definition: Acceptor.cpp:104
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
void block()
Block on the acceptor.
Definition: Acceptor.cpp:170
virtual void destroy(Log *)=0
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Sessions m_sessions
Definition: Acceptor.h:113
This interface must be implemented to define what your FIX application does.
Definition: Application.h:43
virtual void onStop()=0
Implemented to stop a running acceptor.
void start()
Start acceptor.
Definition: Acceptor.cpp:158
Responsible for creating Session objects.
Application encountered serious error during runtime
Definition: Exceptions.h:94
Application is not configured correctly
Definition: Exceptions.h:87
SessionIDs m_sessionIDs
Definition: Acceptor.h:114
Container for setting dictionaries mapped to sessions.
const Dictionary *const getSessionSettings(const SessionID &sessionID) const
Definition: Acceptor.cpp:146
const int TargetCompID
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:99
Base for classes which act as an acceptor for incoming connections.
Definition: Acceptor.h:49
std::map< SessionID, Session * > Sessions
Definition: Acceptor.h:110
const SessionID & getSessionID() const
Definition: Session.h:75
This interface must be implemented to create a Log.
Definition: Log.h:42
Base class for all FIX messages.
Definition: Message.h:67
Field not found inside a message.
Definition: Exceptions.h:57
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
This interface must be implemented to create a MessageStore.
Definition: MessageStore.h:41
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
virtual ~Acceptor()
Definition: Acceptor.cpp:93
const int SenderCompID
static void stopGlobal()
Definition: HttpServer.cpp:53
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
void logout(const std::string &reason="")
Definition: Session.h:57
Session * create(const SessionID &sessionID, const Dictionary &settings)
bool poll(double timeout=0.0)
Poll the acceptor.
Definition: Acceptor.cpp:179
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
Log * m_pLog
Definition: Acceptor.h:119
Application & m_application
Definition: Acceptor.h:115
const int BeginString
virtual void onStart()=0
Implemented to start listening for connections.
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:30
void stop(bool force=false)
Stop acceptor.
Definition: Acceptor.cpp:192
#define THREAD_PROC
Definition: Utility.h:147
void initialize()
Definition: Acceptor.cpp:69
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Acceptor.cpp:36
const int MsgType
std::set< SessionID > getSessions() const
SessionSettings m_settings
Definition: Acceptor.h:117
thread_id m_threadid
Definition: Acceptor.h:112

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