#ifndef NO_CPP0X #include "MDRecoverableConsumer.h" #include "PlatformLoader.h" #include "lib/logging/logging.h" #include using namespace std; namespace bts { namespace md { using namespace md2; set get_symbols(Exchange::type); MDRecoverableConsumer::MDRecoverableConsumer() : // (visual studio 2012) MDRecoverableConsumer(get_symbols(Exchange::TESTEXCHANGE)) _symbols(get_symbols(Exchange::TESTEXCHANGE)), _current(init(_symbols)), _old(), _callbacks(), _seen(), _lock () {} MDRecoverableConsumer::MDRecoverableConsumer(Exchange::type exchange) : // (visual studio 2012) MDRecoverableConsumer(get_symbols(exchange)) _symbols(get_symbols(exchange)), _current(init(_symbols)), _old(), _callbacks(), _seen(), _lock () {} MDRecoverableConsumer::MDRecoverableConsumer(std::set symbols) : _symbols(symbols), _current(init(symbols)), _old(), _callbacks(), _seen(), _lock () { } MDRecoverableConsumer::~MDRecoverableConsumer() {} unique_ptr MDRecoverableConsumer::init(set& symbols) { unique_ptr result(new MDConsumerGroup()); result->consumer = MDConsumer(symbols); return result; } size_t MDRecoverableConsumer::walk_security_list(SecurityCallback callback) { return _current->consumer.walk_security_list(callback); } void MDRecoverableConsumer::on_security(SecurityCallback cb) { // Add a filter to the callback to avoid calling the on_security callback with duplicates auto filtered_cb = [this,cb](const Security& security, MDConsumerHandle handle) { auto key = security.security_key(); if (_seen.find(key) == _seen.end()) { _seen.insert(key); cb(security, handle); } }; _current->consumer.on_security(filtered_cb); Mutex::scoped_lock l (_lock); _callbacks.push_back(filtered_cb); } size_t MDRecoverableConsumer::scan_securities() { return _current->consumer.scan_securities(); } std::shared_ptr MDRecoverableConsumer::get_managed_handle(security_key key) { Mutex::scoped_lock l (_lock); auto handle = _current->consumer.get_managed_handle(key); _current->handles.push_back(handle); return handle; } bool MDRecoverableConsumer::all_heartbeats_ok() { return _current->consumer.all_heartbeats_ok(); } bool MDRecoverableConsumer::try_recover() { Mutex::scoped_lock l (_lock); // Replace current with a new one (if the new one is better) auto temp = init(_symbols); if (!temp->consumer.all_heartbeats_ok()) return false; std::swap(temp, _current); // push current to old _old.push_back(std::move(temp)); // copy old callbacks so we can reinstate them auto old_callbacks = _callbacks; // Add an on_security callback to current to check the old handles l.release(); // re-instante old callbacks for (auto &cb : old_callbacks) { _current->consumer.on_security(cb); } _current->consumer.on_security([&](const Security& security, MDConsumerHandle newhandle) { Mutex::scoped_lock l (_lock); for (auto& group : _old) { for (auto& handle : group->handles) { if (handle->key() == security.security_key()) { // update the handle! (if this is called guarunteed success) *handle = newhandle; bts_assert(handle->is_initialized()); _current->handles.push_back(move(handle)); } } } cleanup(); }); return true; } // cleanup should only ever be called while _lock is locked void MDRecoverableConsumer::cleanup() { for (auto& group : _old) { auto& handles = group->handles; handles.erase( remove_if( handles.begin(), handles.end(), [](shared_ptr& handle) { return handle == nullptr; }), handles.end()); } auto old_sz = _old.size (); _old.erase( remove_if( _old.begin(), _old.end(), [](unique_ptr& pgroup) -> bool { return pgroup->handles.empty(); }), _old.end()); if (old_sz > _old.size ()) { LOG (MarketData,Info,"Cleaned up {} MDConsumer instances" ,old_sz - _old.size () ); } } // // Utility functions set get_symbols(Exchange::type exchange) { set all_symbols; get_system_md_symbols(exchange, SecurityType::UNSET, all_symbols); return all_symbols; } }}//namespace #endif