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;
string host = "127.0.0.1";
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
Helper::waitUntilEnterKey("disconnect the session and terminate the application");
}
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
{
{
return static_cast<const char*>(message.data());
}
{
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)));
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_;
}
{
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
{
{
public:
MySessionStorage();
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 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:
bool negotiated_;
UInt64 sessionVerId_;
Messages inboundMessages_;
Messages outboundMessages_;
};
}