SessionSettings.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 "SessionSettings.h"
#include "Settings.h"
#include "Values.h"
#include <fstream>
namespace FIX
{
SessionSettings::SessionSettings( std::istream& stream, bool resolveEnvVars )
throw( ConfigError )
: m_resolveEnvVars( resolveEnvVars )
{
stream >> *this;
}
SessionSettings::SessionSettings( const std::string& file, bool resolveEnvVars )
throw( ConfigError )
: m_resolveEnvVars( resolveEnvVars )
{
std::ifstream fstream( file.c_str() );
if ( !fstream.is_open() )
throw ConfigError( ( "File " + file + " not found" ).c_str() );
fstream >> *this;
}
std::istream& operator>>( std::istream& stream, SessionSettings& s )
throw( ConfigError )
{
Settings settings(s.m_resolveEnvVars);
stream >> settings;
Settings::Sections section;
section = settings.get( "DEFAULT" );
Dictionary def;
if ( section.size() )
def = section[ 0 ];
s.set( def );
section = settings.get( "SESSION" );
Settings::Sections::size_type session;
Dictionary dict;
for ( session = 0; session < section.size(); ++session )
{
dict = section[ session ];
dict.merge( def );
BeginString beginString
( dict.getString( BEGINSTRING ) );
SenderCompID senderCompID
( dict.getString( SENDERCOMPID ) );
TargetCompID targetCompID
( dict.getString( TARGETCOMPID ) );
std::string sessionQualifier;
if( dict.has( SESSION_QUALIFIER ) )
sessionQualifier = dict.getString( SESSION_QUALIFIER );
SessionID sessionID( beginString, senderCompID, targetCompID, sessionQualifier );
s.set( sessionID, dict );
}
return stream;
}
std::ostream& operator<<( std::ostream& stream, const SessionSettings& s )
{
const Dictionary& defaults = s.m_defaults;
if( defaults.size() )
{
stream << "[DEFAULT]" << std::endl;
Dictionary::iterator i;
for( i = defaults.begin(); i != defaults.end(); ++i )
stream << i->first << "=" << i->second << std::endl;
stream << std::endl;
}
std::set<SessionID> sessions = s.getSessions();
std::set<SessionID>::iterator i;
for( i = sessions.begin(); i != sessions.end(); ++i )
{
stream << "[SESSION]" << std::endl;
const Dictionary& section = s.get( *i );
if( !section.size() ) continue;
Dictionary::iterator i;
for( i = section.begin(); i != section.end(); ++i )
{
if( defaults.has(i->first) && defaults.getString(i->first) == i->second )
continue;
stream << i->first << "=" << i->second << std::endl;
}
stream << std::endl;
}
return stream;
}
const bool SessionSettings::has( const SessionID& sessionID ) const
{
return m_settings.find( sessionID ) != m_settings.end();
}
const Dictionary& SessionSettings::get( const SessionID& sessionID ) const
throw( ConfigError )
{
Dictionaries::const_iterator i;
i = m_settings.find( sessionID );
if ( i == m_settings.end() ) throw ConfigError( "Session not found" );
return i->second;
}
void SessionSettings::set( const SessionID& sessionID,
Dictionary settings )
throw( ConfigError )
{
if( has(sessionID) )
throw ConfigError( "Duplicate Session " + sessionID.toString() );
settings.setString( BEGINSTRING, sessionID.getBeginString() );
settings.setString( SENDERCOMPID, sessionID.getSenderCompID() );
settings.setString( TARGETCOMPID, sessionID.getTargetCompID() );
settings.merge( m_defaults );
validate( settings );
m_settings[ sessionID ] = settings;
}
void SessionSettings::set( const Dictionary& defaults ) throw( ConfigError )
{
m_defaults = defaults;
Dictionaries::iterator i = m_settings.begin();
for( i = m_settings.begin(); i != m_settings.end(); ++i )
i->second.merge( defaults );
}
std::set < SessionID > SessionSettings::getSessions() const
{
std::set < SessionID > result;
Dictionaries::const_iterator i;
for ( i = m_settings.begin(); i != m_settings.end(); ++i )
result.insert( i->first );
return result;
}
void SessionSettings::validate( const Dictionary& dictionary ) const
throw( ConfigError )
{
std::string beginString = dictionary.getString( BEGINSTRING );
if( beginString != BeginString_FIX40 &&
beginString != BeginString_FIX41 &&
beginString != BeginString_FIX42 &&
beginString != BeginString_FIX43 &&
beginString != BeginString_FIX44 &&
beginString != BeginString_FIXT11 )
{
throw ConfigError( std::string(BEGINSTRING) + " must be FIX.4.0 to FIX.4.4 or FIXT.1.1" );
}
std::string connectionType = dictionary.getString( CONNECTION_TYPE );
if( connectionType != "initiator" &&
connectionType != "acceptor" )
{
throw ConfigError( std::string(CONNECTION_TYPE) + " must be 'initiator' or 'acceptor'" );
}
}
}
SessionSettings.h
Settings.h
Values.h
FIX::Dictionary
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:37
FIX::Dictionary::merge
void merge(const Dictionary &)
Merge two dictionaries.
Definition: Dictionary.cpp:154
FIX::Dictionary::iterator
Data::const_iterator iterator
Definition: Dictionary.h:44
FIX::Dictionary::begin
iterator begin() const
Definition: Dictionary.h:84
FIX::Dictionary::size
size_t size() const
Return the number of key/value pairs.
Definition: Dictionary.h:50
FIX::Dictionary::has
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:149
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
FIX::Dictionary::end
iterator end() const
Definition: Dictionary.h:85
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:31
FIX::SessionSettings
Container for setting dictionaries mapped to sessions.
Definition: SessionSettings.h:221
FIX::SessionSettings::SessionSettings
SessionSettings()
Definition: SessionSettings.h:223
FIX::SessionSettings::validate
void validate(const Dictionary &) const
Definition: SessionSettings.cpp:169
FIX::SessionSettings::m_defaults
Dictionary m_defaults
Definition: SessionSettings.h:250
FIX::SessionSettings::set
void set(const SessionID &, Dictionary)
Set a dictionary for a session.
Definition: SessionSettings.cpp:136
FIX::SessionSettings::getSessions
std::set< SessionID > getSessions() const
Definition: SessionSettings.cpp:160
FIX::SessionSettings::has
const bool has(const SessionID &) const
Check if session setings are present.
Definition: SessionSettings.cpp:122
FIX::SessionSettings::get
const Dictionary & get(const SessionID &) const
Get a dictionary for a session.
Definition: SessionSettings.cpp:127
FIX::SessionSettings::m_settings
Dictionaries m_settings
Definition: SessionSettings.h:249
FIX::SessionSettings::get
const Dictionary & get() const
Get global default settings.
Definition: SessionSettings.h:236
FIX::Settings
Internal representation of QuickFIX configuration settings.
Definition: Settings.h:39
FIX::Settings::Sections
std::vector< Dictionary > Sections
Definition: Settings.h:43
FIX::Settings::get
Sections get(const std::string &name) const
Definition: Settings.cpp:162
FIX
Definition: Acceptor.cpp:35
FIX::BeginString_FIX41
const char BeginString_FIX41[]
Definition: Values.h:35
FIX::BeginString_FIX40
const char BeginString_FIX40[]
Definition: Values.h:36
FIX::BeginString_FIX42
const char BeginString_FIX42[]
Definition: Values.h:34
FIX::CONNECTION_TYPE
const char CONNECTION_TYPE[]
Definition: SessionSettings.h:42
FIX::SENDERCOMPID
const char SENDERCOMPID[]
Definition: SessionSettings.h:38
FIX::SESSION_QUALIFIER
const char SESSION_QUALIFIER[]
Definition: SessionSettings.h:40
FIX::resolveEnvVars
std::string resolveEnvVars(const std::string &str)
Definition: Settings.cpp:65
FIX::BEGINSTRING
const char BEGINSTRING[]
Definition: SessionSettings.h:37
FIX::TARGETCOMPID
const char TARGETCOMPID[]
Definition: SessionSettings.h:39
FIX::operator>>
std::istream & operator>>(std::istream &stream, SessionID &sessionID)
Definition: SessionID.h:160
FIX::operator<<
std::ostream & operator<<(std::ostream &stream, const FieldBase &field)
Definition: Field.h:243
FIX::BeginString_FIXT11
const char BeginString_FIXT11[]
Definition: Values.h:30
FIX::BeginString_FIX43
const char BeginString_FIX43[]
Definition: Values.h:33
FIX::BeginString_FIX44
const char BeginString_FIX44[]
Definition: Values.h:32
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:88