SessionSettings.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "SessionSettings.h"
00027 #include "Settings.h"
00028 #include "Values.h"
00029 #include <fstream>
00030 
00031 namespace FIX
00032 {
00033 SessionSettings::SessionSettings( std::istream& stream )
00034 throw( ConfigError )
00035 {
00036   stream >> *this;
00037 }
00038 
00039 SessionSettings::SessionSettings( const std::string& file )
00040 throw( ConfigError )
00041 {
00042   std::ifstream fstream( file.c_str() );
00043   if ( !fstream.is_open() )
00044     throw ConfigError( ( "File " + file + " not found" ).c_str() );
00045   fstream >> *this;
00046 }
00047 
00048 std::istream& operator>>( std::istream& stream, SessionSettings& s )
00049 throw( ConfigError )
00050 {
00051   Settings settings;
00052   stream >> settings;
00053 
00054   Settings::Sections section;
00055 
00056   section = settings.get( "DEFAULT" );
00057   Dictionary def;
00058   if ( section.size() )
00059     def = section[ 0 ];
00060   s.set( def );
00061 
00062   section = settings.get( "SESSION" );
00063   Settings::Sections::size_type session;
00064   Dictionary dict;
00065 
00066   for ( session = 0; session < section.size(); ++session )
00067   {
00068     dict = section[ session ];
00069     dict.merge( def );
00070 
00071     BeginString beginString
00072     ( dict.getString( BEGINSTRING ) );
00073     SenderCompID senderCompID
00074     ( dict.getString( SENDERCOMPID ) );
00075     TargetCompID targetCompID
00076     ( dict.getString( TARGETCOMPID ) );
00077 
00078     std::string sessionQualifier;
00079     if( dict.has( SESSION_QUALIFIER ) )
00080       sessionQualifier = dict.getString( SESSION_QUALIFIER );
00081     SessionID sessionID( beginString, senderCompID, targetCompID, sessionQualifier );
00082     s.set( sessionID, dict );
00083   }
00084   return stream;
00085 }
00086 
00087 std::ostream& operator<<( std::ostream& stream, const SessionSettings& s )
00088 {
00089   const Dictionary& defaults = s.m_defaults;
00090   if( defaults.size() )
00091   {
00092     stream << "[DEFAULT]" << std::endl;
00093     Dictionary::iterator i;
00094     for( i = defaults.begin(); i != defaults.end(); ++i )
00095       stream << i->first << "=" << i->second << std::endl;
00096     stream << std::endl;
00097   }
00098 
00099   std::set<SessionID> sessions = s.getSessions();
00100   std::set<SessionID>::iterator i;
00101   for( i = sessions.begin(); i != sessions.end(); ++i )
00102   {
00103     stream << "[SESSION]" << std::endl;
00104     const Dictionary& section = s.get( *i );
00105     if( !section.size() ) continue;
00106 
00107     Dictionary::iterator i;
00108     for( i = section.begin(); i != section.end(); ++i )
00109     {
00110       if( defaults.has(i->first) && defaults.getString(i->first) == i->second )
00111         continue;
00112       stream << i->first << "=" << i->second << std::endl;
00113     }
00114     stream << std::endl;
00115   }
00116 
00117   return stream;
00118 }
00119 
00120 const bool SessionSettings::has( const SessionID& sessionID ) const
00121 {
00122   return m_settings.find( sessionID ) != m_settings.end();
00123 }
00124 
00125 const Dictionary& SessionSettings::get( const SessionID& sessionID ) const
00126 throw( ConfigError )
00127 {
00128   Dictionaries::const_iterator i;
00129   i = m_settings.find( sessionID );
00130   if ( i == m_settings.end() ) throw ConfigError( "Session not found" );
00131   return i->second;
00132 }
00133 
00134 void SessionSettings::set( const SessionID& sessionID,
00135                            Dictionary settings )
00136 throw( ConfigError )
00137 {
00138   if( has(sessionID) )
00139     throw ConfigError( "Duplicate Session " + sessionID.toString() );
00140 
00141   settings.setString( BEGINSTRING, sessionID.getBeginString() );
00142   settings.setString( SENDERCOMPID, sessionID.getSenderCompID() );
00143   settings.setString( TARGETCOMPID, sessionID.getTargetCompID() );
00144 
00145   settings.merge( m_defaults );
00146   validate( settings );
00147   m_settings[ sessionID ] = settings;
00148 }
00149 
00150 void SessionSettings::set( const Dictionary& defaults ) throw( ConfigError ) 
00151 { 
00152   m_defaults = defaults;
00153   Dictionaries::iterator i = m_settings.begin();
00154   for( i = m_settings.begin(); i != m_settings.end(); ++i )
00155     i->second.merge( defaults );
00156 }
00157 
00158 std::set < SessionID > SessionSettings::getSessions() const
00159 {
00160   std::set < SessionID > result;
00161   Dictionaries::const_iterator i;
00162   for ( i = m_settings.begin(); i != m_settings.end(); ++i )
00163     result.insert( i->first );
00164   return result;
00165 }
00166 
00167 void SessionSettings::validate( const Dictionary& dictionary ) const
00168 throw( ConfigError )
00169 {
00170   std::string beginString = dictionary.getString( BEGINSTRING );
00171   if( beginString != BeginString_FIX40 &&
00172       beginString != BeginString_FIX41 &&
00173       beginString != BeginString_FIX42 &&
00174       beginString != BeginString_FIX43 &&
00175       beginString != BeginString_FIX44 &&
00176       beginString != BeginString_FIXT11 )
00177   {
00178     throw ConfigError( std::string(BEGINSTRING) + " must be FIX.4.0 to FIX.4.4 or FIXT.1.1" );
00179   }
00180 
00181   std::string connectionType = dictionary.getString( CONNECTION_TYPE );
00182   if( connectionType != "initiator" &&
00183       connectionType != "acceptor" )
00184   {
00185     throw ConfigError( std::string(CONNECTION_TYPE) + " must be 'initiator' or 'acceptor'" );
00186   }
00187 }
00188 
00189 }

Generated on Mon Jun 23 2014 23:49:38 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001