This commit is contained in:
Kevin Darby committed 2019-09-24 14:37:30 -04:00
1 parent aa8a56787b
commit f66004b465
6 files changed
+150 -58

No files matched your search

+89
View File
@@ -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 <tuple>
#include <vector>
#include <unordered_map>
#include <optional>
#include <functional>
namespace bts {
namespace md {
SecurityKey default_key(const MDConsumerHandle& h) { return SecurityKey( h.key() ); }
template<auto Key_Func>
class HandleIndex
{
MDClient& _client;
int _sub;
std::optional<MDConsumer> _consumer;
public:
typedef decltype( Key_Func(std::declval<MDConsumerHandle>()) ) KeyType;
utils::LockBox<std::unordered_map<KeyType,MDConsumerHandle>> 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<default_key> KeyHandleIndex;
}}
#endif
+6 -53
View File
@@ -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<MDConsumer> _consumer;
public:
utils::LockBox<std::unordered_map<SecurityKey,MDConsumerHandle>> 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<uint32_t> 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);
+3
View File
@@ -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};
+3
View File
@@ -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};
+44 -2
View File
@@ -1,19 +1,61 @@
#pragma once
#include "IFixSession.h"
#include "IFixRouter.h"
#include "lib/md/HandleIndex.h"
#include <quickfix/MessageCracker.h>
#include <quickfix/Message.h>
#include <quickfix/SessionID.h>
#include <boost/container/flat_map.hpp>
#include <string>
#include <memory>
#include <mutex>
#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<IFixSession> > _sessions;
const md::MDClient& _md;
std::mutex _lock;
};
}}
+5 -3
View File
@@ -4,7 +4,9 @@
namespace bts {
namespace fix {
class IFIXSession : public FIX::Application
{
};
class IFIXSession : public FIX::Application
{
};
}}