Files
spark/src/lib/md/cme/CMEActiveQueryHandler.h
T

108 lines
4.4 KiB
C++

#pragma once
#include "lib/md/cme/CMEActivSubscriptionHandler.h"
#include "lib/md/GeneralMarketDataListener.h"
#include <ActivMiddleware/CxxYZ/unordered_map.h>
#define UNUSED(x) (void)(x)
#include "ActivOneApi/OneApi.h"
namespace bts::md::cme {
class CMEActivQueryHandler : public Activ::OneApi::ITopicQueryHandler {
class TopicDetails {
public:
//! The subscription handle for this topic
Activ::OneApi::ScopedHandle m_handle;
CMEActivSubscriptionHandler sub_handler;
//! The current topic subscription state. Is this topic waiting for a refresh.
Activ::OneApi::TopicSubscriptionState m_state;
/**
* @brief Has this topic been seen for the current topic query. Whilst a query is in an error state there may be missed
* topic deletions, these need to be cleaned up once the OnTopicQueryComplete callback is received.
*/
size_t m_queryVersion;
};
//! Used to track how many times the query has restarted.
// size_t m_queryVersion;
//! Map of subscriptions.
typedef Activ::CxxYZ::unordered_map<Activ::String, TopicDetails> SubscriptionMap;
SubscriptionMap m_subscriptionMap;
//! Used to track how many times the query has restarted.
size_t m_queryVersion;
public:
std::shared_ptr<GeneralMarketDataListener> listner;
persistence::IDGenerator *sec_id_store;
CMEActivQueryHandler(
std::shared_ptr<GeneralMarketDataListener> listner,
persistence::IDGenerator *sec_id_store)
: m_queryVersion(0), listner(listner), sec_id_store(sec_id_store) {}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryStart(const Context &context) {
UNUSED(context);
std::cout << "QUERY start" << std::endl;
++m_queryVersion;
}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryAdd(Topic &topic, const Context &context) {
const String &symbol = topic.GetSymbol();
TopicDetails &topicDetails = m_subscriptionMap[symbol];
if (!topicDetails.m_handle) {
topicDetails.m_queryVersion = m_queryVersion;
topicDetails.m_state = TOPIC_SUBSCRIPTION_STATE_PENDING;
topicDetails.sub_handler = CMEActivSubscriptionHandler(listner, sec_id_store, &topicDetails.m_state);
topicDetails.m_handle =
context.GetSession().Subscribe(DATA_SOURCE_ACTIV, symbol, topicDetails.sub_handler);
} else {
topicDetails.m_queryVersion = m_queryVersion;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryRemove(const TopicId &topicId, const Context &context) {
UNUSED(context);
std::cout << "QUERY topic removed: " << topicId.m_symbol << std::endl;
SubscriptionMap::iterator iter = m_subscriptionMap.find(topicId.m_symbol);
if (m_subscriptionMap.end() != iter)
m_subscriptionMap.erase(iter);
}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryComplete(const Context &context) {
std::cout << "QUERY complete" << std::endl;
UNUSED(context);
// Close the subscriptions for any topics that haven't been received.
SubscriptionMap::iterator iter = m_subscriptionMap.begin();
while (m_subscriptionMap.end() != iter) {
if (iter->second.m_queryVersion != m_queryVersion)
m_subscriptionMap.erase(iter++);
else
++iter;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryError(const StatusCode statusCode, const Context &context) {
std::cout << "QUERY error " << StatusCodeToString(statusCode)
<< std::endl;
UNUSED(context);
}
// ---------------------------------------------------------------------------------------------------------------------------------
void OnTopicQueryFailure(
const StatusCode statusCode, const Context &context) {
std::cout << "QUERY failure " << StatusCodeToString(statusCode)
<< std::endl;
// Shutdown session.
Session::Shutdown(context.GetSession());
UNUSED(context);
}
};
} // namespace bts::md::cme