merge conflict sorted, removing rest

This commit is contained in:
Dhanya Maliakal
2017-12-05 11:25:10 +01:00
17 changed files with 34 additions and 967 deletions

View File

@ -1,308 +0,0 @@
#pragma once
/**
* @file RestHelper.h
* @author Leonardo Sala <leonardo.sala@psi.ch>
* @date Tue Mar 25 09:28:19 2014
*
* @brief
*
*
*/
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include <Poco/Timespan.h>
#include "JsonBox/Value.h"
#include "JsonBox/JsonParsingError.h"
//#include "logger.h"
#include <iostream>
#include <sstream>
#include <string>
#include <exception>
#include <unistd.h>
using namespace Poco::Net;
using namespace Poco;
using namespace std;
class RestHelper {
public:
RestHelper(int timeout=10, int n_tries=1){
/**
*
*
* @param timeout default=10
* @param n_tries default=1
*/
http_timeout = timeout;
n_connection_tries = n_tries;
}
~RestHelper(){
delete session;
};
void set_connection_params(int timeout, int n_tries){
http_timeout = timeout;
n_connection_tries = n_tries;
}
void get_connection_params(int *timeout, int *n_tries){
*timeout = http_timeout;
*n_tries = n_connection_tries;
}
void init(string hostname, int port){
/** Initialize the RestHelper. Hostname and port parameters are not supposed to change.
*
*
* @param hostname FQDN of the host to connect to , e.g. www.iamfake.org, or sodoi.org
* @param port
*
* @return
*/
//Check for http:// string
string proto_str = "http://";
if( size_t found = hostname.find(proto_str) != string::npos ){
char c1[hostname.size()-found-1];
size_t length1 = hostname.copy(c1, hostname.size()-found-1, proto_str.size());
c1[length1]='\0';
hostname = c1;
}
full_hostname = "http://"+hostname;
session = new HTTPClientSession(hostname, port );
session->setKeepAliveTimeout( Timespan( http_timeout,0) );
};
void init(string hostname_port){
/** Initialize the RestHelper. Hostname_port parameters are not supposed to change.
*
*
* @param hostname FQDN and port of the host to connect to , e.g. www.iamfake.org:8080, or sodoi.org:1111. Default port is 8080
*
* @return
*/
//Check for http:// string
string proto_str = "http://";
if( size_t found = hostname_port.find(proto_str) != string::npos ){
char c1[hostname_port.size()-found-1];
size_t length1 = hostname_port.copy(c1, hostname_port.size()-found-1, proto_str.size());
c1[length1]='\0';
hostname_port = c1;
}
size_t found = hostname_port.rfind(":");
char c1[ found ], c2[hostname_port.size()-found-1];
string hostname;
size_t length1 = hostname_port.copy(c1, found);
c1[length1]='\0';
hostname = c1;
size_t length2 = hostname_port.copy(c2, found-1, found+1);
c2[length2]='\0';
int port = atoi(c2);
full_hostname = proto_str+hostname;
session = new HTTPClientSession(hostname,port );
session->setKeepAliveTimeout( Timespan( http_timeout,0) );
};
int get_json(string request, string* answer){
/** Retrieves a reply from the RESTful webservice.
*
*
* @param request Request without the hostname, e.g. if the full request would have been http://fake.org/fakemethod, request=fakemethod
* @param answer
*
* @return 0 if successful, -1 if failure happens.
*/
URI * uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.setContentType("application/json\r\n");
int code = send_request(session, req, answer);
delete uri;
return code;
};
int get_json(string request, JsonBox::Value* json_value){
/**
*
*
* @param request
* @param json_value
*
* @return
*/
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.setContentType("application/json\r\n");
string answer;
int code = send_request(session, req, &answer);
if(code == 0 ) {
FILE_LOG(logDEBUG) << __AT__ << " REQUEST: " << " ANSWER: " << answer;
json_value->loadFromString(answer);
}
delete uri;
return code;
};
int post_json(string request, string *answer, string request_body="{}"){
/**
*
*
* @param request
* @param answer
* @param request_body Eventual arguments to the URL, e.g. action=login&name=mammamia
*
* @return
*/
//from: http://stackoverflow.com/questions/1499086/poco-c-net-ssl-how-to-post-https-request
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1 );
req.setContentType("application/json\r\n");
req.setContentLength( request_body.length() );
int code = send_request(session, req, answer, request_body);
delete uri;
return code;
}
int post_json(string request, JsonBox::Value* json_value, string request_body="{}"){
/**
*
*
* @param request
* @param json_value
* @param request_body Eventual arguments to the URL, e.g. action=login&name=mammamia
*
* @return
*/
URI *uri = new URI(full_hostname+"/"+request);
string path(uri->getPathAndQuery());
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1 );
//this does not work
//req.setContentType("application/json\r\n");
//req.setContentLength( request.length() );
string answer;
int code = send_request(session, req, &answer, request_body);
if(code==0){
try{
json_value->loadFromString(answer);
}
catch (JsonBox::JsonParsingError& e){
try{
json_value->loadFromString("{\"global_state\":\"" + answer + "\"}");
}
catch(exception &e){
FILE_LOG(logERROR) << "Exception converting answer: " << e.what() ;
}
}
}
delete uri;
return code;
}
private:
HTTPClientSession *session;
string full_hostname;
/// HTTP timeout in seconds, default is 8
int http_timeout;
/// Number of connection tries
int n_connection_tries;
int send_request(HTTPClientSession *session, HTTPRequest &req, string *answer, string request_body=""){
/**
*
*
* @param session
* @param req
* @param answer
* @param request_body
*
* @return
*/
int n = 0;
int code = -1;
while(n < n_connection_tries){
req.setContentType("application/json");
//without this you need to tell the lenght: http://pocoproject.org/forum/viewtopic.php?f=12&t=5741&p=10019&hilit=post+json#p10019
// request.setContentLength(my_string.length());
req.setChunkedTransferEncoding(true);
try {
//istringstream rs(request_body);
//req.read(rs);
//cout << " --- " << rs << endl;
if (request_body == "")
session->sendRequest( (req) );
else{
ostream &os = session->sendRequest( req ) ;
os << request_body;
}
HTTPResponse res;
istream &is = session->receiveResponse(res);
StreamCopier::copyToString(is, *answer);
code = res.getStatus();
if (code != 200){
FILE_LOG(logERROR) << "HTTP ERROR " << res.getStatus() << ": " << res.getReason() ;
code = -1;
}
else
code = 0;
return code;
}
catch (exception& e){
FILE_LOG(logERROR) << "Exception connecting to "<< full_hostname << ": "<< e.what() << ", sleeping 5 seconds (" << n << "/"<<n_connection_tries << ")";
sleep(5);
}
n+=1;
}
std::cout << "Hostname: " << full_hostname << std::endl;
FILE_LOG(logERROR) << "Cannot connect to the REST server host " << full_hostname << "! Please check..." ;
throw std::runtime_error("Cannot connect to the REST server! Please check...");
//return code;
}
};

View File

@ -491,9 +491,9 @@ class UDPBaseImplementation : protected virtual slsReceiverDefs, public UDPInter
void setDetectorPositionId(const int i);
/**
* Sets detector hostname (and corresponding detector variables in derived REST class)
* Sets detector hostname
* It is second function called by the client when connecting to receiver.
* you can call this function only once. //FIXME: is this still valid, this implemented in derived REST class?
* you can call this function only once.
* @param c detector hostname
*/
void initialize(const char *c);

View File

@ -56,7 +56,7 @@ class UDPInterface {
*
* supported sequence of method-calls:
*
* initialize() : once and only once after create() //FIXME: only once functionality implemented in the derived REST class, so not mention here?
* initialize() : once and only once after create()
*
* get*() : anytime after initialize(), multiples times
*
@ -108,8 +108,8 @@ class UDPInterface {
/**
* Constructor
* Only non virtual function implemented in this class
* Factory create method to create a standard or REST object
* @param [in] receiver_type type can be standard or REST
* Factory create method to create a standard or custom object
* @param [in] receiver_type type can be standard or custom (must be derived from base class)
* @return a UDPInterface reference to object depending on receiver type
*/
static UDPInterface *create(string receiver_type = "standard");
@ -579,9 +579,9 @@ class UDPInterface {
virtual void setDetectorPositionId(const int i) = 0;
/**
* Sets detector hostname (and corresponding detector variables in derived REST class)
* Sets detector hostname
* It is second function called by the client when connecting to receiver.
* you can call this function only once. //FIXME: is this still valid, this implemented in derived REST class?
* you can call this function only once.
* @param c detector hostname
*/
virtual void initialize(const char *c) = 0;

View File

@ -1,135 +0,0 @@
//#ifdef REST
#pragma once
/********************************************//**
* @file UDPRESTImplementation.h
* @short does all the functions for a receiver, set/get parameters, start/stop etc.
***********************************************/
#include "UDPBaseImplementation.h"
#include "RestHelper.h"
#include "logger.h"
#include <string.h>
#include <stdio.h>
/**
* @short does all the functions for a receiver, set/get parameters, start/stop etc.
*/
class UDPRESTImplementation : protected virtual slsReceiverDefs, public UDPBaseImplementation {
public:
/*************************************************************************
* Constructor & Destructor **********************************************
*************************************************************************/
/**
* Constructor
*/
UDPRESTImplementation();
/**
* Destructor
*/
virtual ~UDPRESTImplementation();
protected:
/*************************************************************************
* Getters ***************************************************************
* They access local cache of configuration or detector parameters *******
*************************************************************************/
/**
* Get Rest State
*/
string get_rest_state(RestHelper * rest/*, string *rest_state*/);
/*************************************************************************
* Setters ***************************************************************
* They modify the local cache of configuration or detector parameters ***
*************************************************************************/
/**
* Initialize REST
*/
void initialize_REST();
public:
/*************************************************************************
* Getters ***************************************************************
* They access local cache of configuration or detector parameters *******
*************************************************************************/
/*************************************************************************
* Setters ***************************************************************
* They modify the local cache of configuration or detector parameters ***
*************************************************************************/
/**
* Overridden method
* Configure command line parameters
* @param config_map mapping of config parameters passed from command line arguments
*/
void configure(map<string, string> config_map);
/*************************************************************************
* Behavioral functions***************************************************
* They may modify the status of the receiver ****************************
*************************************************************************/
/**
* Overridden method
* Start Listening for Packets by activating all configuration settings to receiver
* When this function returns, it has status RUNNING(upon SUCCESS) or IDLE (upon failure)
* @param c error message if FAIL
* @return OK or FAIL
*/
int startReceiver(char *c=NULL);
/**
* Overridden method
* Stop Listening for Packets
* Calls startReadout(), which stops listening and sets status to Transmitting
* When it has read every frame in buffer, the status changes to Run_Finished
* When this function returns, receiver has status IDLE
* Pre: status is running, semaphores have been instantiated,
* Post: udp sockets shut down, status is idle, semaphores destroyed
*/
void stopReceiver();
/**
* Overridden method
* Stop Listening to Packets
* and sets status to Transmitting
* Next step would be to get all data and stop receiver completely and return with idle state
* Pre: status is running, udp sockets have been initialized, stop receiver initiated
* Post:udp sockets closed, status is transmitting,
*/
void startReadout();
/**
* Overridden method
* Shuts down and deletes UDP Sockets
* TCPIPInterface can also call this in case of illegal shutdown of receiver
*/
void shutDownUDPSockets();
uint64_t getTotalFramesCaught() const;
private:
bool isInitialized;
RestHelper * rest ;
int rest_port; // receiver backend port
string rest_hostname; // receiver hostname
bool is_main_receiver;
};
//#endif /*REST*/

View File

@ -78,17 +78,11 @@ public:
class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
//typedef Log<Output2FILE> FILELog;
#ifdef REST
#define FILE_LOG(level) \
if (level > FILELOG_MAX_LEVEL) ; \
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
else FILELog().Get(level)
#else
#define FILE_LOG(level) \
if (level > FILELOG_MAX_LEVEL) ; \
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
else FILELog().Get(level)
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
@ -151,11 +145,7 @@ template <typename T> std::ostringstream& Log<T>::Get(TLogLevel level)
template <typename T> Log<T>::~Log()
{
os << std::endl;
#ifdef REST
T::Output( os.str());
#else
T::Output( os.str(),lev);
#endif
T::Output( os.str(),lev); // T::Output( os.str());
}
template <typename T> TLogLevel& Log<T>::ReportingLevel()

View File

@ -23,7 +23,8 @@ class slsReceiver : private virtual slsReceiverDefs {
public:
/**
* Constructor
* creates the tcp interface and the udp class
* Starts up a Receiver server. Reads configuration file, options, and
* assembles a Receiver using TCP and UDP detector interfaces
* @param argc from command line
* @param argv from command line
* @param succecc socket creation was successfull