Files
spark/src/lib/md/PlatformLoader.cpp
T
2019-04-05 17:58:28 -04:00

238 lines
9.0 KiB
C++

#ifndef NO_CPP0X
#include "PlatformLoader.h"
#include "lib/config/config.h"
#include "MemorySegmentUtils.h"
#include "lib/utils/SharedList.h" // for cleanup_shared_memory
#include "lib/config/protos/MarketData.pb.h"
#include "lib/config/protos/Symbols.pb.h"
#include <algorithm>
#ifdef _LINUX
#include <dlfcn.h>
#endif
using namespace std;
using namespace bts;
using namespace bts::co2;
using namespace bts::api;
namespace bts {
namespace md {
PlatformLoader::PlatformLoader(const md::PlatformConfig &config)
: _config(config), _platform(PLATFORM_UNSET) {
bts_assert_msg(Platform_Parse(config.platform(), &_platform),
"Invalid market data platform " + config.platform());
}
PlatformLoader::~PlatformLoader() {}
void cleanup_shared_memory(const std::set<UserSymbol> &symbols,
Platform platform) {
for (const auto &sym : symbols) {
string name =
hot_segment_name(sym.symbol(), sym.type(), sym.exchange(), platform);
utils::SharedList<int, int>::delete_segments(name);
LOG(md, Info, "Cleaned up /run/shm/{}", name);
name =
cold_segment_name(sym.symbol(), sym.type(), sym.exchange(), platform);
utils::SharedList<int, int>::delete_segments(name);
LOG(md, Info, "Cleaned up /run/shm/{}", name);
}
}
// Does not return
void PlatformLoader::run() {
// Gather symbols to use
set<UserSymbol> symbols;
size_t count = get_system_md_symbols(_config, symbols);
if (count == 0) {
LOG(md, Error, "No Symbols configured for this publisher.");
return;
}
// Quickly try to clean the shared memory segments before anyone gets a stale
// one
cleanup_shared_memory(symbols, _platform);
switch (_platform) {
case PLATFORM_MDP3ONIXS:
break;
case PLATFORM_MDP3EMUMBA:
break;
case PLATFORM_ICEONIXS:
break;
default:
bts_assert(false);
break;
}
bts_assert_msg(false, "Market data run should not return");
}
///////////////////////////////
size_t get_system_md_symbols(set<UserSymbol> &result) {
auto &config = config::get<md::PlatformConfig>();
return get_system_md_symbols(config, result);
}
void add_symbols_to_result(
const google::protobuf::RepeatedPtrField<std::string> &symbols,
Venue exchange, SecurityType type, std::set<UserSymbol> *result) {
bts_assert(result);
for (const auto &s : symbols) {
UserSymbol u(s, exchange, type);
result->insert(u);
}
}
size_t get_system_md_symbols(api::Venue exchange, SecurityType type,
std::set<UserSymbol> &result) {
bts_assert(config::system_symbols::Symbols::descriptor()->field_count() == 4);
{
auto const &e = config::get<config::system_symbols::Symbols>().glbx();
if (exchange == api::VENUE_UNSET || exchange == api::VENUE_GLBX) {
if (type == api::SECURITYTYPE_FUTURE ||
type == api::SECURITYTYPE_UNSET) {
add_symbols_to_result(e.future(), api::VENUE_GLBX,
api::SECURITYTYPE_FUTURE, &result);
}
if (type == api::SECURITYTYPE_OPTION ||
type == api::SECURITYTYPE_UNSET) {
add_symbols_to_result(e.option(), api::VENUE_GLBX,
api::SECURITYTYPE_OPTION, &result);
}
if (type == api::SECURITYTYPE_SPREAD ||
type == api::SECURITYTYPE_UNSET) {
add_symbols_to_result(e.spread(), api::VENUE_GLBX,
api::SECURITYTYPE_SPREAD, &result);
}
}
if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET) {
std::set<std::string> all_symbols;
all_symbols.insert(e.future().begin(), e.future().end());
all_symbols.insert(e.option().begin(), e.option().end());
all_symbols.insert(e.spread().begin(), e.spread().end());
for (const auto &symbol : all_symbols) {
result.insert(UserSymbol(symbol, api::VENUE_BTS,
api::SECURITYTYPE_SYNTHETIC));
}
}
}
{
auto const &e = config::get<config::system_symbols::Symbols>().ice();
if (exchange == api::VENUE_UNSET || exchange == api::VENUE_ICE)
{
if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.future(), api::VENUE_ICE, api::SECURITYTYPE_FUTURE, &result);
if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.option(), api::VENUE_ICE, api::SECURITYTYPE_OPTION, &result);
if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.spread(), api::VENUE_ICE, api::SECURITYTYPE_SPREAD, &result);
}
if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET)
{
std::set<std::string> all_symbols;
all_symbols.insert(e.future().begin(), e.future().end());
all_symbols.insert(e.option().begin(), e.option().end());
all_symbols.insert(e.spread().begin(), e.spread().end());
for (const auto &symbol : all_symbols)
result.insert(UserSymbol(symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC));
}
}
{
auto const &e = config::get<config::system_symbols::Symbols>().sim();
if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIM)
{
if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.future(), api::VENUE_SIM, api::SECURITYTYPE_FUTURE, &result);
if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.option(), api::VENUE_SIM, api::SECURITYTYPE_OPTION, &result);
if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.spread(), api::VENUE_SIM, api::SECURITYTYPE_SPREAD, &result);
}
if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET)
{
std::set<std::string> all_symbols;
all_symbols.insert(e.future().begin(), e.future().end());
all_symbols.insert(e.option().begin(), e.option().end());
all_symbols.insert(e.spread().begin(), e.spread().end());
for (const auto &symbol : all_symbols)
result.insert(UserSymbol(symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC));
}
}
{
auto const &e = config::get<config::system_symbols::Symbols>().simice();
if (exchange == api::VENUE_UNSET || exchange == api::VENUE_SIMICE)
{
if (type == api::SECURITYTYPE_FUTURE || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.future(), api::VENUE_SIMICE, api::SECURITYTYPE_FUTURE, &result);
if (type == api::SECURITYTYPE_OPTION || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.option(), api::VENUE_SIMICE, api::SECURITYTYPE_OPTION, &result);
if (type == api::SECURITYTYPE_SPREAD || type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(e.spread(), api::VENUE_SIMICE, api::SECURITYTYPE_SPREAD, &result);
}
if (exchange == api::VENUE_BTS || exchange == api::VENUE_UNSET)
{
std::set<std::string> all_symbols;
all_symbols.insert(e.future().begin(), e.future().end());
all_symbols.insert(e.option().begin(), e.option().end());
all_symbols.insert(e.spread().begin(), e.spread().end());
for (const auto &symbol : all_symbols)
result.insert(UserSymbol(symbol, api::VENUE_BTS, api::SECURITYTYPE_SYNTHETIC));
}
}
return result.size();
}
size_t get_system_md_symbols(api::Venue exchange, SecurityType type,
std::vector<UserSymbol> *result) {
bts_assert(result);
set<UserSymbol> result_set;
size_t size = get_system_md_symbols(exchange, type, result_set);
result->reserve(size);
result->assign(result_set.begin(), result_set.end());
return size;
}
size_t get_system_md_symbols(const md::PlatformConfig &config,
std::set<UserSymbol> &out_result) {
Venue v;
bts_assert(api::Venue_Parse(config.exchange(), &v));
SecurityType st;
bts_assert(api::SecurityType_Parse(config.type(), &st));
return get_system_md_symbols(v, st, out_result);
}
std::set<UserSymbol> get_system_md_symbols() {
std::set<UserSymbol> all_symbols;
get_system_md_symbols(VENUE_UNSET, SECURITYTYPE_UNSET, all_symbols);
return all_symbols;
}
extern "C" BTS_EXPORT Platform platform_for_exchange(Venue v) {
const auto &pmap = config::get<PlatformMap>();
co2::Platform platform;
if (v == VENUE_SIM)
return PLATFORM_SIM;
if (v == VENUE_ICE && co2::Platform_Parse(pmap.ice_platform(), &platform))
return platform;
//md2_tests.cpp depends on this fall through to yield SIM as the GLBX platform
if (co2::Platform_Parse(pmap.glbx_platform(), &platform))
return platform;
return PLATFORM_UNSET;
}
} // namespace md
} // namespace bts
#endif