#ifndef NO_CPP0X #include "MDRecoverableConsumer.h" #include "PlatformLoader.h" #include "lib/logging/logging.h" #include using namespace std; namespace bts { namespace md { set get_symbols(Venue v=api::VENUE_UNSET); MDRecoverableConsumer::MDRecoverableConsumer(std::shared_ptr ss) : _symbols(get_symbols()), _ss(ss), _current(init(_symbols, ss)), _old(), _callbacks(), _seen(), _lock () {} MDRecoverableConsumer::MDRecoverableConsumer(Venue exchange, std::shared_ptr ss) : // (visual studio 2012) MDRecoverableConsumer(get_symbols(exchange)) _symbols(get_symbols(exchange)), _ss(ss), _current(init(_symbols, ss)), _old(), _callbacks(), _seen(), _lock () {} MDRecoverableConsumer::MDRecoverableConsumer(std::set symbols, std::shared_ptr ss) : _symbols(symbols), _ss(ss), _current(init(symbols, ss)), _old(), _callbacks(), _seen(), _lock () { } MDRecoverableConsumer::~MDRecoverableConsumer() {} unique_ptr MDRecoverableConsumer::init(std::set& symbols, std::shared_ptr& ss) { return std::make_unique(symbols, ss); } 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.id(); 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() const { 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, _ss); 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.id()) { // 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 (md,Info,"Cleaned up {} MDConsumer instances" ,old_sz - _old.size () ); } } // // Utility functions set get_symbols(Venue exchange) { set all_symbols; get_system_md_symbols(exchange, api::SECURITYTYPE_UNSET, all_symbols); return all_symbols; } }}//namespace #endif