#ifndef _WIN32 #include "SyntheticSecurityService.h" #include "lib/api/public/ServiceContext.h" #include "lib/api/services/PersistenceService.pb.h" #include "lib/config/config.h" #include "lib/md/SecurityDB.h" #include "lib/md/SecurityIDRecorder.h" #include "lib/md/common.h" #include "lib/md/get_symbols.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/Event.h" #include 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 synthetics; utils::Event on_security; std::unordered_map meta; utils::Event on_metadata; MetadataStatefulPublisher metadata_publisher; }; SyntheticSecurityService::SyntheticSecurityService(api::ServiceContext &context) : SyntheticSecurityServiceBase(context), _me(new Pimpl(make_metadata_stateful_publisher())) { // load securities std::set symbols; md::get_system_md_symbols(api::VENUE_BTS, api::SECURITYTYPE_UNSET, symbols); md::db::load_securities(symbols, [=, this](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 SyntheticSecurityService::securities() { return _me->synthetics; } utils::EventSubscriber &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 &SyntheticSecurityService::on_metadata() { return _me->on_metadata; } void send_reply( const api::Response &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 &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()) { auto metadata = get_metadata(iter->first); if (metadata && metadata->strategy().deleted()) { // If this security is deleted we can just undelete it Metadata newmeta; // Rather than copy from the existing metatdata create a // default. The existing metadata is from a deleted spread so // may not be valid UI will call update_security_metadata if // non-defaults are needed newmeta.set_security_key(security.id()); newmeta.mutable_strategy()->set_deleted(false); LOG(Api, Info, "Un-deleting security {}", security.id()); auto id = security.id(); update_security_metadata( newmeta, [id, reply](const UpdateResult &result) { if (result.error() == UpdateResult::ERROR_OK) { // undelete was successful. Metadata update will make // this visible again send_reply(reply, CreateResult::ERROR_OK, id); } else { LOG(Api, Info, "Failed to un-delete security {}. Error {}", id, result.error()); send_reply( reply, CreateResult::ERROR_SECURITY_ALREADY_EXISTS, id); } }); } else { 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 &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 &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 synthetic } // namespace api } // namespace bts #endif