diff --git a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt index 7f0072489b04c50152250c6c22b918f627939a88..89bb87ee333392ee82b5df568affcbc91491cd0b 100644 --- a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt +++ b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt @@ -1,5 +1,7 @@ # $Id$ +lofar_add_library(libjsonCpp) + lofar_add_bin_program(MACScheduler MACSchedulerMain.cc MACScheduler.cc diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc index 3ff9fbb305ff263b7f1ef2fbb04541351dbaba31..a26584d59a92d6500107cc8ebff2323e3a72d25e 100644 --- a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.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; } diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h index 5ad201c7bc7dfcc76daf0113177c566b26afefe4..f67263dbcb502f4bdcbdc2d62540582127bec0af 100644 --- a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h @@ -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; diff --git a/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt b/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt index faac0b0e7d4aa88c7f44b2acfb274dca65cf267e..13bb1b7ed071567b3c6adbc33973eeb6e1f8dfb3 100644 --- a/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt +++ b/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt @@ -1,9 +1,11 @@ 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() diff --git a/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc b/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc index 750fcedf349380b3061490b309ac2b356588c831..6d1afcbc4d06ec305443153f2056679a792b972a 100644 --- a/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc +++ b/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc @@ -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 }