ImagingTools Core SDK
List of all members
imtcom::CServerDispatcherComp

ACF component implementing multi-protocol server dispatch for HTTP and WebSocket. More...

#include <CServerDispatcherComp.h>

Inheritance diagram for imtcom::CServerDispatcherComp:
imtcom::IServerDispatcher

Additional Inherited Members

Detailed Description

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:

ACF Component Integration:

Server Status Tracking:

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
Reads IServerConnectionInterface for host/port/security
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:

Protocol Type Server Reference Typical Implementation Port (Default)
PT_HTTP m_httpServerCompPtr imtrest::CTcpServerComp 9001
PT_WEBSOCKET m_wsServerCompPtr imtrest::CWebSocketServerComp 9000
PT_GRPC (not supported) 50101
PT_FILE (not supported) N/A

Usage Examples

Basic Usage

// Create dispatcher component
auto dispatcher = icomp::CreateComponent<CServerDispatcherComp>();
// Create and configure HTTP server
auto httpServer = icomp::CreateComponent<imtrest::CTcpServerComp>();
// ... configure httpServer with connection parameters ...
// Create and configure WebSocket server
auto wsServer = icomp::CreateComponent<imtrest::CWebSocketServerComp>();
// ... configure wsServer with connection parameters ...
// Assign servers to dispatcher
dispatcher->SetReference("HttpServer", httpServer);
dispatcher->SetReference("WebSocketServer", wsServer);
// Start servers
if (dispatcher->StartServer(IServerConnectionInterface::PT_HTTP)) {
qDebug() << "HTTP server started";
}
if (dispatcher->StartServer(IServerConnectionInterface::PT_WEBSOCKET)) {
qDebug() << "WebSocket server started";
}
// Query status
auto httpStatus = dispatcher->GetServerStatus(IServerConnectionInterface::PT_HTTP);
if (httpStatus == imtrest::IServer::SS_RUNNING) {
qDebug() << "HTTP server is running";
}
// Later: stop servers
dispatcher->StopServer(IServerConnectionInterface::PT_HTTP);
@ 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

// Component factory XML configuration:
// <Component type="CServerDispatcherComp" name="ServerDispatcher">
// <Reference name="HttpServer" target="HttpServerImpl"/>
// <Reference name="WebSocketServer" target="WebSocketServerImpl"/>
// </Component>
//
// <Component type="CTcpServerComp" name="HttpServerImpl">
// <Reference name="ServerConnectionInterface" target="ConnectionConfig"/>
// <!-- ... other HTTP server configuration ... -->
// </Component>
//
// <Component type="CWebSocketServerComp" name="WebSocketServerImpl">
// <Reference name="ServerConnectionInterface" target="ConnectionConfig"/>
// <!-- ... other WebSocket server configuration ... -->
// </Component>
//
// <Component type="CServerConnectionInterfaceParamComp" name="ConnectionConfig">
// <Attribute name="DefaultHost" value="0.0.0.0"/>
// <Attribute name="DefaultHttpPort" value="8080"/>
// <Attribute name="DefaultWebSocketPort" value="8081"/>
// </Component>
// Load dispatcher from factory
auto dispatcher = componentFactory->CreateComponent("ServerDispatcher");
// Start all supported protocols
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>();
// ... configure dispatcher ...
}
bool Start() {
// Start servers in order
if (!StartProtocol(IServerConnectionInterface::PT_HTTP)) {
qCritical() << "Failed to start HTTP server";
return false;
}
if (!StartProtocol(IServerConnectionInterface::PT_WEBSOCKET)) {
qWarning() << "Failed to start WebSocket server";
// Continue anyway - WebSocket is optional
}
qInfo() << "Application servers started";
return true;
}
void Stop() {
// Stop in reverse order (graceful shutdown)
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;
}
// Wait for server to reach running state
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; // Already stopped
}
m_dispatcher->StopServer(protocol);
// Wait for graceful shutdown
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:
ServerHealthMonitor(IServerDispatcher* dispatcher)
: m_dispatcher(dispatcher) {
// Monitor server health every 30 seconds
m_timer.setInterval(30000);
connect(&m_timer, &QTimer::timeout, this, &ServerHealthMonitor::CheckHealth);
m_timer.start();
}
signals:
void serverUnhealthy(IServerConnectionInterface::ProtocolType protocol);
void serverRecovered(IServerConnectionInterface::ProtocolType protocol);
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)) {
// Transitioned from healthy to unhealthy
qWarning() << "Server became unhealthy:" << protocol;
emit serverUnhealthy(protocol);
// Attempt restart
m_dispatcher->StopServer(protocol);
if (m_dispatcher->StartServer(protocol)) {
qInfo() << "Server restart initiated:" << protocol;
}
} else if (healthy && !m_lastHealthy.value(protocol, false)) {
// Transitioned from unhealthy to healthy
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:

Status Queries:

Shutdown:

Best Practices

  1. Initialization Order: Configure all server components and their connection parameters before calling StartServer().
  2. Graceful Shutdown: Stop servers in reverse order of startup to ensure dependent connections are closed first (WebSocket before HTTP).
  3. Status Checking: Always check GetServerStatus() before attempting to stop a server to avoid unnecessary operations.
  4. Error Logging: Check StartServer() return value and log detailed errors for troubleshooting deployment issues.
  5. Health Monitoring: Implement periodic status checks to detect and recover from unexpected server failures.
  6. Reference Validation: Ensure HttpServer and WebSocketServer references are set before using the dispatcher.
  7. Wait for Startup: After StartServer(), wait for SS_RUNNING status before considering the server operational (servers may start asynchronously).

Current Limitations

Integration Points

Server Implementations (imtrest):

Configuration (imtcom):

Applications:

See also
IServerDispatcher, IServerConnectionInterface
imtrest::IServer, imtrest::CTcpServerComp, imtrest::CWebSocketServerComp

Definition at line 374 of file CServerDispatcherComp.h.