summaryrefslogtreecommitdiff
path: root/client/UpdatedConfig.cpp
blob: fe04fd9458ee7737d66d07bb10e0f35def2e0663 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright © 2010-2012 b0ris
//.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <QTimer>
#include "UpdatedConfig.h"


UpdatedConfig* UpdatedConfig::self = NULL;

UpdatedConfig* UpdatedConfig::CurrentConfig()
{
	if (self == NULL)
		self = new UpdatedConfig();
	return self;
}

string UpdatedConfig::GetServerAddr()
{
	if (! configValid)
	{
		Logger::Error("No valid server records present!\n");
		return string("");
	}
	string ret = servers[activeSrvIndex].host;
	return ret;
}

UpdatedConfig::UpdatedConfig()
{
	activeSrvIndex = 0;
	time = 0;
	retryFailed = false;
	updateStatus = 0;
	
	if (servers.size() == 0)
	{
		Logger::Error("No server records present. Can't update configuration!\n");
		return;
	}
	
	client = new SslClient(QString::fromLocal8Bit(servers[0].host.c_str()));
	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");
	configValid = false;
	
	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;
			}
			ServerEntry current = servers[activeSrvIndex];
			client->SetServerAddr(QString::fromLocal8Bit(current.host.c_str()));
			configUpdateTimer->stop();
			configUpdateTimer->setInterval(current.retryTimeout * 1000);
			configUpdateTimer->start();
		}
		retryFailed = false;
	}
	
	if (! (updateStatus & (1 << SslClient::Config)))
	{
		client->SendRequest(SslClient::Config);
	}
	else 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::DownloadList)))
	{
		client->SendRequest(SslClient::DownloadList);
	}
	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\n");
		goto end;
	}
	
	//Logger::Debug("data: %s\n", confdata.constData());
	
	/* stop timer - working server found */
	configUpdateTimer->stop();
	
	switch (type)
	{
		case SslClient::Config:
			ParseConfig(confdata.constData());
			Logger::Debug("Config data:\n %s", confdata.constData());
			break;
		case SslClient::GenericProxyList:
			ParseGenericProxies(confdata.constData());
			break;
		case SslClient::StaticProxyList:
			ParseStaticProxies(confdata.constData());
			break;
		case SslClient::FirewallList:
			ParseFirewalls(confdata.constData());
			break;
		case SslClient::DownloadList:
			ParseDownloadList(confdata.constData());
			break;
		case SslClient::DeleteList:
			ParseDeleteList(confdata.constData());
			break;
		default:
			Logger::Warn("Unknown reply type: %x\n", type);
			break;
	}
	
end:
	updateStatus |= (1 << type);
	
	if (updateStatus != UPDATED_STATE_FULL)
	{
		Logger::Trace("Still need to update other config parts. Update status: %x\n", updateStatus);
		update();
	}
	else
	{
		Logger::Info("Config successfully updated!\n");
		client->Disconnect();
		configValid = true;
		
		// reset retry params and setup timer to fire on next planned update
		// but leave activeSrvIndex as it is, so we will reconnect to the same working serv next time
		time = 0;
		updateStatus = 0;
		retryFailed = false;
		
		configUpdateTimer->stop();
		configUpdateTimer->setInterval(updateInterval * 1000);
		if (! configUpdateTimer->isActive())
		{
			configUpdateTimer->start();
		}
		emit updated();
	}
}