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
34namespace FIX
35{
37 MessageStoreFactory& messageStoreFactory,
38 const SessionSettings& settings )
39throw( 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 )
56throw( 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
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
137Session* 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
146const 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
179bool 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
192void 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}
#define THREAD_PROC
Definition Utility.h:184
Base for classes which act as an acceptor for incoming connections.
Definition Acceptor.h:50
virtual void onStop()=0
Implemented to stop a running acceptor.
virtual ~Acceptor()
Definition Acceptor.cpp:93
Sessions m_sessions
Definition Acceptor.h:113
void start()
Start acceptor.
Definition Acceptor.cpp:158
void initialize()
Definition Acceptor.cpp:69
SessionIDs m_sessionIDs
Definition Acceptor.h:114
void stop(bool force=false)
Stop acceptor.
Definition Acceptor.cpp:192
void block()
Block on the acceptor.
Definition Acceptor.cpp:170
SessionSettings m_settings
Definition Acceptor.h:118
std::map< SessionID, Session * > Sessions
Definition Acceptor.h:110
Session * getSession(const std::string &msg, Responder &)
Definition Acceptor.cpp:104
MessageStoreFactory & m_messageStoreFactory
Definition Acceptor.h:116
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition Acceptor.h:99
static THREAD_PROC startThread(void *p)
Definition Acceptor.cpp:242
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition Acceptor.cpp:36
bool poll(double timeout=0.0)
Poll the acceptor.
Definition Acceptor.cpp:179
const Dictionary *const getSessionSettings(const SessionID &sessionID) const
Definition Acceptor.cpp:146
LogFactory * m_pLogFactory
Definition Acceptor.h:120
bool isStopped()
Definition Acceptor.h:87
thread_id m_threadid
Definition Acceptor.h:112
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition Acceptor.cpp:230
Application & m_application
Definition Acceptor.h:115
virtual void onStart()=0
Implemented to start listening for connections.
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition Acceptor.h:97
This interface must be implemented to define what your FIX application does.
Definition Application.h:44
For storage and retrieval of key/value pairs.
Definition Dictionary.h:37
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
FieldBase & getField(FieldBase &field) const
Get a field without type checking.
Definition FieldMap.h:156
static void startGlobal(const SessionSettings &)
static void stopGlobal()
This interface must be implemented to create a Log.
Definition Log.h:43
virtual void destroy(Log *)=0
Base class for all FIX messages.
Definition Message.h:118
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:475
const Header & getHeader() const
Getter for the message header.
Definition Message.h:245
This interface must be implemented to create a MessageStore.
Interface implements sending on and disconnecting a transport.
Definition Responder.h:35
Responsible for creating Session objects.
Session * create(const SessionID &sessionID, const Dictionary &settings)
Maintains the state and implements the logic of a FIX session.
Definition Session.h:46
static void unregisterSession(const SessionID &)
Definition Session.cpp:1547
void logout(const std::string &reason="")
Definition Session.h:57
bool isEnabled()
Definition Session.h:59
static Session * lookupSession(const SessionID &)
Definition Session.cpp:1496
const SessionID & getSessionID() const
Definition Session.h:75
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition SessionID.h:31
Container for setting dictionaries mapped to sessions.
std::set< SessionID > getSessions() const
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
const char CONNECTION_TYPE[]
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition Utility.cpp:416
void process_sleep(double s)
Definition Utility.cpp:466
void thread_join(thread_id thread)
Definition Utility.cpp:437
Application is not configured correctly
Definition Exceptions.h:88
Field not found inside a message.
Definition Exceptions.h:58
Application encountered serious error during runtime
Definition Exceptions.h:95

Generated on Mon Mar 4 2024 21:10:02 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001