OnixS C++ CME iLink 3 Binary Order Entry Handler  1.18.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 << "CME iLink 3 PluggableStorage Sample." << endl
<< endl;
int marketSegmentId = 99;
string host = "127.0.0.1";
Port port = 49152;
bool useEmulator = false;
if (argc < 4)
{
#if defined(ONIXS_ILINK3_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_ILINK3_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, marketSegmentId, &listener, SessionStorageType::Pluggable, &storage);
NewOrderSingle order;
Helper::setOrderFields(order, PartyDetailsListReqID, DefaultSecurityId, DefaultPriceMantissa);
if(session.negotiated())
session.reset(true);
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_ILINK3_NOTHROW
{
return static_cast<const char*>(message.data());
}
const char* end(const NetworkMessage& message) ONIXS_ILINK3_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),
previousSeqNum_(),
uuid_(Session::UndefinedUuid),
previousUuid_(Session::UndefinedUuid),
id_(IdGenerator::newStrId()),
terminated_(false),
negotiated_(false),
sessionCreationTime_()
{}
void MySessionStorage::close(bool doBackup)
{
std::clog << "\nClose the session storage (doBackup=" << doBackup << ").\n";
inSeqNum_ = 1;
outSeqNum_ = 1;
previousSeqNum_ = 0;
previousUuid_ = 0;
terminated_ = false;
negotiated_ = false;
inboundMessages_.clear();
outboundMessages_.clear();
}
const std::string & MySessionStorage::id() const
{
return id_;
}
UInt64 MySessionStorage::uuid() const
{
return uuid_;
}
void MySessionStorage::uuid(UInt64 value)
{
std::clog << "\nSet uuid to " << value << ".\n";
uuid_ = value;
}
UInt64 MySessionStorage::previousUuid() const
{
return previousUuid_;
}
void MySessionStorage::previousUuid(UInt64 value)
{
std::clog << "\nSet previousUuid to " << value << ".\n";
previousUuid_ = value;
}
SeqNumber MySessionStorage::inSeqNum() const
{
return inSeqNum_;
}
void MySessionStorage::inSeqNum(SeqNumber msgSeqNum)
{
std::clog << "\nSet inSeqNum to " << msgSeqNum << ".\n";
inSeqNum_ = msgSeqNum;
}
SeqNumber MySessionStorage::previousSeqNum() const
{
return previousSeqNum_;
}
void MySessionStorage::previousSeqNum(SeqNumber msgSeqNum)
{
std::clog << "\nSet previousSeqNum to " << msgSeqNum << ".\n";
previousSeqNum_ = 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 " << status << ".\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
{
class MySessionStorage : public SessionStorage
{
public:
MySessionStorage();
const std::string & id() const ONIXS_ILINK3_OVERRIDE;
void uuid(UInt64 value) ONIXS_ILINK3_OVERRIDE;
UInt64 previousUuid() const ONIXS_ILINK3_OVERRIDE;
void previousUuid(UInt64 value) ONIXS_ILINK3_OVERRIDE;
SeqNumber inSeqNum() const ONIXS_ILINK3_OVERRIDE;
void inSeqNum(SeqNumber msgSeqNum) ONIXS_ILINK3_OVERRIDE;
SeqNumber previousSeqNum() const ONIXS_ILINK3_OVERRIDE;
void previousSeqNum(SeqNumber msgSeqNum) ONIXS_ILINK3_OVERRIDE;
SeqNumber outSeqNum() const ONIXS_ILINK3_OVERRIDE;
void outSeqNum(SeqNumber msgSeqNum) ONIXS_ILINK3_OVERRIDE;
bool negotiated() const ONIXS_ILINK3_OVERRIDE;
void negotiated(bool status) ONIXS_ILINK3_OVERRIDE;
Timestamp sessionCreationTime() const ONIXS_ILINK3_OVERRIDE;
void sessionCreationTime(Timestamp) ONIXS_ILINK3_OVERRIDE;
void close(bool doBackup = false) ONIXS_ILINK3_OVERRIDE;
void storeInboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp messageReceivingUtcTimestamp = Timestamp()) ONIXS_ILINK3_OVERRIDE;
void storeOutboundMessage(const NetworkMessage& message, SeqNumber msgSeqNum, Timestamp messageSendingUtcTimestamp = Timestamp()) ONIXS_ILINK3_OVERRIDE;
void flush() ONIXS_ILINK3_OVERRIDE;
void warmup(size_t, Timestamp) ONIXS_ILINK3_OVERRIDE;
typedef std::vector<char> Message;
typedef std::list<Message> Messages;
private:
SeqNumber inSeqNum_;
SeqNumber outSeqNum_;
SeqNumber previousSeqNum_;
UInt64 uuid_;
UInt64 previousUuid_;
std::string id_;
bool terminated_;
bool negotiated_;
Timestamp sessionCreationTime_;
Messages inboundMessages_;
Messages outboundMessages_;
};
}