B2BITS FIX Antenna C++ 2.32.0
Loading...
Searching...
No Matches
FAST Codec Quick start

These chapters describe the creation of a simple application that shows how to use the FastCodec, step-by step with samples. Follow these instructions to get it working:

  • Initialize FAST codec
  • Create new FAST coder
  • Create new FAST decoder
  • Create new FIX message
  • Encode
  • Decode

Initialize FAST codec

Execute the following instruction to initialize the FastCodec.

// Initialize the FastCodec
Engine::FastCodec fastCodec("../../../engine.license");
A factory for FastCoder and FastDecoder.
Definition B2BITS_FastCodec.h:76

In the call above you should specify a path to a license file. If the specified file doesn't exist or the license isn't valid, an exception will be thrown.

try
{
// Initialize the FastCodec
Engine::FastCodec fastCodec("../../../engine.license");
}
catch (const Utils::Exception& error)
{
cout << "ERROR: " << error.what() << endl;
}
Generic application exception.
Definition B2BITS_Exception.h:76
virtual const char * what() const
Returns the reason for this exception.

Create new FAST coder

To create new instance of FastCoder, execute the following instruction.

// Create FastCoder
std::auto_ptr<Engine::FastCoder> fastCoder(
fastCodec.createFastCoder(
"additional.xml",
"templates.xml",
@ FIXT11
FIXT 1.1.
Definition B2BITS_PubEngineDefines.h:89
@ FIX50SP2
FIX 5.0 SP2.
Definition B2BITS_PubEngineDefines.h:87

Create new FAST decoder

To create new instance of FastDecoder, execute the following instruction.

// Create FastDecoder
std::auto_ptr<Engine::FastDecoder> fastDecoder(
fastCodec.createFastDecoder(
"additional.xml",
"templates.xml",

Create new FIX message

To create new FIX message, execute the following instruction.

fixMessages[i] = fastCoder->newSkel("X");

The next step is to fill the message fields.

fixMessages[i]->set(FIXFields::ApplVerID, "9");
fixMessages[i]->set(FIXFields::SenderCompID, "MICEX");
fixMessages[i]->set(FIXFields::MsgSeqNum, i);
fixMessages[i]->set(FIXFields::NoMDEntries, 1);
Engine::TagValue* entry = grp->getEntry(0);
{
char mdEntryId[2] = {0};
mdEntryId[0] = (char) i + '0';
entry->set(FIXFields::MDEntryID, mdEntryId);
char symbol[] = "symbol_";
symbol[sizeof(symbol) - 2] = (char) i + '0';
entry->set(FIXFields::Symbol, symbol);
entry->set(FIXFields::RptSeq, i + 10);
entry->set(FIXFields::MDEntryPx, Engine::Decimal(i * 10, i % 4));
entry->set(FIXFields::MDEntrySize, i * 12);
}
Encapsulates float type.
Definition B2BITS_Decimal.h:64
FIX Repeating Group interface.
Definition B2BITS_FIXGroup.h:70
virtual TagValue * getEntry(int index)=0
Returns group entry by index.
virtual FIXGroup * getGroup(int tag, int index)=0
Returns nested repeating group.
Represents tag-value structure.
Definition B2BITS_TagValue.h:315
virtual bool set(int tag, MonthYear value)=0
Updates field value by tag number.
static UTCTimeOnly now()
Returns current system time in UTC.
static UTCTimestamp now(bool withMilliseconds=false)
Returns UTCTimestamp with current time.
static const int NoMDEntries
Definition B2BITS_FIXFields.h:282
static const int SendingTime
Definition B2BITS_FIXFields.h:106
static const int ApplVerID
Definition B2BITS_FIXFields.h:1202
static const int MsgSeqNum
Definition B2BITS_FIXFields.h:89
static const int RptSeq
Definition B2BITS_FIXFields.h:137
static const int MDEntryType
Definition B2BITS_FIXFields.h:283
static const int SenderCompID
Definition B2BITS_FIXFields.h:104
static const int MDEntryPx
Definition B2BITS_FIXFields.h:284
static const int MDEntrySize
Definition B2BITS_FIXFields.h:285
static const int Symbol
Definition B2BITS_FIXFields.h:109
static const int MDEntryTime
Definition B2BITS_FIXFields.h:287
static const int MDUpdateAction
Definition B2BITS_FIXFields.h:293
static const int MDEntryID
Definition B2BITS_FIXFields.h:292

Encode

To encode the FIX message, execute the following instruction.

// Encode message
// Node: the specified buffer will be reused.
fastCoder->encode(fixMessages[i].get(), &buffer);

Decode

To decode the FAST message, execute the following instruction.

// Decode the fast message back to fix message
std::auto_ptr<Engine::FIXMessage> decodedFixMessage(fastDecoder->decode(buffer.getPtr(), buffer.getSize()));

Full sample

The sample below illustrates all above mentioned instructions combined in one application.

#include <iostream>
#include <B2BITS_FIXFields.h>
#include <B2BITS_FIXGroup.h>
#include <B2BITS_FastCodec.h>
namespace
{
// template id, used for encoding fix messages
const int TEMPLATE_ID = 4;
// count of fix messages that are encode/decoded in this example
const int COUNT_FIX_MESSAGES = 3;
}
void runSample(Engine::FastCodec& fastCodec)
{
// Create FastCoder
std::auto_ptr<Engine::FastCoder> fastCoder(
fastCodec.createFastCoder(
"additional.xml",
"templates.xml",
// Create FastDecoder
std::auto_ptr<Engine::FastDecoder> fastDecoder(
fastCodec.createFastDecoder(
"additional.xml",
"templates.xml",
std::auto_ptr<Engine::FIXMessage> fixMessages[COUNT_FIX_MESSAGES];
// Create several FIX messages
for (int i = 0; i < COUNT_FIX_MESSAGES; ++i)
{
fixMessages[i] = fastCoder->newSkel("X");
fixMessages[i]->set(FIXFields::ApplVerID, "9");
fixMessages[i]->set(FIXFields::SenderCompID, "MICEX");
fixMessages[i]->set(FIXFields::MsgSeqNum, i);
fixMessages[i]->set(FIXFields::NoMDEntries, 1);
Engine::FIXGroup* grp = fixMessages[i]->getGroup(FIXFields::NoMDEntries);
Engine::TagValue* entry = grp->getEntry(0);
{
char mdEntryId[2] = {0};
mdEntryId[0] = (char) i + '0';
entry->set(FIXFields::MDEntryID, mdEntryId);
char symbol[] = "symbol_";
symbol[sizeof(symbol) - 2] = (char) i + '0';
entry->set(FIXFields::Symbol, symbol);
entry->set(FIXFields::RptSeq, i + 10);
entry->set(FIXFields::MDEntryPx, Engine::Decimal(i * 10, i % 4));
entry->set(FIXFields::MDEntrySize, i * 12);
}
}
for (int i = 0; i < COUNT_FIX_MESSAGES; ++i)
{
// Print message
int size(0);
char const* raw = fixMessages[i]->toRaw(&size);
std::cout << "Sources message: " << Engine::AsciiString(raw, size) << std::endl;
try
{
// Check if the specified message can be encoded without errors using the specified template.
// This method silently returns a control if no errors detected; otherwise throws an exception with detailed information.
// Usually used for debugging purpose.
fastCoder->tryEncode(fixMessages[i].get(), TEMPLATE_ID);
}
catch (Utils::Exception const& error)
{
std::cerr << "TryEncode Error: " << error.what() << std::endl;
}
// Reset the fast coder dictionary (optional)
fastCoder->resetDictionary();
// Encode message
// Node: the specified buffer will be reused.
fastCoder->encode(fixMessages[i].get(), &buffer);
// Reset the fast decoder dictionary (optional)
fastDecoder->resetDictionary();
// Decode the fast message back to fix message
std::auto_ptr<Engine::FIXMessage> decodedFixMessage(fastDecoder->decode(buffer.getPtr(), buffer.getSize()));
// Print message
raw = decodedFixMessage->toRaw(&size);
std::cout << "Decoded message: " << Engine::AsciiString(raw, size) << std::endl;
}
}
int main()
{
try
{
// Initialize the FIX Antenna Engine
Engine::FixEngine::init("engine.properties");
// Rus the sample
runSample();
}
catch (Utils::Exception const& error)
{
std::cerr << "ERROR: " << error.what() << std::endl;
}
}
std::unique_ptr< FastCoder > createFastCoder(const std::string &pathToAdditionalXml, const std::string &pathToTemplatesXml, FIXVersion scpVer, FIXVersion appVer, const FastMappingOptions &mappingOptions=FastMappingOptions())
Creates FastCoder.
std::unique_ptr< FastDecoder > createFastDecoder(const std::string &pathToAdditionalXml, const std::string &pathToTemplatesXml, FIXVersion scpVer, FIXVersion appVer, const FastMappingOptions &mappingOptions=FastMappingOptions())
Creates FastDecoder.
Buffer.
Definition B2BITS_FastCoder.h:74
char const * getPtr() const
Returns a pointer to the buffer.
Definition B2BITS_FastCoder.h:100
std::size_t getSize() const
Returns size of the buffer.
Definition B2BITS_FastCoder.h:109
static FixEngine * init(InitParameters const &params)
Initializes the Engine.
BasicString< char > AsciiString
AsciiString.
Definition B2BITS_String.h:543