Skip to content
Snippets Groups Projects
Commit 60372dd5 authored by Roy de Goei's avatar Roy de Goei Committed by Jorrit Schaap
Browse files

TMSS-156: Create TMSSBridge class to connect with http request with TMSS

parent 5c22ac6c
No related branches found
No related tags found
1 merge request!116TMSS-156: Resolve TMSS-156
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
lofar_add_bin_program(MACScheduler lofar_add_bin_program(MACScheduler
MACSchedulerMain.cc MACSchedulerMain.cc
MACScheduler.cc MACScheduler.cc
TMMSBridge.cc
ObsClaimer.cc) ObsClaimer.cc)
lofar_add_bin_program(claimTest lofar_add_bin_program(claimTest
......
...@@ -49,6 +49,7 @@ using namespace LOFAR::GCF::PVSS; ...@@ -49,6 +49,7 @@ using namespace LOFAR::GCF::PVSS;
using namespace LOFAR::GCF::TM; using namespace LOFAR::GCF::TM;
using namespace LOFAR::GCF::RTDB; using namespace LOFAR::GCF::RTDB;
using namespace LOFAR::OTDB; using namespace LOFAR::OTDB;
using namespace LOFAR::TMSSBridge;
using namespace LOFAR::Protocols; using namespace LOFAR::Protocols;
using namespace boost::posix_time; using namespace boost::posix_time;
using namespace std; using namespace std;
...@@ -84,6 +85,7 @@ MACScheduler::MACScheduler() : ...@@ -84,6 +85,7 @@ MACScheduler::MACScheduler() :
itsNrPlanned (0), itsNrPlanned (0),
itsNrActive (0), itsNrActive (0),
itsOTDBconnection (0), itsOTDBconnection (0),
itsTMSSconnection (0),
itsMsgQueue (0) itsMsgQueue (0)
{ {
LOG_TRACE_OBJ ("MACscheduler construction"); LOG_TRACE_OBJ ("MACscheduler construction");
...@@ -154,6 +156,10 @@ MACScheduler::~MACScheduler() ...@@ -154,6 +156,10 @@ MACScheduler::~MACScheduler()
delete itsOTDBconnection; delete itsOTDBconnection;
} }
if (itsTMSSconnection) {
delete itsTMSSconnection;
}
if (itsMsgQueue) { if (itsMsgQueue) {
delete itsMsgQueue; delete itsMsgQueue;
} }
...@@ -323,6 +329,11 @@ GCFEvent::TResult MACScheduler::initial_state(GCFEvent& event, GCFPortInterface& ...@@ -323,6 +329,11 @@ GCFEvent::TResult MACScheduler::initial_state(GCFEvent& event, GCFPortInterface&
LOG_INFO ("Connected to the OTDB"); LOG_INFO ("Connected to the OTDB");
itsPropertySet->setValue(PN_MS_OTDB_CONNECTED, GCFPVBool(true)); 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 // Start ChildControl task
LOG_DEBUG ("Enabling ChildControltask"); LOG_DEBUG ("Enabling ChildControltask");
itsChildControl->openService(MAC_SVCMASK_SCHEDULERCTRL, 0); itsChildControl->openService(MAC_SVCMASK_SCHEDULERCTRL, 0);
...@@ -690,6 +701,9 @@ void MACScheduler::_updatePlannedList() ...@@ -690,6 +701,9 @@ void MACScheduler::_updatePlannedList()
// get new list (list is ordered on starttime) of planned observations // get new list (list is ordered on starttime) of planned observations
vector<OTDBtree> plannedDBlist = itsOTDBconnection->getTreeGroup(1, itsPlannedPeriod, itsExclPLcluster); 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()) { if (!plannedDBlist.empty()) {
LOG_DEBUG(formatString("OTDBCheck:First planned observation (%d) is at %s (active over %d seconds)", LOG_DEBUG(formatString("OTDBCheck:First planned observation (%d) is at %s (active over %d seconds)",
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include <Common/ParameterSet.h> #include <Common/ParameterSet.h>
#include "ObsClaimer.h" #include "ObsClaimer.h"
#include "TMSSBridge.h"
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
...@@ -168,6 +169,9 @@ private: ...@@ -168,6 +169,9 @@ private:
// OTDB related variables. // OTDB related variables.
OTDB::OTDBconnection* itsOTDBconnection; // connection to the database 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. // Cluster to exclude for pipelines. Key is used in the getTreeGroup stored-procedure in OTDB.
string itsExclPLcluster; // like !CEP2 or !CEP4 string itsExclPLcluster; // like !CEP2 or !CEP4
......
//# 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
//# 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
...@@ -11,7 +11,7 @@ lofar_add_package(WinCC_Datapoints Deployment/data/PVSS) ...@@ -11,7 +11,7 @@ lofar_add_package(WinCC_Datapoints Deployment/data/PVSS)
lofar_add_package(OTDB_Comps Deployment/data/OTDB) lofar_add_package(OTDB_Comps Deployment/data/OTDB)
lofar_add_package(StaticMetaData Deployment/data/StaticMetaData) lofar_add_package(StaticMetaData Deployment/data/StaticMetaData)
lofar_add_package(WinCCPublisher WinCCPublisher) 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) lofar_add_package(WinCCDBBridge)
......
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