OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
TCPDirectGettingStarted.cpp
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 */
20
21using namespace ONIXS_ICEBOE_NAMESPACE;
23
24#include <Settings/Defaults.h>
25#include <Common/Helpers.h>
26#include <Common/Listener.h>
27#include <Common/Settings.h>
28#include <Common/Signal.h>
29#include <Common/Emulator.h>
30
31using namespace Samples;
32using namespace Threading;
33
34int main(int argc, char * argv[])
35{
36 // `--help` to show options.
37 const AppConfiguration<
43 > cfg{"TCPDirectGettingStarted", argc, argv};
44
45 try
46 {
48
49 auto settings = fillSettings(cfg);
50 const auto emulator = createEmulator(settings, cfg, true);
51
52 // Request Binary Order Gateway credentials from Binary Utility Service Gateway (using OS sockets)
53 const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
54
55 class TcpDirectListener : public Listener
56 {
57 public:
58 explicit TcpDirectListener(const LogonConfiguration& cfg)
59 : traderLogonRequest_(Helper::createTraderLogonRequest(cfg.traderId(), cfg.traderPwd()))
60 , order_(Helper::createOrder(cfg.traderId()))
61 {
62 }
63
64 void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session* session) override
65 {
67
68 try
69 {
70 traderLoggedOn.get_future().get();
71 }
72 catch(const std::exception& e)
73 {
75 return;
76 }
77
78 session->send(order_);
79 }
80
81 void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session * session) override
82 {
83 Listener::onStateChange(newState, prevState, session);
84
85 switch(newState)
86 {
88 {
89 // The TCPDirect technology is NOT thread-safe.
90 // Therefore, the event dispatching and message sending should be performed from the same thread.
91 session->send(traderLogonRequest_);
92 break;
93 }
94
96 finished_ = true;
97 break;
98
99 default:
100 break;
101 }
102 }
103
105
106 void onError(
107 SessionErrorReason::Enum reason, const std::string & text, Session * session, Messaging::SbeMessage msg) override
108 {
109 Listener::onError(reason, text, session, msg);
110 finished_ = true;
111 }
112
113 bool finished() const noexcept {return finished_;}
114
115 private:
116 bool finished_ {false};
117 MessageHolder<TraderLogonRequest> traderLogonRequest_;
119 }
120 listener(cfg);
121
122
123 TcpDirectAttr attr;
124
125 // The default interface name could be provided by the environment variable
126 // ZF_ATTR="interface=<interface-name>", see TCPDirect specs,
127 attr.set("interface", cfg.nif());
128
129 TcpDirectStack stack{attr};
130
131 // A 'BGW' session
132 const auto bgwSession= std::make_unique<BgwSession>(stack, settings, &listener, cfg.storage());
133 bgwSession->connectAsync(bgwCredentials);
134
135 std::clog << "\nPress <Ctrl+C> to disconnect the BGW session and terminate the application." << std::endl;
136
137 while(!listener.finished())
138 {
139 // The TCPDirect technology is NOT thread-safe.
140 // Therefore, the event dispatching and message sending should be performed from the same thread.
141 stack.dispatchEvents();
142
144 break;
145 }
146
147 bgwSession->disconnectAsync();
148
149 while(!stack.isQuiescent())
150 stack.dispatchEvents();
151 }
152 catch(const std::exception & ex)
153 {
154 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
155 return 1;
156 }
157
158 return 0;
159}
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
int main(int argc, char *argv[])
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
Session & send(Messaging::MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg, Messaging::Timestamp sendingTime=UtcWatch::now())
Sends the message.
Definition Session.h:659
TCPDirect Attributes to pass configuration details (a wrapper around the zf_attr struct).
void set(const std::string &name, const std::string &value)
Sets the attribute to the given value.
A high-level wrapper over the TCPDirect network stack.
bool isQuiescent() const noexcept override
void dispatchEvents() noexcept override
This function processes events on a stack and performs the necessary handling.
static MessageHolder< NewOrderRequest > createOrder(const std::string &traderId)
Definition Helpers.h:104
static MessageHolder< TraderLogonRequest > createTraderLogonRequest(const std::string &traderId, const std::string &traderPwd)
Definition Helpers.h:77
void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session *) override
Called when the session changes its state.
Definition Listener.h:185
void onError(const Messaging::Error msg, Session *) override
Called when an Error message is received from the counterparty.
Definition Listener.h:79
void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session *) override
Called when a TraderLogonReport message is received.
Definition Listener.h:114
std::string traderPwd() const
Definition Options.h:553
std::string traderId() const
Definition Options.h:548
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
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:264
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
Trader Logon Report for Trader ID.
Definition Messages.h:9418
@ UnsuccessfulLogonReportInReplyOnLogonRequest
The unsuccessful Logon Report message is received in reply to the LogonRequest message.
@ Established
Session is fully established.
@ Disconnected
Session is disconnected.
static ONIXS_ICEBOE_FORCEINLINE bool interruptDetected() noexcept
Definition Signal.h:52
static void manageSignals() noexcept
Definition Signal.h:46