Merge branch '9m_rest' into developer

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

View File

@ -18,6 +18,7 @@
#include <Poco/Timespan.h> #include <Poco/Timespan.h>
#include "JsonBox/Value.h" #include "JsonBox/Value.h"
#include "JsonBox/JsonParsingError.h"
//#include "logger.h" //#include "logger.h"
@ -220,8 +221,18 @@ class RestHelper {
string answer; string answer;
int code = send_request(session, req, &answer, request_body); int code = send_request(session, req, &answer, request_body);
if(code==0){ if(code==0){
try{
json_value->loadFromString(answer); 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; delete uri;
return code; return code;
} }

View File

@ -42,7 +42,7 @@ public:
/** /**
* Get Rest State * 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); void closeFile(int i = -1);
private: uint64_t getTotalFramesCaught() const;
private:
bool isInitialized; bool isInitialized;
RestHelper * rest ; RestHelper * rest ;
int rest_port; // receiver backend port int rest_port; // receiver backend port
string rest_hostname; // receiver hostname string rest_hostname; // receiver hostname
bool is_main_receiver;
}; };

View File

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

View File

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