OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
Listener.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
20#pragma once
21
22
25
26#include <functional>
27#include <future>
28
29#include "../Common/Helpers.h"
30#include "../Common/Tools.h"
31
32namespace Samples {
33
34using namespace ONIXS_ICEBOE_NAMESPACE;
36using namespace ONIXS_ICEBOE_NAMESPACE::Threading;
37
39{
40 template <typename MessageType>
41 void operator()(const MessageType msg) const
42 {
43 std::clog << msg << std::endl;
44 }
45
46 static void printMsg(const Messaging::SbeMessage msg)
47 {
49 std::clog << "Unknown message type";
50 }
51};
52
54{
55public:
56 ~Listener() override = default;
57
58 void onMessageSending(char* bytes, size_t size, Session*) override
59 {
60 std::clog << "\nOutbound message:\n";
62 }
63
64 void onLogonReport(const Messaging::LogonReport msg, Session*) override
65 {
66 std::clog << "\nReceived:\n" << msg << std::endl;
67 }
68
70 {
71 std::clog << "\nReceived:\n" << msg << std::endl;
72 }
73
74 void onHeartBeat(const Messaging::HeartBeat msg, Session*) override
75 {
76 std::clog << "\nReceived:\n" << msg << std::endl;
77 }
78
79 void onError(const Messaging::Error msg, Session*) override
80 {
81 std::clog << "\nReceived:\n" << msg << std::endl;
82 }
83
84 void onNews(const Messaging::News msg, Session*) override
85 {
86 std::clog << "\nReceived:\n" << msg << std::endl;
87 }
88
90 {
91 std::clog << "\nReceived:\n" << msg << std::endl;
92 }
93
95 {
96 std::clog << "\nReceived:\n" << msg << std::endl;
97 }
98
100 {
101 std::clog << "\nReceived:\n" << msg << std::endl;
102 }
103
105 {
106 std::clog << "\nReceived:\n" << msg << std::endl;
107 }
108
109 void onIPReport(const Messaging::IPReport msg, Session*) override
110 {
111 std::clog << "\nReceived:\n" << msg << std::endl;
112 }
113
115 {
116 std::clog << "\nReceived:\n" << msg << std::endl;
117
118 if(const auto code = msg.exchangeCode())
119 {
120 const auto text = msg.text();
121 assert(nullOpt != text);
122
123 traderLoggedOn.set_exception(std::make_exception_ptr(std::runtime_error(Messaging::format(*code, *text))));
124 return;
125 }
126
127 traderLoggedOn.set_value();
128 }
129
131 {
132 std::clog << "\nReceived:\n" << msg << std::endl;
133 }
134
136 {
137 std::clog << "\nReceived:\n" << msg << std::endl;
138 }
139
141 {
142 std::clog << "\nReceived:\n" << msg << std::endl;
143 }
144
146 {
147 std::clog << "\nReceived:\n" << msg << std::endl;
148 }
149
151 {
152 std::clog << "\nReceived:\n" << msg << std::endl;
153 }
154
156 {
157 std::clog << "\nReceived:\n" << msg << std::endl;
158 }
159
161 {
162 std::clog << "\nReceived:\n" << msg << std::endl;
163 }
164
166 {
167 std::clog << "\nReceived:\n" << msg << std::endl;
168 }
169
171 {
172 std::clog << "\nReceived:\n" << msg << std::endl;
173 }
174
176 {
177 std::clog << "\nReceived:\n" << msg << std::endl;
178 }
179
181 {
182 std::clog << "\nReceived:\n" << msg << std::endl;
183 }
184
186 {
187 std::clog
188 << "\nSession's state is changed, prevState="
189 << SessionStateId::toString(prevState)
190 << ", newState="
191 << SessionStateId::toString(newState)
192 << std::endl;
193 }
194
196 SessionErrorReason::Enum, const std::string& description, Session*, Messaging::SbeMessage msg) override
197 {
198 std::cerr << "\nSession-level error: " << description;
199
200 if(msg.valid())
201 {
202 std::cerr << ",\nCaused by the message: ";
204 }
205
206 std::cerr << std::endl;
207 }
208
210 SessionWarningReason::Enum, const std::string& description, Session*, Messaging::SbeMessage msg) override
211 {
212 std::cerr << "\nSession-level warning: " << description;
213
214 if(msg.valid())
215 {
216 std::cerr << ",\nCaused by the message: ";
218 }
219
220 std::cerr << std::endl;
221 }
222
223 std::promise<void> traderLoggedOn;
224};
225
226template <typename T, typename Stack>
227std::future<T> wait(Stack& stack, std::promise<T>& promise, std::chrono::seconds timeout = std::chrono::seconds{30})
228{
229 auto future = promise.get_future();
230
231 const auto deadline = std::chrono::steady_clock::now() + timeout;
232 std::uint32_t spins = 0;
233
234 for (;;)
235 {
236 stack.dispatchEvents();
237
238 if (future.wait_for(std::chrono::nanoseconds{0}) == std::future_status::ready)
239 return future;
240
241 if ((++spins & 0xFFu) == 0) // every 256 iterations
242 {
243 if (std::chrono::steady_clock::now() >= deadline)
244 {
245 promise.set_exception(std::make_exception_ptr(std::runtime_error("Operation timed out.")));
246 return future;
247 }
248 }
249 }
250}
251
252template <typename T>
253std::future<T> wait(std::promise<T>& promise, std::chrono::seconds timeout = std::chrono::seconds{30})
254{
255 auto future = promise.get_future();
256
257 if(future.wait_for(timeout) != std::future_status::ready)
258 promise.set_exception(std::make_exception_ptr(std::runtime_error("Operation timed out.")));
259
260 return future;
261}
262
263// Request Binary Order Gateway credentials from Binary Utility Service Gateway.
264inline BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
265{
267
268 Listener listener;
269
270 // Create an instance of a `BUS` session.
271 BusSession busSession(settings, &listener);
272
273 // Connect.
274 busSession.connect(host, port);
275
276 const auto bgwCredentialsFuture = busSession.getBgwCredentialsAsync(settings.userId());
277
278 const auto bgwCredentials = bgwCredentialsFuture.get();
279
280 // Disconnect.
281 busSession.disconnect();
282
283 return bgwCredentials;
284}
285
286}
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
BGW session connection credentials.
Session & connect(const std::string &host, Port port)
Establishes the connection.
Threading::SharedFuture< BgwCredentials > getBgwCredentialsAsync(const std::string &userId, bool forceRequest=0) final
Asynchronously provides the BGW session connection credentials for the given user id.
const std::string & userId() const noexcept
ThreadingModel::Enum threadingModel() const noexcept
Session & disconnect(const std::string &reason={})
Terminates the connection.
void onMassQuoteReport(const Messaging::MassQuoteReport msg, Session *) override
Called when a MassQuoteReport message is received.
Definition Listener.h:180
void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session *) override
Called when the session changes its state.
Definition Listener.h:185
void onSecurityDefinitionReject(const Messaging::SecurityDefinitionReject msg, Session *) override
Called when a SecurityDefinitionReject message is received.
Definition Listener.h:99
void onWarning(SessionWarningReason::Enum, const std::string &description, Session *, Messaging::SbeMessage msg) override
Called when a warning condition is detected by the session.
Definition Listener.h:209
void onNews(const Messaging::News msg, Session *) override
Called when a News message is received.
Definition Listener.h:84
std::promise< void > traderLoggedOn
Definition Listener.h:223
void onTraderLogoutReport(const Messaging::TraderLogoutReport msg, Session *) override
Called when a TraderLogoutReport message is received.
Definition Listener.h:130
~Listener() override=default
void onHeartBeat(const Messaging::HeartBeat msg, Session *) override
Called when a HeartBeat message is received.
Definition Listener.h:74
void onSecurityDefinitionReport(const Messaging::SecurityDefinitionReport msg, Session *) override
Called when a SecurityDefinitionReport message is received.
Definition Listener.h:94
void onNewOrderCrossReport(const Messaging::NewOrderCrossReport msg, Session *) override
Called when a NewOrderCrossReport message is received.
Definition Listener.h:170
void onExecutionReport_Modify(const Messaging::ExecutionReport_Modify msg, Session *) override
Called when an ExecutionReport_Modify message is received.
Definition Listener.h:140
void onLogonReport(const Messaging::LogonReport msg, Session *) override
Called when a LogonReport message is received.
Definition Listener.h:64
void onError(const Messaging::Error msg, Session *) override
Called when an Error message is received from the counterparty.
Definition Listener.h:79
void onIPReport(const Messaging::IPReport msg, Session *) override
Called when an IPReport message is received.
Definition Listener.h:109
void onExecutionReport_Cancel(const Messaging::ExecutionReport_Cancel msg, Session *) override
Called when an ExecutionReport_Cancel message is received.
Definition Listener.h:145
void onExecutionReport_Reject(const Messaging::ExecutionReport_Reject msg, Session *) override
Called when an ExecutionReport_Reject message is received.
Definition Listener.h:150
void onExecutionReport_Trade(const Messaging::ExecutionReport_Trade msg, Session *) override
Called when an ExecutionReport_Trade message is received.
Definition Listener.h:160
void onOrderMassCancelReport(const Messaging::OrderMassCancelReport msg, Session *) override
Called when an OrderMassCancelReport message is received.
Definition Listener.h:175
void onExecutionReport_New(const Messaging::ExecutionReport_New msg, Session *) override
Called when an ExecutionReport_New message is received.
Definition Listener.h:135
void onQuoteReport(const Messaging::QuoteReport msg, Session *) override
Called when a QuoteReport message is received.
Definition Listener.h:165
void onSecurityDefinitionReport_Strategy(const Messaging::SecurityDefinitionReport_Strategy msg, Session *) override
Called when a SecurityDefinitionReport_Strategy message is received.
Definition Listener.h:89
void onSecurityDefinitionReport_Product(const Messaging::SecurityDefinitionReport_Product msg, Session *) override
Called when a SecurityDefinitionReport_Product message is received.
Definition Listener.h:104
void onMessageSending(char *bytes, size_t size, Session *) override
Called right before an SBE message is sent to the wire.
Definition Listener.h:58
void onError(SessionErrorReason::Enum, const std::string &description, Session *, Messaging::SbeMessage msg) override
Called when an error condition is detected by the session.
Definition Listener.h:195
void onExecutionReport_Snapshot(const Messaging::ExecutionReport_Snapshot msg, Session *) override
Called when an ExecutionReport_Snapshot message is received.
Definition Listener.h:155
void onLogoutReport(const Messaging::LogoutReport msg, Session *) override
Called when a LogoutReport message is received.
Definition Listener.h:69
void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session *) override
Called when a TraderLogonReport message is received.
Definition Listener.h:114
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
bool processTypified(SbeMessage binary, Processor &&processor)
Casts a given binary message according to template/type information and processes the cast messages b...
std::string format(Int32 index, StrRef input)
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
unsigned short Port
Definition Defines.h:41
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:264
std::future< T > wait(Stack &stack, std::promise< T > &promise, std::chrono::seconds timeout=std::chrono::seconds{30})
Definition Listener.h:227
Error processing client request.
Definition Messages.h:1339
New Order, cancel-replace, or cancel reject.
Definition Messages.h:18849
Heartbeat message. Sent in the absence of application messages.
Definition Messages.h:1160
Response to IPRequest for Binary Gateways.
Definition Messages.h:8474
Logon Report for Gateway ID.
Definition Messages.h:393
Logout Report for Gateway ID.
Definition Messages.h:903
Message identifiers and length of message root.
Definition Messages.h:22567
Report for Order Mass Cancel Request.
Definition Messages.h:22158
Response to Quote Request (RFQ).
Definition Messages.h:21460
Security Definition for futures, options, and FLEX creations. Each report will contain a single marke...
Definition Messages.h:4235
Trader Logon Report for Trader ID.
Definition Messages.h:9418
auto exchangeCode() const noexcept
int32NULL.
Definition Messages.h:9520
auto text() const noexcept
Provides access to text field.
Definition Messages.h:9546
static const std::string & toString(Enum state) noexcept
@ Dedicated
Each session has a sender thread and a receiving thread.
void operator()(const MessageType msg) const
Definition Listener.h:41
static void printMsg(const Messaging::SbeMessage msg)
Definition Listener.h:46