OnixS C++ ICE Binary Order Entry Handler 1.2.3
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 "../Common/Tools.h"
25
26#include <thread>
27#include <future>
28#include <functional>
29#include <chrono>
30#include <iostream>
31
32namespace Samples {
33
34using namespace ONIXS_ICEBOE_NAMESPACE;
36
37class BusSessionGatewayListener final : public ONIXS_ICEBOE_NAMESPACE::Testing::ClientMessageListener
38{
39public:
40 BusSessionGatewayListener(const std::string& host, Port port)
41 : host_(host)
42 , port_(port)
43 {
44 }
45
46 void onIPRequest(const IPRequest request, ONIXS_ICEBOE_NAMESPACE::Testing::Gateway* gateway) override
47 {
48 assert(!request.users().empty());
49
50 auto report = gateway->createIpReport(request.users()[0].userId(), toStrRef(host_), port_, toStrRef("emulator_token"));
51
52 gateway->send(report, gateway->outSeqNum());
53 }
54
55private:
56 const std::string host_{};
57 const Port port_{};
58};
59
60class GatewayListener final : public ONIXS_ICEBOE_NAMESPACE::Testing::ClientMessageListener
61{
62public:
63 void onNewOrderRequest(const NewOrderRequest order, ONIXS_ICEBOE_NAMESPACE::Testing::Gateway* gateway) override
64 {
65 updateReport(order);
66
67 gateway->send(report_, gateway->outSeqNum());
68 }
69
70
71protected:
72 void updateReport(const NewOrderRequest& order)
73 {
74 report_->clOrdId(order.clOrdId())
75 .originatorUserId(order.originatorUserId())
76 .side(order.side())
77 .symbol(order.symbol())
78 .ordType(order.ordType())
79 .side(order.side())
80 .leavesQty(0)
81 .meAcceptanceTime(UtcWatch::now())
82 .timePriority(UtcWatch::now())
83 .orderStatus(OrderStatusEnum::Fill)
84 .execId(OrderExecID{1, 1, UtcWatch::now()})
85 .memo("HelloFromEmulator")
86 ;
87 }
88
90};
91
92template <class Listener>
94{
95public:
96 GatewayEmulatorThread(const SessionSettings::LicenseStores& licenseStores, const std::string & host, Port port, std::unique_ptr<Listener> clientMessageListener)
97 : gateway_(licenseStores, port, host.c_str(), std::chrono::seconds{10}, std::chrono::seconds{10})
98 , clientMessageListener_(std::move(clientMessageListener))
99 {
100 std::promise<void> emulatorTaskPromise;
101 emulatorTaskDone_ = emulatorTaskPromise.get_future();
102
103 std::thread(
104 std::bind(
105 [&](std::promise<void>& p)
106 {
107 try
108 {
109 gateway_.run(*clientMessageListener_, &stopRequested_);
110 p.set_value();
111 }
112 catch (const std::exception& ex)
113 {
114 std::cerr << "Exception in Emulator's thread: " << ex.what() << '.' << std::endl;
115 p.set_exception(std::current_exception());
116 }
117 catch (...)
118 {
119 std::cerr << "UNKNOWN Exception in Emulator's thread." << std::endl;
120 p.set_exception(std::current_exception());
121 }
122 },
123 std::move(emulatorTaskPromise)))
124 .detach();
125 }
126
128 {
129 stopRequested_.store(true);
130
131 if(emulatorTaskDone_.valid())
132 emulatorTaskDone_.wait_for(std::chrono::seconds(10));
133 }
134
135 void wait(std::chrono::seconds timeout = std::chrono::seconds(10))
136 {
137 if(emulatorTaskDone_.valid())
138 {
139 std::future<void> f = std::move(emulatorTaskDone_);
140 f.wait_for(timeout);
141 f.get();
142 }
143 }
144
145 const Testing::Gateway& gateway() const noexcept
146 {
147 return gateway_;
148 }
149
150private:
151 std::atomic<bool> stopRequested_ {false};
152 Testing::Gateway gateway_;
153 std::unique_ptr<Listener> clientMessageListener_;
154 std::future<void> emulatorTaskDone_;
155};
156
157
158inline
159std::pair<std::unique_ptr<GatewayEmulatorThread<BusSessionGatewayListener>>, std::unique_ptr<GatewayEmulatorThread<GatewayListener>>> createEmulator(const SessionSettings& settings, const ConnectivityConfiguration& cfg, bool tcpDirect = false)
160{
161 if(cfg.useEmulator())
162 {
163#if !defined (_WIN32)
164 if(tcpDirect)
165 throw std::runtime_error("Emulator is not supported for TCPDirect.");
166#endif
167
168 std::clog << "!Emulator is used!" << std::endl;
169
170 auto bgwSessionGatewayListener = makeUnique<GatewayListener>();
171 auto bgwSessionEmulatorThread = makeUnique<GatewayEmulatorThread<GatewayListener>>(settings.licenseStores(), "127.0.0.1", 0, std::move(bgwSessionGatewayListener));
172
173 auto busSessionGatewayListener = makeUnique<BusSessionGatewayListener>(bgwSessionEmulatorThread->gateway().host(), bgwSessionEmulatorThread->gateway().port());
174 auto busSessionEmulatorThread = makeUnique<GatewayEmulatorThread<BusSessionGatewayListener>>(settings.licenseStores(), "127.0.0.1", cfg.port(), std::move(busSessionGatewayListener));
175
176 return std::make_pair(std::move(busSessionEmulatorThread), std::move(bgwSessionEmulatorThread));
177 }
178
179 return {};
180}
181
182}
#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:40
void onIPRequest(const IPRequest request, OnixS::ICE::BOE::Testing::Gateway *gateway) override
Called when a IPRequest message is received.
Definition Emulator.h:46
GatewayEmulatorThread(const SessionSettings::LicenseStores &licenseStores, const std::string &host, Port port, std::unique_ptr< Listener > clientMessageListener)
Definition Emulator.h:96
void wait(std::chrono::seconds timeout=std::chrono::seconds(10))
Definition Emulator.h:135
const Testing::Gateway & gateway() const noexcept
Definition Emulator.h:145
void updateReport(const NewOrderRequest &order)
Definition Emulator.h:72
void onNewOrderRequest(const NewOrderRequest order, OnixS::ICE::BOE::Testing::Gateway *gateway) override
Called when a NewOrderRequest message is received.
Definition Emulator.h:63
MessageHolder< ExecutionReport_New > report_
Definition Emulator.h:89
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:159
std::unique_ptr< T > makeUnique(Args &&...args)
Definition Tools.h:30
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