ImagingTools Core SDK
List of all members
imtcom::CAsyncConnectionCheckerComp

Timer-based asynchronous connection status checker using HTTP requests. More...

#include <CAsyncConnectionCheckerComp.h>

Inheritance diagram for imtcom::CAsyncConnectionCheckerComp:
imtcom::IConnectionStatusProvider

Additional Inherited Members

- Public Types inherited from imtcom::IConnectionStatusProvider
enum  ConnectionStatus { CS_UNKNOWN = 0 , CS_DISCONNECTED , CS_CONNECTED }
 Connection status enumeration. More...
 

Detailed Description

Timer-based asynchronous connection status checker using HTTP requests.

CAsyncConnectionCheckerComp implements IConnectionStatusProvider using a timer-driven approach that periodically sends HTTP requests to verify network connectivity. It provides non-blocking health checks with configurable intervals, timeouts, and retry logic.

This component is ideal for periodic server availability monitoring, network connectivity validation, and health checking scenarios where you need to detect when a server becomes unreachable or recovers.

Key Features

Timer-Based Checking:

HTTP-Based Validation:

Status Management:

URL Configuration:

Architecture

|
+-------------------------+
| |
v v
QTimer QNetworkAccessManager
(periodic trigger) (HTTP request execution)
| |
v v
OnTimeout() ReplyFinished()
| |
v v
SendRequest() Update m_status
| |
+------------+------------+
|
v
Notify IChangeable observers
(only if status changed)
Timer-based asynchronous connection status checker using HTTP requests.

Component Attributes

Attribute Type Description Default Required
CheckInterval int Health check interval in seconds 60 No
UrlParam IUrlParam URL parameter provider for target endpoint "UrlParam" No
ServerPrefix QByteArray Server prefix for URL construction "ServerPrefix" Yes

Component Lifecycle

  1. OnComponentCreated(): Initializes QNetworkAccessManager and starts timer
  2. Timer Triggers: Calls OnTimeout() at configured intervals
  3. SendRequest(): Initiates HTTP GET request to target URL
  4. ReplyFinished(): Processes response and updates status
  5. SetStatus(): Updates status and notifies observers if changed
  6. OnComponentDestroyed(): Stops timer and cleans up network manager

Status Determination Logic

CS_CONNECTED:

CS_DISCONNECTED:

CS_UNKNOWN:

Usage Examples

Basic Usage

// Create checker component
auto checker = icomp::CreateComponent<CAsyncConnectionCheckerComp>();
// Configure check interval (30 seconds)
checker->SetAttributeValue("CheckInterval", 30);
// Create URL parameter with target server
auto urlParam = icomp::CreateComponent<imtbase::CUrlParamComp>();
urlParam->SetUrl(QUrl("https://api.example.com/health"));
// Assign URL parameter to checker
checker->SetReference("UrlParam", urlParam);
// Subscribe to status changes
QObject::connect(checker.get(), &IChangeable::OnChanged,
[checker](const IChangeable::ChangeSet&) {
auto status = checker->GetConnectionStatus();
if (status == IConnectionStatusProvider::CS_CONNECTED) {
qDebug() << "Server is reachable";
qWarning() << "Server is unreachable";
}
});
// Component automatically starts checking after OnComponentCreated()
@ CS_DISCONNECTED
Connection is unavailable or failed health check.

Health Endpoint Monitoring

class ServerHealthMonitor : public QObject {
Q_OBJECT
public:
ServerHealthMonitor(const QUrl& healthEndpoint) {
m_checker = icomp::CreateComponent<CAsyncConnectionCheckerComp>();
// Configure for frequent checks (every 10 seconds)
m_checker->SetAttributeValue("CheckInterval", 10);
// Set health endpoint URL
auto urlParam = icomp::CreateComponent<imtbase::CUrlParamComp>();
urlParam->SetUrl(healthEndpoint); // e.g., /api/health
m_checker->SetReference("UrlParam", urlParam);
// Monitor status changes
connect(m_checker.get(), &IChangeable::OnChanged,
this, &ServerHealthMonitor::OnStatusChanged);
// Initialize counters
m_consecutiveFailures = 0;
m_lastUptime = QDateTime::currentDateTime();
}
signals:
void serverDown();
void serverUp();
void alertThresholdExceeded(int failureCount);
private slots:
void OnStatusChanged() {
auto status = m_checker->GetConnectionStatus();
if (status == IConnectionStatusProvider::CS_CONNECTED) {
if (m_consecutiveFailures > 0) {
// Server recovered
qInfo() << "Server recovered after"
<< m_consecutiveFailures << "failures";
emit serverUp();
}
m_consecutiveFailures = 0;
m_lastUptime = QDateTime::currentDateTime();
} else if (status == IConnectionStatusProvider::CS_DISCONNECTED) {
m_consecutiveFailures++;
if (m_consecutiveFailures == 1) {
// First failure
qWarning() << "Server health check failed";
emit serverDown();
} else if (m_consecutiveFailures == 3) {
// Multiple failures - send alert
qCritical() << "Server down for" << m_consecutiveFailures
<< "consecutive checks";
emit alertThresholdExceeded(m_consecutiveFailures);
}
}
}
private:
std::shared_ptr<CAsyncConnectionCheckerComp> m_checker;
int m_consecutiveFailures;
QDateTime m_lastUptime;
};

Multi-Server Monitoring

class MultiServerMonitor : public QObject {
Q_OBJECT
public:
void AddServer(const QString& name, const QUrl& url) {
auto checker = icomp::CreateComponent<CAsyncConnectionCheckerComp>();
checker->SetAttributeValue("CheckInterval", 30);
auto urlParam = icomp::CreateComponent<imtbase::CUrlParamComp>();
urlParam->SetUrl(url);
checker->SetReference("UrlParam", urlParam);
connect(checker.get(), &IChangeable::OnChanged,
[this, name, checker]() {
OnServerStatusChanged(name, checker->GetConnectionStatus());
});
m_checkers[name] = checker;
m_status[name] = IConnectionStatusProvider::CS_UNKNOWN;
}
QStringList GetOnlineServers() const {
QStringList online;
for (auto it = m_status.begin(); it != m_status.end(); ++it) {
if (it.value() == IConnectionStatusProvider::CS_CONNECTED) {
online << it.key();
}
}
return online;
}
int GetHealthyServerCount() const {
return GetOnlineServers().size();
}
signals:
void serverStatusChanged(const QString& serverName,
IConnectionStatusProvider::ConnectionStatus status);
private:
void OnServerStatusChanged(const QString& name,
IConnectionStatusProvider::ConnectionStatus status) {
if (m_status[name] != status) {
m_status[name] = status;
QString statusStr = (status == IConnectionStatusProvider::CS_CONNECTED)
? "ONLINE" : "OFFLINE";
qInfo() << "Server" << name << "is now" << statusStr;
emit serverStatusChanged(name, status);
}
}
QMap<QString, std::shared_ptr<CAsyncConnectionCheckerComp>> m_checkers;
QMap<QString, IConnectionStatusProvider::ConnectionStatus> m_status;
};
// Usage:
// MultiServerMonitor monitor;
// monitor.AddServer("Primary", QUrl("https://primary.example.com/health"));
// monitor.AddServer("Backup", QUrl("https://backup.example.com/health"));
// monitor.AddServer("Cache", QUrl("https://cache.example.com/health"));

Best Practices

  1. Check Intervals: Balance responsiveness and resource usage
    • Critical services: 5-10 seconds
    • Normal monitoring: 30-60 seconds
    • Background checks: 120+ seconds
  2. Timeout Handling: Network timeouts are detected by QNetworkAccessManager
    • Default Qt timeout: 30 seconds
    • Configure shorter timeouts for faster failure detection
  3. Resource Management: Component automatically cleans up on destruction
    • Timer stopped in OnComponentDestroyed()
    • QNetworkAccessManager deleted
    • No manual cleanup required
  4. Status Notifications: Only fires IChangeable::OnChanged when status changes
    • Reduces unnecessary event processing
    • Use for reactive UI updates
  5. URL Configuration: Use IUrlParam for dynamic URL changes
    • Allows runtime reconfiguration
    • Supports URL updates without recreating component
  6. Health Endpoints: Design dedicated health check endpoints
    • Lightweight responses
    • Quick processing
    • Return 200 OK if healthy
    • Don't require authentication if possible

Troubleshooting

Status Always CS_DISCONNECTED:

Status Never Updates:

False Negatives (Shows Disconnected When Online):

See also
IConnectionStatusProvider, CSimpleConnectionCheckerComp, CInternetConnectionCheckerComp
imtbase::IUrlParam, QNetworkAccessManager, QTimer

Definition at line 338 of file CAsyncConnectionCheckerComp.h.