131 lines
3.8 KiB
C++
131 lines
3.8 KiB
C++
#ifndef FAULT_TOLERANCE_MANAGER_H
|
|
#define FAULT_TOLERANCE_MANAGER_H
|
|
|
|
#include <QObject>
|
|
#include <QTimer>
|
|
#include <QElapsedTimer>
|
|
#include <QMutex>
|
|
#include <QMap>
|
|
#include <QSet>
|
|
#include "../common_structures/TradingStructures.h"
|
|
|
|
// Fault tolerance configuration
|
|
struct FaultToleranceConfig {
|
|
int heartbeatIntervalMs = 5000; // Heartbeat interval
|
|
int heartbeatTimeoutMs = 15000; // Heartbeat timeout
|
|
int maxReconnectAttempts = 3; // Maximum reconnection attempts
|
|
int reconnectDelayMs = 2000; // Reconnection delay
|
|
bool autoReconnectEnabled = true; // Auto-reconnect enabled
|
|
bool failoverEnabled = true; // Failover enabled
|
|
QStringList backupServers; // Backup server list
|
|
};
|
|
|
|
// Connection statistics
|
|
struct ConnectionStats {
|
|
qint64 totalConnections = 0;
|
|
qint64 failedConnections = 0;
|
|
qint64 successfulConnections = 0;
|
|
qint64 totalReconnects = 0;
|
|
double availabilityRate = 0.0;
|
|
QDateTime lastConnectionTime;
|
|
QDateTime lastFailureTime;
|
|
|
|
void updateStats(bool success) {
|
|
totalConnections++;
|
|
if (success) {
|
|
successfulConnections++;
|
|
lastConnectionTime = QDateTime::currentDateTime();
|
|
} else {
|
|
failedConnections++;
|
|
lastFailureTime = QDateTime::currentDateTime();
|
|
}
|
|
availabilityRate = totalConnections > 0 ?
|
|
static_cast<double>(successfulConnections) / totalConnections : 0.0;
|
|
}
|
|
};
|
|
|
|
class FaultToleranceManager : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
static FaultToleranceManager* instance();
|
|
|
|
// Configuration
|
|
void setConfig(const FaultToleranceConfig& config);
|
|
FaultToleranceConfig getConfig() const;
|
|
|
|
// Connection management
|
|
bool connectToServer(const QString& serverAddress, int port);
|
|
void disconnectFromServer();
|
|
void reconnect();
|
|
|
|
// Status queries
|
|
Trading::ConnectionState getConnectionState() const;
|
|
bool isConnected() const;
|
|
bool isDegraded() const;
|
|
ConnectionStats getStatistics() const;
|
|
|
|
// Heartbeat management
|
|
void startHeartbeat();
|
|
void stopHeartbeat();
|
|
void sendHeartbeat();
|
|
|
|
// Failover management
|
|
void enableFailover(bool enabled);
|
|
void addBackupServer(const QString& serverAddress, int port);
|
|
void removeBackupServer(const QString& serverAddress);
|
|
QStringList getBackupServers() const;
|
|
|
|
// Recovery actions
|
|
void triggerFailover();
|
|
void resetConnection();
|
|
void forceReconnect();
|
|
|
|
signals:
|
|
void connectionStateChanged(Trading::ConnectionState newState, Trading::ConnectionState oldState);
|
|
void connectionEstablished();
|
|
void connectionLost();
|
|
void connectionDegraded();
|
|
void heartbeatTimeout();
|
|
void failoverTriggered(const QString& newServer);
|
|
void reconnectAttempt(int attempt, int maxAttempts);
|
|
void connectionStatsUpdated(const ConnectionStats& stats);
|
|
|
|
private:
|
|
explicit FaultToleranceManager(QObject *parent = nullptr);
|
|
~FaultToleranceManager();
|
|
|
|
void initialize();
|
|
void cleanup();
|
|
void updateConnectionState(Trading::ConnectionState newState);
|
|
void attemptReconnect();
|
|
void performFailover();
|
|
void updateHeartbeat();
|
|
void checkHeartbeatTimeout();
|
|
|
|
// Connection state
|
|
Trading::ConnectionState m_currentState = Trading::ConnectionState::Disconnected;
|
|
QString m_currentServer;
|
|
int m_currentPort = 0;
|
|
int m_reconnectAttempts = 0;
|
|
|
|
// Configuration
|
|
FaultToleranceConfig m_config;
|
|
|
|
// Statistics
|
|
ConnectionStats m_stats;
|
|
|
|
// Timers
|
|
QTimer* m_heartbeatTimer;
|
|
QTimer* m_reconnectTimer;
|
|
QElapsedTimer m_lastHeartbeatTime;
|
|
|
|
// Backup servers
|
|
QMap<QString, int> m_backupServers;
|
|
|
|
// Synchronization
|
|
mutable QMutex m_mutex;
|
|
};
|
|
|
|
#endif // FAULT_TOLERANCE_MANAGER_H
|