This sample demonstrates how to use a pluggable session storage.
#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";
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;
}
#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),
previousSeqNum_(),
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;
}
{
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)
{
}
}
#include <vector>
#include <list>
namespace Samples
{
using namespace OnixS::CME::iLink3::Messaging;
class MySessionStorage : public SessionStorage
{
public:
MySessionStorage();
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;
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_;
};
}