195 lines
6.3 KiB
C++
195 lines
6.3 KiB
C++
#ifndef WIN32
|
|
#include "SyntheticSecurityService.h"
|
|
|
|
#include "lib/config/config.h"
|
|
#include "lib/api/public/ServiceContext.h"
|
|
#include "lib/api/services/PersistenceService.pb.h"
|
|
#include "lib/md/common.h"
|
|
#include "lib/md/SecurityIDRecorder.h"
|
|
#include "lib/md/SecurityDB.h"
|
|
#include "lib/md/get_symbols.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/Event.h"
|
|
#include <set>
|
|
|
|
namespace bts {
|
|
namespace api {
|
|
namespace synthetic {
|
|
|
|
struct SecurityLess {
|
|
bool operator()(const api::Security& left, const api::Security& right) {
|
|
return left.id() < right.id();
|
|
}
|
|
};
|
|
typedef SyntheticSecurityServiceBase::MetadataStatefulPublisher MetadataPublisher;
|
|
|
|
struct SyntheticSecurityService::Pimpl {
|
|
Pimpl(const MetadataStatefulPublisher& fn) :
|
|
recorder({utils::Dispatcher::background()}),
|
|
synthetics(), on_security(),
|
|
meta(), on_metadata(), metadata_publisher(fn)
|
|
{
|
|
}
|
|
md::SecurityIDRecorder recorder;
|
|
std::unordered_map<SecurityKey, Security> synthetics;
|
|
utils::Event<Security> on_security;
|
|
std::unordered_map<SecurityKey, Metadata> meta;
|
|
utils::Event<Metadata> on_metadata;
|
|
MetadataStatefulPublisher metadata_publisher;
|
|
};
|
|
|
|
SyntheticSecurityService::SyntheticSecurityService(api::ServiceContext& context)
|
|
: SyntheticSecurityServiceBase(context),
|
|
_me(new Pimpl(make_metadata_stateful_publisher())) {
|
|
|
|
// load securities
|
|
std::set<md::UserSymbol> symbols;
|
|
md::get_system_md_symbols(api::VENUE_BTS, api::SECURITYTYPE_UNSET, symbols);
|
|
md::db::load_securities(symbols, [=](const api::Security& security) {
|
|
if (security.Contract_case() == api::Security::kSynthetic) {
|
|
auto key = SecurityKey(security.id());
|
|
_me->synthetics.insert(std::make_pair(key, security));
|
|
}
|
|
});
|
|
|
|
// Load metadata
|
|
auto metadata = md::db::load_metadata();
|
|
for (const auto& entry : metadata) {
|
|
auto iter = _me->synthetics.find(SecurityKey(entry.security_key()));
|
|
if (iter != _me->synthetics.end()) {
|
|
if (entry.strategy().deleted()) {
|
|
_me->synthetics.erase(iter);
|
|
}
|
|
else {
|
|
_me->meta.insert(std::make_pair(entry.security_key(), entry));
|
|
_me->metadata_publisher.add_or_update(entry);
|
|
}
|
|
}
|
|
}
|
|
if (_me->meta.size() > 0) {
|
|
_me->metadata_publisher.flush();
|
|
}
|
|
|
|
}
|
|
SyntheticSecurityService::~SyntheticSecurityService() { }
|
|
|
|
|
|
const std::unordered_map<SecurityKey, Security> SyntheticSecurityService::securities() {
|
|
return _me->synthetics;
|
|
}
|
|
utils::EventSubscriber<Security>& SyntheticSecurityService::on_securities() {
|
|
return _me->on_security;
|
|
}
|
|
const Metadata* SyntheticSecurityService::get_metadata(SecurityKey key) {
|
|
auto iter = _me->meta.find(key);
|
|
if (iter != _me->meta.end()) {
|
|
return &iter->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
utils::EventSubscriber<Metadata>& SyntheticSecurityService::on_metadata() {
|
|
return _me->on_metadata;
|
|
}
|
|
|
|
void send_reply(const api::Response<CreateResult>& fn, CreateResult::Error err, uint32_t key=md::invalid_security_key) {
|
|
CreateResult r;
|
|
r.set_key(key);
|
|
r.set_error(err);
|
|
fn(r);
|
|
LOG(md, Info, "Create new synthetic security key={} response={}", key, CreateResult::Error_Name(err));
|
|
}
|
|
|
|
void SyntheticSecurityService::create_synthetic(
|
|
const Security& security,
|
|
const api::Response<CreateResult>& reply) {
|
|
|
|
CreateResult result;
|
|
// Validate security is synthetic
|
|
if (security.Contract_case() != Security::kSynthetic) {
|
|
send_reply(reply, CreateResult::ERROR_NOT_SYNTHETIC);
|
|
return;
|
|
}
|
|
if (security.id() != md::invalid_security_key) {
|
|
send_reply(reply, CreateResult::ERROR_ID_ALREADY_ASSIGNED);
|
|
return;
|
|
}
|
|
if (security.exchange() != api::VENUE_BTS) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_VENUE);
|
|
return;
|
|
}
|
|
if (security.symbol().empty()) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_SYMBOL);
|
|
return;
|
|
}
|
|
if (security.synthetic().name().empty()) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_NAME);
|
|
return;
|
|
}
|
|
|
|
// Assign security key
|
|
_me->recorder.assign_key(security, [this, reply](const api::Security& security) {
|
|
// Notify listeners that a new security is added
|
|
auto iter = _me->synthetics.find(SecurityKey(security.id()));
|
|
if (iter != _me->synthetics.end()) {
|
|
send_reply(reply, CreateResult::ERROR_SECURITY_ALREADY_EXISTS, security.id());
|
|
}
|
|
else {
|
|
_me->synthetics.insert(std::make_pair(security.id(), security));
|
|
_me->on_security.publish(security);
|
|
// signal success
|
|
send_reply(reply, CreateResult::ERROR_OK, security.id());
|
|
}
|
|
});
|
|
}
|
|
|
|
void SyntheticSecurityService::update_security_metadata(
|
|
const Metadata& request,
|
|
const api::Response<UpdateResult>& reply) {
|
|
UpdateResult result;
|
|
if (request.security_key() == md::invalid_security_key) {
|
|
result.set_error(UpdateResult::ERROR_MISSING_KEY);
|
|
reply(result);
|
|
return;
|
|
}
|
|
auto key = SecurityKey(request.security_key());
|
|
if (_me->synthetics.find(key) == _me->synthetics.end()) {
|
|
result.set_error(UpdateResult::ERROR_UNKNOWN_SECURITY);
|
|
reply(result);
|
|
return;
|
|
}
|
|
// update the on disk database
|
|
if (!md::db::store_metadata(request)) {
|
|
result.set_error(UpdateResult::ERROR_DATABASE);
|
|
reply(result);
|
|
return;
|
|
}
|
|
// update the local database
|
|
_me->meta[key] = request;
|
|
// notify subscribers
|
|
_me->on_metadata.publish(request);
|
|
_me->metadata_publisher.add_or_update(request);
|
|
_me->metadata_publisher.flush();
|
|
LOG(md, Info, "Update synthetic security key={} response={}", key, UpdateResult::Error_Name(result.error()));
|
|
|
|
reply(result);
|
|
}
|
|
|
|
void SyntheticSecurityService::query_security_metadata(
|
|
const MetadataQuery& request,
|
|
const api::Response<MetadataCollection>& reply) {
|
|
|
|
MetadataCollection result;
|
|
for (const auto k : request.security_key()) {
|
|
SecurityKey key(k);
|
|
auto iter = _me->meta.find(key);
|
|
if (iter != _me->meta.end()) {
|
|
result.mutable_data()->Add()->CopyFrom(iter->second);
|
|
}
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
|
|
}}}//namespace
|
|
|
|
#endif |