summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Borisov <borisov.alexandr@rambler.ru>2011-11-22 01:32:14 +0200
committerAlex Borisov <borisov.alexandr@rambler.ru>2011-11-22 01:32:14 +0200
commit17d203d2fcb93673163cde0eda6e28b38007f910 (patch)
tree1016b09a1b6f22d65856987a7992db4c1ea46675
parente555b3102f7be659f53bc24e81bcba90d97aad81 (diff)
DOwnload/Delete file list request and parsing. FIX bug while reading other configs
-rw-r--r--client/Config.cpp141
-rw-r--r--client/Config.h63
-rw-r--r--client/ProxyClientApp.cpp4
-rw-r--r--client/SslClient.cpp4
-rw-r--r--client/SslClient.h2
-rw-r--r--client/UpdatedConfig.cpp10
6 files changed, 181 insertions, 43 deletions
diff --git a/client/Config.cpp b/client/Config.cpp
index a4a6c07..ba62ee0 100644
--- a/client/Config.cpp
+++ b/client/Config.cpp
@@ -11,7 +11,7 @@ using namespace std;
/*
- * Firewall Entry class
+ * FirewallEntry nested class section
*/
void Config::FirewallEntry::Parse(string entry)
{
@@ -19,7 +19,7 @@ void Config::FirewallEntry::Parse(string entry)
size_t lp1 = 0, lp2 = 0;
//extract action part
- lp2 = entry.find(" ", lp1);
+ lp2 = entry.find(' ', lp1);
string action = entry.substr(lp1, lp2-lp1);
if (action.compare("block") == 0)
{
@@ -34,14 +34,65 @@ void Config::FirewallEntry::Parse(string entry)
Logger::Error("Unknown firewall action! Skipping.\n");
return;
}
- //extract host part
+ //extract host part
lp1 = lp2 + 1;
host.clear();
host = entry.substr(lp1);
}
+
+/*
+ * FileEntry nested class section
+ */
+void Config::FileEntry::Parse(string entry, ActionType action)
+{
+ Logger::Trace("Parsing file action %s\n", entry.c_str());
+
+ size_t start = 0, end = 0;
+ if (action == DeleteAction)
+ {
+ start = 1;
+ end = entry.find('"', start);
+ path = entry.substr(start, end - 1);
+ }
+ else if (action == DownloadAction)
+ {
+ end = entry.find(' ', start);
+ path = entry.substr(start, end);
+ start = end + 1;
+ md5 = entry.substr(start);
+ }
+ else
+ {
+ Logger::Error("Unknown FileEntry action!\n");
+ }
+
+ Logger::Debug("path: %s, md5: %s\n", path.c_str(), md5.c_str());
+}
+
+
/*
- * Config class
+ * ServerEntry nested class section
+ */
+Config::ServerEntry::ServerEntry(string entry)
+{
+ ServerEntry();
+
+ /* 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);
+ start = end+1;
+ end = entry.find(' ');
+ timeout = atoi(entry.substr(start, end).c_str());
+ start = end+1;
+ end = entry.find(' ');
+ retryTimeout = atoi(entry.substr(start, end).c_str());
+}
+
+
+/*
+ * Config nested class section
*/
Config::Config(): StaticProxySpeedLow(50)
{
@@ -270,8 +321,6 @@ void Config::ParseGenericProxies(string data)
{
proxies.getline(str, str_size, ';');
proxies.ignore(2, '\n');
- if (proxies.eof())
- break;
string entry = str;
ProxyEntryGeneric proxy;
proxy.Parse(entry);
@@ -291,8 +340,6 @@ void Config::ParseStaticPorxies(string data)
{
proxies.getline(str, str_size, ';');
proxies.ignore(2, '\n');
- if (proxies.eof())
- break;
string entry = str;
ProxyEntryStatic proxy;
proxy.Parse(entry);
@@ -311,8 +358,6 @@ void Config::ParseFirewalls(string data)
{
rules.getline(str, str_size, ';');
rules.ignore(2, '\n');
- if (rules.eof())
- break;
string entry = str;
FirewallEntry rule;
rule.Parse(entry);
@@ -320,6 +365,58 @@ void Config::ParseFirewalls(string data)
}
}
+void Config::ParseDeleteList(string data)
+{
+ Logger::Debug("Delete list:\n%s", data.c_str());
+
+ //delete all entries with DeleteAction
+ for (unsigned i = 0; i < fileActions.size(); i++)
+ {
+ if (fileActions[i].action == FileEntry::DeleteAction)
+ {
+ fileActions.erase(fileActions.begin() + i);
+ }
+ }
+
+ stringstream files(data, ios_base::in);
+ const int str_size = 512;
+ char str[str_size] = {0};
+ while (! files.eof())
+ {
+ files.getline(str, str_size, ';');
+ files.ignore(2, '\n');
+ string entry = str;
+ FileEntry file;
+ file.Parse(entry, FileEntry::DeleteAction);
+ fileActions.push_back(file);
+ }
+}
+
+void Config::ParseDownloadList(string data)
+{
+ //delete all entries with DownloadAction
+ for (unsigned i = 0; i < fileActions.size(); i++)
+ {
+ if (fileActions[i].action == FileEntry::DownloadAction)
+ {
+ fileActions.erase(fileActions.begin() + i);
+ }
+ }
+
+ stringstream files(data, ios_base::in);
+ const int str_size = 512;
+ char str[str_size] = {0};
+ while (! files.eof())
+ {
+ files.getline(str, str_size, ';');
+ //there is no \n or \r\n
+ string entry = str;
+ FileEntry file;
+ file.Parse(entry, FileEntry::DownloadAction);
+ fileActions.push_back(file);
+ }
+}
+
/**
* @deprecated left for testing purposes, will be removed
@@ -374,27 +471,3 @@ void Config::ReadStaticProxy()
ParseStaticPorxies(proxies);
delete[] buffer;
}
-
-
-/***
- * Nested class section
- */
-Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retryTimeout(60)
-{
-}
-
-Config::ServerEntry::ServerEntry(string entry)
-{
- ServerEntry();
-
- /* 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);
- start = end+1;
- end = entry.find(' ');
- timeout = atoi(entry.substr(start, end).c_str());
- start = end+1;
- end = entry.find(' ');
- retryTimeout = atoi(entry.substr(start, end).c_str());
-}
diff --git a/client/Config.h b/client/Config.h
index bf75930..2392898 100644
--- a/client/Config.h
+++ b/client/Config.h
@@ -42,6 +42,54 @@ public:
};
/**
+ * @brief class that represent file actions that should be taken: update/delete
+ */
+ class FileEntry
+ {
+ public:
+ /**
+ * @enum possible file action types
+ */
+ enum ActionType
+ {
+ /**
+ * @brief unknown file action (default)
+ */
+ UnknownAction,
+ /**
+ * @brief delete file from filesystem (if it exists)
+ */
+ DeleteAction,
+ /**
+ * @brief download file to filesystem (if it isn't exists)
+ */
+ DownloadAction
+ };
+
+ FileEntry(): action(UnknownAction) {}
+
+ /**
+ * @brief action that should be taken
+ */
+ ActionType action;
+
+ /**
+ * @brief file path
+ */
+ string path;
+
+ /**
+ * @brief md5hash value for this file ('upload' action only)
+ */
+ string md5;
+
+ /**
+ * @brief Extract and set instance variables from config line
+ */
+ void Parse(string entry, ActionType action);
+ };
+
+ /**
* @brief Time between consecutive program updates
*/
unsigned ClientUpdateInterval;
@@ -167,7 +215,7 @@ protected:
/**
* @brief Initialize ServerEntry instance to default values
*/
- ServerEntry();
+ ServerEntry(): host("127.0.0.1"), timeout(120), retryTimeout(60) {}
/**
* @brief initilize ServerEntry instance from config file entry<br/>
@@ -224,6 +272,18 @@ protected:
void ParseFirewalls(string data);
/**
+ * @brief parse list of file to be deleted
+ * @param data string containing path to aforementioned file
+ */
+ void ParseDeleteList(string data);
+
+ /**
+ * @brief parse list of files to be downloaded
+ * @param data string containing path to new file and it's md5 hash
+ */
+ void ParseDownloadList(string data);
+
+ /**
* @brief time between subsequent config updates
*/
unsigned updateInterval;
@@ -232,6 +292,7 @@ private:
vector<ProxyEntryGeneric> genericProxy;
vector<ProxyEntryStatic> staticProxy;
vector<FirewallEntry> firewalls;
+ vector<FileEntry> fileActions;
void ReadGenericProxy();
void ReadStaticProxy();
};
diff --git a/client/ProxyClientApp.cpp b/client/ProxyClientApp.cpp
index e132d53..4b233e5 100644
--- a/client/ProxyClientApp.cpp
+++ b/client/ProxyClientApp.cpp
@@ -92,6 +92,10 @@ void ProxyClientApp::configUpdated()
updatedMsg.setStandardButtons(QMessageBox::Ok);
updatedMsg.setIcon(QMessageBox::Information);
updatedMsg.exec();
+
+ /* delete files */
+
+ /* download files */
}
void ProxyClientApp::quitApp()
diff --git a/client/SslClient.cpp b/client/SslClient.cpp
index 9f17449..b3a2d15 100644
--- a/client/SslClient.cpp
+++ b/client/SslClient.cpp
@@ -86,7 +86,7 @@ void SslClient::SendRequest(RequestType type)
case GenericProxyList:
case StaticProxyList:
case FirewallList:
- case UploadList:
+ case DownloadList:
case DeleteList:
rcode = type;
break;
@@ -145,7 +145,7 @@ void SslClient::DataRecieved()
case GenericProxyList:
case StaticProxyList:
case FirewallList:
- case UploadList:
+ case DownloadList:
case DeleteList:
type = (RequestType)rcode;
break;
diff --git a/client/SslClient.h b/client/SslClient.h
index 77af070..8172bad 100644
--- a/client/SslClient.h
+++ b/client/SslClient.h
@@ -66,7 +66,7 @@ public:
/**
* @brief Request list of files that should exist on client PC
*/
- UploadList = 0x05,
+ DownloadList = 0x05,
/**
* @brief Request list of files to be deleted on client PC
*/
diff --git a/client/UpdatedConfig.cpp b/client/UpdatedConfig.cpp
index e999d7e..6c69f12 100644
--- a/client/UpdatedConfig.cpp
+++ b/client/UpdatedConfig.cpp
@@ -70,9 +70,9 @@ void UpdatedConfig::update()
{
client->SendRequest(SslClient::FirewallList);
}
- else if (! (updateStatus & (1 << SslClient::UploadList)))
+ else if (! (updateStatus & (1 << SslClient::DownloadList)))
{
- client->SendRequest(SslClient::UploadList);
+ client->SendRequest(SslClient::DownloadList);
}
else if (! (updateStatus & (1 << SslClient::DeleteList)))
{
@@ -125,11 +125,11 @@ void UpdatedConfig::gotServerReply(SslClient::RequestType &type, QByteArray &con
case SslClient::FirewallList:
ParseFirewalls(confdata.constData());
break;
- case SslClient::UploadList:
- Logger::Debug("UploadList: %s\n", confdata.constData());
+ case SslClient::DownloadList:
+ ParseDownloadList(confdata.constData());
break;
case SslClient::DeleteList:
- Logger::Debug("DeleteList: %s\n", confdata.constData());
+ ParseDeleteList(confdata.constData());
break;
default:
Logger::Warn("Unknown reply type: %x\n", type);