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
{
class Header : public FieldMap
{
enum { REQUIRED_FIELDS = 8 };
public:
Header() : FieldMap( message_order( message_order::header ), REQUIRED_FIELDS )
{}
Header(const message_order & order) : FieldMap(order)
{}
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() ); }
};
class Trailer : public FieldMap
{
enum { REQUIRED_FIELDS = 1 };
public:
Trailer() : FieldMap( message_order( message_order::trailer ), REQUIRED_FIELDS )
{}
Trailer(const message_order & order) : FieldMap(order)
{}
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() ); }
};
class Message : public FieldMap
{
friend class DataDictionary;
friend class Session;
enum field_type { header, body, trailer };
public:
Message();
Message( const message_order& hdrOrder, const message_order& trlOrder, const message_order& order);
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_order& hdrOrder, const message_order& trlOrder, const message_order& order, const std::string& string, const FIX::DataDictionary& dataDictionary,
bool validate = true )
throw( InvalidMessage );
Message( const message_order& hdrOrder, const message_order& trlOrder, const message_order& order, const std::string& string, const FIX::DataDictionary& sessionDataDictionary,
const FIX::DataDictionary& applicationDataDictionary, bool validate = true )
throw( InvalidMessage );
Message( const Message& copy );
~Message();
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 );
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& tag) const
{ tag = m_tag;
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
{
MsgType msgType;
if( m_header.getFieldIfSet( msgType ) )
return isAdminMsgType( msgType );
return false;
}
bool isApp() const
{
MsgType msgType;
if( m_header.getFieldIfSet( msgType ) )
return !isAdminMsgType( msgType );
return false;
}
bool isEmpty()
{ return m_header.isEmpty() && FieldMap::isEmpty() && m_trailer.isEmpty(); }
void clear()
{
m_tag = 0;
m_validStructure = true;
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 isHeaderField( int field,
const DataDictionary* pD );
static bool isTrailerField( int field );
static bool isTrailerField( const FieldBase& field,
const DataDictionary* pD = 0 );
static bool isTrailerField( int field,
const DataDictionary* pD );
SessionID getSessionID( const std::string& qualifier = "" ) const
throw( FieldNotFound );
void setSessionID( const SessionID& sessionID );
#ifdef HAVE_EMX
void setSubMessageType(const std::string & subMsgType) { m_subMsgType.assign(subMsgType); }
const std::string & getSubMessageType() const { return m_subMsgType; }
#endif
private:
FieldBase extractField(
const std::string& string, std::string::size_type& pos,
const DataDictionary* pSessionDD = 0, const DataDictionary* pAppDD = 0,
const Group* pGroup = 0) const;
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() const;
std::string toXMLFields(const FieldMap& fields, int space) const;
protected:
mutable Header m_header;
mutable Trailer m_trailer;
bool m_validStructure;
int m_tag;
#ifdef HAVE_EMX
std::string m_subMsgType;
#endif
static SmartPtr<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
DataDictionary.h
FieldMap.h
Fields.h
Group.h
SessionID.h
Values.h
FIX::DataDictionary
Represents a data dictionary for a version of FIX.
Definition DataDictionary.h:50
FIX::DataDictionary::isDataField
bool isDataField(int field) const
Definition DataDictionary.h:343
FIX::FieldBase
Base representation of all Field classes.
Definition Field.h:50
FIX::FieldMap
Stores and organizes a collection of Fields.
Definition FieldMap.h:47
FIX::FieldMap::replaceGroup
void replaceGroup(int num, int tag, const FieldMap &group)
Replace a specific instance of a group.
Definition FieldMap.cpp:102
FIX::FieldMap::isEmpty
bool isEmpty()
Check if map contains any fields.
Definition FieldMap.cpp:196
FIX::FieldMap::calculateLength
int calculateLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition FieldMap.cpp:233
FIX::FieldMap::clear
void clear()
Clear all fields from the map.
Definition FieldMap.cpp:182
FIX::FieldMap::hasGroup
bool hasGroup(int tag) const
Check to see any instance of a group exists.
Definition FieldMap.cpp:168
FIX::FieldMap::getFieldIfSet
bool getFieldIfSet(FieldBase &field) const
Get a field if set.
Definition FieldMap.h:146
FIX::FieldMap::addGroup
void addGroup(int tag, const FieldMap &group, bool setCount=true)
Add a group.
Definition FieldMap.cpp:83
FIX::FieldMap::calculateTotal
int calculateTotal(int checkSumField=FIELD::CheckSum) const
Definition FieldMap.cpp:258
FIX::FieldMap::removeGroup
void removeGroup(int num, int tag)
Remove a specific instance of a group.
Definition FieldMap.cpp:111
FIX::FieldMap::getGroup
FieldMap & getGroup(int num, int tag, FieldMap &group) const
Get a specific instance of a group.
Definition FieldMap.h:207
FIX::Group
Base class for all FIX repeating groups.
Definition Group.h:41
FIX::Group::field
int field() const
Definition Group.h:56
FIX::Header
Definition Message.h:42
FIX::Header::getGroup
Group & getGroup(unsigned num, FIX::Group &group) const
Definition Message.h:58
FIX::Header::REQUIRED_FIELDS
@ REQUIRED_FIELDS
Definition Message.h:43
FIX::Header::hasGroup
bool hasGroup(unsigned num, const FIX::Group &group) const
Definition Message.h:71
FIX::Header::addGroup
void addGroup(const FIX::Group &group)
Definition Message.h:52
FIX::Header::Header
Header(const message_order &order)
Definition Message.h:49
FIX::Header::removeGroup
void removeGroup(unsigned num, const FIX::Group &group)
Definition Message.h:64
FIX::Header::replaceGroup
void replaceGroup(unsigned num, const FIX::Group &group)
Definition Message.h:55
FIX::Header::Header
Header()
Definition Message.h:46
FIX::Header::hasGroup
bool hasGroup(const FIX::Group &group) const
Definition Message.h:69
FIX::Header::removeGroup
void removeGroup(const FIX::Group &group)
Definition Message.h:66
FIX::Message
Base class for all FIX messages.
Definition Message.h:118
FIX::Message::getGroup
Group & getGroup(unsigned num, FIX::Group &group) const
Definition Message.h:167
FIX::Message::clear
void clear()
Definition Message.h:291
FIX::Message::getSessionID
SessionID getSessionID(const std::string &qualifier="") const
Returns the session ID of the intended recipient.
Definition Message.cpp:576
FIX::Message::InitializeXML
static bool InitializeXML(const std::string &string)
Set global data dictionary for encoding messages into XML.
Definition Message.cpp:139
FIX::Message::bodyLength
int bodyLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition Message.h:258
FIX::Message::Message
Message()
Definition Message.cpp:43
FIX::Message::m_trailer
Trailer m_trailer
Definition Message.h:398
FIX::Message::removeGroup
void removeGroup(unsigned num, const FIX::Group &group)
Definition Message.h:173
FIX::Message::validate
void validate() const
Definition Message.cpp:597
FIX::Message::isHeaderField
static bool isHeaderField(int field)
Definition Message.cpp:497
FIX::Message::IsDataField
static bool IsDataField(int field, const DataDictionary *pSessionDD, const DataDictionary *pAppDD)
Definition Message.h:379
FIX::Message::setString
void setString(const std::string &string, bool validate)
Definition Message.h:218
FIX::Message::getTrailer
const Trailer & getTrailer() const
Getter for the message trailer.
Definition Message.h:249
FIX::Message::m_tag
int m_tag
Definition Message.h:400
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:475
FIX::Message::isTrailerField
static bool isTrailerField(int field)
Definition Message.cpp:550
FIX::Message::toApplVerID
static ApplVerID toApplVerID(const BeginString &value)
Definition Message.h:307
FIX::Message::isApp
bool isApp() const
Definition Message.h:280
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:215
FIX::Message::toBeginString
static BeginString toBeginString(const ApplVerID &applVerID)
Definition Message.h:328
FIX::Message::getHeader
Header & getHeader()
Mutable getter for the message header.
Definition Message.h:247
FIX::Message::isAdminMsgType
static bool isAdminMsgType(const MsgType &msgType)
Definition Message.h:300
FIX::Message::toXMLFields
std::string toXMLFields(const FieldMap &fields, int space) const
Definition Message.cpp:295
FIX::Message::DataDictionary
friend class DataDictionary
Definition Message.h:119
FIX::Message::toXML
std::string toXML() const
Get a XML representation of the message.
Definition Message.cpp:270
FIX::Message::m_validStructure
bool m_validStructure
Definition Message.h:399
FIX::Message::checkSum
int checkSum(int checkSumField=FIELD::CheckSum) const
Definition Message.h:266
FIX::Message::setString
void setString(const std::string &string, bool validate, const FIX::DataDictionary *pDataDictionary)
Definition Message.h:221
FIX::Message::getTrailer
Trailer & getTrailer()
Mutable getter for the message trailer.
Definition Message.h:251
FIX::Message::toString
std::string toString(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Get a string representation of the message.
Definition Message.cpp:236
FIX::Message::setSessionID
void setSessionID(const SessionID &sessionID)
Sets the session ID of the intended recipient.
Definition Message.cpp:590
FIX::Message::s_dataDictionary
static SmartPtr< DataDictionary > s_dataDictionary
Definition Message.h:404
FIX::Message::removeGroup
void removeGroup(const FIX::Group &group)
Definition Message.h:175
FIX::Message::hasValidStructure
bool hasValidStructure(int &tag) const
Definition Message.h:253
FIX::Message::reverseRoute
void reverseRoute(const Header &)
Add header informations depending on a source message.
Definition Message.cpp:150
FIX::Message::isEmpty
bool isEmpty()
Definition Message.h:288
FIX::Message::m_header
Header m_header
Definition Message.h:397
FIX::Message::field_type
field_type
Definition Message.h:122
FIX::Message::trailer
@ trailer
Definition Message.h:122
FIX::Message::body
@ body
Definition Message.h:122
FIX::Message::header
@ header
Definition Message.h:122
FIX::Message::replaceGroup
void replaceGroup(unsigned num, const FIX::Group &group)
Definition Message.h:164
FIX::Message::hasGroup
bool hasGroup(const FIX::Group &group) const
Definition Message.h:178
FIX::Message::getHeader
const Header & getHeader() const
Getter for the message header.
Definition Message.h:245
FIX::Message::addGroup
void addGroup(const FIX::Group &group)
Definition Message.h:161
FIX::Message::hasGroup
bool hasGroup(unsigned num, const FIX::Group &group) const
Definition Message.h:180
FIX::Message::isAdmin
bool isAdmin() const
Definition Message.h:272
FIX::Message::extractField
FieldBase extractField(const std::string &string, std::string::size_type &pos, const DataDictionary *pSessionDD=0, const DataDictionary *pAppDD=0, const Group *pGroup=0) const
Definition Message.cpp:639
FIX::Message::setGroup
void setGroup(const std::string &msg, const FieldBase &field, const std::string &string, std::string::size_type &pos, FieldMap &map, const DataDictionary &dataDictionary)
Definition Message.cpp:431
FIX::Session
Maintains the state and implements the logic of a FIX session.
Definition Session.h:46
FIX::SessionID
Unique session id consists of BeginString, SenderCompID and TargetCompID.
Definition SessionID.h:31
FIX::Trailer
Definition Message.h:77
FIX::Trailer::addGroup
void addGroup(const FIX::Group &group)
Definition Message.h:87
FIX::Trailer::Trailer
Trailer(const message_order &order)
Definition Message.h:84
FIX::Trailer::Trailer
Trailer()
Definition Message.h:81
FIX::Trailer::replaceGroup
void replaceGroup(unsigned num, const FIX::Group &group)
Definition Message.h:90
FIX::Trailer::hasGroup
bool hasGroup(const FIX::Group &group) const
Definition Message.h:104
FIX::Trailer::removeGroup
void removeGroup(const FIX::Group &group)
Definition Message.h:101
FIX::Trailer::getGroup
Group & getGroup(unsigned num, FIX::Group &group) const
Definition Message.h:93
FIX::Trailer::REQUIRED_FIELDS
@ REQUIRED_FIELDS
Definition Message.h:78
FIX::Trailer::hasGroup
bool hasGroup(unsigned num, const FIX::Group &group) const
Definition Message.h:106
FIX::Trailer::removeGroup
void removeGroup(unsigned num, const FIX::Group &group)
Definition Message.h:99
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::BeginString_FIX50
const char BeginString_FIX50[]
Definition Values.h:31
FIX::BeginString_FIX43
const char BeginString_FIX43[]
Definition Values.h:33
FIX::BeginString_FIX44
const char BeginString_FIX44[]
Definition Values.h:32
FIX::identifyType
MsgType identifyType(const std::string &message)
Parse the type of a message from a string.
Definition Message.h:417
FIX::FieldNotFound
Field not found inside a message.
Definition Exceptions.h:58
FIX::InvalidMessage
Not a recognizable message.
Definition Exceptions.h:81
FIX::MessageParseError
Unable to parse message.
Definition Exceptions.h:74
FIX::message_order
Sorts fields in header, normal, or trailer order.
Definition MessageSorters.h:114