Message.h
Go to the documentation of this file.
/* -*- C++ -*- */
/****************************************************************************
** 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.
**
****************************************************************************/
#ifndef FIX_MESSAGE
#define FIX_MESSAGE
#ifdef _MSC_VER
#pragma warning( disable: 4786 )
#endif
#include "FieldMap.h"
#include "Fields.h"
#include "Group.h"
#include "SessionID.h"
#include "DataDictionary.h"
#include "Values.h"
#include <vector>
#include <memory>
namespace FIX
{
static int const headerOrder[] =
{
FIELD::BeginString,
FIELD::BodyLength,
FIELD::MsgType
};
class Header : public FieldMap
{
public:
Header() : FieldMap(message_order( message_order::header ) )
{}
};
class Trailer : public FieldMap
{
public:
Trailer() : FieldMap(message_order( message_order::trailer ) )
{}
};
class Message : public FieldMap
{
friend class DataDictionary;
friend class Session;
enum field_type { header, body, trailer };
public:
Message();
Message( const std::string& string, bool validate = true )
throw( InvalidMessage );
Message( const std::string& string, const FIX::DataDictionary& dataDictionary,
bool validate = true )
throw( InvalidMessage );
Message( const std::string& string, const FIX::DataDictionary& sessionDataDictionary,
const FIX::DataDictionary& applicationDataDictionary, bool validate = true )
throw( InvalidMessage );
Message( const Message& copy )
: FieldMap( copy )
{
m_header = copy.m_header;
m_trailer = copy.m_trailer;
m_validStructure = copy.m_validStructure;
m_field = copy.m_field;
}
static bool InitializeXML( const std::string& string );
void addGroup( const FIX::Group& group )
{ FieldMap::addGroup( group.field(), group ); }
void replaceGroup( unsigned num, const FIX::Group& group )
{ FieldMap::replaceGroup( num, group.field(), group ); }
Group& getGroup( unsigned num, FIX::Group& group ) const throw( FieldNotFound )
{ group.clear();
return static_cast < Group& >
( FieldMap::getGroup( num, group.field(), group ) );
}
void removeGroup( unsigned num, const FIX::Group& group )
{ FieldMap::removeGroup( num, group.field() ); }
void removeGroup( const FIX::Group& group )
{ FieldMap::removeGroup( group.field() ); }
bool hasGroup( const FIX::Group& group ) const
{ return FieldMap::hasGroup( group.field() ); }
bool hasGroup( unsigned num, const FIX::Group& group ) const
{ return FieldMap::hasGroup( num, group.field() ); }
protected:
// Constructor for derived classes
Message( const BeginString& beginString, const MsgType& msgType )
: m_validStructure( true )
{
m_header.setField( beginString );
m_header.setField( msgType );
}
public:
std::string toString( int beginStringField = FIELD::BeginString,
int bodyLengthField = FIELD::BodyLength,
int checkSumField = FIELD::CheckSum ) const;
std::string& toString( std::string&,
int beginStringField = FIELD::BeginString,
int bodyLengthField = FIELD::BodyLength,
int checkSumField = FIELD::CheckSum ) const;
std::string toXML() const;
std::string& toXML( std::string& ) const;
void reverseRoute( const Header& );
void setString( const std::string& string )
throw( InvalidMessage )
{ setString(string, true); }
void setString( const std::string& string, bool validate )
throw( InvalidMessage )
{ setString(string, validate, 0); }
void setString( const std::string& string,
bool validate,
const FIX::DataDictionary* pDataDictionary )
throw( InvalidMessage )
{ setString(string, validate, pDataDictionary, pDataDictionary); }
void setString( const std::string& string,
bool validate,
const FIX::DataDictionary* pSessionDataDictionary,
const FIX::DataDictionary* pApplicationDataDictionary )
throw( InvalidMessage );
void setGroup( const std::string& msg, const FieldBase& field,
const std::string& string, std::string::size_type& pos,
FieldMap& map, const DataDictionary& dataDictionary );
bool setStringHeader( const std::string& string );
const Header& getHeader() const { return m_header; }
Header& getHeader() { return m_header; }
const Trailer& getTrailer() const { return m_trailer; }
Trailer& getTrailer() { return m_trailer; }
bool hasValidStructure(int& field) const
{ field = m_field;
return m_validStructure;
}
int bodyLength( int beginStringField = FIELD::BeginString,
int bodyLengthField = FIELD::BodyLength,
int checkSumField = FIELD::CheckSum ) const
{ return m_header.calculateLength(beginStringField, bodyLengthField, checkSumField)
+ calculateLength(beginStringField, bodyLengthField, checkSumField)
+ m_trailer.calculateLength(beginStringField, bodyLengthField, checkSumField);
}
int checkSum( int checkSumField = FIELD::CheckSum ) const
{ return ( m_header.calculateTotal(checkSumField)
+ calculateTotal(checkSumField)
+ m_trailer.calculateTotal(checkSumField) ) % 256;
}
bool isAdmin() const
{
if( m_header.isSetField(FIELD::MsgType) )
{
const MsgType& msgType = FIELD_GET_REF( m_header, MsgType );
return isAdminMsgType( msgType );
}
return false;
}
bool isApp() const
{
if( m_header.isSetField(FIELD::MsgType) )
{
const MsgType& msgType = FIELD_GET_REF( m_header, MsgType );
return !isAdminMsgType( msgType );
}
return false;
}
bool isEmpty()
{ return m_header.isEmpty() && FieldMap::isEmpty() && m_trailer.isEmpty(); }
void clear()
{
m_field = 0;
m_header.clear();
FieldMap::clear();
m_trailer.clear();
}
static bool isAdminMsgType( const MsgType& msgType )
{ if ( msgType.getValue().length() != 1 ) return false;
return strchr
( "0A12345",
msgType.getValue().c_str() [ 0 ] ) != 0;
}
static ApplVerID toApplVerID(const BeginString& value)
{
if( value == BeginString_FIX40 )
return ApplVerID(ApplVerID_FIX40);
if( value == BeginString_FIX41 )
return ApplVerID(ApplVerID_FIX41);
if( value == BeginString_FIX42 )
return ApplVerID(ApplVerID_FIX42);
if( value == BeginString_FIX43 )
return ApplVerID(ApplVerID_FIX43);
if( value == BeginString_FIX44 )
return ApplVerID(ApplVerID_FIX44);
if( value == BeginString_FIX50 )
return ApplVerID(ApplVerID_FIX50);
if( value == "FIX.5.0SP1" )
return ApplVerID(ApplVerID_FIX50SP1);
if( value == "FIX.5.0SP2" )
return ApplVerID(ApplVerID_FIX50SP2);
return ApplVerID(ApplVerID(value));
}
static BeginString toBeginString( const ApplVerID& applVerID )
{
if( applVerID == ApplVerID_FIX40 )
return BeginString(BeginString_FIX40);
else if( applVerID == ApplVerID_FIX41 )
return BeginString(BeginString_FIX41);
else if( applVerID == ApplVerID_FIX42 )
return BeginString(BeginString_FIX42);
else if( applVerID == ApplVerID_FIX43 )
return BeginString(BeginString_FIX43);
else if( applVerID == ApplVerID_FIX44 )
return BeginString(BeginString_FIX44);
else if( applVerID == ApplVerID_FIX50 )
return BeginString(BeginString_FIX50);
else if( applVerID == ApplVerID_FIX50SP1 )
return BeginString(BeginString_FIX50);
else if( applVerID == ApplVerID_FIX50SP2 )
return BeginString(BeginString_FIX50);
else
return BeginString("");
}
static bool isHeaderField( int field );
static bool isHeaderField( const FieldBase& field,
const DataDictionary* pD = 0 );
static bool isTrailerField( int field );
static bool isTrailerField( const FieldBase& field,
const DataDictionary* pD = 0 );
SessionID getSessionID( const std::string& qualifier = "" ) const
throw( FieldNotFound );
void setSessionID( const SessionID& sessionID );
private:
FieldBase extractField(
const std::string& string, std::string::size_type& pos,
const DataDictionary* pSessionDD = 0, const DataDictionary* pAppDD = 0,
const Group* pGroup = 0);
static bool IsDataField(
int field,
const DataDictionary* pSessionDD,
const DataDictionary* pAppDD )
{
if( (pSessionDD && pSessionDD->isDataField( field )) ||
(pAppDD && pAppDD != pSessionDD && pAppDD->isDataField( field )) )
{
return true;
}
return false;
}
void validate();
std::string toXMLFields(const FieldMap& fields, int space) const;
protected:
mutable Header m_header;
mutable Trailer m_trailer;
bool m_validStructure;
int m_field;
static std::auto_ptr<DataDictionary> s_dataDictionary;
};
inline std::ostream& operator <<
( std::ostream& stream, const Message& message )
{
std::string str;
stream << message.toString( str );
return stream;
}
inline MsgType identifyType( const std::string& message )
throw( MessageParseError )
{
std::string::size_type pos = message.find( "\001" "35=" );
if ( pos == std::string::npos ) throw MessageParseError();
std::string::size_type startValue = pos + 4;
std::string::size_type soh = message.find_first_of( '\001', startValue );
if ( soh == std::string::npos ) throw MessageParseError();
std::string value = message.substr( startValue, soh - startValue );
return MsgType( value );
}
}
#endif //FIX_MESSAGE
FIX::FIELD::ApplVerID
const int ApplVerID
Definition: FixFieldNumbers.h:425
FIX::Message::checkSum
int checkSum(int checkSumField=FIELD::CheckSum) const
Definition: Message.h:213
FIX::Message::getGroup
Group & getGroup(unsigned num, FIX::Group &group) const
Definition: Message.h:109
FIX::FieldMap::calculateTotal
int calculateTotal(int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:218
FIX::FieldMap::hasGroup
bool hasGroup(int field) const
Check to see any instance of a group exists.
Definition: FieldMap.cpp:129
FIX::BeginString_FIX44
const char BeginString_FIX44[]
Definition: Values.h:32
FIX::Message::isAdminMsgType
static bool isAdminMsgType(const MsgType &msgType)
Definition: Message.h:250
FIX::Trailer
Definition: Message.h:54
FIX::Message::getTrailer
Trailer & getTrailer()
Mutable getter for the message trailer.
Definition: Message.h:198
FIX::Message::IsDataField
static bool IsDataField(int field, const DataDictionary *pSessionDD, const DataDictionary *pAppDD)
Definition: Message.h:320
FIX::Message::Message
Message(const Message &copy)
Definition: Message.h:91
FIX::ApplVerID_FIX44
const char ApplVerID_FIX44[]
Definition: FixValues.h:816
FIX::DataDictionary
Represents a data dictionary for a version of FIX.
Definition: DataDictionary.h:49
FIX::BeginString_FIX42
const char BeginString_FIX42[]
Definition: Values.h:34
FIX::Session
Maintains the state and implements the logic of a FIX session.
Definition: Session.h:45
FIX::MessageParseError
Unable to parse message.
Definition: Exceptions.h:73
FIX::Message::isApp
bool isApp() const
Definition: Message.h:229
FIX::DataDictionary::isDataField
bool isDataField(int field) const
Definition: DataDictionary.h:285
Group.h
FIX::Message::toApplVerID
static ApplVerID toApplVerID(const BeginString &value)
Definition: Message.h:257
FIX::Header::Header
Header()
Definition: Message.h:50
FIX::Trailer::Trailer
Trailer()
Definition: Message.h:57
FIX::FieldMap::removeGroup
void removeGroup(int num, int field)
Remove a specific instance of a group.
Definition: FieldMap.cpp:86
SessionID.h
FIX::BeginString_FIX50
const char BeginString_FIX50[]
Definition: Values.h:31
FIX::Message::replaceGroup
void replaceGroup(unsigned num, const FIX::Group &group)
Definition: Message.h:106
FIX::Message::setString
void setString(const std::string &string, bool validate)
Definition: Message.h:165
FIX::Message::isEmpty
bool isEmpty()
Definition: Message.h:239
FIX::Group::field
int field() const
Definition: Group.h:56
FIX::Header
Definition: Message.h:47
FIX::BeginString_FIX43
const char BeginString_FIX43[]
Definition: Values.h:33
FIX
Definition: Acceptor.cpp:34
FIX::Message::getHeader
const Header & getHeader() const
Getter for the message header.
Definition: Message.h:192
FIX::Group
Base class for all FIX repeating groups.
Definition: Group.h:40
FIX::FieldMap
Stores and organizes a collection of Fields.
Definition: FieldMap.h:46
FIX::ApplVerID_FIX40
const char ApplVerID_FIX40[]
Definition: FixValues.h:811
FIX::Message::removeGroup
void removeGroup(unsigned num, const FIX::Group &group)
Definition: Message.h:115
FIX::Message::bodyLength
int bodyLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition: Message.h:205
FIX::Message::getTrailer
const Trailer & getTrailer() const
Getter for the message trailer.
Definition: Message.h:196
FIX::FieldMap::isEmpty
bool isEmpty()
Check if map contains any fields.
Definition: FieldMap.cpp:157
FIX::Message::hasGroup
bool hasGroup(unsigned num, const FIX::Group &group) const
Definition: Message.h:122
FIX::ApplVerID_FIX43
const char ApplVerID_FIX43[]
Definition: FixValues.h:815
FIX::Message
Base class for all FIX messages.
Definition: Message.h:67
FIX::FieldNotFound
Field not found inside a message.
Definition: Exceptions.h:57
DataDictionary.h
FIX::FieldMap::calculateLength
int calculateLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:194
FieldMap.h
FIX::ApplVerID_FIX50SP1
const char ApplVerID_FIX50SP1[]
Definition: FixValues.h:807
FIX::Message::hasGroup
bool hasGroup(const FIX::Group &group) const
Definition: Message.h:120
FIX::Message::getHeader
Header & getHeader()
Mutable getter for the message header.
Definition: Message.h:194
FIX::BeginString_FIX41
const char BeginString_FIX41[]
Definition: Values.h:35
FIX::Message::addGroup
void addGroup(const FIX::Group &group)
Definition: Message.h:103
FIX::FieldMap::addGroup
void addGroup(int field, const FieldMap &group, bool setCount=true)
Add a group.
Definition: FieldMap.cpp:58
FIX::Message::removeGroup
void removeGroup(const FIX::Group &group)
Definition: Message.h:117
FIX::Message::field_type
field_type
Definition: Message.h:72
FIX::Message::m_field
int m_field
Definition: Message.h:341
FIX::Message::s_dataDictionary
static std::auto_ptr< DataDictionary > s_dataDictionary
Definition: Message.h:342
FIX::FieldBase
Base representation of all Field classes.
Definition: Field.h:45
FIX::message_order
Sorts fields in header, normal, or trailer order.
Definition: MessageSorters.h:113
FIX::Message::clear
void clear()
Definition: Message.h:242
FIX::Message::m_header
Header m_header
Definition: Message.h:338
FIX::FIELD::BeginString
const int BeginString
Definition: FixFieldNumbers.h:499
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition: SessionID.h:30
FIX::BeginString_FIX40
const char BeginString_FIX40[]
Definition: Values.h:36
FIX::identifyType
MsgType identifyType(const std::string &message)
Parse the type of a message from a string.
Definition: Message.h:355
FIX::ApplVerID_FIX50SP2
const char ApplVerID_FIX50SP2[]
Definition: FixValues.h:809
FIX::ApplVerID_FIX42
const char ApplVerID_FIX42[]
Definition: FixValues.h:814
FIX::InvalidMessage
Not a recognizable message.
Definition: Exceptions.h:80
FIX::Message::hasValidStructure
bool hasValidStructure(int &field) const
Definition: Message.h:200
FIX::FIELD::BodyLength
const int BodyLength
Definition: FixFieldNumbers.h:1478
FIX::Message::setString
void setString(const std::string &string)
Set a message based on a string representation This will fill in the fields on the message by parsing...
Definition: Message.h:162
FIELD_GET_REF
#define FIELD_GET_REF(MAP, FLD)
Definition: FieldMap.h:238
FIX::Message::Message
Message(const BeginString &beginString, const MsgType &msgType)
Definition: Message.h:127
FIX::FieldMap::replaceGroup
void replaceGroup(int num, int field, const FieldMap &group)
Replace a specific instance of a group.
Definition: FieldMap.cpp:77
FIX::FIELD::MsgType
const int MsgType
Definition: FixFieldNumbers.h:363
Fields.h
FIX::ApplVerID_FIX50
const char ApplVerID_FIX50[]
Definition: FixValues.h:810
FIX::Message::m_validStructure
bool m_validStructure
Definition: Message.h:340
FIX::FIELD::CheckSum
const int CheckSum
Definition: FixFieldNumbers.h:1426
FIX::headerOrder
static int const headerOrder[]
Definition: Message.h:40
Values.h
FIX::ApplVerID_FIX41
const char ApplVerID_FIX41[]
Definition: FixValues.h:812
FIX::Message::m_trailer
Trailer m_trailer
Definition: Message.h:339
FIX::FieldMap::clear
void clear()
Clear all fields from the map.
Definition: FieldMap.cpp:143
FIX::Message::toBeginString
static BeginString toBeginString(const ApplVerID &applVerID)
Definition: Message.h:278
FIX::Message::isAdmin
bool isAdmin() const
Definition: Message.h:219
FIX::Message::setString
void setString(const std::string &string, bool validate, const FIX::DataDictionary *pDataDictionary)
Definition: Message.h:168
FIX::FieldMap::getGroup
FieldMap & getGroup(int num, int field, FieldMap &group) const
Get a specific instance of a group.
Definition: FieldMap.h:163