diff --git a/src/lib/md/HandleIndex.h b/src/lib/md/HandleIndex.h new file mode 100644 index 000000000..ec3ea60fd --- /dev/null +++ b/src/lib/md/HandleIndex.h @@ -0,0 +1,89 @@ +#pragma once +#ifndef _WIN32 + +#include "lib/domain_model/Security.h" +#include "lib/md/MDConsumerHandle.h" +#include "lib/md/MDClient.h" +#include "lib/md/ExchangeOrderKey.h" + +#include "lib/utils/LockBox.h" + +#include +#include +#include +#include +#include + + +namespace bts { +namespace md { + +SecurityKey default_key(const MDConsumerHandle& h) { return SecurityKey( h.key() ); } + +template +class HandleIndex +{ + MDClient& _client; + int _sub; + std::optional _consumer; + +public: + typedef decltype( Key_Func(std::declval()) ) KeyType; + utils::LockBox> index; + + + HandleIndex(md::MDClient& client) : + _client(client), + _sub(0), + _consumer(std::nullopt), + index() + { + _sub = _client.on_consumer().subscribe([=](const MDConsumer& c) { on_consumer(c); }); + on_consumer( _client.current_consumer() ); + } + virtual ~HandleIndex() + { + _client.on_consumer().unsubscribe(_sub); + } + +private: + void on_consumer(const MDConsumer& c) + { + MDConsumer temp(c); + if (temp.all_heartbeats_ok()) + { + auto existing = _consumer ? _consumer->id() : 0; + LOG(md,Info, "Updating HandleIndex MDConsumer. Old {} new {}", existing, temp.id()); + // clear the index + index.lock([](auto& index) { index.clear(); }); + // Swap to new consumer + if (_consumer) + { + _consumer->swap(temp); + } + else + { + _consumer = temp; + } + // Subscribe to on_security + auto id = _consumer->id(); + _consumer->on_security([this,id](const SecurityPtr&, MDConsumerHandle handle) + { + if (_consumer->id() == id) + { + index.lock([this, handle](auto& index) + { + index[ Key_Func(handle) ] = handle; + }); + } + }); + } + } +}; + +typedef HandleIndex KeyHandleIndex; + +}} + + +#endif \ No newline at end of file diff --git a/src/lib/md/MBOServiceServer.cpp b/src/lib/md/MBOServiceServer.cpp index 16b8175fa..ac6d4cc2b 100644 --- a/src/lib/md/MBOServiceServer.cpp +++ b/src/lib/md/MBOServiceServer.cpp @@ -8,6 +8,8 @@ #include "MDClient.h" #include "ExchangeOrderKey.h" #include "MBOProcessor.h" +#include "MDClient.h" +#include "HandleIndex.h" #include "lib/utils/LockBox.h" @@ -21,55 +23,6 @@ using namespace bts::api::orderman; namespace bts { namespace md { -// -// Simple class to handle MD recovery and maintain an index of SecurityKey=>MDConsumerHandle - -class HandleIndex { - MDClient& _client; - int _sub; - std::optional _consumer; - -public: - utils::LockBox> index; - - HandleIndex(md::MDClient& client): - _client(client), - _sub(0), - _consumer(std::nullopt), - index() { - _sub = _client.on_consumer().subscribe([=](const MDConsumer& c) { on_consumer(c); }); - on_consumer(_client.current_consumer()); - } - - ~HandleIndex() { _client.on_consumer().unsubscribe(_sub); } - - -private: - void on_consumer(const MDConsumer& c) { - MDConsumer temp(c); - if (temp.all_heartbeats_ok()) { - auto existing = _consumer ? _consumer->id() : 0; - LOG(md,Info, "Updateing MBOService MDConsumer. Old {} new {}", existing, temp.id()); - // clear the index - index.lock([](auto& index) { index.clear(); }); - // Swap to new consumer - if (_consumer) { - _consumer->swap(temp); - } - else { _consumer = temp; } - // Subscribe to on_security - auto id = _consumer->id(); - _consumer->on_security([this,id](const SecurityPtr&, MDConsumerHandle handle) { - if (_consumer->id() == id) { - index.lock([handle](auto& index) { - index[SecurityKey(handle.key())] = handle; - }); - } - }); - } - } -}; - class ActiveOrder { SecurityKey _security; SparkOrderKey _id; @@ -111,7 +64,7 @@ struct MBOServiceImpl { MBOServiceImpl( api::ServiceContext& ctx, utils::Dispatcher& d, - md::MDClient& c, + MDClient& c, MBOServiceBase::PiqPublisher&& pub ): dispatcher(d), @@ -124,10 +77,10 @@ struct MBOServiceImpl { {} utils::Dispatcher& dispatcher; - md::MDClient& client; + MDClient& client; OrderService orders; - HandleIndex handle_index; + KeyHandleIndex handle_index; MBOServiceBase::PiqPublisher publish; utils::DispatcherTimer timer; @@ -162,7 +115,7 @@ std::optional search_orders(const ExchangeOrderKey oid, const std::vec return std::nullopt; } -bool update_position(const HandleIndex& handles, ActiveOrder& order) { +bool update_position(const KeyHandleIndex& handles, ActiveOrder& order) { const auto& index = handles.index; auto handle = index.lock([id=order.security_id()](const auto& index) { auto iter = index.find(id); diff --git a/src/server/sparkd/fix/FixInit.cpp b/src/server/sparkd/fix/FixInit.cpp index 608208bfc..1016afeb4 100644 --- a/src/server/sparkd/fix/FixInit.cpp +++ b/src/server/sparkd/fix/FixInit.cpp @@ -21,6 +21,9 @@ namespace bts namespace fix { +GCC_DIAG_OFF("-Wdeprecated") +GCC_DIAG_OFF("-Wignored-qualifiers") + int FixInit::start_acceptor(const std::string& settings_file, bool orders_allowed) { std::ifstream str{settings_file}; diff --git a/src/server/sparkd/fix/FixRouter.cpp b/src/server/sparkd/fix/FixRouter.cpp index cca79f1c0..29241ec69 100644 --- a/src/server/sparkd/fix/FixRouter.cpp +++ b/src/server/sparkd/fix/FixRouter.cpp @@ -21,6 +21,9 @@ namespace bts namespace fix { +GCC_DIAG_OFF("-Wdeprecated") +GCC_DIAG_OFF("-Wignored-qualifiers") + int FixRouter::start_acceptor(const std::string& settings_file, bool orders_allowed) { std::ifstream str{settings_file}; diff --git a/src/server/sparkd/fix/FixRouter.h b/src/server/sparkd/fix/FixRouter.h index b244c7c33..8f44205d8 100644 --- a/src/server/sparkd/fix/FixRouter.h +++ b/src/server/sparkd/fix/FixRouter.h @@ -1,19 +1,61 @@ #pragma once #include "IFixSession.h" +#include "IFixRouter.h" +#include "lib/md/HandleIndex.h" #include +#include +#include +#include #include +#include +#include + +#include "lib/utils/warn_ctrl.h" + +GCC_DIAG_OFF("-Woverloaded-virtual") +GCC_DIAG_OFF("-Wdeprecated") +GCC_DIAG_OFF("-Wignored-qualifiers") + namespace bts { +namespace md { class MDClient; } namespace fix { -class FixRouter : public IFixSession, public FIX::MessageCracker +class FixRouter : public IFixSession, public FIX::MessageCracker, public IFixRouter { public: + FixRouter(const md::MDClient&); + ~FixRouter(); + + void send(const FIX::Message&, const FIX::SessionID&); + void send_session_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message* refmessage = nullptr); + void send_biz_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message& refmessage, int reason_code = 0); + +public: + void fromAdmin(const FIX::Message& message, const FIX::SessionID& sessionID) + throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon); + void fromApp(const FIX::Message &message,const FIX::SessionID &sessionID) + throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType); + + +void onCreate(const FIX::SessionID&); +void onLogon(const FIX::SessionID&); +void onLogout(const FIX::SessionID&); +void toAdmin(FIX::Message&, const FIX::SessionID&); +void toApp(FIX::Message&, const FIX::SessionID&) throw(FIX::DoNotSend); + +private: + IFixSession& session(const FIX::SessionID&); + void handle_session_exception(const std::exception& ex, const FIX::SessionID& sessionID, const FIX::Message* message = nullptr); + void handle_field_not_found(const FIX::FieldNotFound& ex, const FIX::SessionID& sess, const FIX::Message& message); - +private: + boost::container::flat_map< FIX::SessionID, std::shared_ptr > _sessions; + const md::MDClient& _md; + std::mutex _lock; }; }} \ No newline at end of file diff --git a/src/server/sparkd/fix/IFixSession.h b/src/server/sparkd/fix/IFixSession.h index 462af6b20..f8f973a3c 100644 --- a/src/server/sparkd/fix/IFixSession.h +++ b/src/server/sparkd/fix/IFixSession.h @@ -4,7 +4,9 @@ namespace bts { namespace fix { - class IFIXSession : public FIX::Application - { - }; + +class IFIXSession : public FIX::Application +{ +}; + }} \ No newline at end of file