OnixS C++ ICE Binary Order Entry Handler 1.1.1
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 34 of file TCPDirectGettingStarted.cpp.

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_;
118 MessageHolder<NewOrderRequest> order_;
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}
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: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::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
@ 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