ACF component implementing multi-protocol server dispatch for HTTP and WebSocket.
More...
ACF component implementing multi-protocol server dispatch for HTTP and WebSocket.
CServerDispatcherComp is the primary implementation of IServerDispatcher, providing lifecycle management for HTTP and WebSocket server instances. It acts as a facade for controlling multiple server implementations from the imtrest module through a unified protocol-based API.
This component enables applications to start, stop, and monitor multiple communication protocols simultaneously on the same host, with independent control over each protocol.
Key Features
Protocol Support:
Server Lifecycle Management:
- Independent start/stop control per protocol
- Status monitoring with ServerStatus enum
- Graceful startup and shutdown
- Error handling and logging
ACF Component Integration:
- References to server components (HttpServer, WebSocketServer)
- Component-based dependency injection
- Factory-compatible configuration
Server Status Tracking:
- Delegates to underlying IServer implementations
- Real-time status queries
- States: SS_UNKNOWN, SS_STOPPED, SS_STARTING, SS_RUNNING, SS_STOPPING
Architecture
|
|
+--------------+---------------+
| |
StartServer(PT_HTTP) StartServer(PT_WEBSOCKET)
| |
v v
m_httpServerCompPtr m_wsServerCompPtr
| |
v v
CTcpServerComp CWebSocketServerComp
(HTTP/HTTPS Server) (WebSocket/WSS Server)
| |
v v
ACF component implementing multi-protocol server dispatch for HTTP and WebSocket.
Interface for describing server connection configuration with multi-protocol support.
Interface for managing the lifecycle of multi-protocol servers.
Component References
| Reference | Type | Description | Default |
| HttpServer | imtrest::IServer | HTTP/HTTPS server implementation | "HttpServer" |
| WebSocketServer | imtrest::IServer | WebSocket/WSS server implementation | "WebSocketServer" |
Both server components must be properly configured with IServerConnectionInterface references to provide host, port, and security settings.
Protocol-to-Server Mapping
The dispatcher routes protocol operations to specific server implementations:
Usage Examples
Basic Usage
auto dispatcher = icomp::CreateComponent<CServerDispatcherComp>();
auto httpServer = icomp::CreateComponent<imtrest::CTcpServerComp>();
auto wsServer = icomp::CreateComponent<imtrest::CWebSocketServerComp>();
dispatcher->SetReference("HttpServer", httpServer);
dispatcher->SetReference("WebSocketServer", wsServer);
qDebug() << "HTTP server started";
}
qDebug() << "WebSocket server started";
}
if (httpStatus == imtrest::IServer::SS_RUNNING) {
qDebug() << "HTTP server is running";
}
@ PT_WEBSOCKET
WebSocket/WSS protocol for real-time bidirectional communication (default port: 9000)
@ PT_HTTP
HTTP/HTTPS protocol for RESTful APIs (default port: 9001)
Factory-Based Creation
auto dispatcher = componentFactory->CreateComponent("ServerDispatcher");
auto protocols = dispatcher->GetSupportedProtocols();
for (auto protocol : protocols) {
if (dispatcher->StartServer(protocol)) {
qDebug() << "Started server for protocol:" << protocol;
}
}
Application Lifecycle Integration
class ApplicationServer : public QObject {
public:
ApplicationServer() {
m_dispatcher = icomp::CreateComponent<CServerDispatcherComp>();
}
bool Start() {
if (!StartProtocol(IServerConnectionInterface::PT_HTTP)) {
qCritical() << "Failed to start HTTP server";
return false;
}
if (!StartProtocol(IServerConnectionInterface::PT_WEBSOCKET)) {
qWarning() << "Failed to start WebSocket server";
}
qInfo() << "Application servers started";
return true;
}
void Stop() {
qInfo() << "Stopping application servers...";
StopProtocol(IServerConnectionInterface::PT_WEBSOCKET);
StopProtocol(IServerConnectionInterface::PT_HTTP);
qInfo() << "Application servers stopped";
}
private:
bool StartProtocol(IServerConnectionInterface::ProtocolType protocol) {
if (!m_dispatcher->StartServer(protocol)) {
return false;
}
int retries = 10;
while (retries-- > 0) {
auto status = m_dispatcher->GetServerStatus(protocol);
if (status == imtrest::IServer::SS_RUNNING) {
return true;
}
QThread::msleep(100);
}
qCritical() << "Server failed to reach running state:" << protocol;
return false;
}
void StopProtocol(IServerConnectionInterface::ProtocolType protocol) {
auto status = m_dispatcher->GetServerStatus(protocol);
if (status != imtrest::IServer::SS_RUNNING) {
return;
}
m_dispatcher->StopServer(protocol);
int retries = 20;
while (retries-- > 0) {
status = m_dispatcher->GetServerStatus(protocol);
if (status == imtrest::IServer::SS_STOPPED) {
break;
}
QThread::msleep(100);
}
}
std::shared_ptr<CServerDispatcherComp> m_dispatcher;
};
Health Monitoring
class ServerHealthMonitor : public QObject {
Q_OBJECT
public:
: m_dispatcher(dispatcher) {
m_timer.setInterval(30000);
connect(&m_timer, &QTimer::timeout, this, &ServerHealthMonitor::CheckHealth);
m_timer.start();
}
signals:
private slots:
void CheckHealth() {
auto protocols = m_dispatcher->GetSupportedProtocols();
for (auto protocol : protocols) {
auto status = m_dispatcher->GetServerStatus(protocol);
bool healthy = (status == imtrest::IServer::SS_RUNNING);
if (!healthy && m_lastHealthy.value(protocol, false)) {
qWarning() << "Server became unhealthy:" << protocol;
emit serverUnhealthy(protocol);
m_dispatcher->StopServer(protocol);
if (m_dispatcher->StartServer(protocol)) {
qInfo() << "Server restart initiated:" << protocol;
}
} else if (healthy && !m_lastHealthy.value(protocol, false)) {
qInfo() << "Server recovered:" << protocol;
emit serverRecovered(protocol);
}
m_lastHealthy[protocol] = healthy;
}
}
private:
IServerDispatcher* m_dispatcher;
QTimer m_timer;
QMap<IServerConnectionInterface::ProtocolType, bool> m_lastHealthy;
};
ProtocolType
Communication protocols supported by server connections.
Error Handling
The dispatcher handles several error scenarios:
Startup Failures:
- Port already in use → StartServer() returns false
- Invalid configuration → StartServer() returns false
- Missing server reference → StartServer() returns false
- Permission denied → StartServer() returns false (logged)
Status Queries:
- Invalid protocol → Returns SS_UNKNOWN
- Null server reference → Returns SS_UNKNOWN
Shutdown:
- StopServer() on already-stopped server → Returns true (no-op)
- StopServer() with null reference → Returns false
Best Practices
- Initialization Order: Configure all server components and their connection parameters before calling StartServer().
- Graceful Shutdown: Stop servers in reverse order of startup to ensure dependent connections are closed first (WebSocket before HTTP).
- Status Checking: Always check GetServerStatus() before attempting to stop a server to avoid unnecessary operations.
- Error Logging: Check StartServer() return value and log detailed errors for troubleshooting deployment issues.
- Health Monitoring: Implement periodic status checks to detect and recover from unexpected server failures.
- Reference Validation: Ensure HttpServer and WebSocketServer references are set before using the dispatcher.
- Wait for Startup: After StartServer(), wait for SS_RUNNING status before considering the server operational (servers may start asynchronously).
Current Limitations
- Only supports HTTP and WebSocket protocols
- gRPC and File protocols are not implemented
- No built-in health monitoring (implement externally)
- No automatic restart on failure
- No load balancing or server pooling
Integration Points
Server Implementations (imtrest):
Configuration (imtcom):
Applications:
- imtserverapp: Application server lifecycle
- imtservergql: GraphQL server management
- imtbase: System status reporting
- See also
- IServerDispatcher, IServerConnectionInterface
-
imtrest::IServer, imtrest::CTcpServerComp, imtrest::CWebSocketServerComp
Definition at line 374 of file CServerDispatcherComp.h.