Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "DapServiceClient.h"
DapServiceClient::DapServiceClient(QObject *apParent)
: QObject(apParent)
{
// Initialization of the service connection socket
m_pClientSocket = new DapUiSocket(this);
// Signal-slot connection broadcasting service connection error
connect(m_pClientSocket,static_cast<void(DapUiSocket::*)(DapUiSocketError)> (&DapUiSocket::error),
this, &DapServiceClient::handleSocketError);
// Signal-slot connection broadcasting signal of successful connection to the service
connect(m_pClientSocket,SIGNAL(connected()), this, SLOT(connectedToService()));
// Signal-slot connection transmitting a signal to disconnect from the service
connect(m_pClientSocket,SIGNAL(disconnected()), this, SLOT(disconnectFromService()));
// Signal-slot connection that reconnects to the service
connect(&m_reconnectTimer, SIGNAL(timeout()), this, SLOT(reconnectToService()));
}
/// Get a socket pointer.
/// @return A pointer to a socket.
DapUiSocket *DapServiceClient::getClientSocket() const
{
return m_pClientSocket;
}
/// Establish a connection with the service when the latter is launched.
void DapServiceClient::onServiceStarted()
{
qInfo() << "Service started.";
connectToService();
}
/// Handle event of successful connection to the service.
void DapServiceClient::connectedToService()
{
qInfo() << "Connected to the service";
m_launchAttemptCounter = 0;
m_isServiceConnected = true;
stopReconnectingToService();
emit sigConnected();
}
/// Start the process of reconnecting to the service.
void DapServiceClient::startReconnectingToService()
{
if(!m_reconnectTimer.isActive()) {
qInfo() << "Start trying to reconnect to service";
m_reconnectTimer.start(RECONNECT_TIMEOUT_MS);
}
}
/// Stop the process of reconnecting to the service.
void DapServiceClient::stopReconnectingToService()
{
if(m_reconnectTimer.isActive())
{
m_reconnectTimer.stop();
qInfo() << "Reconnect timer stopped";
}
}
/// Handle socket error.
/// @param aSocketEror Socket error code.
void DapServiceClient::handleSocketError(DapUiSocketError aSocketEror)
{
qDebug() << m_pClientSocket->errorString();
startReconnectingToService();
emit sigSocketError(aSocketEror);
emit sigSocketErrorString(m_pClientSocket->errorString());
}
/// Reconnect service.
void DapServiceClient::reconnectToService()
{
++m_launchAttemptCounter;
DapServiceError resultInit = DapServiceError::NO_ERRORS;
if(m_launchAttemptCounter == NUMBER_LAUNCH_ATTEMPTS)
{
qCritical() << "Server not running after `serviceStart` operation";
QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "Unable to start service", QMessageBox::Ok);
exit(-1);
}
else
{
resultInit = init();
}
if(resultInit == DapServiceError::NO_ERRORS)
{
connectToService();
}
}
/// Initiate the service monitor.
DapServiceError DapServiceClient::init()
{
DapServiceError result = DapServiceClientNative::init();
handleServiceError(result);
return result;
}
/// Handle service error.
/// @param aServiceEror Service error code.
void DapServiceClient::handleServiceError(DapServiceError aServiceEror)
{
switch (aServiceEror)
{
case DapServiceError::NO_ERRORS:
break;
case DapServiceError::USER_COMMAND_ABORT:
QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "User abort service comand", QMessageBox::Ok);
exit(-1);
case DapServiceError::UNABLE_START_SERVICE:
QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "Start the service with administrator rights", QMessageBox::Ok);
qCritical() << "Start the service with administrator rights";
break;
case DapServiceError::UNABLE_STOP_SERVICE:
qCritical() << "Can't stop service";
break;
case DapServiceError::UNKNOWN_ERROR:
qCritical() << "Got unknown error";
break;
case DapServiceError::SERVICE_NOT_FOUND:
qCritical() << "Service not found";
break;
}
if(aServiceEror != DapServiceError::NO_ERRORS)
{
emit sigServiceError(aServiceEror);
}
}
/// Establish a connection with the service.
void DapServiceClient::connectToService()
{
if(m_pClientSocket->state() == QAbstractSocket::ConnectedState)
return;
qInfo() << "with parametr: " << DAP_BRAND;
m_pClientSocket->connectToServer(DAP_BRAND);
}
/// Handle service outage.
void DapServiceClient::disconnectFromService()
{
m_isServiceConnected = true;
startReconnectingToService();
emit sigDisconnected();
}