Commit 938f0631 authored by Lais Frigerio's avatar Lais Frigerio

SCRUM#145 - Remove c++11 flag and not make web service request with cpr lib

Signed-off-by: 's avatarLais Frigerio <laaisfrigerio@gmail.com>
parent 14f2641c
Pipeline #12910 passed with stages
in 21 minutes and 23 seconds
......@@ -7,17 +7,19 @@ set ( VERSION_MAJOR 0 )
set ( VERSION_MINOR 0 )
# cpr requires c++11
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
if(WIN32)
add_definitions( -DBOOST_ALL_NO_LIB )
set(Boost_USE_STATIC_LIBS ON)
add_definitions(-DCURL_STATICLIB)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_WIN32_WINNT=0x0501)
# add_compile_options("/std:c++11")
# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++11)
else()
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
# src : main, collect functions + jsoncpp library
file ( GLOB SOURCES src/agent/*.cpp )
......@@ -37,7 +39,6 @@ option( RUN_CURL_TESTS "Set to ON to run cURL tests." OFF )
option( CURL_STATICLIB "Set to ON to generated curl statically." ON )
add_subdirectory ( lib/cpr )
include_directories ( ${CPR_INCLUDE_DIRS} )
include_directories ( ${CURL_INCLUDE_DIRS} )
# headers
......@@ -53,7 +54,7 @@ include ( "modules/boost.cmake" )
# compile
set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" )
add_executable ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} )
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CPR_LIBRARIES} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} )
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} )
add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost)
......
......@@ -26,11 +26,11 @@ class Connection{
long httpCode = 0; /*!< Code return from webSerice (200, 404, 502 and so on) */
public:
bool downloadAgent (std::string, Agent*, int);
bool getVersionByProxy(Agent*, std::string*, std::string);
int getInstallerNameByProxy(std::string, Agent*, std::string*);
void setCurlOption(std::string, Agent*, std::string*);
int sendJsonByProxy(Agent*, std::string, int);
bool downloadAgent (std::string, Agent*, bool);
bool getVersion(Agent*, std::string*, std::string, bool);
int getInstallerName(std::string, Agent*, std::string*, bool);
void setCurlOption(std::string, Agent*, std::string*, bool);
int sendJson(Agent*, std::string, int, bool);
/** Default callback function that is necessary to make a curl request.
* @brief get agent binary (agent.run or agent.exe)
......
......@@ -2,7 +2,6 @@
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cpr/cpr.h>
#include <json/json.h>
#include <agent/common.h>
#include <agent/connection.h>
......@@ -24,6 +23,6 @@ const std::string url = "http://localhost:3001";
int check_update(Agent*);
bool create_directories(std::string);
bool compare_version(Agent*, std::string);
bool download(Agent*, std::string, int);
bool download(Agent*, std::string, bool);
void execute_agent(Agent*, boost::filesystem::path);
bool get_agent_installer_name(Agent*, std::string*, int);
bool get_agent_installer_name(Agent*, std::string*, bool);
......@@ -17,7 +17,6 @@
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/filesystem/fstream.hpp>
#include <cpr/cpr.h>
namespace pt = boost::posix_time;
namespace po = boost::program_options;
......
#pragma once
#include <iostream>
#include <cpr/cpr.h>
#include <json/json.h>
#include <agent/agent.h>
#include <agent/connection.h>
......
......@@ -31,7 +31,7 @@
* @param agent object agent
* @param option use proxy or not (option == 0 - use proxy)
*/
bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
bool Connection::downloadAgent(std::string url, Agent* agent, bool option) {
curl = curl_easy_init();
if (!curl) {
agent->logFile << __DATE__ + std::string(" - ERROR: Curl ") +
......@@ -79,13 +79,15 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
&Connection::callBackDownload);
if (option == 0) {
if (option) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
......@@ -147,28 +149,34 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
* @param version pointer variable thats receive version or filename from web service.
*/
void Connection::setCurlOption(std::string url, Agent* agent,
std::string* out) {
std::string* out, bool opt) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
if (opt) {
curl_easy_setopt(curl, CURLOPT_PROXY,
agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Connection::callback);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
response = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
......@@ -181,9 +189,10 @@ void Connection::setCurlOption(std::string url, Agent* agent,
* @param url references to webService
* @param agent object agent
* @param version pointer variable thats receive version from web service;
* @param opt is proxy request or not
*/
bool Connection::getVersionByProxy(Agent* agent, std::string *version,
std::string url) {
bool Connection::getVersion(Agent* agent, std::string *version,
std::string url, bool opt) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
......@@ -193,8 +202,8 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
this->setCurlOption(url, agent, version);
if (httpCode == 200)
this->setCurlOption(url, agent, version, opt);
if (this->httpCode == 200)
return true;
else
return false;
......@@ -207,12 +216,13 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
* @param url references to webService
* @param agent object agent
* @param version pointer variable thats receive file name from web service;
* @param opt is proxy request or not
*/
int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
std::string* filename) {
int Connection::getInstallerName(std::string url, Agent* agent,
std::string* filename, bool opt) {
curl = curl_easy_init();
this->setCurlOption(url, agent, filename);
return httpCode;
this->setCurlOption(url, agent, filename, opt);
return this->httpCode;
}
/** Contact webservice server using proxy to send json (inventory, net_usage or user_history)
......@@ -221,15 +231,19 @@ int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
*
* @param agent object agent
* @param json references to json
* @param opt opt == 0 (send inventory) | opt == 2 (send net bandwidth) | otherwise (send user history)
* @param send send == 0 (send inventory) | send == 2 (send net bandwidth) | otherwise (send user history)
* @param opt is proxy request or not
*/
int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
int Connection::sendJson(Agent* agent, std::string json,
int send, bool opt) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
......@@ -243,9 +257,9 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
// opt == 1 {send inventory}
// opt = 2 {send net bandwidth}
// otherwise, {send user history}
if (opt == 1)
if (send == 1)
base_url = base_url + std::string("/api/agent/collect/inventory");
else if (opt == 2)
else if (send == 2)
base_url = base_url + std::string("/api/agent/collect/net_usage");
else
base_url = base_url + std::string("/api/agent/collect/user_history");
......@@ -254,12 +268,15 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, base_url.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
if (opt) {
curl_easy_setopt(curl, CURLOPT_PROXY,
agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
}
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
......
......@@ -35,29 +35,22 @@ int check_update(Agent* agent) {
Json::Reader reader;
Json::Value version_json;
Json::FastWriter fastWriter;
int option = 1;
/* Contact webservice server to get client version. */
auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() +
"/api/agent/version"},
cpr::Header{{"content-type", "application/json"}});
// Check error with connection
if (p.status_code != 200) {
option = 0;
// Case if not connected before, try it with proxy
std::string web_service = agent->getConf()->getWebService() +
bool option = false;
std::string web_service = agent->getConf()->getWebService() +
std::string("/api/agent/version");
if (!agent->getConnection()->getVersionByProxy(agent, &version,
web_service)) {
if (!agent->getConnection()->getVersion(agent, &version,
web_service, 0)) {
agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
"version from simmc web service." << std::endl;
"version from simmc web service without pŕoxy." << std::endl;
option = true;
if (!agent->getConnection()->getVersion(agent, &version,
web_service, 1)) {
agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
"version from simmc web service with proxy." << std::endl;
}
} else {
version = p.text;
}
// p.text - response from api
reader.parse(version, version_json, false);
version = fastWriter.write(version_json["version"]);
boost::regex re("(\")|(\n)");
......@@ -130,7 +123,7 @@ bool compare_version(Agent *agent, std::string version) {
}
/* Contact webservice server to request a agent installer. */
bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
bool get_agent_installer_name(Agent* agent, std::string* filename, bool opt) {
Json::FastWriter fastWriter;
Json::Value filename_json;
Json::Reader reader;
......@@ -147,17 +140,13 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
+ std::string(os) + std::string("/")
+ std::to_string(agent->getConf()->getIdPoint());
if (opt == 1) {
auto p = cpr::Get(cpr::Url{ws}, // NOLINT [whitespace/line_length]
cpr::Header{{"content-type", "application/json"}});
status_code = p.status_code;
*filename = p.text;
} else {
status_code = agent->getConnection()->getInstallerNameByProxy(ws, agent, filename); // NOLINT [whitespace/line_length]
}
if (opt)
status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]
else
status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]
if (status_code == 200) {
// p.text - response from api
reader.parse(*filename, filename_json, false);
*filename = fastWriter.write(filename_json["filename"]);
......@@ -173,11 +162,9 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
/**
* To make agent download.
* Curl library is used to make a webService request to get agent zipfile.
* option -> indicate proxy or not
* 0 -> proxy
* 1 -> not proxy
* option -> indicate request with proxy or not
*/
bool download(Agent* agent, std::string filename, int option) {
bool download(Agent* agent, std::string filename, bool option) {
std::string web_service = agent->getConf()->getWebService() +
std::string("/install/") +
std::string(filename);
......
......@@ -42,18 +42,14 @@ int send_inventory(bool print, Agent* agent) {
std::cout << "Empty inventory object" << std::endl;
}
auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/inventory"},
cpr::Body{fastWriter.write(inventory)},
cpr::Header{{"content-type", "application/json"}});
if (p.status_code == 200)
if (agent->getConnection()->sendJson(agent,
fastWriter.write(inventory), 1, 0) == 200)
return 1;
else if (agent->getConnection()->sendJson(agent,
fastWriter.write(inventory), 1, 1) == 200)
return 1;
else
if (agent->getConnection()->sendJsonByProxy(agent,
fastWriter.write(inventory), 1) == 200)
return 1;
return 0;
return 0;
}
/***************************************
......@@ -77,18 +73,14 @@ int send_net_bandwidth(bool print, Agent* agent) {
std::cout << "Empty network_bandwidth object" << std::endl;
}
auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/net_usage"},
cpr::Body{fastWriter.write(net)},
cpr::Header{{"content-type", "application/json"}});
if (p.status_code == 200)
if (agent->getConnection()->sendJson(agent,
fastWriter.write(net), 2, 0) == 200)
return 1;
else if (agent->getConnection()->sendJson(agent,
fastWriter.write(net), 2, 1) == 200)
return 1;
else
if (agent->getConnection()->sendJsonByProxy(agent,
fastWriter.write(net), 2) == 200)
return 1;
return 0;
return 0;
}
/**********************************
......@@ -112,19 +104,14 @@ int send_user_history(bool print, Agent* agent) {
std::cout << "Empty user_history object" << std::endl;
}
auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/user_history"},
cpr::Parameters{{"type", "user_history"}},
cpr::Body{fastWriter.write(u_h)},
cpr::Header{{"content-type", "application/json"}});
if (p.status_code == 200)
if (agent->getConnection()->sendJson(agent,
fastWriter.write(u_h), 3, 0) == 200)
return 1;
else if (agent->getConnection()->sendJson(agent,
fastWriter.write(u_h), 3, 1) == 200)
return 1;
else
if (agent->getConnection()->sendJsonByProxy(agent,
fastWriter.write(u_h), 3) == 200)
return 1;
return 0;
return 0;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment