112 lines
3.9 KiB
C++
112 lines
3.9 KiB
C++
#include "OrderManager.h"
|
|
#include "server/sparkd/orderman2/order_factory/OrderFactory.h"
|
|
#include "server/sparkd/securities/ISecurityDB.h"
|
|
#include "server/sparkd/securities/MapSecurityDB.h"
|
|
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
|
#include "server/sparkd/orderman2/events/StoredOrderEvent.h"
|
|
#include "server/sparkd/orderman2/events/ActivateOrderEvent.h"
|
|
#include "server/sparkd/orderman2/events/UpdateOrderEvent.h"
|
|
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
|
|
#include "server/sparkd/orderman2/orders/Order.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace bts {
|
|
namespace orderman2 {
|
|
|
|
enum
|
|
class OrderManager::State {WAIT_SEC, WAIT_ORDERS, READY } ;
|
|
|
|
struct OrderManager::PImpl
|
|
{
|
|
PImpl(config::JetFIX::Connection const &cfg) :
|
|
state (State::WAIT_SEC),
|
|
sec_db(),
|
|
orders(),
|
|
order_factory(sec_db),
|
|
stored_order_sub(),
|
|
cfg(cfg),
|
|
gw_name(GatewayName(cfg.name()))
|
|
{}
|
|
|
|
State state;
|
|
securities::MapSecurityDB sec_db;
|
|
std::unordered_map<OrderKey,Order *> orders;
|
|
OrderFactory order_factory;
|
|
mercury::Actor::SubHandle stored_order_sub;
|
|
const config::JetFIX::Connection cfg;
|
|
const GatewayName gw_name;
|
|
};
|
|
|
|
OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {}
|
|
OrderManager::~OrderManager () {}
|
|
|
|
void OrderManager::init()
|
|
{
|
|
me->state = State::WAIT_SEC;
|
|
subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
|
|
}
|
|
|
|
void OrderManager::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
|
|
{
|
|
if (update.events) {
|
|
|
|
// update our security db
|
|
for (auto ev : *(update.events)) {
|
|
auto sec_ptr = ev.security;
|
|
me->sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr;
|
|
}
|
|
|
|
if (me->state == State::WAIT_SEC) {
|
|
// obtain recovered orders
|
|
me->state = State::WAIT_ORDERS;
|
|
me->stored_order_sub =
|
|
subscribe<StoredOrderEvent> (me->gw_name,
|
|
[this] (auto const&orders)
|
|
{this->on_order_recovery(orders);});
|
|
}
|
|
}
|
|
}
|
|
|
|
void OrderManager::on_order_recovery(const mercury::Update<StoredOrderEvent> &update)
|
|
{
|
|
if (update.events)
|
|
if (me->state == State::WAIT_ORDERS) {
|
|
|
|
// Got the first batch of persisted orders which has to match
|
|
// the orders stored on disk (since we are the only ones
|
|
// producing new orders)
|
|
for (auto const &ev : *(update.events)) {
|
|
auto &order = me->order_factory.recover(*ev.order_record);
|
|
me->orders[order.key()] = ℴ
|
|
order.pre_activate(me->sec_db, me->order_factory);
|
|
order.activate();
|
|
}
|
|
|
|
// Don't need to monitor persisted orders anymore, all the
|
|
// orders we care about will be generated by us.
|
|
unsubscribe(me->stored_order_sub);
|
|
|
|
// Now we are ready to subscribe to the execution gateway
|
|
// events. Since we know all the orders that were stored in
|
|
// disk we will be able to match any execution reports we
|
|
// might get to persisted orders. Had we subscribed to
|
|
// gatewat events earlier we would not be able to tell wheter
|
|
// an order was already stored in disk or not when we
|
|
// received an execution report for it. Given that we know
|
|
// for sure that we have all the persisted orders, anything
|
|
// we can't match will signify that we crashed after we sent
|
|
// an order to the gateway but before we were able to persist
|
|
// it. In that case we will do our best to recover the order
|
|
// from the execution report.
|
|
subscribe<NewExecutionReportEvent> (me->gw_name,
|
|
[this] (auto const&ev) {on_execution_report(ev);});
|
|
}
|
|
}
|
|
|
|
void OrderManager::on_execution_report(const mercury::Update<NewExecutionReportEvent> &)
|
|
{
|
|
}
|
|
|
|
}}
|