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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include <QTimer>
#include "UpdatedConfig.h"
UpdatedConfig* UpdatedConfig::self = NULL;
UpdatedConfig *UpdatedConfig::CurrentConfig()
{
if (self == NULL)
self = new UpdatedConfig();
return self;
}
UpdatedConfig::UpdatedConfig()
{
activeSrvIndex = 0;
time = 0;
retryFailed = false;
client = new SslClient(QString::fromStdString(servers[0].host));
connect(client, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)),
this, SLOT(gotServerReply(SslClient::RequestType&, QByteArray&)));
connect(client, SIGNAL(ConnectionError()),
this, SLOT(connectionError()));
configUpdateTimer = new QTimer;
configUpdateTimer->setInterval(servers[activeSrvIndex].retryTimeout * 1000);
connect(configUpdateTimer, SIGNAL(timeout()),
this, SLOT(update()));
configUpdateTimer->stop();
update();
}
void UpdatedConfig::update()
{
Logger::Trace("Going to update configuration\n");
if (retryFailed)
{
/* count retries and switch between server records */
time += servers[activeSrvIndex].retryTimeout;
Logger::Trace("Previous connection attempt failed or there were connection problems\n");
if (time >= servers[activeSrvIndex].timeout)
{
time = 0;
activeSrvIndex++;
if (activeSrvIndex == servers.size())
{
activeSrvIndex = 0;
}
}
retryFailed = false;
}
if (! (updateStatus & (1 << SslClient::Config)))
{
client->SendRequest(SslClient::Config);
}
if (! (updateStatus & (1 << SslClient::GenericProxyList)))
{
client->SendRequest(SslClient::GenericProxyList);
}
else if (! (updateStatus & (1 << SslClient::StaticProxyList)))
{
client->SendRequest(SslClient::StaticProxyList);
}
else if (! (updateStatus & (1 << SslClient::FirewallList)))
{
client->SendRequest(SslClient::FirewallList);
}
else if (! (updateStatus & (1 << SslClient::UploadList)))
{
client->SendRequest(SslClient::UploadList);
}
else if (! (updateStatus & (1 << SslClient::DeleteList)))
{
client->SendRequest(SslClient::DeleteList);
}
else
{
Logger::Warn("Unknown config update status: %x\n", updateStatus);
}
}
void UpdatedConfig::connectionError()
{
retryFailed = true;
Logger::Trace("Connection error. Starting reconnection timer\n");
/* start update timer (the case if working server is no longer responding) */
if (! configUpdateTimer->isActive())
{
time = 0;
configUpdateTimer->start();
}
}
void UpdatedConfig::gotServerReply(SslClient::RequestType &type, QByteArray &confdata)
{
Logger::Trace("Got server reply w type: %x\n", type);
if (confdata.size() == 0)
{
Logger::Warn("Empty server reply recieved");
goto end;
}
//Logger::Debug("data: %s\n", confdata.constData());
/* stop timer - working server found */
configUpdateTimer->stop();
switch (type)
{
case SslClient::Config:
ParseConfig(confdata.constData());
break;
case SslClient::GenericProxyList:
ParseGenericProxies(confdata.constData());
break;
case SslClient::StaticProxyList:
ParseStaticPorxies(confdata.constData());
break;
case SslClient::FirewallList:
ParseFirewalls(confdata.constData());
break;
case SslClient::UploadList:
Logger::Debug("UploadList: %s\n", confdata.constData());
break;
case SslClient::DeleteList:
Logger::Debug("DeleteList: %s\n", confdata.constData());
break;
default:
Logger::Warn("Unknown reply type: %x\n", type);
break;
}
updateStatus |= (1 << type);
end:
if (updateStatus != 0x7E)
{
Logger::Trace("Still need to update other config parts. Update status: %x\n", updateStatus);
update();
}
else
{
Logger::Info("Config successfully updated!\n");
client->Disconnect();
/* reset retry params and setup timer to fire on next planned update */
time = 0;
updateStatus = 0;
activeSrvIndex = 0;
retryFailed = false;
configUpdateTimer->stop();
configUpdateTimer->setInterval(updateInterval * 1000);
if (! configUpdateTimer->isActive())
{
configUpdateTimer->start();
}
emit updated();
}
}
|