ImagingTools Core SDK
Public Types | Public Member Functions | List of all members
imtcom::IConnectionStatusProviderabstract

Interface for monitoring network connection status with observable state changes. More...

#include <IConnectionStatusProvider.h>

Inheritance diagram for imtcom::IConnectionStatusProvider:
imtcom::CAsyncConnectionCheckerComp imtcom::CSimpleConnectionCheckerComp imtrest::CWebSocketServerComp

Public Types

enum  ConnectionStatus { CS_UNKNOWN = 0 , CS_DISCONNECTED , CS_CONNECTED }
 Connection status enumeration. More...
 

Public Member Functions

virtual ConnectionStatus GetConnectionStatus () const =0
 Get the current connection status.
 

Detailed Description

Interface for monitoring network connection status with observable state changes.

The IConnectionStatusProvider defines a contract for components that monitor network connectivity and provide real-time status updates. It extends IChangeable to support reactive programming patterns, allowing clients to observe connection state transitions.

This interface is central to connection health monitoring in ImtCore, supporting various monitoring strategies from simple endpoint checks to comprehensive internet availability detection.

Key Features

Observable Status:

Three-State Model:

Multiple Strategies:

Connection States

CS_UNKNOWN (0)

Meaning: Connection status has not been determined yet.

Typical Scenarios:

Recommended Actions:

CS_DISCONNECTED (1)

Meaning: Connection is unavailable, unreachable, or failed health check.

Typical Scenarios:

Recommended Actions:

CS_CONNECTED (2)

Meaning: Connection is established and health check succeeded.

Typical Scenarios:

Recommended Actions:

Implementations

CAsyncConnectionCheckerComp

Strategy: Timer-based asynchronous HTTP requests

Features:

Use Cases:

Configuration:

CSimpleConnectionCheckerComp

Strategy: GraphQL client request validation

Features:

Use Cases:

Benefits:

CInternetConnectionCheckerComp

Strategy: External internet availability detection

Features:

Use Cases:

Configuration:

Usage Examples

Basic Usage

// Create connection checker
auto checker = icomp::CreateComponent<CAsyncConnectionCheckerComp>();
// Configure via component attributes (check interval, timeout, etc.)
// ...
// Get current status
auto status = checker->GetConnectionStatus();
switch (status) {
qDebug() << "Online";
break;
qDebug() << "Offline";
break;
qDebug() << "Status unknown";
break;
}
@ CS_CONNECTED
Connection is established and healthy.
@ CS_DISCONNECTED
Connection is unavailable or failed health check.
@ CS_UNKNOWN
Connection status unknown or not yet determined.

Observing Status Changes

// Subscribe to status changes
QObject::connect(checker.get(), &istd::IChangeable::OnChanged,
[checker](const istd::IChangeable::ChangeSet& changeSet) {
auto status = checker->GetConnectionStatus();
if (status == IConnectionStatusProvider::CS_CONNECTED) {
qDebug() << "Connection restored!";
// Enable network features, process queued operations
ProcessQueuedRequests();
qWarning() << "Connection lost!";
// Disable network features, show offline indicator
ShowOfflineIndicator();
}
});

Retry Logic with Exponential Backoff

class ConnectionManager {
public:
ConnectionManager(IConnectionStatusProvider* checker)
: m_checker(checker), m_retryDelay(1000) {
connect(checker, &IChangeable::OnChanged,
this, &ConnectionManager::OnStatusChanged);
}
private:
void OnStatusChanged() {
auto status = m_checker->GetConnectionStatus();
if (status == IConnectionStatusProvider::CS_CONNECTED) {
m_retryDelay = 1000; // Reset backoff
qDebug() << "Connected successfully";
} else if (status == IConnectionStatusProvider::CS_DISCONNECTED) {
// Schedule retry with exponential backoff
QTimer::singleShot(m_retryDelay, this, &ConnectionManager::AttemptReconnect);
m_retryDelay = qMin(m_retryDelay * 2, 60000); // Max 60 seconds
qWarning() << "Disconnected, retry in" << m_retryDelay << "ms";
}
}
void AttemptReconnect() {
// Trigger reconnection logic
}
IConnectionStatusProvider* m_checker;
int m_retryDelay;
};
Interface for monitoring network connection status with observable state changes.

UI Status Indicator

// QML example with model update binder
class ConnectionStatusModel : public QObject {
Q_OBJECT
Q_PROPERTY(QString statusText READ statusText NOTIFY statusChanged)
Q_PROPERTY(QColor statusColor READ statusColor NOTIFY statusChanged)
public:
ConnectionStatusModel(IConnectionStatusProvider* provider)
: m_observer(this, &ConnectionStatusModel::OnStatusChanged) {
m_observer.Observe(provider);
}
signals:
void statusChanged();
private:
void OnStatusChanged(const IChangeable::ChangeSet&,
const IConnectionStatusProvider* provider) {
m_status = provider->GetConnectionStatus();
emit statusChanged();
}
QString statusText() const {
switch (m_status) {
case CS_CONNECTED: return "Online";
case CS_DISCONNECTED: return "Offline";
default: return "Unknown";
}
}
QColor statusColor() const {
switch (m_status) {
case CS_CONNECTED: return QColor(Qt::green);
case CS_DISCONNECTED: return QColor(Qt::red);
default: return QColor(Qt::gray);
}
}
ConnectionStatus m_status = CS_UNKNOWN;
TModelUpdateBinder<IConnectionStatusProvider, ConnectionStatusModel> m_observer;
};
virtual ConnectionStatus GetConnectionStatus() const =0
Get the current connection status.

Best Practices

Check Intervals:

Timeout Configuration:

Retry Logic:

Observable Pattern:

Error Handling:

See also
CAsyncConnectionCheckerComp, CSimpleConnectionCheckerComp, CInternetConnectionCheckerComp
istd::IChangeable, imtbase::TModelUpdateBinder

Definition at line 320 of file IConnectionStatusProvider.h.

Member Enumeration Documentation

◆ ConnectionStatus

Connection status enumeration.

Represents the current state of network connectivity as determined by the monitoring implementation.

See also
GetConnectionStatus()
Enumerator
CS_UNKNOWN 

Connection status unknown or not yet determined.

CS_DISCONNECTED 

Connection is unavailable or failed health check.

CS_CONNECTED 

Connection is established and healthy.

Definition at line 332 of file IConnectionStatusProvider.h.

Member Function Documentation

◆ GetConnectionStatus()

virtual ConnectionStatus imtcom::IConnectionStatusProvider::GetConnectionStatus ( ) const
pure virtual

Get the current connection status.

Returns the current state of the connection as determined by the most recent health check or monitoring activity.

This method is typically very lightweight and suitable for frequent calls, as it returns cached status rather than performing an actual connectivity test.

Returns
Current connection status (CS_UNKNOWN, CS_DISCONNECTED, or CS_CONNECTED).
See also
ConnectionStatus
Example:
auto status = statusProvider->GetConnectionStatus();
// Safe to perform network operations
SendNetworkRequest();
} else {
// Connection unavailable, queue operation or show error
QueueForLater();
}
Note
Status changes trigger IChangeable::OnChanged notifications. Subscribe to these signals rather than polling GetConnectionStatus() for reactive updates.