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.
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
{
}
{
cout <<
"ERROR: " << error.
what() << endl;
}
Create new FAST coder
To create new instance of FastCoder, execute the following instruction.
std::auto_ptr<Engine::FastCoder> fastCoder(
fastCodec.createFastCoder(
"additional.xml",
"templates.xml",
Create new FAST decoder
To create new instance of FastDecoder, execute the following instruction.
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.
{
char mdEntryId[2] = {0};
mdEntryId[0] = (char) i + '0';
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);
}
Encode
To encode the FIX message, execute the following instruction.
fastCoder->encode(fixMessages[i].get(), &buffer);
Decode
To decode the FAST message, execute the following instruction.
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
{
const int TEMPLATE_ID = 4;
const int COUNT_FIX_MESSAGES = 3;
}
{
std::auto_ptr<Engine::FastCoder> fastCoder(
"additional.xml",
"templates.xml",
std::auto_ptr<Engine::FastDecoder> fastDecoder(
"additional.xml",
"templates.xml",
std::auto_ptr<Engine::FIXMessage> fixMessages[COUNT_FIX_MESSAGES];
for (int i = 0; i < COUNT_FIX_MESSAGES; ++i)
{
fixMessages[i] = fastCoder->
newSkel(
"X");
{
char mdEntryId[2] = {0};
mdEntryId[0] = (char) i + '0';
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);
}
}
Engine::FastCoder::Buffer buffer;
for (int i = 0; i < COUNT_FIX_MESSAGES; ++i)
{
int size(0);
char const* raw = fixMessages[i]->toRaw(&size);
try
{
fastCoder->tryEncode(fixMessages[i].get(), TEMPLATE_ID);
}
{
std::cerr <<
"TryEncode Error: " << error.
what() << std::endl;
}
fastCoder->resetDictionary();
fastCoder->encode(fixMessages[i].get(), &buffer);
fastDecoder->resetDictionary();
std::auto_ptr<Engine::FIXMessage> decodedFixMessage(fastDecoder->decode(buffer.getPtr(), buffer.getSize()));
raw = decodedFixMessage->toRaw(&size);
}
}
int main()
{
try
{
runSample();
}
{
std::cerr <<
"ERROR: " << error.
what() << std::endl;
}
}