From 68dd02cc19e6c6f0294020e974098b6c564bee98 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lais=20Frig=C3=A9rio?= <lfs@inf.ufpr.br>
Date: Fri, 13 Jan 2017 11:06:04 -0200
Subject: [PATCH] Updating the main.cpp file and including the datasid .cpp
 files

---
 src/linux/datasid-agent.cpp  | 113 ++++++++++++++++++++
 src/linux/datasid-common.cpp |  63 +++++++++++
 src/linux/datasid-conf.cpp   | 156 +++++++++++++++++++++++++++
 src/linux/datasid-parse.cpp  | 202 +++++++++++++++++++++++++++++++++++
 src/linux/datasid-proxy.cpp  |  96 +++++++++++++++++
 src/main.cpp                 |   2 +
 6 files changed, 632 insertions(+)
 create mode 100644 src/linux/datasid-agent.cpp
 create mode 100644 src/linux/datasid-common.cpp
 create mode 100644 src/linux/datasid-conf.cpp
 create mode 100644 src/linux/datasid-parse.cpp
 create mode 100644 src/linux/datasid-proxy.cpp

diff --git a/src/linux/datasid-agent.cpp b/src/linux/datasid-agent.cpp
new file mode 100644
index 00000000..3d71b94e
--- /dev/null
+++ b/src/linux/datasid-agent.cpp
@@ -0,0 +1,113 @@
+/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of datasid
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+#include <string>
+#include "../../include/agent/main.h"
+
+/*
+ * set Functions
+ */
+
+void Agent::setConfDir(std::string dir) {
+    this->confDir = this->prefix + dir;
+}
+
+void Agent::setDefaultConf(std::string defaultConf) {
+    this->defaultConf = this->prefix + defaultConf;
+}
+
+void Agent::setLogFile(std::string logDir, std::string file) {
+    this->logFile = logDir + file;
+}
+
+void Agent::setPrefix(std::string p, int t) {
+    this->prefix = getAbsolutePath(p, t);
+}
+
+void Agent::setVersion(std::string v) {
+    this->version = v;
+}
+
+/*
+ * get Functions
+ */
+
+std::string Agent::getConfDir() const {
+    return this->confDir;
+}
+
+std::string Agent::getDefaultConf() const {
+    return  this->defaultConf;
+}
+
+std::string Agent::getLogFile() const {
+    return this->logFile;
+}
+
+std::string Agent::getPrefix() const {
+    return this->prefix;
+}
+
+std::string Agent::getVersion() const {
+    return this->version;
+}
+
+/*
+ * getAbsolutePath Function:
+ * get the absolute path of the agent
+ */
+
+std::string getAbsolutePath(std::string str, int tot) {
+    int i = str.size(), count = 0;
+        while (i-->-1)
+        if (str[i] == '/')
+            if (++count == tot)
+                break;
+    return str.substr(0, i);
+}
+
+/*
+ * prefixIsSet Function
+ * Check if the absolute path of agent was setted.
+ */
+
+bool prefixIsSet(std::string prefix) {
+    if (prefix.size() == 0)
+        throw " ERROR: Prefix not set. ";
+    return true;
+}
+
+int datasid_agent() {
+    Agent agent;
+    Conf conf;
+    Proxy proxy;
+
+    /* Set agent prefix */
+    agent.setPrefix(__FILE__, 3);
+
+    /* Set confif file */
+    agent.setDefaultConf("/conf/datasid-conf.json");
+
+    /* Run datasid-common.cpp */
+    if (!datasid_common(&conf, &proxy, &agent))
+        exit(1);
+
+    return 1;
+}
diff --git a/src/linux/datasid-common.cpp b/src/linux/datasid-common.cpp
new file mode 100644
index 00000000..bf89b085
--- /dev/null
+++ b/src/linux/datasid-common.cpp
@@ -0,0 +1,63 @@
+/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of datasid
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+#include "../../include/agent/linux/datasid-common.h"
+
+/*
+ * fileExist Function
+ * verify if a file exist
+ */
+
+bool fileExist(const std::string& file) {
+    std::ifstream myFile(file.c_str(), std::ifstream::in);
+    if (myFile.is_open()) {
+        myFile.close();
+        return true;
+    }
+    myFile.close();
+    return false;
+}
+
+
+bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent) {
+    try {
+        prefixIsSet(agent->getPrefix());
+    } catch (const char* msg) {
+        std::cout << __DATE__ << msg << std::endl;
+        return false;
+    }
+
+    /* Read config file and declare only valid variables */
+
+    if (!fileExist(agent->getDefaultConf())) {
+        std::cout << __DATE__ << "- ERROR: Config file not found. " << std::endl;
+        return false;
+    } else
+       datasid_conf(agent, conf);
+
+    /* Read proxy file and declare only valid variables */
+
+    if (!fileExist(conf->getProxyConf()))
+        std::cout << __DATE__ << "- WARNING: Proxy file not found. " << std::endl;
+    else
+        datasid_proxy(conf, proxy);
+
+    return true;
+}
diff --git a/src/linux/datasid-conf.cpp b/src/linux/datasid-conf.cpp
new file mode 100644
index 00000000..99f967bc
--- /dev/null
+++ b/src/linux/datasid-conf.cpp
@@ -0,0 +1,156 @@
+/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of datasid
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <locale> 
+#include <sstream>
+#include "../../include/agent/linux/datasid-conf.h"
+/*
+ * set Functions
+ */
+
+void Conf::setDataDir(std::string data) {
+    this->dataDir = data;
+}
+
+void Conf::setLogDir(std::string log) {
+    this->logDir = log;
+}
+
+void Conf::setProxyConf(std::string p) {
+    this->proxyConf = p;
+}
+
+void Conf::setWebService(std::string web) {
+    this->webService = web;
+}
+
+void Conf::setDelay(int d) {
+    this->delay = d;
+}
+
+void Conf::setDaysToExpireLog(int d) {
+    this->daysToExpireLog = d;
+}
+
+void Conf::setUpdateTries(int u) {
+    this->updateTries = u;
+}
+
+void Conf::setUpdatTimeOut(int u) {
+    this->updateTimeOut = u;
+}
+
+/*
+ * get Functions
+ */
+
+std::string Conf::getDataDir() const {
+    return  this->dataDir;
+}
+
+std::string Conf::getLogDir() const {
+    return this->logDir;
+}
+
+std::string Conf::getProxyConf() const {
+    return this->proxyConf;
+}
+
+std::string Conf::getWebService() const {
+    return this->webService;
+}
+
+int Conf::getDelay() const {
+    return this->delay;
+}
+
+int Conf::getDaysToExpireLog() const {
+    return this->daysToExpireLog;
+}
+
+int Conf::getUpdateTries() const {
+    return this->updateTries;
+}
+
+int Conf::getUpdateTimeOut() const {
+    return this->updateTimeOut;
+}
+
+/*
+ * setConf Function
+ * If variables from config file setted,
+ * then set the conf object
+ */
+
+bool setConf(Conf* conf, Agent* agent, std::map<std::string, std::string>* content) {
+    if (content->count("DATADIR") == 0 ||
+        content->count("LOGDIR") == 0 ||
+        content->count("PROXYCONF") == 0 ||
+        content->count("WEBSERVICE") == 0||
+        content->count("DELAY") == 0 ||
+        content->count("DAYSTOEXPIRELOG") == 0 ||
+        content->count("UPDATETRIES") == 0 ||
+        content->count("UPDATETIMEOUT") == 0 )
+            return false;
+
+    /* 
+     * agent.getPrefix() contains the absolute path of the agent. 
+     */
+
+    std::string dataDir = agent->getPrefix() + (*content)["DATADIR"];
+    std::string dataLog = agent->getPrefix() + (*content)["LOGDIR"];
+    std::string proxyConf = agent->getPrefix() + (*content)["PROXYCONF"];
+    std::string webService = (*content)["WEBSERVICE"];
+
+    int delay = atoi((*content)["DELAY"].c_str());
+    int daysToExpireLog = atoi((*content)["DAYSTOEXPIRELOG"].c_str());
+    int updateTries = atoi((*content)["UPDATETRIES"].c_str());
+    int updateTimeOut = atoi((*content)["UPDATETIMEOUT"].c_str());
+
+    conf->setDataDir(dataDir);
+    conf->setLogDir(dataLog);
+    conf->setProxyConf(proxyConf);
+    conf->setWebService(webService);
+
+    conf->setDelay(delay);
+    conf->setDaysToExpireLog(daysToExpireLog);
+    conf->setUpdateTries(updateTries);
+    conf->setUpdatTimeOut(updateTimeOut);
+
+    return true;
+}
+
+void datasid_conf(Agent* agent, Conf* conf) {
+    std::map<std::string, std::string> content;
+
+    if (readFile(agent->getDefaultConf(), &content)) {
+        if (!setConf(conf, agent, &content)) {
+            std::cout << __DATE__ << " ERROR: to parse variables " <<
+            "from config file." << std::endl;
+            exit(1);
+        }
+    } else {
+        std::cout << __DATE__ << " ERROR: config file not found." << std::endl;
+        exit(1);
+    }
+}
diff --git a/src/linux/datasid-parse.cpp b/src/linux/datasid-parse.cpp
new file mode 100644
index 00000000..86fd337e
--- /dev/null
+++ b/src/linux/datasid-parse.cpp
@@ -0,0 +1,202 @@
+/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of datasid
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+#include <regex>
+#include "../../include/agent/linux/datasid-parse.h"
+
+/* 
+ * trim Function
+ * Erases leading tabs, leading spaces; trailing tabs, trailing spaces. 
+ */
+
+void trim(std::string* str) {
+    int i = 0;
+
+    while (i < static_cast<int>(str->length()) && ((*str)[i] == ' '
+        || (*str)[i] == '\t'))
+        ++i;
+
+    if (i > 0)
+        str->erase(0, i);
+
+    int j = i = str->length();
+
+    while (i > 0 && ((*str)[i - 1] == ' ' || (*str)[i - 1] == '\t'))
+        --i;
+
+    if (i < j)
+        str->erase(i, j);
+}
+
+/* 
+ * normalize Function
+ * Erases tabs and spaces between the variable's name and its value. 
+ */
+
+void normalize(std::string* str) {
+    size_t i, j;
+    i = j = 0;
+    
+    trim(str);
+
+    while (i < str->length()) {
+
+        if ((*str)[i] == ' ' || (*str)[i] == '\t') {
+            j = i + 1;
+
+            while (j < str->length() && ((*str)[j] == ' ' || (*str)[j] == '\t'))
+                ++j;
+
+            if (j == i)
+                str->erase(i, 1);
+            else
+                str->erase(i, j - i);
+        } else
+            ++i;
+    }
+}
+
+/* 
+ * spaceOnly Functio
+ * Check if a line consists only of spaces and tabs 
+ */
+
+bool spaceOnly(const std::string* line) {
+    for (int i = 0, j = line->length(); i < j; ++i)
+        if ((*line)[i] != ' ' && (*line)[i] != '\t')
+            return false;
+    return true;
+}
+
+/* 
+ * isValid Function
+ * Check if a line is valid
+ * The line is valide when has "key":"value"; 
+ */
+
+bool isValid(std::string* line) {
+    std::string e = *line;
+    int i = 0;
+
+    normalize(line);
+
+    /* if the line is a comment */
+
+    if (std::regex_match((*line), std::regex("((\\/\\/)|(\\/\\*)|(\\#))(.*)")))
+        return false;
+
+    /* if the line start with a different character than quote */
+
+    if (!std::regex_match((*line), std::regex("(\")(.*)")))
+        return false;
+
+    /* 
+     * if the line start with quote, 
+     * but is not a pattern : "key":"value" 
+     */
+
+    if ((*line)[i] = '"') {
+
+        int n = std::count((*line).begin(), (*line).end(), '"');
+
+        /* if the line doesn't has four quotes */
+        if (n < 4)
+            return false;
+
+        /* if the second character is quote. */
+
+        if ((*line)[i+1] == '"')
+            return false;
+
+        n = line->find_last_of('"');
+
+        /* if the second to last character is quote. */
+
+        if ((*line)[n-1] == '"')
+            return false;
+
+        n = line->find_last_of(':');
+
+        /* if the line doesn't has the colon character. */
+
+        if (n == -1)
+            return false;
+
+        /* if the position of the colon character is string ends. */
+
+        if (n == (line->length())-1)
+            return false;
+        
+    }
+
+    return true;
+}
+
+/*
+ * parse Function
+ * After check if the line is valid,
+ * than parse the "key":"value" to a map.
+ */
+
+void parse(std::string* line, std::map<std::string, std::string>* content) {
+    std::locale loc;
+
+    /* if the line is a "key":"value" */
+
+    if ((*line)[0] == '"') {
+        std::string name = line->substr(1, line->find(':')-2);
+
+        for (int i = 0; i < name.length(); i++)
+            name[i] = toupper(name[i], loc);
+
+        std::string value = line->substr(line->find(':')+2);
+        value = value.substr(0, value.find('"'));
+
+        (*content)[name] = value;
+    }
+}
+
+/*
+ * readFile Function
+ * read a file (config or proxy file)
+ * Than check if the line is valid.
+ * Case is valid, parse the "key":"value" to a map.
+ * After all, get de value by key and "initialize"
+ * a conf or proxy object.
+ */
+
+bool readFile(const std::string file, std::map<std::string, std::string>* content) {
+    content->clear();
+
+    std::ifstream config(file.c_str(), std::ifstream::in);
+
+    if (!config.is_open())
+        return false;
+
+    std::string buffer;
+
+    while (getline(config, buffer, '\n')) {
+        if (!spaceOnly(&buffer))
+            if (isValid(&buffer))
+                parse(&buffer, content);
+    }
+
+    return true;
+}
diff --git a/src/linux/datasid-proxy.cpp b/src/linux/datasid-proxy.cpp
new file mode 100644
index 00000000..1e78cf9f
--- /dev/null
+++ b/src/linux/datasid-proxy.cpp
@@ -0,0 +1,96 @@
+/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of datasid
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+#include <iostream>
+#include "../../include/agent/linux/datasid-proxy.h"
+
+/*
+ * set Functions
+ */
+
+void Proxy::setHost(std::string h) {
+    this->host = h;
+}
+
+void Proxy::setPort(std::string p) {
+    this->port = p;
+}
+
+void Proxy::setUser(std::string u) {
+    this->user = u;
+}
+
+void Proxy::setPassword(std::string p) {
+    this->password = p;
+}
+
+/*
+ * get Functions
+ */
+
+std::string Proxy::getHost() const {
+    return this->host;
+}
+
+std::string Proxy::getPort() const {
+    return this->port;
+}
+
+std::string Proxy::getUser() const {
+    return this->user;
+}
+
+std::string Proxy::getPassword() const {
+    return this->password;
+}
+
+/*
+ * setProxy Function
+ * If variables from proxy file setted,
+ * then set the proxy object
+ */
+
+bool setProxy(Proxy* proxy, std::map<std::string, std::string>* content) {
+    if (content->count("PROXYHOST") == 0 ||
+        content->count("PROXYPORT") == 0 ||
+        content->count("PROXYUSER") == 0 ||
+        content->count("PROXYPASSWORD") == 0)
+            return false;
+
+    proxy->setHost((*content)["PROXYHOST"]);
+    proxy->setPort((*content)["PROXYPORT"]);
+    proxy->setUser((*content)["PROXYUSER"]);
+    proxy->setPassword((*content)["PROXYPASSWORD"]);
+
+    return true;
+}
+
+void datasid_proxy(Conf* conf, Proxy* proxy) {
+    std::map<std::string, std::string> content;
+
+    if (readFile(conf->getProxyConf(), &content)) {
+        if (!setProxy(proxy, &content)) {
+            std::cout << __DATE__ << " ERROR: to parse variables" <<
+            " from proxy file." << std::endl;
+            exit(1);
+        }
+    } else
+        std::cout << __DATE__ << " WARNING: proxy file not found." << std::endl;
+}
diff --git a/src/main.cpp b/src/main.cpp
index 3bdd5da8..3248d2e0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,5 +31,7 @@ int main() {
     send_net_bandwidth();
     send_user_history();
 
+    datasid_agent();
+
     return 0;
 }
-- 
GitLab