#include #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(""); } return servers[activeSrvIndex].host; } 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::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"); 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; } } 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: ParseStaticPorxies(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; } updateStatus |= (1 << type); end: 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 */ time = 0; updateStatus = 0; activeSrvIndex = 0; retryFailed = false; configUpdateTimer->stop(); configUpdateTimer->setInterval(updateInterval * 1000); if (! configUpdateTimer->isActive()) { configUpdateTimer->start(); } emit updated(); } }