Merge branch '9m_rest' into 2.2-rc

This commit is contained in:
Dhanya Maliakal 2016-12-15 14:32:34 +01:00
commit ed3129029d
4 changed files with 163 additions and 118 deletions

View File

@ -18,6 +18,7 @@
#include <Poco/Timespan.h>
#include "JsonBox/Value.h"
#include "JsonBox/JsonParsingError.h"
//#include "logger.h"
@ -220,8 +221,18 @@ class RestHelper {
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;
}

View File

@ -42,7 +42,7 @@ public:
/**
* Get Rest State
*/
int get_rest_state(RestHelper * rest, string *rest_state);
string get_rest_state(RestHelper * rest/*, string *rest_state*/);
/*************************************************************************
@ -138,12 +138,15 @@ public:
*/
void closeFile(int i = -1);
private:
uint64_t getTotalFramesCaught() const;
private:
bool isInitialized;
RestHelper * rest ;
int rest_port; // receiver backend port
string rest_hostname; // receiver hostname
bool is_main_receiver;
};

View File

@ -28,6 +28,7 @@ UDPInterface * UDPInterface::create(string receiver_type){
}
#ifdef REST
else if (receiver_type == "REST"){
FILE_LOG(logINFO) << "Starting " << receiver_type;
return new UDPRESTImplementation();
}
#endif

View File

@ -4,7 +4,6 @@
* @short does all the functions for a receiver, set/get parameters, start/stop etc.
***********************************************/
#include "UDPRESTImplementation.h"
#include <stdlib.h> // exit()
@ -29,10 +28,11 @@ UDPRESTImplementation::UDPRESTImplementation(){
//TODO I do not really know what to do with bottom...
// Default values
isInitialized = false;
isInitialized = true;
rest = NULL;
rest_hostname = "localhost";
rest_port = 8081;
rest_port = 8080;
is_main_receiver = false;
}
@ -42,6 +42,7 @@ UDPRESTImplementation::~UDPRESTImplementation(){
void UDPRESTImplementation::configure(map<string, string> config_map){
// am I ever getting there?
FILE_LOG(logWARNING) << __AT__ << " called";
map<string, string>::const_iterator pos;
@ -65,16 +66,18 @@ void UDPRESTImplementation::configure(map<string, string> config_map){
}
int UDPRESTImplementation::get_rest_state(RestHelper * rest, string *rest_state){
string UDPRESTImplementation::get_rest_state(RestHelper * rest/*, string *rest_state*/){
JsonBox::Value answer;
//string rest_state = "";
int code = rest->get_json("state", &answer);
if ( code != -1 ){
(*rest_state) = answer["state"].getString();
}
string rest_state = "";
return code;
int code = rest->get_json("v1/state", &answer);
if ( code != -1 ){
rest_state = answer["global_state"].getString();
}
//rest_state = *prs;
return rest_state;
}
@ -83,26 +86,43 @@ void UDPRESTImplementation::initialize_REST(){
FILE_LOG(logDEBUG) << __AT__ << " called";
FILE_LOG(logDEBUG) << __AT__ << " REST status is initialized: " << isInitialized;
string rest_state = "";
std::string answer = "";
// HORRIBLE FIX to get the main receiver
string filename = getFileName();
if (filename.substr(filename.length() - 2) == "d0"){
is_main_receiver = true;
}
if (rest_hostname.empty()) {
FILE_LOG(logDEBUG) << __AT__ <<"can't initialize with empty string or NULL for detectorHostname";
throw;
}
else if (isInitialized == true) {
rest = new RestHelper() ;
rest->init(rest_hostname, rest_port);
rest->set_connection_params(1, 3);
int code;
if (!is_main_receiver){
isInitialized = true;
status = slsReceiverDefs::IDLE;
return;
}
if (isInitialized == true) {
FILE_LOG(logDEBUG) << __AT__ << "already initialized, can't initialize several times";
}
else {
FILE_LOG(logDEBUG) << __AT__ << "with receiverHostName=" << rest_hostname << ":" << rest_port;
rest = new RestHelper() ;
std::string answer;
int code;
try{
rest->init(rest_hostname, rest_port);
code = get_rest_state(rest, &answer);
std::cout << "AAAAAAAa " << answer << std::endl;
rest_state = get_rest_state(rest);
if (code != 0){
FILE_LOG(logERROR) << __AT__ << " REST state returned: " << answer;
if (rest_state == ""){
FILE_LOG(logERROR) << __AT__ << " REST state returned: " << rest_state;
throw;
}
else{
@ -116,6 +136,7 @@ void UDPRESTImplementation::initialize_REST(){
throw;
}
//JsonBox::Object json_object;
//json_object["configfile"] = JsonBox::Value("FILENAME");
JsonBox::Value json_request;
@ -129,20 +150,24 @@ void UDPRESTImplementation::initialize_REST(){
//ss << json_request;
ss >> test;
rest_state = get_rest_state(rest);
code = rest->get_json("state", &answer);
//code = rest->get_json("v1/state", &answer);
FILE_LOG(logDEBUG) << __AT__ << " state got " << code << " " << answer << "\n";
if (answer != "INITIALIZED"){
test = "{\"path\":\"" + string( getFilePath() ) + "\"}";
code = rest->post_json("state/initialize", &answer, test);
if (rest_state != "INITIALIZED"){
test = "{\"path\":\"" + string( getFilePath() ) + "\", \"n_frames\":10}";
code = rest->post_json("v1/state/initialize", &answer, test);
}
else{
test = "{\"path\":\"" + string( getFilePath() ) + "\"}";
code = rest->post_json("state/configure", &answer, test);
test = "{\"path\":\"" + string( getFilePath() ) + "\", \"n_frames\":10}";
code = rest->post_json("v1/state/configure", &answer, test);
}
FILE_LOG(logDEBUG) << __AT__ << " state/configure got " << code;
code = rest->get_json("state", &answer);
FILE_LOG(logDEBUG) << __AT__ << " state got " << code << " " << answer << "\n";
rest_state = get_rest_state(rest);
FILE_LOG(logDEBUG) << __AT__ << " state got " << rest_state << "\n";
/*
@ -152,9 +177,7 @@ void UDPRESTImplementation::initialize_REST(){
std::cout << "JSON " << json_value["status"] << std::endl;
*/
}
FILE_LOG(logDEBUG) << __func__ << ": initialize() done";
}
@ -171,8 +194,6 @@ int UDPRESTImplementation::startReceiver(char message[]){
initialize_REST();
FILE_LOG(logDEBUG) << __FILE__ << "::" << __func__ << " initialized";
cout << "Starting Receiver" << endl;
std::string answer;
int code;
//char *intStr = itoa(a);
@ -186,17 +207,19 @@ int UDPRESTImplementation::startReceiver(char message[]){
string str_n = ss2.str();
cout << "Starting Receiver" << endl;
std::string request_body = "{\"settings\": {\"bit_depth\": " + str_dr + ", \"nimages\": " + str_n + "}}";
string rest_state = "";
std::string request_body = "{\"settings\": {\"bit_depth\": " + str_dr + ", \"n_frames\": " + str_n + "}}";
//std::string request_body = "{\"settings\": {\"nimages\":1, \"scanid\":999, \"bit_depth\":16}}";
if(is_main_receiver){
FILE_LOG(logDEBUG) << __FILE__ << "::" << " sending this configuration body: " << request_body;
code = rest->post_json("state/configure", &answer, request_body);
code = rest->get_json("state", &answer);
code = rest->post_json("v1/state/configure", &answer, request_body);
code = rest->get_json("v1/state", &answer);
FILE_LOG(logDEBUG) << __FILE__ << "::" << " got: " << answer;
//code = rest->post_json("state/open", &answer);
//code = rest->get_json("state", &answer);
rest_state = get_rest_state(rest);
code = rest->post_json("v1/state/open", &answer);
}
status = RUNNING;
@ -251,6 +274,7 @@ int UDPRESTImplementation::shutDownUDPSockets(){
FILE_LOG(logDEBUG) << __AT__ << "called";
// this is just to be sure, it could be removed
/*
for(int i=0;i<numListeningThreads;i++){
if(udpSocket[i]){
FILE_LOG(logDEBUG) << __AT__ << " closing UDP socket #" << i;
@ -259,43 +283,42 @@ int UDPRESTImplementation::shutDownUDPSockets(){
udpSocket[i] = NULL;
}
}
*/
JsonBox::Value answer;
int code;
string be_state = "";
string rest_state = "";
FILE_LOG(logDEBUG) << __AT__ << " numListeningThreads=" << numListeningThreads;
//FILE_LOG(logDEBUG) << __AT__ << " numListeningThreads=" << numListeningThreads;
if (rest == NULL){
FILE_LOG(logWARNING) << __AT__ << "No REST object initialized, closing...";
return OK;
}
// getting the state
FILE_LOG(logWARNING) << "PLEASE WAIT WHILE CHECKING AND SHUTTING DOWN ALL CONNECTIONS!";
code = rest->get_json("state", &answer);
be_state = answer["state"].getString();
if (is_main_receiver){
// LEO: this is probably wrong
if (be_state == "OPEN"){
while (be_state != "TRANSIENT"){
code = rest->get_json("state", &answer);
be_state = answer["state"].getString();
cout << "be_State: " << be_state << endl;
FILE_LOG(logWARNING) << "PLEASE WAIT WHILE CHECKING AND SHUTTING DOWN ALL CONNECTIONS!";
rest_state = get_rest_state(rest);
while (rest_state != "OPEN"){
rest_state = get_rest_state(rest);
usleep(10000);
}
//while (rest_state != "TRANSIENT"){
// rest_state = get_rest_state(rest);
// usleep(10000);
//}
code = rest->post_json("state/close", &answer);
std::cout <<code << " " << answer << std::endl;
code = rest->post_json("state/reset", &answer);
std::cout << code << " " << answer << std::endl;
code = rest->post_json("v1/state/close", &answer);
code = rest->post_json("v1/state/reset", &answer);
code = rest->get_json("state", &answer);
std::cout << code << " " << answer << std::endl;
//rest_state = get_rest_state(rest);
//std::cout << rest_state << std::endl;
}
status = slsReceiverDefs::RUN_FINISHED;
//LEO: not sure it's needed
delete rest;
//delete rest;
FILE_LOG(logDEBUG) << __AT__ << "finished";
return OK;
@ -308,7 +331,7 @@ int UDPRESTImplementation::shutDownUDPSockets(){
* in your gui to get data from receiver. you probably have a different way
* of reconstructing complete data set from all receivers
*/
void UDPRESTImplementation::readFramee(char* c,char** raw, uint64_t &startAcq, uint64_t &startFrame){
void UDPRESTImplementation::readFrame(char* c,char** raw, uint64_t &startAcq, uint64_t &startFrame){
FILE_LOG(logDEBUG) << __AT__ << " called";
strcpy(c,"");
*raw = NULL;
@ -317,6 +340,7 @@ void UDPRESTImplementation::readFramee(char* c,char** raw, uint64_t &startAcq, u
/* FIXME
* Its called by TCP in case of illegal shut down such as Ctrl + c.
* Upto you what you want to do with it.
@ -327,5 +351,11 @@ void UDPRESTImplementation::closeFile(int ithr){
}
uint64_t UDPRESTImplementation::getTotalFramesCaught() const{
FILE_LOG(logDEBUG) << __AT__ << " starting";
return (0);
}
#endif