This sample shows how to connect to the CME iLink 3 Gateway or the local Gateway emulator. When the session is established, the NewOrderSingle
message is filled using the Tag-based API and sent to the counterparty.
#include "../Common/Helpers.h"
#include "../Common/Listener.h"
using namespace Samples;
typedef MessageHolder<TagBased::NewOrderSingle514> NewOrderSingleFix;
void setOrderFields(
NewOrderSingleFix& order, Int64 partyDetailsListReqId, int securityId, Int64 priceMantissa)
{
order->
;
}
class FixListener :
{
public:
void onMessage(
const Messaging::TagBased::Message& msg,
Session*)
ONIXS_ILINK3_OVERRIDE
{
std::clog << "\nReceived:\n" << msg << std::endl;
if(BusinessReject521::TemplateId == msg.templateId())
{
std::clog
<< "\tReject reason: "
<< BusinessRejectReason::toString(
<< std::endl;
}
}
void onStateChange(
SessionStateId::Enum newState,
SessionStateId::Enum prevState,
{
std::clog
<< "\nSession's state is changed, prevState="
<< SessionStateId::toString(prevState)
<< ", newState="
<< SessionStateId::toString(newState)
<< std::endl;
}
void onError(
SessionErrorReason::Enum,
const std::string& description,
Session*, Messaging::SbeMessage) ONIXS_ILINK3_OVERRIDE
{
cerr << "\nSession-level error: " << description << std::endl;
}
void onWarning(
SessionWarningReason::Enum,
const std::string& description,
Session*, Messaging::SbeMessage) ONIXS_ILINK3_OVERRIDE
{
cerr << "\nSession-level warning: " << description << std::endl;
}
};
int main(int argc, char * argv[])
{
clog << "CME iLink 3 Tag-Based Messaging 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] (SecurityId) (PriceMantissa)" << std::endl;
return 1;
#endif
}
else
{
marketSegmentId = atoi(argv[1]);
host = argv[2];
port = atoi(argv[3]);
}
const int securityId = argc > 4 ? atoi(argv[4]) : DefaultSecurityId;
const Int64 priceMantissa = argc > 5 ? fromStr<Int64>(argv[5]) : DefaultPriceMantissa;
try
{
SignalHelper::manageLinuxSignals();
const SessionSettings settings = fillSettings(useEmulator);
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
std::future<void> emulatorTaskDone;
std::unique_ptr<Testing::Gateway> gateway;
if (useEmulator)
{
std::promise<void> emulatorTaskPromise;
emulatorTaskDone = emulatorTaskPromise.get_future();
gateway.reset(new Testing::Gateway(settings.licenseStores(), port, host.c_str()));
std::thread(
std::bind(
[&](std::promise<void>& p)
{
try
{
gateway->acceptConnection();
gateway->acceptSession();
gateway->receiveTypedMessage<TagBased::NewOrderSingle514>();
gateway->waitUntilTerminate();
p.set_value();
}
catch (const std::exception& ex)
{
std::cerr << "Exception in Emulator's thread: " << ex.what() << '.' << std::endl;
p.set_exception(
current_exception());
}
catch(...)
{
std::cerr << "UNKNOWN Exception in Emulator's thread." << std::endl;
p.set_exception(
current_exception());
}
},
std::move(emulatorTaskPromise))
).detach();
}
#endif
NewOrderSingleFix order;
setOrderFields(order, PartyDetailsListReqID, securityId, priceMantissa);
FixListener listener;
Session session(settings, marketSegmentId, &listener);
session
clog << "\nThe order was sent." << endl;
Helper::waitUntilEnterKey("disconnect the session and terminate the application");
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
if(emulatorTaskDone.valid())
{
emulatorTaskDone.wait_for(std::chrono::seconds(10));
emulatorTaskDone.get();
}
#endif
}
catch(const std::exception & ex)
{
cerr << "\nEXCEPTION: " << ex.what() << endl;
return 1;
}
return 0;
}