Files
spark/src/lib/md/PlatformLoader.cpp
T
2018-02-22 15:57:52 -05:00

160 lines
5.3 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;
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() == 1);
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));
}
}
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);
}
extern "C" BTS_EXPORT Platform platform_for_exchange(Venue) {
return co2::PLATFORM_MDP3ONIXS;
// using namespace bts::md;
// const auto& pmap = config::get<PlatformMap>();
// const PlatformMap::PlatformProps platforms[] = {
// pmap.sim(), pmap.srlabs(), pmap.spryware(), pmap.csm(), pmap.cme(), pmap.ice(), pmap.replay(), pmap.activ(), pmap.mdp3(), pmap.csm_onixs(), pmap.blue()
// };
// const Platform types[] = {
// PLATFORM_SIM, PLATFORM_SRLABS, PLATFORM_SPRYWARE, PLATFORM_CSM, PLATFORM_CME, PLATFORM_ICE, PLATFORM_REPLAY, PLATFORM_ACTIV, PLATFORM_MDP3ONIXS, PLATFORM_CSM_ONIXS, PLATFORM_BLUE
// };
// static_assert(BTS_ARRAYSIZE(platforms) == BTS_ARRAYSIZE(types),"Platforms should be the same size as types");
// for (size_t x = 0; x < BTS_ARRAYSIZE(platforms); x++)
// {
// for (int y = 0; y < platforms[x].exchange_size(); ++y) {
// if (platforms[x].exchange(y) == exchange) {
// return types[x];
// }
// }
// }
// return PLATFORM_UNSET;
}
}}//namespace
#endif