OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
Emulator.h
Go to the documentation of this file.
1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19#pragma once
20
23
24#include <thread>
25#include <future>
26#include <functional>
27#include <chrono>
28#include <iostream>
29
30namespace Samples {
31
32using namespace ONIXS_ICEBOE_NAMESPACE;
34
35class BusSessionGatewayListener final : public ONIXS_ICEBOE_NAMESPACE::Testing::ClientMessageListener
36{
37public:
38 BusSessionGatewayListener(const std::string& host, Port port)
39 : host_(host)
40 , port_(port)
41 {
42 }
43
44 void onIPRequest(const IPRequest request, ONIXS_ICEBOE_NAMESPACE::Testing::Gateway* gateway) override
45 {
46 assert(!request.users().empty());
47
48 auto report = gateway->createIpReport(request.users()[0].userId(), toStrRef(host_), port_, toStrRef("emulator_token"));
49
50 gateway->send(report, gateway->outSeqNum());
51 }
52
53private:
54 const std::string host_{};
55 const Port port_{};
56};
57
58class GatewayListener final : public ONIXS_ICEBOE_NAMESPACE::Testing::ClientMessageListener
59{
60public:
61 void onNewOrderRequest(const NewOrderRequest order, ONIXS_ICEBOE_NAMESPACE::Testing::Gateway* gateway) override
62 {
63 updateReport(order);
64
65 gateway->send(report_, gateway->outSeqNum());
66 }
67
68
69protected:
70 void updateReport(const NewOrderRequest& order)
71 {
72 report_->clOrdId(order.clOrdId())
73 .originatorUserId(order.originatorUserId())
74 .side(order.side())
75 .symbol(order.symbol())
76 .ordType(order.ordType())
77 .side(order.side())
78 .leavesQty(0)
79 .meAcceptanceTime(UtcWatch::now())
80 .timePriority(UtcWatch::now())
81 .orderStatus(OrderStatusEnum::Fill)
82 .execId(OrderExecID{1, 1, UtcWatch::now()})
83 .memo("HelloFromEmulator")
84 ;
85 }
86
88};
89
90template <class Listener>
92{
93public:
94 GatewayEmulatorThread(const SessionSettings::LicenseStores& licenseStores, const std::string & host, Port port, std::unique_ptr<Listener> clientMessageListener)
95 : gateway_(licenseStores, port, host.c_str(), std::chrono::seconds{10}, std::chrono::seconds{10})
96 , clientMessageListener_(std::move(clientMessageListener))
97 {
98 std::promise<void> emulatorTaskPromise;
99 emulatorTaskDone_ = emulatorTaskPromise.get_future();
100
101 std::thread(
102 std::bind(
103 [&](std::promise<void>& p)
104 {
105 try
106 {
107 gateway_.run(*clientMessageListener_, &stopRequested_);
108 p.set_value();
109 }
110 catch (const std::exception& ex)
111 {
112 std::cerr << "Exception in Emulator's thread: " << ex.what() << '.' << std::endl;
113 p.set_exception(std::current_exception());
114 }
115 catch (...)
116 {
117 std::cerr << "UNKNOWN Exception in Emulator's thread." << std::endl;
118 p.set_exception(std::current_exception());
119 }
120 },
121 std::move(emulatorTaskPromise)))
122 .detach();
123 }
124
126 {
127 stopRequested_.store(true);
128
129 if(emulatorTaskDone_.valid())
130 emulatorTaskDone_.wait_for(std::chrono::seconds(10));
131 }
132
133 void wait(std::chrono::seconds timeout = std::chrono::seconds(10))
134 {
135 if(emulatorTaskDone_.valid())
136 {
137 std::future<void> f = std::move(emulatorTaskDone_);
138 f.wait_for(timeout);
139 f.get();
140 }
141 }
142
143 const Testing::Gateway& gateway() const noexcept
144 {
145 return gateway_;
146 }
147
148private:
149 std::atomic<bool> stopRequested_ {false};
150 Testing::Gateway gateway_;
151 std::unique_ptr<Listener> clientMessageListener_;
152 std::future<void> emulatorTaskDone_;
153};
154
155
156inline
157std::pair<std::unique_ptr<GatewayEmulatorThread<BusSessionGatewayListener>>, std::unique_ptr<GatewayEmulatorThread<GatewayListener>>> createEmulator(const SessionSettings& settings, const ConnectivityConfiguration& cfg, bool tcpDirect = false)
158{
159 if(cfg.useEmulator())
160 {
161#if !defined (_WIN32)
162 if(tcpDirect)
163 throw std::runtime_error("Emulator is not supported for TCPDirect.");
164#endif
165
166 std::clog << "!Emulator is used!" << std::endl;
167
168 auto bgwSessionGatewayListener = std::make_unique<GatewayListener>();
169 auto bgwSessionEmulatorThread = std::make_unique<GatewayEmulatorThread<GatewayListener>>(settings.licenseStores(), "127.0.0.1", 0, std::move(bgwSessionGatewayListener));
170
171 auto busSessionGatewayListener = std::make_unique<BusSessionGatewayListener>(bgwSessionEmulatorThread->gateway().host(), bgwSessionEmulatorThread->gateway().port());
172 auto busSessionEmulatorThread = std::make_unique<GatewayEmulatorThread<BusSessionGatewayListener>>(settings.licenseStores(), "127.0.0.1", cfg.port(), std::move(busSessionGatewayListener));
173
174 return std::make_pair(std::move(busSessionEmulatorThread), std::move(bgwSessionEmulatorThread));
175 }
176
177 return {};
178}
179
180}
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
const LicenseStores & licenseStores() const noexcept
ICE Gateway Emulator.
Definition Gateway.h:53
BusSessionGatewayListener(const std::string &host, Port port)
Definition Emulator.h:38
void onIPRequest(const IPRequest request, OnixS::ICE::BOE::Testing::Gateway *gateway) override
Called when a IPRequest message is received.
Definition Emulator.h:44
GatewayEmulatorThread(const SessionSettings::LicenseStores &licenseStores, const std::string &host, Port port, std::unique_ptr< Listener > clientMessageListener)
Definition Emulator.h:94
void wait(std::chrono::seconds timeout=std::chrono::seconds(10))
Definition Emulator.h:133
const Testing::Gateway & gateway() const noexcept
Definition Emulator.h:143
void updateReport(const NewOrderRequest &order)
Definition Emulator.h:70
void onNewOrderRequest(const NewOrderRequest order, OnixS::ICE::BOE::Testing::Gateway *gateway) override
Called when a NewOrderRequest message is received.
Definition Emulator.h:61
MessageHolder< ExecutionReport_New > report_
Definition Emulator.h:87
std::vector< std::string > LicenseStores
Folders that contain license file(s).
StrRef toStrRef(const std::string &str)
Constructs a StrRef instance over th std::string content.
Definition StrRef.h:392
unsigned short Port
Definition Defines.h:41
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
Definition Emulator.h:157
Request IP Address, Port, and Session Tokens for Binary Order Gateways.
Definition Messages.h:8123
auto clOrdId() const noexcept
Provides access to clOrdID field.
Definition Messages.h:10375
auto side() const noexcept
Provides access to side field.
Definition Messages.h:10293
auto originatorUserId() const noexcept
Provides access to originatorUserID field.
Definition Messages.h:10311
auto symbol() const noexcept
Provides access to symbol field.
Definition Messages.h:10257
auto ordType() const noexcept
Provides access to ordType field.
Definition Messages.h:10331