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/Config.cpp | |
parent | fe4520fc09dca52c158437f5621f5909d7d6974f (diff) |
Self-updated config. Various fixes and improvements. TODO: test client/server communication
Diffstat (limited to 'client/Config.cpp')
-rwxr-xr-x | client/Config.cpp | 277 |
1 files changed, 146 insertions, 131 deletions
diff --git a/client/Config.cpp b/client/Config.cpp index acb3684..79a4aae 100755 --- a/client/Config.cpp +++ b/client/Config.cpp @@ -9,117 +9,38 @@ using namespace std; -Config* Config::self = NULL; -Config *Config::CurrentConfig() -{ - if (self == NULL) - self = new Config(); - return self; -} - -Config::Config(): QObject(), StaticProxySpeedLow(50) -{ - configValid = false; - +Config::Config(): StaticProxySpeedLow(50) +{ Logger::Info("Parsing config.cfg to determine initial configuration\n"); - ifstream configFile(QString(this_app->applicationDirPath()+ "/config.cfg").toLocal8Bit().data(), std::ios::in); + QString configPath = this_app->applicationDirPath()+ "/config.cfg"; + ifstream configFile(configPath.toLocal8Bit().data(), std::ios::in); if (!configFile) { Logger::Fatal("Can't open file: config.cfg\n"); return; } + string config = ""; const int str_size = 512; char str[str_size] = {0}; while (!configFile.eof()) { - configFile.getline(str, str_size, ';'); - configFile.ignore(2, '\n'); - if (configFile.eof()) - break; - string entry = str; - size_t eqpos = entry.find('='); - if (eqpos == string::npos) - { - Logger::Warn("No '=' at the config line: %s\n", entry.c_str()); - continue; - } - string key = entry.substr(0, eqpos); - string value = entry.substr(eqpos+1); - Logger::Trace("Found option: %s = %s\n", key.c_str(), value.c_str()); - if (key.compare("config_update_interval") == 0) - { - updateInterval = atoi(value.c_str()); - } - else if (key.compare("client_update_interval") == 0) - { - ClientUpdateInterval = atoi(value.c_str()); - } - else if (key.compare("server") == 0) - { - ServerEntry server(value); - servers.push_back(server); - } - else if (key.compare("welcome_msg") == 0) - { - WelcomeMsg = value; - } - else if (key.compare("config_downloaded_msg") == 0) - { - ConfigLoadedMsg = value; - } - else if (key.compare("top_panel_text") == 0) - { - TopPanelText = value; - } - else if (key.compare("bottom_panel_text") == 0) - { - BottomPanelText = value; - } - else if (key.compare("speed_visibility") == 0) - { - IsSpeedVisible = false; - if (value.compare("1") == 0) - { - IsSpeedVisible = true; - } - } - else - { - Logger::Warn("Unrecognized config option: %s\n", key.c_str()); - } + configFile.read(str, str_size); + config += str; } - - configUpdateTimer = new QTimer; - configUpdateTimer->setInterval(updateInterval * 1000); - connect(configUpdateTimer, SIGNAL(timeout()), - this, SLOT(updateConfig())); - - client = new SslClient(QString::fromStdString(servers[0].host)); - connect(client, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)), - this, SLOT(gotServerReply(SslClient::RequestType&, QByteArray&))); - client->SendRequest(SslClient::Config); + ParseConfig(str); } void Config::AcquireConfig() { /* reset existing values */ - configValid = false; genericProxy.clear(); staticProxy.clear(); /* read new values */ - if (ReadGenericProxy()) - return ; - if (ReadStaticProxy()) - return ; - configValid = true; -} - -bool Config::IsConfigValid() -{ - return configValid; + ReadGenericProxy(); + ReadStaticProxy(); } vector<string> Config::GetCountries() @@ -233,86 +154,180 @@ unsigned Config::GetStaticProxyGuiLines() return maxLine; } -int Config::ReadGenericProxy() + +void Config::ParseConfig(string data) +{ + stringstream config(data, ios_base::in); + + const int str_size = 512; + char str[str_size] = {0}; + while (! config.eof()) + { + config.getline(str, str_size, ';'); + config.ignore(2, '\n'); + if (config.eof()) + break; + string entry = str; + size_t eqpos = entry.find('='); + if (eqpos == string::npos) + { + Logger::Warn("No '=' at the config line: %s\n", entry.c_str()); + continue; + } + string key = entry.substr(0, eqpos); + string value = entry.substr(eqpos+1); + Logger::Trace("Found option: %s = %s\n", key.c_str(), value.c_str()); + if (key.compare("config_update_interval") == 0) + { + updateInterval = atoi(value.c_str()); + } + else if (key.compare("client_update_interval") == 0) + { + ClientUpdateInterval = atoi(value.c_str()); + } + else if (key.compare("server") == 0) + { + ServerEntry server(value); + servers.push_back(server); + } + else if (key.compare("welcome_msg") == 0) + { + WelcomeMsg = value; + } + else if (key.compare("config_downloaded_msg") == 0) + { + ConfigLoadedMsg = value; + } + else if (key.compare("top_panel_text") == 0) + { + TopPanelText = value; + } + else if (key.compare("bottom_panel_text") == 0) + { + BottomPanelText = value; + } + else if (key.compare("speed_visibility") == 0) + { + IsSpeedVisible = false; + if (value.compare("1") == 0) + { + IsSpeedVisible = true; + } + } + else + { + Logger::Warn("Unrecognized config option: %s\n", key.c_str()); + } + } +} + +void Config::ParseGenericProxies(string data) +{ + stringstream proxies(data, ios_base::in); + + const int str_size = 512; + char str[str_size] = {0}; + while (! proxies.eof()) + { + proxies.getline(str, str_size, ';'); + proxies.ignore(2, '\n'); + if (proxies.eof()) + break; + string entry = str; + ProxyEntryGeneric *proxy = new ProxyEntryGeneric; + proxy->Parse(entry); + genericProxy.push_back(*proxy); + } +} + +void Config::ParseStaticPorxies(string data) +{ + stringstream proxies(data, ios_base::in); + + const int str_size = 512; + char str[str_size] = {0}; + while (! proxies.eof()) + { + proxies.getline(str, str_size, ';'); + proxies.ignore(2, '\n'); + if (proxies.eof()) + break; + string entry = str; + ProxyEntryStatic *proxy = new ProxyEntryStatic; + proxy->Parse(entry); + staticProxy.push_back(*proxy); + } +} + +void Config::ParseFirewalls(string data) +{ + +} + + +/** + * @todo remove Config::ReadGenericProxy method + */ +void Config::ReadGenericProxy() { Logger::Info("Parsing generic proxy list\n"); string filname = QCoreApplication::applicationDirPath().toStdString() + "/config/proxy_list.cfg"; ifstream proxyFile(filname.c_str(), std::ios::in); - if (!proxyFile) + if (! proxyFile) { Logger::Error("Can't open file %s\n", filname.c_str()); - return -1; + return; } - + + string proxies = ""; const int str_size = 512; char str[str_size] = {0}; while (!proxyFile.eof()) { - proxyFile.getline(str, str_size, ';'); - proxyFile.ignore(2, '\n'); - if (proxyFile.eof()) - break; - string entry = str; - ProxyEntryGeneric *proxy = new ProxyEntryGeneric; - proxy->Parse(entry); - genericProxy.push_back(*proxy); + proxyFile.read(str, str_size); + proxies += str; } proxyFile.close(); - return 0; + + ParseGenericProxies(proxies); } -int Config::ReadStaticProxy() +/** + * @todo remove Config::ReadStaticProxy method + */ +void Config::ReadStaticProxy() { Logger::Info("Parsing static proxy list\n"); string filename = QCoreApplication::applicationDirPath().toStdString() + "/config/static_proxy_list.cfg"; ifstream proxyFile(filename.c_str(), std::ios::in); - if (!proxyFile) + if (! proxyFile) { Logger::Error("Can't open file %s\n", filename.c_str()); - return -1; + return; } + string proxies = ""; const int str_size = 512; char str[str_size] = {0}; while (!proxyFile.eof()) { - proxyFile.getline(str, str_size, ';'); - proxyFile.ignore(2, '\n'); - if (proxyFile.eof()) - break; - string entry = str; - ProxyEntryStatic *proxy = new ProxyEntryStatic; - proxy->Parse(entry); - staticProxy.push_back(*proxy); + proxyFile.read(str, str_size); + proxies += str; } proxyFile.close(); - return 0; -} - -void Config::updateConfig() -{ - Logger::Trace("Going to update entire configuration\n"); - /** - * @todo Count retries and switch between server records - */ -} - -void Config::gotServerReply(SslClient::RequestType &type, QByteArray &confdata) -{ - Logger::Trace("Got server reply w type: %x\n", type); - Logger::Trace("%s\n", confdata.data()); + ParseStaticPorxies(proxies); } /*** * Nested class section */ -Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retry(60) +Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retryTimeout(60) { } @@ -320,7 +335,7 @@ Config::ServerEntry::ServerEntry(string entry) { ServerEntry(); - /* processing string: "8.8.8.8 600 60" */ + /* processing server entry e.g.: "8.8.8.8 600 60" */ size_t start = 0, end = 0; end = entry.find(' '); host = entry.substr(start, end); @@ -329,5 +344,5 @@ Config::ServerEntry::ServerEntry(string entry) timeout = atoi(entry.substr(start, end).c_str()); start = end+1; end = entry.find(' '); - retry = atoi(entry.substr(start, end).c_str()); + retryTimeout = atoi(entry.substr(start, end).c_str()); } |