235 lines
6.4 KiB
C++
235 lines
6.4 KiB
C++
#include "Gateway.h"
|
|
|
|
#include "gateway/GatewayError.h"
|
|
#include "gateway/IGatewayObserver.h"
|
|
#include "gateway/OrderHandle.h"
|
|
#include "gateway/detail/common/IL3GatewayCfg.h"
|
|
#include "gateway/detail/common/IL3MsgFramer.h"
|
|
#include "gateway/detail/handlers/IL3InitHandler.h"
|
|
#include "gateway/detail/handlers/IL3InitRec.h"
|
|
#include "gateway/detail/handlers/IL3RealTimeHandler.h"
|
|
#include "gateway/detail/parsing/IL3Parser.h"
|
|
#include "gateway/detail/store/IIL3SessionStore.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "system/Clock.h"
|
|
#include "system/reactor/IConnection.h"
|
|
|
|
#include <variant>
|
|
|
|
namespace bts::spartan {
|
|
|
|
enum class Gateway::State { SETUP,CONNECTING, INITIALIZING, READY, ERROR };
|
|
|
|
struct Gateway::PImpl {
|
|
IL3MsgFramer incoming_msg_framer;
|
|
std::variant<IL3InitHandler, IL3RealTimeHandler> handler;
|
|
IGatewayObserver &observer;
|
|
IConnection &connection;
|
|
IIL3SessionStore &store;
|
|
Clock &clock;
|
|
State state;
|
|
const IL3GatewayCfg cfg;
|
|
};
|
|
|
|
Gateway::Gateway() : me() {}
|
|
Gateway::~Gateway() {}
|
|
|
|
void Gateway::init(
|
|
IGatewayObserver &observer,
|
|
IL3GatewayCfg const &cfg,
|
|
IIL3SessionStore &store,
|
|
IConnection &connection,
|
|
Clock &clock) {
|
|
bts_assert(!me);
|
|
me.reset(new PImpl{
|
|
{}, // Framer
|
|
{}, // Current handler
|
|
observer,
|
|
connection,
|
|
store,
|
|
clock,
|
|
State::SETUP,
|
|
cfg});
|
|
}
|
|
|
|
void Gateway::run() {
|
|
bts_assert(me->state == State::SETUP);
|
|
me->state = State::CONNECTING;
|
|
me->connection.connect();
|
|
// Expect on_connect or on_disconnect
|
|
}
|
|
|
|
void Gateway::on_connect() {
|
|
bts_assert(me->state == State::CONNECTING);
|
|
transition(State::INITIALIZING);
|
|
me->handler.emplace<IL3InitHandler>();
|
|
std::get<IL3InitHandler>(me->handler)
|
|
.start(me->cfg, *this, *this, me->clock);
|
|
}
|
|
|
|
void Gateway::on_disconnect() {
|
|
switch (me->state) {
|
|
case State::CONNECTING:
|
|
on_error({GatewayError::FAILED_TO_CONNECT});
|
|
break;
|
|
default:
|
|
on_error({GatewayError::UNEXPECTED_DISCONNECT});
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Gateway::on_data(DataPacket const &dp) {
|
|
GatewayError error;
|
|
|
|
me->incoming_msg_framer.on_bytes(dp.payload, [this, &error](std::string_view msg) {
|
|
switch (me->state) {
|
|
case State::READY:
|
|
case State::INITIALIZING: {
|
|
auto on_bytes = [msg, &error](auto &handler) {
|
|
auto ok = IL3Parser::parse(msg, handler);
|
|
if (!ok)
|
|
error = GatewayError{
|
|
GatewayError::UNKNOWN_MESSAGE, "Could not parse message"};
|
|
};
|
|
std::visit(on_bytes, me->handler);
|
|
break;
|
|
}
|
|
case State::SETUP:
|
|
case State::CONNECTING:
|
|
bts_assert(false);
|
|
break;
|
|
case State::ERROR:
|
|
break;
|
|
}
|
|
me->store.store_incoming(msg);
|
|
if (error)
|
|
on_error(error);
|
|
});
|
|
}
|
|
|
|
OrderHandle Gateway::queue(NewOrder const &o) {
|
|
bts_assert(me->state == State::READY);
|
|
return std::get<IL3RealTimeHandler>(me->handler).queue(o);
|
|
}
|
|
void Gateway::queue(OrderHandle h, CancelReplace const &cr) {
|
|
bts_assert(me->state == State::READY);
|
|
std::get<IL3RealTimeHandler>(me->handler).queue(h,cr);
|
|
}
|
|
|
|
void Gateway::queue(OrderHandle h, Cancel const &c) {
|
|
bts_assert(me->state == State::READY);
|
|
std::get<IL3RealTimeHandler>(me->handler).queue(h,c);
|
|
}
|
|
|
|
void Gateway::send_queued() {
|
|
bts_assert(me->state == State::READY);
|
|
IL3MsgSender::flush();
|
|
}
|
|
|
|
void Gateway::on_init_success(IL3InitRec const &rec) {
|
|
bts_assert(me->state == State::INITIALIZING);
|
|
transition(State::READY);
|
|
auto rec_cpy = rec;
|
|
me->handler.emplace<IL3RealTimeHandler>();
|
|
std::get<IL3RealTimeHandler>(me->handler)
|
|
.start(me->cfg, rec, *this, *this, me->clock);
|
|
}
|
|
|
|
void Gateway::on_init_failure(GatewayError const &error) {
|
|
bts_assert(me->state == State::INITIALIZING);
|
|
on_error(error);
|
|
}
|
|
|
|
void Gateway::on_flush(std::string_view bytes) {
|
|
me->connection.send(bytes);
|
|
me->store.store_incoming(bytes);
|
|
}
|
|
|
|
void Gateway::on_error(GatewayError const &e) {
|
|
auto segment = me->cfg.segment;
|
|
|
|
switch (me->state) {
|
|
case State::READY:
|
|
case State::INITIALIZING:
|
|
LOG(Spartan, Error, "Shutting down gateway {} Error: {}", segment, e);
|
|
terminate();
|
|
break;
|
|
case State::CONNECTING:
|
|
LOG(Spartan, Error,
|
|
"Error while attempting to connect gateway on segment {} Error: {}",
|
|
segment, e);
|
|
transition(State::ERROR);
|
|
break;
|
|
case State::ERROR:
|
|
LOG(Spartan, Error, "Multiple errors on gateway on segment {} Error: {}",
|
|
segment, e);
|
|
transition(State::ERROR);
|
|
break;
|
|
case State::SETUP:
|
|
bts_assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
const char *Gateway::state_name(State state) {
|
|
switch (state) {
|
|
case State::CONNECTING:
|
|
return "CONNECTING";
|
|
case State::INITIALIZING:
|
|
return "INITIALIZING";
|
|
case State::READY:
|
|
return "READY";
|
|
case State::ERROR:
|
|
return "ERROR";
|
|
case State::SETUP:
|
|
return "SETUP";
|
|
}
|
|
bts_assert(false);
|
|
return nullptr;
|
|
}
|
|
|
|
void Gateway::transition(State new_state) {
|
|
auto segment = me->cfg.segment;
|
|
LOG(Spartan, Warn, "Transitioning gateway {} from {} to {}", segment,
|
|
log::CharPtrWrapper{state_name(me->state)},
|
|
log::CharPtrWrapper{state_name(new_state)});
|
|
me->state = new_state;
|
|
}
|
|
|
|
void Gateway::on_realtime_error(GatewayError const &e) {
|
|
bts_assert_d(me->state == State::READY);
|
|
if (e.error_code() == GatewayError::ATTEMPT_TO_CANCEL_UNKNOWN_ORDER ||
|
|
e.error_code() == GatewayError::ATTEMPT_TO_CANCEL_REPLACE_UNKNOWN_ORDER)
|
|
// These can happen during the normal course of business if an
|
|
// order is already canceled or filled at the exchange but the
|
|
// corresponding network packet has not been received yet.
|
|
return;
|
|
else
|
|
on_error(e);
|
|
}
|
|
|
|
void Gateway::on_realtime_order_update(OrderUpdate const &update) {
|
|
bts_assert_d(me->state == State::READY);
|
|
me->observer.on_order_update(update);
|
|
}
|
|
|
|
void Gateway::on_realtime_ready() {
|
|
bts_assert_d(me->state == State::READY);
|
|
me->observer.on_gateway_ready();
|
|
}
|
|
|
|
void Gateway::terminate() {
|
|
bts_assert_d(me->state == State::READY ||
|
|
me->state == State::INITIALIZING);
|
|
|
|
auto terminate = enqueue<::il3::Terminate507>();
|
|
terminate.uUID(me->cfg.uuid.value());
|
|
terminate.requestTimestamp(me->clock.now().value());
|
|
terminate.errorCodes(0);
|
|
terminate.splitMsg(::il3::SplitMsg::NULL_VALUE);
|
|
flush();
|
|
transition(State::ERROR);
|
|
}
|
|
|
|
} // namespace bts::spartan
|