OnixS C++ B3 BOE Binary Order Entry  1.2.0
API Documentation
Pluggable Storage Sample

This sample demonstrates how to use a pluggable session storage.

Source code

PluggableStorage.cpp

#include "MySessionStorage.h"
#include "../Common/Listener.h"
#include "../Common/Helpers.h"
#include "../Settings/Defaults.h"
using namespace Samples;
int main(int argc, char* argv[])
{
clog << "B3 BOE PluggableStorage Sample." << endl << endl;
MarketSegmentID marketSegmentId = DefaultMarketSegmentId;
string host = "127.0.0.1";
Port port = 49152;
bool useEmulator = false;
if (argc < 4)
{
#if defined(ONIXS_B3_BOE_HAS_GATEWAY_EMULATOR)
useEmulator = true;
#else
std::cerr << "usage: [MarketSegmentId] [Host] [Port]" << std::endl;
return 1;
#endif
}
else
{
marketSegmentId = atoi(argv[1]);
host = argv[2];
port = atoi(argv[3]);
}
try
{
const SessionSettings settings = fillSettings(useEmulator);
#if defined(ONIXS_B3_BOE_HAS_GATEWAY_EMULATOR)
std::unique_ptr<GatewayEmulatorThread> gateway;
if (useEmulator)
gateway.reset(new GatewayEmulatorThread(settings.licenseStores(), host, port));
#endif
MySessionStorage storage;
Listener listener;
Session session(settings, &listener, SessionStorageType::Pluggable, &storage);
NewOrderSingle order;
Helper::setOrderFields(order, marketSegmentId, DefaultSecurityId, DefaultAccount, DefaultPriceMantissa);
session
.connect(host, port)
.send(order);
Helper::waitUntilEnterKey("disconnect the session and terminate the application");
session.disconnect();
}
catch (const exception& ex)
{
cerr << "EXCEPTION: " << ex.what() << endl;
return 1;
}
return 0;
}

MySessionStorage.cpp

#include "MySessionStorage.h"
#include "../Common/Helpers.h"
#include "../Common/Listener.h"
namespace Samples
{
namespace
{
const char* begin(const NetworkMessage& message) ONIXS_B3_BOE_NOTHROW
{
return static_cast<const char*>(message.data());
}
const char* end(const NetworkMessage& message) ONIXS_B3_BOE_NOTHROW
{
return static_cast<const char*>(advanceByBytes(message.data(), message.size()));
}
void storeMessage(MySessionStorage::Messages & storage, const NetworkMessage& message)
{
storage.push_back(MySessionStorage::Message(begin(message), end(message)));
if (!processTypified(message.message(), MessagePrinter()))
throw runtime_error("Unknown message type");
}
}
MySessionStorage::MySessionStorage()
: inSeqNum_(1)
, outSeqNum_(1)
, id_(IdGenerator::newStrId())
, negotiated_(false)
, sessionCreationTime_()
, sessionVerId_(Session::UndefinedSessionVerID)
{}
UInt64 MySessionStorage::sessionVerId() const
{
return sessionVerId_;
}
void MySessionStorage::sessionVerId(UInt64 value)
{
std::clog << "\nSet sessionVerId to " << value << ".\n";
sessionVerId_ = value;
}
void MySessionStorage::close(bool doBackup)
{
std::clog << "\nClose the session storage (doBackup=" << doBackup << ").\n";
inSeqNum_ = 1;
outSeqNum_ = 1;
negotiated_ = false;
inboundMessages_.clear();
outboundMessages_.clear();
}
const std::string & MySessionStorage::id() const
{
return id_;
}
SeqNumber MySessionStorage::inSeqNum() const
{
return inSeqNum_;
}
void MySessionStorage::inSeqNum(SeqNumber msgSeqNum)
{
std::clog << "\nSet inSeqNum to " << msgSeqNum << ".\n";
inSeqNum_ = msgSeqNum;
}
SeqNumber MySessionStorage::outSeqNum() const
{
return outSeqNum_;
}
void MySessionStorage::outSeqNum(SeqNumber msgSeqNum)
{
std::clog << "\nSet outSeqNum to " << msgSeqNum << ".\n";
outSeqNum_ = msgSeqNum;
}
bool MySessionStorage::negotiated() const
{
return negotiated_;
}
void MySessionStorage::negotiated(bool status)
{
std::clog << "\nSet negotiated to " << std::boolalpha << status << std::noboolalpha << ".\n";
negotiated_ = status;
}
Timestamp MySessionStorage::sessionCreationTime() const
{
return sessionCreationTime_;
}
void MySessionStorage::sessionCreationTime(Timestamp time)
{
std::clog << "\nSet sessionCreationTime to " << time << "\n";
sessionCreationTime_ = time;
}
void MySessionStorage::storeInboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp)
{
std::clog << "\nStore inbound message:\n";
storeMessage(inboundMessages_, message);
inSeqNum_ = msgSeqNum;
}
void MySessionStorage::storeOutboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp)
{
std::clog << "\nStore outbound message:\n";
storeMessage(outboundMessages_, message);
outSeqNum_ = msgSeqNum;
}
void MySessionStorage::flush()
{
}
void MySessionStorage::warmup(size_t, Timestamp)
{
}
}

MySessionStorage.h

#include <vector>
#include <list>
namespace Samples
{
using namespace OnixS::B3::BOE;
using namespace OnixS::B3::BOE::Messaging;
class MySessionStorage : public SessionStorage
{
public:
MySessionStorage();
UInt64 sessionVerId() const ONIXS_B3_BOE_OVERRIDE;
void sessionVerId(UInt64 value) ONIXS_B3_BOE_OVERRIDE;
const std::string & id() const ONIXS_B3_BOE_OVERRIDE;
SeqNumber inSeqNum() const ONIXS_B3_BOE_OVERRIDE;
void inSeqNum(SeqNumber msgSeqNum) ONIXS_B3_BOE_OVERRIDE;
SeqNumber outSeqNum() const ONIXS_B3_BOE_OVERRIDE;
void outSeqNum(SeqNumber msgSeqNum) ONIXS_B3_BOE_OVERRIDE;
bool negotiated() const ONIXS_B3_BOE_OVERRIDE;
void negotiated(bool status) ONIXS_B3_BOE_OVERRIDE;
Timestamp sessionCreationTime() const ONIXS_B3_BOE_OVERRIDE;
void sessionCreationTime(Timestamp) ONIXS_B3_BOE_OVERRIDE;
void close(bool doBackup = false) ONIXS_B3_BOE_OVERRIDE;
void storeInboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp messageReceivingUtcTimestamp = Timestamp()) ONIXS_B3_BOE_OVERRIDE;
void storeOutboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp messageSendingUtcTimestamp = Timestamp()) ONIXS_B3_BOE_OVERRIDE;
void flush() ONIXS_B3_BOE_OVERRIDE;
void warmup(size_t, Timestamp) ONIXS_B3_BOE_OVERRIDE;
typedef std::vector<char> Message;
typedef std::list<Message> Messages;
private:
SeqNumber inSeqNum_;
SeqNumber outSeqNum_;
std::string id_;
bool negotiated_;
Timestamp sessionCreationTime_;
UInt64 sessionVerId_;
Messages inboundMessages_;
Messages outboundMessages_;
};
}