OnixS C++ ICE Binary Order Entry Handler 1.2.3
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 // `text` is optional on the wire and capped at 100 characters, so it need not carry the
119 // values the code's entry asks for. Let `Messaging::format` describe such a frame instead
120 // of dereferencing an absent `text` and throwing out of this callback, which would leave
121 // `traderLoggedOn` unsatisfied and every waiter on it blocked until its timeout.
122 if(msg.exchangeCode())
123 {
124 traderLoggedOn.set_exception(std::make_exception_ptr(std::runtime_error(Messaging::format(msg))));
125 return;
126 }
127
128 traderLoggedOn.set_value();
129 }
130
132 {
133 std::clog << "\nReceived:\n" << msg << std::endl;
134 }
135
137 {
138 std::clog << "\nReceived:\n" << msg << std::endl;
139 }
140
142 {
143 std::clog << "\nReceived:\n" << msg << std::endl;
144 }
145
147 {
148 std::clog << "\nReceived:\n" << msg << std::endl;
149 }
150
152 {
153 std::clog << "\nReceived:\n" << msg << std::endl;
154 }
155
157 {
158 std::clog << "\nReceived:\n" << msg << std::endl;
159 }
160
162 {
163 std::clog << "\nReceived:\n" << msg << std::endl;
164 }
165
167 {
168 std::clog << "\nReceived:\n" << msg << std::endl;
169 }
170
172 {
173 std::clog << "\nReceived:\n" << msg << std::endl;
174 }
175
177 {
178 std::clog << "\nReceived:\n" << msg << std::endl;
179 }
180
182 {
183 std::clog << "\nReceived:\n" << msg << std::endl;
184 }
185
187 {
188 std::clog
189 << "\nSession's state is changed, prevState="
190 << SessionStateId::toString(prevState)
191 << ", newState="
192 << SessionStateId::toString(newState)
193 << std::endl;
194 }
195
197 SessionErrorReason::Enum, const std::string& description, Session*, Messaging::SbeMessage msg) override
198 {
199 std::cerr << "\nSession-level error: " << description;
200
201 if(msg.valid())
202 {
203 std::cerr << ",\nCaused by the message: ";
205 }
206
207 std::cerr << std::endl;
208 }
209
211 SessionWarningReason::Enum, const std::string& description, Session*, Messaging::SbeMessage msg) override
212 {
213 std::cerr << "\nSession-level warning: " << description;
214
215 if(msg.valid())
216 {
217 std::cerr << ",\nCaused by the message: ";
219 }
220
221 std::cerr << std::endl;
222 }
223
224 std::promise<void> traderLoggedOn;
225};
226
227template <typename T, typename Stack>
228std::future<T> wait(Stack& stack, std::promise<T>& promise, std::chrono::seconds timeout = std::chrono::seconds{30})
229{
230 auto future = promise.get_future();
231
232 const auto deadline = std::chrono::steady_clock::now() + timeout;
233 std::uint32_t spins = 0;
234
235 for (;;)
236 {
237 stack.dispatchEvents();
238
239 if (future.wait_for(std::chrono::nanoseconds{0}) == std::future_status::ready)
240 return future;
241
242 if ((++spins & 0xFFu) == 0) // every 256 iterations
243 {
244 if (std::chrono::steady_clock::now() >= deadline)
245 {
246 promise.set_exception(std::make_exception_ptr(std::runtime_error("Operation timed out.")));
247 return future;
248 }
249 }
250 }
251}
252
253template <typename T>
254std::future<T> wait(std::promise<T>& promise, std::chrono::seconds timeout = std::chrono::seconds{30})
255{
256 auto future = promise.get_future();
257
258 if(future.wait_for(timeout) != std::future_status::ready)
259 promise.set_exception(std::make_exception_ptr(std::runtime_error("Operation timed out.")));
260
261 return future;
262}
263
264// Request Binary Order Gateway credentials from Binary Utility Service Gateway.
265inline BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
266{
268
269 Listener listener;
270
271 // Create an instance of a `BUS` session.
272 BusSession busSession(settings, &listener);
273
274 // Connect.
275 busSession.connect(host, port);
276
277 const auto bgwCredentialsFuture = busSession.getBgwCredentialsAsync(settings.userId());
278
279 const auto bgwCredentials = bgwCredentialsFuture.get();
280
281 // Disconnect.
282 busSession.disconnect();
283
284 return bgwCredentials;
285}
286
287}
#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.
SbeMessage() noexcept
Initializes a blank instance.
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:181
void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session *) override
Called when the session changes its state.
Definition Listener.h:186
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:210
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:224
void onTraderLogoutReport(const Messaging::TraderLogoutReport msg, Session *) override
Called when a TraderLogoutReport message is received.
Definition Listener.h:131
~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:171
void onExecutionReport_Modify(const Messaging::ExecutionReport_Modify msg, Session *) override
Called when an ExecutionReport_Modify message is received.
Definition Listener.h:141
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:146
void onExecutionReport_Reject(const Messaging::ExecutionReport_Reject msg, Session *) override
Called when an ExecutionReport_Reject message is received.
Definition Listener.h:151
void onExecutionReport_Trade(const Messaging::ExecutionReport_Trade msg, Session *) override
Called when an ExecutionReport_Trade message is received.
Definition Listener.h:161
void onOrderMassCancelReport(const Messaging::OrderMassCancelReport msg, Session *) override
Called when an OrderMassCancelReport message is received.
Definition Listener.h:176
void onExecutionReport_New(const Messaging::ExecutionReport_New msg, Session *) override
Called when an ExecutionReport_New message is received.
Definition Listener.h:136
void onQuoteReport(const Messaging::QuoteReport msg, Session *) override
Called when a QuoteReport message is received.
Definition Listener.h:166
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:196
void onExecutionReport_Snapshot(const Messaging::ExecutionReport_Snapshot msg, Session *) override
Called when an ExecutionReport_Snapshot message is received.
Definition Listener.h:156
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:322
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:265
std::future< T > wait(Stack &stack, std::promise< T > &promise, std::chrono::seconds timeout=std::chrono::seconds{30})
Definition Listener.h:228
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
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