Skip to content
Snippets Groups Projects
Commit c5548888 authored by vagrant's avatar vagrant
Browse files

TMSS-156: Using libjsoncpp for reading json file and first try to build it

parent 6409ac56
No related branches found
No related tags found
1 merge request!116TMSS-156: Resolve TMSS-156
# $Id$
lofar_add_library(libjsonCpp)
lofar_add_bin_program(MACScheduler
MACSchedulerMain.cc
MACScheduler.cc
......
......@@ -22,26 +22,15 @@
#include "TMSSBridge.h"
#ifdef _GET_BOOST_BEAST_WORKING_
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#endif
#include <boost/property_tree/json_parser.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#ifdef _GET_BOOST_BEAST_WORKING_
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
#endif
namespace pt = boost::property_tree;
using namespace std;
......@@ -80,7 +69,7 @@ vector<int> TMSSBridge::getSubTaskIDStartingInThreeMinutes(ptime currentTime, st
{
string currentTimeStr = to_iso_extended_string(currentTime);
string queryStr = "/api/?start_time__lt=" + currentTimeStr + "&cluster__name==" + clusterName;
string result = httpGET(itsHost, itsPort, queryStr);
Json::Value result = httpGET(itsHost, itsPort, queryStr);
vector<string> urlList = translateHttpResultToSortedUrlList(result);
}
......@@ -89,95 +78,38 @@ vector<int> TMSSBridge::getSubTaskIDStartingInThreeMinutes(ptime currentTime, st
//
// Translate the HTTP result to json format and parse the json format
// How to handle json structure in C++?
// Need to check respons status code of http (200)?
// Get the number of taskIDs like python
// json_response = response.json()
// json_response.get('count'))
// ... and results[idx].items('url)
// if multiple found sort on startTime !
vector<string> TMSSBridge::translateHttpResultToSortedUrlList(string result)
vector<string> TMSSBridge::translateHttpResultToSortedUrlList(Json::Value jsonResult)
{
vector<string> urlList;
// Create a root
pt::ptree root;
// Load json
pt::read_json(result, root);
// Read values
int nbrSubTasksFound = root.get<int>("count", 0);
string msg = root.get<string>("some.complex.path");
urlList.push_back(msg);
// Read values
int nbrSubTasksFound = jsonResult["count"].asInt();
urlList.push_back("20000000000");
return urlList;
}
//
// Performs an HTTP GET and return response
// Performs an HTTP GET and return the response body
// Need to check respons status code of http (200)
// Example:
// httpGET("http://127.0.0.1", 8000, "/api/subtask/?start_time__lt=2020-03-04T12:03:00"
// results in a json string output
//
string TMSSBridge::httpGET(const string& host, const int port, const string& target)
Json::Value TMSSBridge::httpGET(const string& host, const int port, const string& target)
{
string res = "dummy";
#ifdef _GET_BOOST_BEAST_WORKING_
try
{
// The io_context is required for all I/O
net::io_context ioc;
// These objects perform our I/O
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
stream.connect(results);
// Set up an HTTP GET request message, HTTP version: 1.1(default)
http::request<http::string_body> req{http::verb::get, target, "1.1"};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(stream, req);
// This buffer is used for reading and must be persisted
beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
// Receive the HTTP response
http::read(stream, buffer, res);
// Gracefully close the socket
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes
// so don't bother reporting it.
//
if(ec && ec != beast::errc::not_connected)
throw beast::system_error{ec};
// If we get here then the connection is closed gracefully
}
catch(std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
#endif
return res;
// TODO Execute using curl stuff and endup with Json::Value
// Check for http result 200
Json::Value jsonResult("dummy");
return jsonResult;
}
......
......@@ -24,6 +24,7 @@
#define TMSSBRIDGE_H
#include <boost/date_time/posix_time/posix_time.hpp>
#include <jsoncpp/json/json.h>
namespace LOFAR {
namespace MainCU {
......@@ -43,15 +44,14 @@ public:
vector<int> getSubTaskIDStartingInThreeMinutes(ptime currentTime, string clusterName);
// Actually the next method is private, make it public to be able to use in UnitTest++
vector<string> translateHttpResultToSortedUrlList(string result);
vector<string> translateHttpResultToSortedUrlList(Json::Value result);
private:
// Copying is not allowed
TMSSBridge(const TMSSBridge&);
TMSSBridge& operator=(const TMSSBridge&);
// http request to TMSS
string httpGET(const string& host, int const port, const string& target);
Json::Value httpGET(const string& host, int const port, const string& target);
string itsUser;
string itsPassword;
......
include(LofarCTest)
lofar_find_package(UnitTest++)
find_library(JSONCPP jsoncpp)
if(UNITTEST++_FOUND)
lofar_add_test(tTMSSBridge tTMSSBridge.cc DEPENDS TMSSBridge.cc)
target_link_libraries(tTMSSBridge jsoncpp)
lofar_add_test(tTMSSBridge tTMSSBridge.cc ../TMSSBridge.cc)
else()
message("WARNING UnitTest++ not found. Test tTMSSBridge will not be run!")
endif()
......
......@@ -28,6 +28,7 @@
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <jsonccp/json/json.h>
namespace pt = boost::property_tree;
......@@ -37,26 +38,37 @@ using namespace boost::property_tree;
using namespace LOFAR::MainCU;
Json::Value readFile(const std::string& jsonFile)
{
ifstream ifs(jsonFile.c_str());
if (!ifs) {
throw runtime_error("Failed to open file " + jsonFile);
}
Json::Value jsonValue;
try {
ifs >> jsonValue;
} catch(exception& e) {
throw runtime_error("Error parsing file " + jsonFile + ":\n" + e.what());
}
return jsonValue;
}
TEST(tmss_translate_http_result_ok)
{
// check if json read goes well as precondition for the test
// See jsoncpp https://github.com/open-source-parsers/jsoncpp/wiki
std::ifstream jsonFile("test_resonse.json");
if (!jsonFile) {
std::cerr << "Error opening file\n";
// some assert error
}
ptree jsontree;
read_json(jsonFile, jsontree);
Json::Value jsonData(readFile("test_response.json"));
int v0 = jsontree.get<int>("count");
int count = jsonData["count"];
// check if count is equal to 4 .... otherwise precond test did not match so will make no sense to continue
std::cout << count;
TMSSBridge testTMSSBridge;
// Read file json
std::vector<string> testResult;
testResult = testTMSSBridge.translateHttpResultToSortedUrlList("json string");
testResult = testTMSSBridge.translateHttpResultToSortedUrlList(jsonData);
// Do some check like CHECK_EQUAL
}
......@@ -64,11 +76,12 @@ TEST(tmss_translate_http_result_ok)
TEST(tmss_translate_http_empty_result)
{
Json::Value jsonData("{}");
TMSSBridge testTMSSBridge;
// Read file json
std::vector<string> testResult;
testResult = testTMSSBridge.translateHttpResultToSortedUrlList("");
testResult = testTMSSBridge.translateHttpResultToSortedUrlList(jsonData);
// Do some check like CHECK_EQUAL
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment