diff --git a/src/lib/md/MDClient.cpp b/src/lib/md/MDClient.cpp new file mode 100644 index 000000000..0463f6810 --- /dev/null +++ b/src/lib/md/MDClient.cpp @@ -0,0 +1,74 @@ +#include "MDClient.h" +#include "get_symbols.h" +#include + +namespace bts { +namespace md { + +struct MDClientImpl{ + utils::Dispatcher& _dispatcher; + std::shared_ptr _ss; + utils::DispatcherTimer _security_timer; + utils::DispatcherTimer _recovery_timer; + MDConsumer _consumer; + utils::Event _event; + + MDClientImpl(utils::Dispatcher& d, std::shared_ptr s): + _dispatcher(d), + _ss(s), + _security_timer(d, config::get().security_scan_interval(),[this]() { scan_securities(); }), + _recovery_timer(d, config::get().recovery_interval(), [this]() { check_heartbeats(); }), + _consumer(s,true), + _event() + { + _security_timer.start(); + _recovery_timer.start(); + } + + std::set symbols() { + std::set all_symbols; + Venue venue; + bts_assert(api::Venue_Parse(config::get().exchange(), &venue)); + SecurityType type; + bts_assert(api::SecurityType_Parse(config::get().type(), &type)); + get_system_md_symbols(venue, type, all_symbols); + return all_symbols; + } + + void check_heartbeats() { + if (!_consumer.all_heartbeats_ok()) { + MDConsumer temp(symbols(), _ss); + if (temp.all_heartbeats_ok()) { + _consumer.swap(temp); + LOG(md,Info, "Recovering from bad MD. Old consumer {} new consumer {}", _consumer.id(), temp.id()); + // Subscribers are responsible for keeping the old consumer reference if they need it + _event.publish(_consumer); + } + } + } + + void scan_securities() { + if (_consumer.all_heartbeats_ok()) { + _consumer.scan_securities(); + } + } +}; + + +MDClient::MDClient(utils::Dispatcher& d, std::shared_ptr s) : _self(new MDClientImpl(d,s)) { + +} +MDClient::~MDClient() { +} + +utils::EventSubscriber& MDClient::on_consumer() { + return _self->_event; +} +MDConsumer MDClient::current_consumer() const { + return _self->_consumer; +} +SecurityOracle MDClient::oracle() { + return SecurityOracle(*this); +} + +}}//namespace \ No newline at end of file diff --git a/src/lib/md/MDClient.h b/src/lib/md/MDClient.h new file mode 100644 index 000000000..b82549ae7 --- /dev/null +++ b/src/lib/md/MDClient.h @@ -0,0 +1,26 @@ +#pragma once +#include "MDConsumer.h" +#include "lib/utils/Dispatcher.h" +#include "lib/utils/Event.h" +#include "ISyntheticSecurityService.h" +#include "SecurityOracle.h" +#include + +namespace bts { +namespace md { + +struct MDClientImpl; +class MDClient { + std::unique_ptr _self; + +public: + MDClient(utils::Dispatcher&, std::shared_ptr); + ~MDClient(); + + utils::EventSubscriber& on_consumer(); + MDConsumer current_consumer() const; + SecurityOracle oracle(); +}; + + +}}//namespace \ No newline at end of file diff --git a/src/lib/md/SecurityOracle.cpp b/src/lib/md/SecurityOracle.cpp new file mode 100644 index 000000000..4d49affe6 --- /dev/null +++ b/src/lib/md/SecurityOracle.cpp @@ -0,0 +1,31 @@ +#include "SecurityOracle.h" +#include "MDClient.h" + +namespace bts { +namespace md { + +struct SecurityOracleImpl { + SecurityOracleImpl(MDClient& client): + client(client), + consumer(client.current_consumer()) + { + sub = client.on_consumer().subscribe([this](const MDConsumer& c) { + consumer = c; + }); + } + ~SecurityOracleImpl() { + client.on_consumer().unsubscribe(sub); + } + MDClient& client; + MDConsumer consumer; + int sub; + +}; + +SecurityOracle::SecurityOracle(MDClient& client): _self(new SecurityOracleImpl(client)) { } +SecurityOracle::~SecurityOracle() { } +SecurityPtr SecurityOracle::get_security(SecurityKey key) const { + return _self->consumer.get_security(key); +} + +}}//namespace \ No newline at end of file diff --git a/src/lib/md/SecurityOracle.h b/src/lib/md/SecurityOracle.h new file mode 100644 index 000000000..fc9ec3ea6 --- /dev/null +++ b/src/lib/md/SecurityOracle.h @@ -0,0 +1,19 @@ +#pragma once +#include "SecurityPtr.h" +#include + +namespace bts { +namespace md { + +class MDClient; + +struct SecurityOracleImpl; +class SecurityOracle { + std::unique_ptr _self; +public: + SecurityOracle(MDClient&); + ~SecurityOracle(); + SecurityPtr get_security(SecurityKey key) const; +}; + +}}//namespace \ No newline at end of file