diff options
author | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-11-11 00:42:50 +0200 |
---|---|---|
committer | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-11-11 00:42:50 +0200 |
commit | d9a7ccf4eb0fe6d44cf1a7361b331889a98ff85b (patch) | |
tree | a01b1a8265601788d2e4b3003940bc2f33669755 /client/UpdatedConfig.cpp | |
parent | fe4520fc09dca52c158437f5621f5909d7d6974f (diff) |
Self-updated config. Various fixes and improvements. TODO: test client/server communication
Diffstat (limited to 'client/UpdatedConfig.cpp')
-rw-r--r-- | client/UpdatedConfig.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/client/UpdatedConfig.cpp b/client/UpdatedConfig.cpp new file mode 100644 index 0000000..a08d417 --- /dev/null +++ b/client/UpdatedConfig.cpp @@ -0,0 +1,134 @@ + +#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 & SslClient::Config)) + { + client->SendRequest(SslClient::Config); + } + else*/ + if (! (updateStatus & SslClient::GenericProxyList)) + { + client->SendRequest(SslClient::GenericProxyList); + } + else if (! (updateStatus & SslClient::StaticProxyList)) + { + client->SendRequest(SslClient::StaticProxyList); + } + else if (! (updateStatus & SslClient::FirewallList)) + { + client->SendRequest(SslClient::FirewallList); + } + 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; + } + + /* stop timer - working server found */ + configUpdateTimer->stop(); + + switch (type) + { + case SslClient::Config: + ParseConfig(confdata.constData()); + updateStatus |= type; + break; + case SslClient::GenericProxyList: + ParseGenericProxies(confdata.constData()); + updateStatus |= type; + break; + case SslClient::StaticProxyList: + ParseStaticPorxies(confdata.constData()); + updateStatus |= type; + break; + case SslClient::FirewallList: + ParseFirewalls(confdata.constData()); + updateStatus |= type; + break; + default: + Logger::Warn("Unknown reply type: %x\n", type); + break; + } + +end: + if ((updateStatus & 0x0F) != 0x0F) + { + Logger::Trace("Still need to update other config parts\n"); + update(); + } +}
\ No newline at end of file |