Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy 

SessionSettings.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) quickfixengine.org  All rights reserved.
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 #include "CallStack.h"
00026 
00027 #include "SessionSettings.h"
00028 #include "Settings.h"
00029 #include "Values.h"
00030 #include <fstream>
00031 
00032 namespace FIX
00033 {
00034 SessionSettings::SessionSettings( std::istream& stream )
00035 throw( ConfigError )
00036 {
00037   stream >> *this;
00038 }
00039 
00040 SessionSettings::SessionSettings( const std::string& file )
00041 throw( ConfigError )
00042 {
00043   std::ifstream fstream( file.c_str() );
00044   if ( !fstream.is_open() )
00045     throw ConfigError( ( "File " + file + " not found" ).c_str() );
00046   fstream >> *this;
00047 }
00048 
00049 std::istream& operator>>( std::istream& stream, SessionSettings& s )
00050 throw( ConfigError )
00051 {
00052   Settings settings;
00053   stream >> settings;
00054 
00055   Settings::Sections section;
00056 
00057   section = settings.get( "DEFAULT" );
00058   Dictionary def;
00059   if ( section.size() )
00060     def = section[ 0 ];
00061   s.set( def );
00062 
00063   section = settings.get( "SESSION" );
00064   Settings::Sections::size_type session;
00065   Dictionary dict;
00066 
00067   for ( session = 0; session < section.size(); ++session )
00068   {
00069     dict = section[ session ];
00070     dict.merge( def );
00071 
00072     BeginString beginString
00073     ( dict.getString( BEGINSTRING ) );
00074     SenderCompID senderCompID
00075     ( dict.getString( SENDERCOMPID ) );
00076     TargetCompID targetCompID
00077     ( dict.getString( TARGETCOMPID ) );
00078 
00079     std::string sessionQualifier;
00080     if( dict.has( SESSION_QUALIFIER ) )
00081       sessionQualifier = dict.getString( SESSION_QUALIFIER );
00082     SessionID sessionID( beginString, senderCompID, targetCompID, sessionQualifier );
00083     s.set( sessionID, dict );
00084   }
00085   return stream;
00086 }
00087 
00088 std::ostream& operator<<( std::ostream& stream, const SessionSettings& s )
00089 {
00090   const Dictionary& defaults = s.m_defaults;
00091   if( defaults.size() )
00092   {
00093     stream << "[DEFAULT]" << std::endl;
00094     Dictionary::iterator i;
00095     for( i = defaults.begin(); i != defaults.end(); ++i )
00096       stream << i->first << "=" << i->second << std::endl;
00097     stream << std::endl;
00098   }
00099 
00100   std::set<SessionID> sessions = s.getSessions();
00101   std::set<SessionID>::iterator i;
00102   for( i = sessions.begin(); i != sessions.end(); ++i )
00103   {
00104     stream << "[SESSION]" << std::endl;
00105     const Dictionary& section = s.get( *i );
00106     if( !section.size() ) continue;
00107 
00108     Dictionary::iterator i;
00109     for( i = section.begin(); i != section.end(); ++i )
00110     {
00111       if( defaults.has(i->first) && defaults.getString(i->first) == i->second )
00112         continue;
00113       stream << i->first << "=" << i->second << std::endl;
00114     }
00115     stream << std::endl;
00116   }
00117 
00118   return stream;
00119 }
00120 
00121 const bool SessionSettings::has( const SessionID& sessionID ) const
00122 { QF_STACK_PUSH(SessionSettings::has)
00123   return m_settings.find( sessionID ) != m_settings.end();
00124   QF_STACK_POP
00125 }
00126 
00127 const Dictionary& SessionSettings::get( const SessionID& sessionID ) const
00128 throw( ConfigError )
00129 { QF_STACK_PUSH(SessionSettings::get)
00130 
00131   Dictionaries::const_iterator i;
00132   i = m_settings.find( sessionID );
00133   if ( i == m_settings.end() ) throw ConfigError( "Session not found" );
00134   return i->second;
00135 
00136   QF_STACK_POP
00137 }
00138 
00139 void SessionSettings::set( const SessionID& sessionID,
00140                            Dictionary settings )
00141 throw( ConfigError )
00142 { QF_STACK_PUSH(SessionSettings::set)
00143 
00144   if( has(sessionID) )
00145     throw ConfigError( "Duplicate Session " + sessionID.toString() );
00146 
00147   settings.setString( BEGINSTRING, sessionID.getBeginString() );
00148   settings.setString( SENDERCOMPID, sessionID.getSenderCompID() );
00149   settings.setString( TARGETCOMPID, sessionID.getTargetCompID() );
00150 
00151   settings.merge( m_defaults );
00152   validate( settings );
00153   m_settings[ sessionID ] = settings;
00154 
00155   QF_STACK_POP
00156 }
00157 
00158 void SessionSettings::set( const Dictionary& defaults ) throw( ConfigError ) 
00159 { 
00160   m_defaults = defaults;
00161   Dictionaries::iterator i = m_settings.begin();
00162   for( i = m_settings.begin(); i != m_settings.end(); ++i )
00163     i->second.merge( defaults );
00164 }
00165 
00166 std::set < SessionID > SessionSettings::getSessions() const
00167 { QF_STACK_PUSH(SessionSettings::getSessions)
00168 
00169   std::set < SessionID > result;
00170   Dictionaries::const_iterator i;
00171   for ( i = m_settings.begin(); i != m_settings.end(); ++i )
00172     result.insert( i->first );
00173   return result;
00174 
00175   QF_STACK_POP
00176 }
00177 
00178 void SessionSettings::validate( const Dictionary& dictionary ) const
00179 throw( ConfigError )
00180 {
00181   std::string beginString = dictionary.getString( BEGINSTRING );
00182   if( beginString != BeginString_FIX40 &&
00183       beginString != BeginString_FIX41 &&
00184       beginString != BeginString_FIX42 &&
00185       beginString != BeginString_FIX43 &&
00186       beginString != BeginString_FIX44 &&
00187       beginString != BeginString_FIXT11 )
00188   {
00189     throw ConfigError( std::string(BEGINSTRING) + " must be FIX.4.0 to FIX.4.4 or FIXT.1.1" );
00190   }
00191 
00192   std::string connectionType = dictionary.getString( CONNECTION_TYPE );
00193   if( connectionType != "initiator" &&
00194       connectionType != "acceptor" )
00195   {
00196     throw ConfigError( std::string(CONNECTION_TYPE) + " must be 'initiator' or 'acceptor'" );
00197   }
00198 }
00199 
00200 }

Generated on Mon Apr 5 20:59:51 2010 for QuickFIX by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2001