From 60372dd526a521e9f87da4ae86402239a6d640e3 Mon Sep 17 00:00:00 2001 From: goei <goei@astron.nl> Date: Thu, 5 Mar 2020 16:27:19 +0100 Subject: [PATCH] TMSS-156: Create TMSSBridge class to connect with http request with TMSS --- .../MainCU/src/MACScheduler/CMakeLists.txt | 1 + .../MainCU/src/MACScheduler/MACScheduler.cc | 14 ++ .../MainCU/src/MACScheduler/MACScheduler.h | 4 + MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc | 153 ++++++++++++++++++ MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h | 50 ++++++ MAC/CMakeLists.txt | 2 +- 6 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc create mode 100644 MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h diff --git a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt index cb70cfd7ff9..146df0f381e 100644 --- a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt +++ b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt @@ -3,6 +3,7 @@ lofar_add_bin_program(MACScheduler MACSchedulerMain.cc MACScheduler.cc + TMMSBridge.cc ObsClaimer.cc) lofar_add_bin_program(claimTest diff --git a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.cc b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.cc index 7ac95d6472d..47d30ca70e6 100644 --- a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.cc +++ b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.cc @@ -49,6 +49,7 @@ using namespace LOFAR::GCF::PVSS; using namespace LOFAR::GCF::TM; using namespace LOFAR::GCF::RTDB; using namespace LOFAR::OTDB; +using namespace LOFAR::TMSSBridge; using namespace LOFAR::Protocols; using namespace boost::posix_time; using namespace std; @@ -84,6 +85,7 @@ MACScheduler::MACScheduler() : itsNrPlanned (0), itsNrActive (0), itsOTDBconnection (0), + itsTMSSconnection (0), itsMsgQueue (0) { LOG_TRACE_OBJ ("MACscheduler construction"); @@ -154,6 +156,10 @@ MACScheduler::~MACScheduler() delete itsOTDBconnection; } + if (itsTMSSconnection) { + delete itsTMSSconnection; + } + if (itsMsgQueue) { delete itsMsgQueue; } @@ -323,6 +329,11 @@ GCFEvent::TResult MACScheduler::initial_state(GCFEvent& event, GCFPortInterface& LOG_INFO ("Connected to the OTDB"); itsPropertySet->setValue(PN_MS_OTDB_CONNECTED, GCFPVBool(true)); + LOG_INFO ("Trying to connect to the TMSS " ); + itsTMSSconnection= new TMSSBridge(); + LOG_INFO ("Connected to the TMSSBridge"); + + // Start ChildControl task LOG_DEBUG ("Enabling ChildControltask"); itsChildControl->openService(MAC_SVCMASK_SCHEDULERCTRL, 0); @@ -690,6 +701,9 @@ void MACScheduler::_updatePlannedList() // get new list (list is ordered on starttime) of planned observations vector<OTDBtree> plannedDBlist = itsOTDBconnection->getTreeGroup(1, itsPlannedPeriod, itsExclPLcluster); + // to be replaced by TMSS call (do we need a switch?)... + vector<int> subTaskID = itsTMSSconnection->getSubTaskIDStartingInThreeMinutes(itsExclPLcluster); + // the subtasksIDs should map somehow on the OTDBtree vector if (!plannedDBlist.empty()) { LOG_DEBUG(formatString("OTDBCheck:First planned observation (%d) is at %s (active over %d seconds)", diff --git a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h index 188d0136488..33702c4900f 100644 --- a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h +++ b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h @@ -48,6 +48,7 @@ #include <Common/ParameterSet.h> #include "ObsClaimer.h" +#include "TMSSBridge.h" #include <boost/date_time/posix_time/posix_time.hpp> @@ -168,6 +169,9 @@ private: // OTDB related variables. OTDB::OTDBconnection* itsOTDBconnection; // connection to the database + // TMSS Bridge + TMSSBridge::TMSS* itsTMSSconnection; // connection to TMSS + // Cluster to exclude for pipelines. Key is used in the getTreeGroup stored-procedure in OTDB. string itsExclPLcluster; // like !CEP2 or !CEP4 diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc new file mode 100644 index 00000000000..cfeb5426c7e --- /dev/null +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc @@ -0,0 +1,153 @@ +//# TMSSBridge.cc: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS +//# +//# Copyright (C) 2004-2012 +//# ASTRON (Netherlands Foundation for Research in Astronomy) +//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +//# +//# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//# +//# $Id$ +#include "TMSSBridge.h" + + +#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> +#include <boost/property_tree/json_parser.hpp> +#include <cstdlib> +#include <iostream> +#include <string> + +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> + +namespace LOFAR { + namespace MainCU { + +// +// TMSSBridge Constructor +// +TMSSBridge::TMSSBridge(): + itsUser ("test"), + itsPassword ("test"), + itsHost ("http://127.0.0.1"), // just to start with maybe user and password need + itsPort (8000) +{ + +} + + +// +// TMSSBridge Destructor +// +TMSSBridge::~TMSSBridge() +{ + +} + + +// +// get all subTaskIDS that should run within three minutes (ordered in time if multiple are found) +// for given cluster +// +vector<int> TMSSBridge::getSubTaskIDStartingInThreeMinutes(ptime currentTime, string clusterName) +{ + currentTimeStr = std::string to_iso_extended_string(currentTime); + queryStr = "/api/?start_time__lt=" + currentTimeStr + "&cluster__name==" + clusterName; + result = httpGET(itsHost, itsPort, queryStr); + + // 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) + + +} + + + + +// +// Performs an HTTP GET and return response +// 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(auto const host, auto const port, auto const target) +{ + 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; + } + return res; +} + + + + };//MainCU +};//LOFAR + + diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h new file mode 100644 index 00000000000..d3c72b5c9b8 --- /dev/null +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h @@ -0,0 +1,50 @@ +//# TMSSBridge.h: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS +//# +//# Copyright (C) 2008 +//# ASTRON (Netherlands Foundation for Research in Astronomy) +//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +//# +//# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//# +//# $Id$ + +#ifndef TMSSBRIDGE_H +#define TMSSBRIDGE_H + +namespace LOFAR { + namespace MainCU + + +class TMSSBridge +{ +public: + // Just creates an object and registers the connection parameters. + TMSSBridge (); + ~TMSSBridge (); + + vector<int> getSubTaskIDStartingInThreeMinutes(ptime currentTime, string clusterName); +private: + // Copying is not allowed + TMSSBridge(const TMSSBridge&); + TMSSBridge& operator=(const TMSSBridge&); + + // http request to TMSS + string httpGET(auto const host, auto const port, auto const target); + +}; + + };//MainCU +};//LOFAR +#endif \ No newline at end of file diff --git a/MAC/CMakeLists.txt b/MAC/CMakeLists.txt index c1c9d2dcfb9..62b099b2e98 100644 --- a/MAC/CMakeLists.txt +++ b/MAC/CMakeLists.txt @@ -11,7 +11,7 @@ lofar_add_package(WinCC_Datapoints Deployment/data/PVSS) lofar_add_package(OTDB_Comps Deployment/data/OTDB) lofar_add_package(StaticMetaData Deployment/data/StaticMetaData) lofar_add_package(WinCCPublisher WinCCPublisher) -lofar_add_package(WinCCREST) +# RGOE does not build on buildhost to skip for now... lofar_add_package(WinCCREST) lofar_add_package(WinCCDBBridge) -- GitLab