OnixS C++ ICE Binary Order Entry Handler 1.2.3
API Documentation
Loading...
Searching...
No Matches
TCPDirectGettingStarted.cpp File Reference

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 35 of file TCPDirectGettingStarted.cpp.

36{
37 // `--help` to show options.
38 const AppConfiguration<
44 > cfg{"TCPDirectGettingStarted", argc, argv};
45
46 try
47 {
49
50 auto settings = fillSettings(cfg);
51 const auto emulator = createEmulator(settings, cfg, true);
52
53 // Request Binary Order Gateway credentials from Binary Utility Service Gateway (using OS sockets)
54 const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
55
56 class TcpDirectListener : public Listener
57 {
58 public:
59 explicit TcpDirectListener(const LogonConfiguration& cfg)
60 : traderLogonRequest_(Helper::createTraderLogonRequest(cfg.traderId(), cfg.traderPwd()))
61 , order_(Helper::createOrder(cfg.traderId()))
62 {
63 }
64
65 void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session* session) override
66 {
68
69 try
70 {
71 traderLoggedOn.get_future().get();
72 }
73 catch(const std::exception& e)
74 {
76 return;
77 }
78
79 session->send(order_);
80 }
81
82 void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session * session) override
83 {
84 Listener::onStateChange(newState, prevState, session);
85
86 switch(newState)
87 {
89 {
90 // The TCPDirect technology is NOT thread-safe.
91 // Therefore, the event dispatching and message sending should be performed from the same thread.
92 session->send(traderLogonRequest_);
93 break;
94 }
95
97 finished_ = true;
98 break;
99
100 default:
101 break;
102 }
103 }
104
106
107 void onError(
108 SessionErrorReason::Enum reason, const std::string & text, Session * session, Messaging::SbeMessage msg) override
109 {
110 Listener::onError(reason, text, session, msg);
111 finished_ = true;
112 }
113
114 bool finished() const noexcept {return finished_;}
115
116 private:
117 bool finished_ {false};
118 MessageHolder<TraderLogonRequest> traderLogonRequest_;
119 MessageHolder<NewOrderRequest> order_;
120 }
121 listener(cfg);
122
123
124 TcpDirectAttr attr;
125
126 // The default interface name could be provided by the environment variable
127 // ZF_ATTR="interface=<interface-name>", see TCPDirect specs,
128 attr.set("interface", cfg.nif());
129
130 TcpDirectStack stack{attr};
131
132 // A 'BGW' session
133 const auto bgwSession= makeUnique<BgwSession>(stack, settings, &listener, cfg.storage());
134 bgwSession->connectAsync(bgwCredentials);
135
136 std::clog << "\nPress <Ctrl+C> to disconnect the BGW session and terminate the application." << std::endl;
137
138 while(!listener.finished())
139 {
140 // The TCPDirect technology is NOT thread-safe.
141 // Therefore, the event dispatching and message sending should be performed from the same thread.
142 stack.dispatchEvents();
143
145 break;
146 }
147
148 bgwSession->disconnectAsync();
149
150 while(!stack.isQuiescent())
151 stack.dispatchEvents();
152 }
153 catch(const std::exception & ex)
154 {
155 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
156 return 1;
157 }
158
159 return 0;
160}
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.
void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session *) override
Called when the session changes its state.
Definition Listener.h:186
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::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
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:265
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
std::unique_ptr< T > makeUnique(Args &&...args)
Definition Tools.h:30
@ 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