314 lines
9.7 KiB
C++
314 lines
9.7 KiB
C++
#include "GLBXiLink3ExecutionGateway.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/orderman_cfg.pb.h"
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/jetfix/common/OutFIX.h"
|
|
#include "lib/jetfix/store/session_name.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
|
#include "lib/orderman/gateways/GLBXExecutionGateway.h"
|
|
#include "lib/orderman/ilink3/IL3GW.h"
|
|
#include "lib/orderman/ilink3/IL3MessageFramer.h"
|
|
#include "lib/orderman/ilink3/IL3Reactor.h"
|
|
#include "lib/orderman/ilink3/IL3SecDB.h"
|
|
#include "lib/orderman/ilink3/SessionStore.h"
|
|
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include <cstddef>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
namespace ilink3 {
|
|
|
|
struct OrderStats {
|
|
size_t n_new_orders = 0;
|
|
size_t n_cxl_replaces = 0;
|
|
size_t n_cxls = 0;
|
|
};
|
|
|
|
struct GLBXiLink3ExecutionGateway::PImpl {
|
|
PImpl(
|
|
config::JetFIX::Connection const &cfg,
|
|
trace::PerfLog &log,
|
|
md::SecurityOracle o)
|
|
: state(), cfg(cfg), reactor(), start_of_pause(), on_exe_report(),
|
|
on_gw_status_change(), on_cancel_reject(), perf_log(log), db(o),
|
|
gw_name(cfg.name()), session_store(), gw(), connection(nullptr),
|
|
connection_failures(0), started(false), replay_messages(),
|
|
has_replayed(false) {
|
|
TCPEndpoint endpoint{
|
|
cfg.socket_connect_host_ip(), (ushort)cfg.socket_connect_port()};
|
|
connection = &reactor.register_connection({endpoint});
|
|
}
|
|
|
|
State state;
|
|
config::JetFIX::Connection const cfg;
|
|
IL3Reactor reactor;
|
|
IL3MessageFramer framer;
|
|
TimeStamp start_of_pause;
|
|
|
|
OnExeReport on_exe_report;
|
|
OnGwStatusChange on_gw_status_change;
|
|
orderman::OnCancelReject on_cancel_reject;
|
|
orderman::OnExchangePositionState on_exchange_position_state;
|
|
orderman::OnExchangeOpenOrdersState on_exchange_open_orders;
|
|
trace::PerfLog &perf_log;
|
|
IL3SecDB db;
|
|
GatewayName gw_name;
|
|
std::optional<SessionStore> session_store;
|
|
std::optional<IL3GW> gw;
|
|
IL3Connection *connection;
|
|
size_t connection_failures;
|
|
bool started;
|
|
|
|
// We need to keep this collection alive for the duration of the
|
|
// program because we will be passing pointers to ExecutionReport
|
|
// to other threads
|
|
std::vector<ExecutionReport> replay_messages;
|
|
bool has_replayed;
|
|
OrderStats order_stats = {};
|
|
};
|
|
|
|
GLBXiLink3ExecutionGateway::GLBXiLink3ExecutionGateway(
|
|
config::JetFIX::Connection const &cfg,
|
|
trace::PerfLog &log,
|
|
md::SecurityOracle o)
|
|
: me(new PImpl(cfg, log, o)) {
|
|
LOG(OrderMan, Info, "GLBXiLink3ExecutionGateway::cons cfg: {}",
|
|
utils::pb::to_string(cfg));
|
|
me->state = State::WAITING_FOR_SECURITIES;
|
|
}
|
|
|
|
GLBXiLink3ExecutionGateway::~GLBXiLink3ExecutionGateway() {}
|
|
|
|
void GLBXiLink3ExecutionGateway::start() {
|
|
bts_assert(!me->started);
|
|
me->started = true;
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::dispatch_incoming_fix_messages() {
|
|
if (me->started) {
|
|
switch (me->state.value) {
|
|
case State::WAITING_FOR_SECURITIES:
|
|
break;
|
|
case State::PAUSED:
|
|
on_time();
|
|
break;
|
|
case State::READY:
|
|
case State::CONNECTING:
|
|
me->reactor.do_work();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::on_time() {
|
|
bts_assert(me->state == State::PAUSED);
|
|
|
|
// If we are still paused bail early, this avoids logging too much
|
|
// or spending too much CPU
|
|
if (pause_in_effect())
|
|
return;
|
|
|
|
me->start_of_pause = Timer::now();
|
|
|
|
// If we alread exceed the maximum number of failures per run
|
|
// bail. This is a safety measure to avoid overwhelming the
|
|
// exchange in the event of a bug or operational problem that
|
|
// results in repeated connection failures.
|
|
if (max_failures_reached()) {
|
|
LOG(iLink3, Warn,
|
|
"Maximum number of connection failures reached for {}. Reboot to try "
|
|
"again",
|
|
me->gw_name.value());
|
|
return;
|
|
}
|
|
|
|
// If we reach this point we are no longer in the pause window and
|
|
// we have not exceed our connection failure limit. The only thing
|
|
// to check now is wheter we are inside the exchange's active
|
|
// hours.
|
|
auto current_session = jetfix::session_name(me->cfg);
|
|
if (current_session.empty()) {
|
|
LOG(iLink3, Info, "Outside of session in ", me->gw_name.value());
|
|
return;
|
|
}
|
|
// Yep, the stars are all aligned, lets try to connect
|
|
connect(current_session);
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::connect(jetfix::SessionName const &sn) {
|
|
bts_assert(me->state == State::PAUSED);
|
|
transition(State::CONNECTING);
|
|
Config c;
|
|
c.cme_session = me->cfg.sender_compid().substr(0, 3);
|
|
c.firm = me->cfg.sender_compid().substr(3);
|
|
c.failover_on_disconnect = false;
|
|
|
|
me->session_store.emplace(me->db, sn, &me->cfg);
|
|
if (!me->has_replayed) {
|
|
me->has_replayed = true;
|
|
me->replay_messages = me->session_store.value().replay();
|
|
for (auto const &er : me->replay_messages)
|
|
me->on_exe_report(er, {});
|
|
}
|
|
|
|
c.raw_message_logger =
|
|
[store = &me->session_store.value()](auto d, auto sn, auto const &msg) {
|
|
store->async_store_raw_msg(d, sn, msg);
|
|
};
|
|
|
|
me->gw.emplace(
|
|
*me->connection, me->framer, &me->db,
|
|
me->session_store->starting_session_descriptor(), c, &me->cfg);
|
|
|
|
me->gw->subscribe_cancel_reject([this](auto &cr) {
|
|
bts_assert(me->state == State::READY || me->state == State::CONNECTING);
|
|
me->on_cancel_reject(cr);
|
|
});
|
|
|
|
me->gw->subscribe_execution_reports([this](auto er) {
|
|
bts_assert(me->state == State::READY || me->state == State::CONNECTING);
|
|
me->on_exe_report(*er, {});
|
|
});
|
|
|
|
me->gw->subscribe_session(
|
|
[this](auto const &s) { me->session_store->async_update_session(s); });
|
|
|
|
me->gw->subscribe_state([this](auto gw_state) {
|
|
LOG(iLink3, Info, "Internal gateway state change in {}. State is now {}",
|
|
me->gw_name.value(), gw_state);
|
|
switch (me->state.value) {
|
|
case State::CONNECTING:
|
|
if (gw_state == ilink3::State::READY) {
|
|
transition(State::READY);
|
|
me->on_gw_status_change(true);
|
|
} else if (gw_state == ilink3::State::ERROR) {
|
|
me->connection_failures++;
|
|
pause();
|
|
}
|
|
break;
|
|
case State::READY:
|
|
if (gw_state == ilink3::State::ERROR) {
|
|
me->connection_failures++;
|
|
pause();
|
|
me->on_gw_status_change(false);
|
|
}
|
|
break;
|
|
default:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
});
|
|
me->gw->connect();
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::pause() {
|
|
me->start_of_pause = Timer::now();
|
|
me->gw.reset();
|
|
transition(State::PAUSED);
|
|
}
|
|
|
|
bool GLBXiLink3ExecutionGateway::pause_in_effect() const {
|
|
return sec_diff(Timer::now(), me->start_of_pause) <=
|
|
me->cfg.seconds_to_pause();
|
|
}
|
|
|
|
bool GLBXiLink3ExecutionGateway::max_failures_reached() const {
|
|
return me->connection_failures > me->cfg.max_connection_attempts();
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::on_securities(
|
|
const mercury::Update<NewSecuritiesEvent> &update) {
|
|
if (update.events) {
|
|
|
|
for (auto const &ev : *(update.events)) {
|
|
auto sec_ptr = ev.security;
|
|
me->db.add_security(*sec_ptr);
|
|
}
|
|
|
|
if (me->state == State::WAITING_FOR_SECURITIES) {
|
|
LOG(iLink3, Warn,
|
|
"Received first set of {} securities. Proceeding "
|
|
"with initialization of execution gateway {}",
|
|
update.events->size(), me->gw_name.value());
|
|
// got our security synopsis, ready to start the GW
|
|
transition(State::PAUSED);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::on_admin_data(
|
|
const mercury::Update<NewAdminDataEvent> &) {}
|
|
|
|
void GLBXiLink3ExecutionGateway::send_msgs(
|
|
std::vector<OrderMsg> const &msgs, trace::CorID corid) {
|
|
|
|
if (me->state == State::READY) {
|
|
for (auto &m : msgs) {
|
|
// NewOrderSingle
|
|
auto create_ptr = std::get_if<CreateOrderMsg>(&m);
|
|
if (create_ptr) {
|
|
me->gw->send_order(*create_ptr, corid);
|
|
me->order_stats.n_new_orders++;
|
|
continue;
|
|
}
|
|
// CancelReplace
|
|
auto cancel_replace_ptr = std::get_if<CancelReplaceOrderMsg>(&m);
|
|
if (cancel_replace_ptr) {
|
|
LOG(Latency, Info, "Cancel Replace. CorID:{}.{} Order:{}",
|
|
std::string{corid.channel}, corid.sn,
|
|
cancel_replace_ptr->exchange_order_id);
|
|
|
|
me->gw->send_cancel_replace(*cancel_replace_ptr, corid);
|
|
me->order_stats.n_cxl_replaces++;
|
|
continue;
|
|
}
|
|
// Cancel
|
|
auto cancel_ptr = std::get_if<CancelOrderMsg>(&m);
|
|
if (cancel_ptr) {
|
|
me->gw->send_cancel(*cancel_ptr, corid);
|
|
me->order_stats.n_cxls++;
|
|
continue;
|
|
}
|
|
}
|
|
me->gw->flush();
|
|
LOG(OrderMan, Info,
|
|
"Order Stats n_new_orders:{} n_cxl_replaces:{} n_cxls:{}",
|
|
me->order_stats.n_new_orders, me->order_stats.n_cxl_replaces,
|
|
me->order_stats.n_cxls);
|
|
}
|
|
}
|
|
|
|
OnExeReport &GLBXiLink3ExecutionGateway::on_exe_report() {
|
|
return me->on_exe_report;
|
|
}
|
|
|
|
OnGwStatusChange &GLBXiLink3ExecutionGateway::on_gw_status_change() {
|
|
return me->on_gw_status_change;
|
|
}
|
|
|
|
orderman::OnCancelReject &GLBXiLink3ExecutionGateway::on_cancel_reject() {
|
|
return me->on_cancel_reject;
|
|
}
|
|
|
|
orderman::OnExchangePositionState &
|
|
GLBXiLink3ExecutionGateway::on_exchange_position_state() {
|
|
return me->on_exchange_position_state;
|
|
}
|
|
|
|
OnExchangeOpenOrdersState &
|
|
GLBXiLink3ExecutionGateway::on_exchange_open_orders() {
|
|
return me->on_exchange_open_orders;
|
|
}
|
|
|
|
void GLBXiLink3ExecutionGateway::transition(State new_state) {
|
|
LOG(iLink3, Info, "Transitioning {} from {} to {}", me->gw_name.value(),
|
|
me->state, new_state);
|
|
me->state = new_state;
|
|
}
|
|
|
|
} // namespace ilink3
|
|
} // namespace orderman
|
|
} // namespace bts
|