156 lines
3.8 KiB
C++
156 lines
3.8 KiB
C++
|
|
#include "qtradecore.h"
|
|||
|
|
#include <QDebug>
|
|||
|
|
#include <QCoreApplication>
|
|||
|
|
|
|||
|
|
QTradeCore* QTradeCore::instance()
|
|||
|
|
{
|
|||
|
|
static QTradeCore* instance = nullptr;
|
|||
|
|
static QMutex mutex;
|
|||
|
|
|
|||
|
|
if (!instance) {
|
|||
|
|
QMutexLocker locker(&mutex);
|
|||
|
|
if (!instance) {
|
|||
|
|
instance = new QTradeCore();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return instance;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QTradeCore::QTradeCore(QObject *parent) : QObject(parent)
|
|||
|
|
{
|
|||
|
|
// 延迟初始化,在initialize()方法中创建实例
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QTradeCore::~QTradeCore()
|
|||
|
|
{
|
|||
|
|
shutdown();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void QTradeCore::initialize()
|
|||
|
|
{
|
|||
|
|
if (m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Already initialized";
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
qDebug() << "QTradeCore: Initializing...";
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 初始化事件总线
|
|||
|
|
m_eventBus = QEventBus::instance();
|
|||
|
|
|
|||
|
|
// 初始化订阅管理器
|
|||
|
|
m_subscriptionManager = QSubscriptionManager::instance();
|
|||
|
|
|
|||
|
|
// 初始化大单管理器
|
|||
|
|
m_bigOrderManager = QBigOrderManager::instance();
|
|||
|
|
|
|||
|
|
// 初始化日志管理器
|
|||
|
|
m_logManager = QLogManager::Instance();
|
|||
|
|
|
|||
|
|
// 初始化订单处理器
|
|||
|
|
m_orderProcessor = new QOrderProcessor(this);
|
|||
|
|
|
|||
|
|
// 设置初始状态
|
|||
|
|
m_initialized = true;
|
|||
|
|
|
|||
|
|
qDebug() << "QTradeCore: Initialization completed successfully";
|
|||
|
|
emit systemInitialized();
|
|||
|
|
|
|||
|
|
} catch (const std::exception& e) {
|
|||
|
|
qCritical() << "QTradeCore: Initialization failed:" << e.what();
|
|||
|
|
emit errorOccurred(QString("Initialization failed: %1").arg(e.what()));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void QTradeCore::shutdown()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
qDebug() << "QTradeCore: Shutting down...";
|
|||
|
|
|
|||
|
|
// 清理资源
|
|||
|
|
if (m_orderProcessor) {
|
|||
|
|
delete m_orderProcessor;
|
|||
|
|
m_orderProcessor = nullptr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 注意:单例对象由它们自己管理生命周期
|
|||
|
|
// 我们只清理我们直接创建的对象
|
|||
|
|
|
|||
|
|
m_initialized = false;
|
|||
|
|
|
|||
|
|
qDebug() << "QTradeCore: Shutdown completed";
|
|||
|
|
emit systemShutdown();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QSubscriptionManager* QTradeCore::subscriptionManager()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Not initialized, cannot access subscription manager";
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
return m_subscriptionManager;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QOrderProcessor* QTradeCore::orderProcessor()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Not initialized, cannot access order processor";
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
return m_orderProcessor;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QBigOrderManager* QTradeCore::bigOrderManager()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Not initialized, cannot access big order manager";
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
return m_bigOrderManager;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QLogManager* QTradeCore::logManager()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Not initialized, cannot access log manager";
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
return m_logManager;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QEventBus* QTradeCore::eventBus()
|
|||
|
|
{
|
|||
|
|
if (!m_initialized) {
|
|||
|
|
qWarning() << "QTradeCore: Not initialized, cannot access event bus";
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
return m_eventBus;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QString QTradeCore::getSystemStatus() const
|
|||
|
|
{
|
|||
|
|
QString status;
|
|||
|
|
|
|||
|
|
if (m_initialized) {
|
|||
|
|
status = "System is running\n";
|
|||
|
|
|
|||
|
|
// Build component list manually to avoid lambda issues
|
|||
|
|
QStringList components;
|
|||
|
|
if (m_subscriptionManager) components << "SubscriptionManager";
|
|||
|
|
if (m_orderProcessor) components << "OrderProcessor";
|
|||
|
|
if (m_bigOrderManager) components << "BigOrderManager";
|
|||
|
|
if (m_logManager) components << "LogManager";
|
|||
|
|
if (m_eventBus) components << "EventBus";
|
|||
|
|
|
|||
|
|
status += QString("Components: %1\n").arg(components.join(", "));
|
|||
|
|
} else {
|
|||
|
|
status = "System is not initialized";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return status;
|
|||
|
|
}
|