diff --git a/src/data_files/files/fix/spark_fix.cfg b/src/data_files/files/fix/spark_fix.cfg index 92cc433ef..6b4450867 100644 --- a/src/data_files/files/fix/spark_fix.cfg +++ b/src/data_files/files/fix/spark_fix.cfg @@ -13,8 +13,8 @@ ResetOnLogout=Y ResetOnDisconnect=Y HeartBtInt=30 LogonTimeout=30 -FileLogPath=./fix/log/ -FileStorePath=./fix/store/ +FileLogPath=data_files/fix/log/ +FileStorePath=data_files/fix/store/ SSLEnable=N # SSLEnable=Y @@ -28,13 +28,13 @@ SSLEnable=N [SESSION] BeginString=FIX.4.4 -SenderCompID=SPARK +SenderCompID=SPARKFIX TargetCompID=CLIENT1 DataDictionary=data_files/fix/spec/fix/FIX44.xml [SESSION] BeginString=FIX.4.4 -SenderCompID=SPARK +SenderCompID=SPARKFIX TargetCompID=CLIENT2 DataDictionary=data_files/fix/spec/fix/FIX44.xml diff --git a/src/lib/config/defaults/fix.conf b/src/lib/config/defaults/fix.conf index 8c5e10626..ff380bfba 100644 --- a/src/lib/config/defaults/fix.conf +++ b/src/lib/config/defaults/fix.conf @@ -1,6 +1,7 @@ Fix { value { + enable_acceptor: true allow_orders: true fix_config_file: 'spark_fix.cfg' } diff --git a/src/lib/config/protos/fix.proto b/src/lib/config/protos/fix.proto index c2225390d..024f22f2c 100644 --- a/src/lib/config/protos/fix.proto +++ b/src/lib/config/protos/fix.proto @@ -3,6 +3,7 @@ syntax='proto2'; package bts.config; message Fix { + optional bool enable_acceptor = 5 [default = false]; optional bool allow_orders = 10 [default = false]; optional string fix_config_file = 20; } diff --git a/src/lib/md/HandleIndex.h b/src/lib/md/HandleIndex.h index 7563929fe..61fb114d0 100644 --- a/src/lib/md/HandleIndex.h +++ b/src/lib/md/HandleIndex.h @@ -18,16 +18,17 @@ namespace bts { namespace md { -SecurityKey default_key(const MDConsumerHandle& h) +security_key default_key(const MDConsumerHandle& h) { auto&& key = h.key(); - return SecurityKey( key ); + return key; } std::string desc_key(const MDConsumerHandle& h) { - auto sec = h.get_normalized_security(); - return securities::descriptor(sec); + auto&& sec = h.get_normalized_security(); + auto&& desc = securities::descriptor(sec); + return desc; } template @@ -86,11 +87,13 @@ private: { handle_index.lock([this, handle](auto& index) { - index[ Key_Func(handle) ] = handle; + auto key = Key_Func(handle); + index[ key ] = handle; }); - security_index.lock([this, handle, sec](auto& index) + security_index.lock([this, handle, &sec](auto& index) { - index[ Key_Func(handle) ] = sec; + auto key = Key_Func(handle); + index[ key ] = sec; }); } }); diff --git a/src/lib/md/MBOServiceServer.cpp b/src/lib/md/MBOServiceServer.cpp index dc468f671..827fac229 100644 --- a/src/lib/md/MBOServiceServer.cpp +++ b/src/lib/md/MBOServiceServer.cpp @@ -118,7 +118,7 @@ std::optional search_orders(const ExchangeOrderKey oid, const std::vec bool update_position(const KeyHandleIndex& handles, ActiveOrder& order) { const auto& index = handles.handle_index; auto handle = index.lock([id=order.security_id()](const auto& index) { - auto iter = index.find(id); + auto iter = index.find(id.value()); return iter != index.end() ? iter->second : MDConsumerHandle {}; }); @@ -254,7 +254,7 @@ void MBOServiceServer::get_orders_for_security(const MBOSecurityQuery& request, PositionData response; response.set_security_id(request.security_id()); auto handle = _me->handle_index.handle_index.lock([id=request.security_id()](const auto& index) { - auto iter = index.find(SecurityKey(id)); + auto iter = index.find(id); return iter != index.end() ? iter->second : MDConsumerHandle {}; }); diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 00c2fa0db..566fef69a 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -4,6 +4,7 @@ #include "lib/api/public/ServiceContext.h" #include "lib/config/config.h" #include "lib/config/protos/Ports.pb.h" +#include "lib/config/protos/fix.pb.h" #include "lib/config/protos/quickbus_cfg.pb.h" #include "lib/logging/logging.h" #include "lib/os/os.h" @@ -48,7 +49,7 @@ namespace bts { namespace cmd { -int Run::execute(std::vector const ¶ms) { +int Run::execute(std::vector const &) { using namespace bts; LOG(Gen, Warn, "------------------------------------ Sparkd launched " @@ -105,8 +106,11 @@ int Run::execute(std::vector const ¶ms) { metrics::ShellLatencyServer shell_latency_service( sc, stats, utils::Dispatcher::background()); - if(std::find(params.begin(), params.end(), "nofix") == params.end()) - fix::FixInit::start_acceptor(sc, mdclient); + if(config::get().enable_acceptor()) + { + threading::make_thread("SparkFix", [&sc, &mdclient]() { fix::FixInit::start_acceptor(sc, mdclient); }) + .detach(); + } auto bg_health = std::make_shared("spark_background_thread"); diff --git a/src/server/sparkd/cmds/RunFix.cpp b/src/server/sparkd/cmds/RunFix.cpp index 549178bec..b8073796e 100644 --- a/src/server/sparkd/cmds/RunFix.cpp +++ b/src/server/sparkd/cmds/RunFix.cpp @@ -9,6 +9,7 @@ #include "lib/os/os.h" #include "lib/quickbus/quickbus.h" #include "lib/utils/Dispatcher.h" +#include "lib/utils/thread_manager.h" #include "lib/config/protos/toggles.pb.h" #include "lib/md/SyntheticSecurityService.h" @@ -29,9 +30,9 @@ int RunFix::execute(std::vector const &) api::synthetic::SyntheticSecurityService synthetic_service(sc); md::MDClient mdclient(&utils::Dispatcher::background(), &synthetic_service); - - fix::FixInit::start_acceptor(sc, mdclient); + threading::make_thread("SparkFix", [&sc, &mdclient]() { fix::FixInit::start_acceptor(sc, mdclient); }) + .detach(); return 0; } diff --git a/src/server/sparkd/fix/DropCopySession.cpp b/src/server/sparkd/fix/DropCopySession.cpp index 95642701d..7ea15e14a 100644 --- a/src/server/sparkd/fix/DropCopySession.cpp +++ b/src/server/sparkd/fix/DropCopySession.cpp @@ -18,11 +18,21 @@ namespace bts { namespace fix { + +struct DropCopySession::PImpl +{ + rxcpp::observable filtered_orders; + rxcpp::observable filtered_fills; + PImpl() + { + } +}; DropCopySession::DropCopySession(bts::api::ServiceContext& sc, IFixRouter& rt, const FIX::SessionID& id) : - SessionBase(sc, rt, id), + SessionBase(sc, rt, id), _orderservice( sc.locate() ), - _order_cache() + _order_cache(), + me(new PImpl()) { } @@ -39,9 +49,11 @@ void DropCopySession::onLogon(const FIX::SessionID& sessionID) void DropCopySession::onLogout(const FIX::SessionID& sessionID) { + me->filtered_orders.finally([] () {} ); + me->filtered_fills.finally([] () {} ); + bts_assert(sessionID == _sess); LOG(Fix, Info, "Logged out {}", sessionID.toString()); - } void DropCopySession::fromApp(const FIX::Message &message,const FIX::SessionID &sessionID) @@ -107,7 +119,7 @@ void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID) //var on_orders = _orderservice.Value.subscribei_orders().Naggle(interval, scheduler).PermissionFilter(_adminobservable).Table().Cache(); //var on_fills = _orderservice.Value.subscribei_fills().Naggle(interval, scheduler).PermissionFilter(_adminobservable).Table().Cache(); - auto&& filtered_orders = _adminobservable.combine_latest(_orderservice.subscribe_orders()) + me->filtered_orders = _adminobservable.combine_latest(_orderservice.subscribe_orders()) .map( [] (auto& tuple) -> auto { std::shared_ptr admin = std::get<0>(tuple); @@ -131,12 +143,13 @@ void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID) bts::api::orderman::OrdersUpdate newupdate(all, added, changed, removed); return newupdate; - }).buffer_with_time( std::chrono::milliseconds(500) ); + }); + //.buffer_with_time( std::chrono::milliseconds(500) ); - filtered_orders.subscribe([this, &sessionID](const auto& updatevec) + me->filtered_orders.subscribe([this, sessionID](const auto& update) { - for(auto& update : updatevec) - { + // for(auto& update : updatevec) + // { for(auto& order : update.added()) { auto&& ostat = build_order_status(sessionID, *order); @@ -151,10 +164,10 @@ void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID) auto&& ostat = build_order_status(sessionID, *order.current(), true); _router.send(ostat, sessionID); } - } + // } }); - auto&& filtered_fills = _adminobservable.combine_latest(_orderservice.subscribe_fills()) + me->filtered_fills = _adminobservable.combine_latest(_orderservice.subscribe_fills()) .map( [] (auto& tuple) -> auto { std::shared_ptr admin = std::get<0>(tuple); @@ -178,12 +191,13 @@ void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID) bts::api::orderman::FillsUpdate newupdate(all, added, changed, removed); return newupdate; - }).buffer_with_time( std::chrono::milliseconds(500) ); + }); + //.buffer_with_time( std::chrono::milliseconds(500) ); - filtered_fills.subscribe([this, &sessionID](const auto& updatevec) + me->filtered_fills.subscribe([this, sessionID](const auto& update) { - for(auto& update : updatevec) - { + // for(auto& update : updatevec) + // { for(auto& fill : update.added()) { auto&& fi = build_fill(sessionID, *fill); @@ -194,7 +208,7 @@ void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID) auto&& fi = build_fill(sessionID, *fill.current(), true); _router.send(fi, sessionID); } - } + // } }); } @@ -336,7 +350,8 @@ FIX44::ExecutionReport DropCopySession::build_order_status(const FIX::SessionID& auto ts = order.state().last_change(); report.setField(FIX::FIELD::TransactTime, google::protobuf::util::TimeUtil::ToString(ts)); - report.setField(FIX::FIELD::Text, FIX::Text(order.state().last_msg())); + if(order.state().last_msg() != "") + report.setField(FIX::FIELD::Text, FIX::Text(order.state().last_msg())); sec = resolve_execution_report_security(sec, report); if (native.exchange_order_id() != "") { diff --git a/src/server/sparkd/fix/DropCopySession.h b/src/server/sparkd/fix/DropCopySession.h index 9a100463e..1a5e5bfbb 100644 --- a/src/server/sparkd/fix/DropCopySession.h +++ b/src/server/sparkd/fix/DropCopySession.h @@ -46,11 +46,16 @@ protected: virtual FIX44::ExecutionReport build_fill(const FIX::SessionID& sess, const bts::api::orderman::Fill& fill, bool change = false); virtual FIX44::ExecutionReport build_order_status(const FIX::SessionID& sess, const bts::api::orderman::OrderRecord& order, bool change = false); -protected: +protected: bts::api::orderman::OrderService _orderservice; utils::LockBox< std::unordered_map> > _order_cache; utils::LockBox< std::unordered_map > _fix_clorderids; + +private: + struct PImpl; + std::shared_ptr me; + }; }} diff --git a/src/server/sparkd/fix/FixInit.cpp b/src/server/sparkd/fix/FixInit.cpp index 2b3c5ab9a..3465f271b 100644 --- a/src/server/sparkd/fix/FixInit.cpp +++ b/src/server/sparkd/fix/FixInit.cpp @@ -53,7 +53,7 @@ int FixInit::start_acceptor(bts::api::ServiceContext& sc, md::MDClient& md) int scount = sess.size(); UNUSED(scount); - FixRouter application(md, [&](IFixRouter& router, const auto& id) + FixRouter application(md, [&sc, orders_allowed](IFixRouter& router, const auto& id) { if(orders_allowed) return std::shared_ptr(new OrderSession(sc, router, id)); @@ -65,6 +65,8 @@ int FixInit::start_acceptor(bts::api::ServiceContext& sc, md::MDClient& md) FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory ); acceptor.start(); + utils::Dispatcher::current().run(); + acceptor.stop(); return 0; } diff --git a/src/server/sparkd/fix/FixRouter.cpp b/src/server/sparkd/fix/FixRouter.cpp index 3b91c4142..3df80a461 100644 --- a/src/server/sparkd/fix/FixRouter.cpp +++ b/src/server/sparkd/fix/FixRouter.cpp @@ -3,6 +3,7 @@ #include "IFixSession.h" #include "SecurityIndex.h" #include "lib/md/MDConsumerHandle.h" +#include "lib/utils/LockBox.h" #include #include @@ -27,7 +28,7 @@ namespace fix struct FixRouter::PImpl { - boost::container::flat_map< FIX::SessionID, std::shared_ptr > sessions; + utils::LockBox< boost::container::flat_map< FIX::SessionID, std::shared_ptr > > sessions; SecurityIndex index; std::mutex lock; Factory factory; @@ -38,7 +39,7 @@ struct FixRouter::PImpl lock(), factory(factory) { - } + } }; FixRouter::FixRouter(md::MDClient& md, Factory factory) : me(new PImpl(md, factory)) @@ -51,9 +52,9 @@ FixRouter::~FixRouter() void FixRouter::send(FIX::Message& mess, const FIX::SessionID& sess) { -std::lock_guard guard(me->lock); try { + std::lock_guard guard(me->lock); FIX::Session::sendToTarget(mess, sess); } catch(FIX::SessionNotFound) @@ -174,16 +175,19 @@ void FixRouter::toApp(FIX::Message& message, const FIX::SessionID& sessionID) th IFixSession& FixRouter::session(const FIX::SessionID& sid) { -std::lock_guard guard(me->lock); - auto it = me->sessions.find(sid); - if(it == me->sessions.end()) + auto ptr = me->sessions.lock([this, sid] (auto& sessions) { - auto ptr = me->factory(*this, sid); - me->sessions[sid] = ptr; - return *ptr; - } - else - return *it->second; + auto it = sessions.find(sid); + if(it == sessions.end()) + { + auto ptr = me->factory(*this, sid); + sessions[sid] = ptr; + return ptr; + } + else + return it->second; + }); + return *ptr; } void FixRouter::handle_session_exception(const std::exception& ex, const FIX::SessionID& sessionID, const FIX::Message* message) diff --git a/src/server/sparkd/fix/OrderSession.cpp b/src/server/sparkd/fix/OrderSession.cpp index 8f8b44d0c..5418ad22e 100644 --- a/src/server/sparkd/fix/OrderSession.cpp +++ b/src/server/sparkd/fix/OrderSession.cpp @@ -19,6 +19,13 @@ #include #include +/* +order for 4 lot CLZ9 +8=FIX.4.49=15635=D49=SPARKFIX56=CLIENT134=150=3073797=Y52=20000809-20:20:5011=900010081=ACC A21=255=SIM:CLZ954=138=440=259=044=5060=20000809-18:20:32207=SIM10=045 + +order for 4 lot CLZ9-CLH0 +8=FIX.4.49=15635=D49=SPARKFIX56=CLIENT134=150=3073797=Y52=20000809-20:20:5011=900010081=ACC A21=255=SIM:CLZ9-CLH054=138=440=259=044=7460=20000809-18:20:32207=SIM10=045 +*/ namespace bts { namespace fix { @@ -69,7 +76,7 @@ void OrderSession::onMessage(const FIX44::OrderCancelRequest& cancel, const FIX: } api::orderman::Cancel can; can.set_order_key(key); - _orderservice.cancel_order(can, [&] (const auto& resp) + _orderservice.cancel_order(can, [this, s, cancel] (const auto& resp) { if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK) { @@ -103,7 +110,7 @@ void OrderSession::onMessage(const FIX44::OrderCancelReplaceRequest& replace, co upd.set_order_key(key); upd.set_price(price); upd.set_quantity(sz); - _orderservice.update_order(upd, [&] (const auto& resp) + _orderservice.update_order(upd, [this, s, replace] (const auto& resp) { if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK) { @@ -114,9 +121,6 @@ void OrderSession::onMessage(const FIX44::OrderCancelReplaceRequest& replace, co void OrderSession::onMessage(const FIX44::NewOrderSingle& create, const FIX::SessionID& s) { - UNUSED(create); - UNUSED(s); - api::Venue ven = convert_exchange(FIX::SecurityExchange(create.getField(FIX::FIELD::SecurityExchange))); if (ven == bts::api::VENUE_UNSET) throw FIX::IncorrectTagValue(FIX::FIELD::SecurityExchange); @@ -168,7 +172,7 @@ void OrderSession::onMessage(const FIX44::NewOrderSingle& create, const FIX::Ses { throw FIX::IncorrectTagValue(FIX::FIELD::OrderQty); } - _orderservice.create_order(cr, [&] (const auto& resp) + _orderservice.create_order(cr, [this, s, create] (const auto& resp) { if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK) { @@ -188,6 +192,7 @@ void OrderSession::set_ordertype(api::orderman::OrderDefinition& def, const FIX4 else if(ot == "2") { def.mutable_custom_def()->mutable_limit(); + price_req = true; } else if(ot == "3") { @@ -202,6 +207,7 @@ void OrderSession::set_ordertype(api::orderman::OrderDefinition& def, const FIX4 } else if(ot == "4") { + price_req = true; auto s = def.mutable_custom_def()->mutable_stop_limit(); double stoppx = 0.0; try diff --git a/src/server/sparkd/fix/SecurityIndex.cpp b/src/server/sparkd/fix/SecurityIndex.cpp index 855dc399d..5919385e1 100644 --- a/src/server/sparkd/fix/SecurityIndex.cpp +++ b/src/server/sparkd/fix/SecurityIndex.cpp @@ -49,7 +49,7 @@ md::SecurityPtr SecurityIndex::lookup(md::security_key key) md::SecurityPtr ptr = nullptr; me->key_index.security_index.lock([&ptr, &key] (const auto& secmap) { - auto it = secmap.find( SecurityKey(key) ); + auto it = secmap.find( key ); if(it != secmap.end()) ptr = it->second; }); diff --git a/src/server/sparkd/fix/SessionBase.cpp b/src/server/sparkd/fix/SessionBase.cpp index cfd562554..1be9bd3b9 100644 --- a/src/server/sparkd/fix/SessionBase.cpp +++ b/src/server/sparkd/fix/SessionBase.cpp @@ -66,8 +66,10 @@ void SessionBase::fromAdmin(const FIX::Message& message, const FIX::SessionID& s { try { + LOG(Fix, Info, "LOGIN: {}", message.toString() ); FIX44::Logon msg = (FIX44::Logon)message; - _user_name = msg.getField(FIX::FIELD::Username); + if(msg.isSetField(FIX::FIELD::Username)) + _user_name = msg.getField(FIX::FIELD::Username); std::string pass; if(msg.isSetField(FIX::FIELD::Password)) pass = msg.getField(FIX::FIELD::Password); @@ -75,18 +77,27 @@ void SessionBase::fromAdmin(const FIX::Message& message, const FIX::SessionID& s bts::api::user::AuthenticateLoginRequest auth; auth.set_login(_user_name); auth.set_password(pass); - _authservice.authenticate_login(auth, [this] (auto resp) + _authservice.authenticate_login(auth, [this, sessionID] (auto resp) { if (resp.success() && resp.value()->is_valid()) + { _token = resp.value()->token(); + } else - throw FIX::RejectLogon("Logon rejected " + resp.value()->reason()); + { + //I can't just throw here cuz c++ + std::string reason = "LOGIN FAILED " + resp.value()->reason(); + LOG(Fix, Error, reason.c_str()); + try + { + auto sess = FIX::Session::lookupSession(sessionID); + if(sess != nullptr) + sess->disconnect(); + } + catch(...) {} + } }); } - catch (FIX::RejectLogon) - { - throw; - } catch (std::exception ex) { throw FIX::RejectLogon("Logon rejected " + std::string(ex.what())); diff --git a/src/server/sparkd/fix/SessionBase.h b/src/server/sparkd/fix/SessionBase.h index 14e8fcf3d..74d7d4cf5 100644 --- a/src/server/sparkd/fix/SessionBase.h +++ b/src/server/sparkd/fix/SessionBase.h @@ -74,7 +74,7 @@ protected: bts::api::admin::AdminService _adminservice; rxcpp::observable> _adminobservable; IFixRouter& _router; - const FIX::SessionID& _sess; + FIX::SessionID _sess; utils::LockBox _admin_data; std::string _user_name;