Files
spark/src/lib/orderman/actors/MDSupplementarySource.cpp
T
2018-12-05 21:52:49 +00:00

73 lines
2.4 KiB
C++

#include "MDSupplementarySource.h"
#include "lib/logging/logging.h"
#include "lib/md/MDConsumer.h"
#include "lib/md/MDConsumerHandle.h"
#include "lib/orderman/events/SettlementRequestEvent.h"
#include "lib/orderman/events/SettlementUpdateEvent.h"
#include "lib/os/os.h"
namespace bts {
namespace orderman {
struct HandleRecord {
md::MDConsumerHandle handle;
md::IntPrice settlement_price;
};
struct MDSupplementarySource::PImpl {
PImpl(md::MDConsumer &md_consumer)
: md_consumer(md_consumer), handle_map(), settlement_update() {}
md::MDConsumer md_consumer;
// list of sec subscriptions vs their handles
std::map<uint32_t, HandleRecord> handle_map;
mercury::Stream<SettlementUpdateEvent> settlement_update;
};
MDSupplementarySource::MDSupplementarySource(md::MDConsumer &md_consumer)
: me(new PImpl(md_consumer)) {}
MDSupplementarySource::~MDSupplementarySource() {}
void MDSupplementarySource::init() {
me->settlement_update = make_stream<SettlementUpdateEvent>();
subscribe<SettlementRequestEvent>([this](auto const &update) {
if (update.events) {
for (auto const &ev : *update.events) {
auto k = ev.security_key.value();
auto it = me->handle_map.find(k);
if (it == me->handle_map.end()) {
LOG(OrderMan, Info, "MDSupplementarySource::init key:{}", k);
me->handle_map[k] = {me->md_consumer.get_handle(k), md::INT_NAN};
} else {
// late joiners
auto const &hr = it->second;
if (!hr.settlement_price.is_nan()) {
SettlementUpdateEvent ev(hr.settlement_price, SecurityKey(k));
publish(me->settlement_update, ev);
}
}
}
}
});
}
void MDSupplementarySource::on_poll() {
for (auto &kvp : me->handle_map) {
SecurityKey key(kvp.first);
HandleRecord &hr = kvp.second;
auto settlement_price = hr.handle.settlement_price();
if (!settlement_price.is_nan() &&
settlement_price != hr.settlement_price) {
LOG(OrderMan, Info,
"MDSupplementarySource::on_poll new settle key:{} price:{}", key,
hr.settlement_price.price());
hr.settlement_price = settlement_price;
SettlementUpdateEvent ev(settlement_price, key);
publish(me->settlement_update, ev);
}
}
}
} // namespace orderman
} // namespace bts