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 {
36 Acceptor::Acceptor( Application& application,
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 
52 Acceptor::Acceptor( Application& application,
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 
103 Session* Acceptor::getSession
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 
158 void Acceptor::start() throw ( ConfigError, RuntimeError )
159 {
160  m_stop = false;
163 
165 
166  if( !thread_spawn( &startThread, this, m_threadid ) )
167  throw RuntimeError("Unable to spawn thread");
168 }
169 
170 void Acceptor::block() throw ( ConfigError, RuntimeError )
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 }
HttpServer.h
FIX::Acceptor::getSession
Session * getSession(const std::string &msg, Responder &)
Definition: Acceptor.cpp:121
Acceptor.h
FIX::Acceptor::onInitialize
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:116
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1513
FIX::Session::unregisterSession
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1564
FIX::MessageStoreFactory
This interface must be implemented to create a MessageStore.
Definition: MessageStore.h:58
FIX::Acceptor::getSessionSettings
const Dictionary *const getSessionSettings(const SessionID &sessionID) const
Definition: Acceptor.cpp:163
SessionFactory.h
FIX::Acceptor::m_messageStoreFactory
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:133
FIX::CONNECTION_TYPE
const char CONNECTION_TYPE[]
Definition: SessionSettings.h:59
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:49
FIX::Acceptor::m_pLogFactory
LogFactory * m_pLogFactory
Definition: Acceptor.h:137
FIX::Session::getSessionID
const SessionID & getSessionID() const
Definition: Session.h:109
FIX::Acceptor::Acceptor
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Acceptor.cpp:53
FIX::Acceptor::start
void start()
Start acceptor.
Definition: Acceptor.cpp:175
FIX::HttpServer::stopGlobal
static void stopGlobal()
Definition: HttpServer.cpp:70
FIX::Application
This interface must be implemented to define what your FIX application does.
Definition: Application.h:60
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:104
FIX::RuntimeError
Application encountered serious error during runtime
Definition: Exceptions.h:111
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:47
FIX::Acceptor::~Acceptor
virtual ~Acceptor()
Definition: Acceptor.cpp:110
FIX::SessionSettings
Container for setting dictionaries mapped to sessions.
Definition: SessionSettings.h:237
FIX::Acceptor::startThread
static THREAD_PROC startThread(void *p)
Definition: Acceptor.cpp:259
THREAD_PROC
#define THREAD_PROC
Definition: Utility.h:184
FIX::Acceptor::isLoggedOn
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Acceptor.cpp:247
FIX::Acceptor::Sessions
std::map< SessionID, Session * > Sessions
Definition: Acceptor.h:127
FIX::Acceptor::block
void block()
Block on the acceptor.
Definition: Acceptor.cpp:187
FIX::SessionSettings::getSessions
std::set< SessionID > getSessions() const
Definition: SessionSettings.cpp:177
FIX::Acceptor::m_pLog
Log * m_pLog
Definition: Acceptor.h:138
FIX::LogFactory::destroy
virtual void destroy(Log *)=0
FIX::Acceptor::m_sessionIDs
SessionIDs m_sessionIDs
Definition: Acceptor.h:131
FIX::Acceptor::onStop
virtual void onStop()=0
Implemented to stop a running acceptor.
FIX::process_sleep
void process_sleep(double s)
Definition: Utility.cpp:483
FIX::Session::isEnabled
bool isEnabled()
Definition: Session.h:93
FIX
Definition: Acceptor.cpp:34
FIX::Acceptor::m_stop
bool m_stop
Definition: Acceptor.h:141
FIX::Acceptor::stop
void stop(bool force=false)
Stop acceptor.
Definition: Acceptor.cpp:209
FIX::Acceptor::initialize
void initialize()
Definition: Acceptor.cpp:86
FIX::HttpServer::startGlobal
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:54
Session.h
FIX::thread_join
void thread_join(thread_id thread)
Definition: Utility.cpp:454
FIX::Session::logout
void logout(const std::string &reason="")
Definition: Session.h:91
FIX::SessionFactory
Responsible for creating Session objects.
Definition: SessionFactory.h:63
FIX::SessionFactory::create
Session * create(const SessionID &sessionID, const Dictionary &settings)
Definition: SessionFactory.cpp:58
FIX::Acceptor::onConfigure
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition: Acceptor.h:114
FIX::LogFactory
This interface must be implemented to create a Log.
Definition: Log.h:59
FIX::Acceptor::poll
bool poll(double timeout=0.0)
Poll the acceptor.
Definition: Acceptor.cpp:196
FIX::Acceptor::m_settings
SessionSettings m_settings
Definition: Acceptor.h:135
Utility.h
FIX::Acceptor::isStopped
bool isStopped()
Definition: Acceptor.h:104
FIX::SessionSettings::get
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Definition: SessionSettings.cpp:144
FIX::Acceptor::m_application
Application & m_application
Definition: Acceptor.h:132
FIX::Acceptor::m_sessions
Sessions m_sessions
Definition: Acceptor.h:130
FIX::Acceptor::m_threadid
thread_id m_threadid
Definition: Acceptor.h:129
FIX::thread_spawn
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:433
FIX::Session
Maintains the state and implements the logic of a FIX session.
Definition: Session.h:62

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001