Acceptor.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "Acceptor.h"
#include "Utility.h"
#include "Session.h"
#include "SessionFactory.h"
#include "HttpServer.h"
#include <algorithm>
#include <fstream>
namespace FIX
{
Acceptor::Acceptor( Application& application,
MessageStoreFactory& messageStoreFactory,
const SessionSettings& settings )
throw( ConfigError )
: m_threadid( 0 ),
m_application( application ),
m_messageStoreFactory( messageStoreFactory ),
m_settings( settings ),
m_pLogFactory( 0 ),
m_pLog( 0 ),
m_firstPoll( true ),
m_stop( true )
{
initialize();
}
Acceptor::Acceptor( Application& application,
MessageStoreFactory& messageStoreFactory,
const SessionSettings& settings,
LogFactory& logFactory )
throw( ConfigError )
: m_threadid( 0 ),
m_application( application ),
m_messageStoreFactory( messageStoreFactory ),
m_settings( settings ),
m_pLogFactory( &logFactory ),
m_pLog( logFactory.create() ),
m_firstPoll( true ),
m_stop( true )
{
initialize();
}
void Acceptor::initialize() throw ( ConfigError )
{
std::set < SessionID > sessions = m_settings.getSessions();
std::set < SessionID > ::iterator i;
if ( !sessions.size() )
throw ConfigError( "No sessions defined" );
SessionFactory factory( m_application, m_messageStoreFactory,
m_pLogFactory );
for ( i = sessions.begin(); i != sessions.end(); ++i )
{
if ( m_settings.get( *i ).getString( CONNECTION_TYPE ) == "acceptor" )
{
m_sessionIDs.insert( *i );
m_sessions[ *i ] = factory.create( *i, m_settings.get( *i ) );
}
}
if ( !m_sessions.size() )
throw ConfigError( "No sessions defined for acceptor" );
}
Acceptor::~Acceptor()
{
Sessions::iterator i;
for ( i = m_sessions.begin(); i != m_sessions.end(); ++i )
delete i->second;
if( m_pLogFactory && m_pLog )
m_pLogFactory->destroy( m_pLog );
}
Session* Acceptor::getSession
( const std::string& msg, Responder& responder )
{
Message message;
if ( !message.setStringHeader( msg ) )
return 0;
BeginString beginString;
SenderCompID clSenderCompID;
TargetCompID clTargetCompID;
MsgType msgType;
try
{
message.getHeader().getField( beginString );
message.getHeader().getField( clSenderCompID );
message.getHeader().getField( clTargetCompID );
message.getHeader().getField( msgType );
if ( msgType != "A" ) return 0;
SenderCompID senderCompID( clTargetCompID );
TargetCompID targetCompID( clSenderCompID );
SessionID sessionID( beginString, senderCompID, targetCompID );
Sessions::iterator i = m_sessions.find( sessionID );
if ( i != m_sessions.end() )
{
i->second->setResponder( &responder );
return i->second;
}
}
catch ( FieldNotFound& ) {}
return 0;
}
Session* Acceptor::getSession( const SessionID& sessionID ) const
{
Sessions::const_iterator i = m_sessions.find( sessionID );
if( i != m_sessions.end() )
return i->second;
else
return 0;
}
const Dictionary* const Acceptor::getSessionSettings( const SessionID& sessionID ) const
{
try
{
return &m_settings.get( sessionID );
}
catch( ConfigError& )
{
return 0;
}
}
void Acceptor::start() throw ( ConfigError, RuntimeError )
{
m_stop = false;
onConfigure( m_settings );
onInitialize( m_settings );
HttpServer::startGlobal( m_settings );
if( !thread_spawn( &startThread, this, m_threadid ) )
throw RuntimeError("Unable to spawn thread");
}
void Acceptor::block() throw ( ConfigError, RuntimeError )
{
m_stop = false;
onConfigure( m_settings );
onInitialize( m_settings );
startThread(this);
}
bool Acceptor::poll( double timeout ) throw ( ConfigError, RuntimeError )
{
if( m_firstPoll )
{
m_stop = false;
onConfigure( m_settings );
onInitialize( m_settings );
m_firstPoll = false;
}
return onPoll( timeout );
}
void Acceptor::stop( bool force )
{
if( isStopped() ) return;
HttpServer::stopGlobal();
std::vector<Session*> enabledSessions;
Sessions sessions = m_sessions;
Sessions::iterator i = sessions.begin();
for ( ; i != sessions.end(); ++i )
{
Session* pSession = Session::lookupSession(i->first);
if( pSession && pSession->isEnabled() )
{
enabledSessions.push_back( pSession );
pSession->logout();
Session::unregisterSession( pSession->getSessionID() );
}
}
if( !force )
{
for ( int second = 1; second <= 10 && isLoggedOn(); ++second )
process_sleep( 1 );
}
m_stop = true;
onStop();
if( m_threadid )
thread_join( m_threadid );
m_threadid = 0;
std::vector<Session*>::iterator session = enabledSessions.begin();
for( ; session != enabledSessions.end(); ++session )
(*session)->logon();
}
bool Acceptor::isLoggedOn()
{
Sessions sessions = m_sessions;
Sessions::iterator i = sessions.begin();
for ( ; i != sessions.end(); ++i )
{
if( i->second->isLoggedOn() )
return true;
}
return false;
}
THREAD_PROC Acceptor::startThread( void* p )
{
Acceptor * pAcceptor = static_cast < Acceptor* > ( p );
pAcceptor->onStart();
return 0;
}
}
FIX::Session::isEnabled
bool isEnabled()
Definition: Session.h:59
FIX::Acceptor::isLoggedOn
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Acceptor.cpp:230
FIX::HttpServer::startGlobal
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:37
FIX::thread_join
void thread_join(thread_id thread)
Definition: Utility.cpp:415
FIX::Acceptor::isStopped
bool isStopped()
Definition: Acceptor.h:87
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1403
FIX::Session::unregisterSession
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1454
FIX::CONNECTION_TYPE
const char CONNECTION_TYPE[]
Definition: SessionSettings.h:42
FIX::Session
Maintains the state and implements the logic of a FIX session.
Definition: Session.h:45
FIX::Responder
Interface implements sending on and disconnecting a transport.
Definition: Responder.h:34
Utility.h
FIX::Acceptor::onPoll
virtual bool onPoll(double second)=0
Implemented to connect and poll for events.
FIX::Message::setStringHeader
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:380
FIX::process_sleep
void process_sleep(double s)
Definition: Utility.cpp:444
FIX::Acceptor::getSession
Session * getSession(const std::string &msg, Responder &)
Definition: Acceptor.cpp:104
FIX::Acceptor::startThread
static THREAD_PROC startThread(void *p)
Definition: Acceptor.cpp:242
FIX::Acceptor::onConfigure
virtual void onConfigure(const SessionSettings &)
Implemented to configure acceptor.
Definition: Acceptor.h:97
Acceptor.h
FIX::thread_spawn
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:394
FIX::Acceptor::block
void block()
Block on the acceptor.
Definition: Acceptor.cpp:170
FIX::LogFactory::destroy
virtual void destroy(Log *)=0
FIX::SessionSettings::getSessions
std::set< SessionID > getSessions() const
Definition: SessionSettings.cpp:158
FIX::Acceptor::getSessionSettings
const Dictionary *const getSessionSettings(const SessionID &sessionID) const
Definition: Acceptor.cpp:146
FIX::SessionSettings::get
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Definition: SessionSettings.cpp:125
FIX
Definition: Acceptor.cpp:34
FIX::Message::getHeader
const Header & getHeader() const
Getter for the message header.
Definition: Message.h:192
FIX::Acceptor::m_sessions
Sessions m_sessions
Definition: Acceptor.h:113
FIX::Application
This interface must be implemented to define what your FIX application does.
Definition: Application.h:43
FIX::Acceptor::onStop
virtual void onStop()=0
Implemented to stop a running acceptor.
FIX::FieldMap::getField
FieldBase & getField(FieldBase &field) const
Get a field without type checking.
Definition: FieldMap.h:112
FIX::Acceptor::start
void start()
Start acceptor.
Definition: Acceptor.cpp:158
FIX::SessionFactory
Responsible for creating Session objects.
Definition: SessionFactory.h:46
FIX::RuntimeError
Application encountered serious error during runtime
Definition: Exceptions.h:94
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:87
FIX::Acceptor::m_sessionIDs
SessionIDs m_sessionIDs
Definition: Acceptor.h:114
FIX::SessionSettings
Container for setting dictionaries mapped to sessions.
Definition: SessionSettings.h:130
FIX::FIELD::TargetCompID
const int TargetCompID
Definition: FixFieldNumbers.h:217
FIX::Acceptor::onInitialize
virtual void onInitialize(const SessionSettings &)
Implemented to initialize acceptor.
Definition: Acceptor.h:99
FIX::Acceptor
Base for classes which act as an acceptor for incoming connections.
Definition: Acceptor.h:49
FIX::Acceptor::Sessions
std::map< SessionID, Session *> Sessions
Definition: Acceptor.h:110
FIX::Acceptor::m_stop
bool m_stop
Definition: Acceptor.h:122
FIX::LogFactory
This interface must be implemented to create a Log.
Definition: Log.h:42
FIX::Message
Base class for all FIX messages.
Definition: Message.h:67
FIX::FieldNotFound
Field not found inside a message.
Definition: Exceptions.h:57
FIX::Session::getSessionID
const SessionID & getSessionID() const
Definition: Session.h:75
FIX::MessageStoreFactory
This interface must be implemented to create a MessageStore.
Definition: MessageStore.h:41
FIX::Acceptor::m_pLogFactory
LogFactory * m_pLogFactory
Definition: Acceptor.h:118
SessionFactory.h
FIX::Acceptor::~Acceptor
virtual ~Acceptor()
Definition: Acceptor.cpp:93
FIX::FIELD::SenderCompID
const int SenderCompID
Definition: FixFieldNumbers.h:124
FIX::HttpServer::stopGlobal
static void stopGlobal()
Definition: HttpServer.cpp:53
FIX::Acceptor::m_messageStoreFactory
MessageStoreFactory & m_messageStoreFactory
Definition: Acceptor.h:116
FIX::Session::logout
void logout(const std::string &reason="")
Definition: Session.h:57
FIX::SessionFactory::create
Session * create(const SessionID &sessionID, const Dictionary &settings)
Definition: SessionFactory.cpp:41
FIX::Acceptor::poll
bool poll(double timeout=0.0)
Poll the acceptor.
Definition: Acceptor.cpp:179
FIX::Dictionary
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
FIX::Acceptor::m_pLog
Log * m_pLog
Definition: Acceptor.h:119
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
FIX::Acceptor::m_application
Application & m_application
Definition: Acceptor.h:115
FIX::FIELD::BeginString
const int BeginString
Definition: FixFieldNumbers.h:499
FIX::Acceptor::onStart
virtual void onStart()=0
Implemented to start listening for connections.
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:30
FIX::Acceptor::stop
void stop(bool force=false)
Stop acceptor.
Definition: Acceptor.cpp:192
THREAD_PROC
#define THREAD_PROC
Definition: Utility.h:158
FIX::Acceptor::initialize
void initialize()
Definition: Acceptor.cpp:69
HttpServer.h
FIX::Acceptor::Acceptor
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Acceptor.cpp:36
Session.h
FIX::FIELD::MsgType
const int MsgType
Definition: FixFieldNumbers.h:363
FIX::Acceptor::m_firstPoll
bool m_firstPoll
Definition: Acceptor.h:121
FIX::Acceptor::m_settings
SessionSettings m_settings
Definition: Acceptor.h:117
FIX::Acceptor::m_threadid
thread_id m_threadid
Definition: Acceptor.h:112