From f61d8195994540d88e2043ac9900290dbfd77509 Mon Sep 17 00:00:00 2001 From: Leonardo Sala Date: Thu, 1 Dec 2016 07:43:46 +0000 Subject: [PATCH] wip for Eiger9M --- slsReceiverSoftware/include/RestHelper.h | 4 +- .../include/UDPRESTImplementation.h | 9 +- slsReceiverSoftware/src/UDPInterface.cpp | 1 + .../src/UDPRESTImplementation.cpp | 261 ++++++++++-------- 4 files changed, 162 insertions(+), 113 deletions(-) diff --git a/slsReceiverSoftware/include/RestHelper.h b/slsReceiverSoftware/include/RestHelper.h index 92846750d..f808cea01 100644 --- a/slsReceiverSoftware/include/RestHelper.h +++ b/slsReceiverSoftware/include/RestHelper.h @@ -167,8 +167,8 @@ class RestHelper { string answer; int code = send_request(session, req, &answer); if(code == 0 ) { - FILE_LOG(logDEBUG) << __AT__ << " REQUEST: " << " ANSWER: " << answer; - json_value->loadFromString(answer); + //FILE_LOG(logDEBUG) << __AT__ << " REQUEST: " << " ANSWER: " << answer; + json_value->loadFromString(answer); } delete uri; return code; diff --git a/slsReceiverSoftware/include/UDPRESTImplementation.h b/slsReceiverSoftware/include/UDPRESTImplementation.h index 374199721..14296f648 100644 --- a/slsReceiverSoftware/include/UDPRESTImplementation.h +++ b/slsReceiverSoftware/include/UDPRESTImplementation.h @@ -42,8 +42,8 @@ public: /** * Get Rest State */ - int get_rest_state(RestHelper * rest, string *rest_state); - + string get_rest_state(RestHelper * rest/*, string *rest_state*/); + /************************************************************************* * Setters *************************************************************** @@ -138,12 +138,15 @@ public: */ void closeFile(int i = -1); + uint64_t getTotalFramesCaught() const; + + private: - bool isInitialized; RestHelper * rest ; int rest_port; // receiver backend port string rest_hostname; // receiver hostname + bool is_main_receiver; }; diff --git a/slsReceiverSoftware/src/UDPInterface.cpp b/slsReceiverSoftware/src/UDPInterface.cpp index 17491bc3a..a6bd2cfac 100644 --- a/slsReceiverSoftware/src/UDPInterface.cpp +++ b/slsReceiverSoftware/src/UDPInterface.cpp @@ -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 diff --git a/slsReceiverSoftware/src/UDPRESTImplementation.cpp b/slsReceiverSoftware/src/UDPRESTImplementation.cpp index ab650257f..f16fadb03 100644 --- a/slsReceiverSoftware/src/UDPRESTImplementation.cpp +++ b/slsReceiverSoftware/src/UDPRESTImplementation.cpp @@ -29,10 +29,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 +43,7 @@ UDPRESTImplementation::~UDPRESTImplementation(){ void UDPRESTImplementation::configure(map config_map){ + // am I ever getting there? FILE_LOG(logWARNING) << __AT__ << " called"; map::const_iterator pos; @@ -65,16 +67,20 @@ void UDPRESTImplementation::configure(map 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); + string rest_state = ""; + + int code = rest->get_json("v1/state", &answer); if ( code != -1 ){ - (*rest_state) = answer["state"].getString(); + + rest_state = answer["global_state"].getString(); + std::cout << rest_state << std::endl; } + //rest_state = *prs; - return code; + return rest_state; } @@ -83,78 +89,102 @@ void UDPRESTImplementation::initialize_REST(){ FILE_LOG(logDEBUG) << __AT__ << " called"; FILE_LOG(logDEBUG) << __AT__ << " REST status is initialized: " << isInitialized; + + std::cout<< fileIndex << " " << getUDPPortNumber() << std::endl; + string rest_state = ""; + std::string answer = ""; + + // HORRIBLE FIX ME ASAP! + if (getUDPPortNumber() == 50011){ + is_main_receiver = true; + } + + // if (is_main_receiver){ + 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) { - 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() ; + rest->init(rest_hostname, rest_port); + rest->set_connection_params(1, 3); + + int code; - 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; - - - if (code != 0){ - FILE_LOG(logERROR) << __AT__ << " REST state returned: " << answer; - throw; - } - else{ - isInitialized = true; - status = slsReceiverDefs::IDLE; - } - FILE_LOG(logDEBUG) << __func__ << "Answer: " << answer; - } - catch(std::string e){ - FILE_LOG(logERROR) << __func__ << ": " << e; - throw; - } - - //JsonBox::Object json_object; - //json_object["configfile"] = JsonBox::Value("FILENAME"); - JsonBox::Value json_request; - //json_request["configfile"] = "config.py"; - json_request["path"] = filePath; - - stringstream ss; - string test; - //std::cout << "GetSTring: " << json_request << std::endl; - json_request.writeToStream(ss, false); - //ss << json_request; - ss >> test; - - - code = rest->get_json("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); - } - else{ - test = "{\"path\":\"" + string( getFilePath() ) + "\"}"; - code = rest->post_json("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"; - - - /* - std::std::cout << string << std::endl; << "---- REST test 3: true, json object "<< std::endl; - JsonBox::Value json_value; - code = rest.get_json("status", &json_value); - std::cout << "JSON " << json_value["status"] << std::endl; - */ + 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; + + try{ + rest_state = get_rest_state(rest); + std::cout << "AAAAAAAa " << rest_state << std::endl; + + + if (rest_state == ""){ + FILE_LOG(logERROR) << __AT__ << " REST state returned: " << rest_state; + throw; + } + else{ + isInitialized = true; + status = slsReceiverDefs::IDLE; + } + FILE_LOG(logDEBUG) << __func__ << "Answer: " << answer; + } + catch(std::string e){ + FILE_LOG(logERROR) << __func__ << ": " << e; + throw; + } + + + //JsonBox::Object json_object; + //json_object["configfile"] = JsonBox::Value("FILENAME"); + JsonBox::Value json_request; + //json_request["configfile"] = "config.py"; + json_request["path"] = filePath; + + stringstream ss; + string test; + //std::cout << "GetSTring: " << json_request << std::endl; + json_request.writeToStream(ss, false); + //ss << json_request; + ss >> test; + + rest_state = get_rest_state(rest); + + //code = rest->get_json("v1/state", &answer); + FILE_LOG(logDEBUG) << __AT__ << " state got " << code << " " << answer << "\n"; + 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() ) + "\"}"; + test = "{\"path\":\"" + string( getFilePath() ) + "\", \"n_frames\":10}"; + code = rest->post_json("v1/state/configure", &answer, test); + } + FILE_LOG(logDEBUG) << __AT__ << " state/configure got " << code; + + rest_state = get_rest_state(rest); + + FILE_LOG(logDEBUG) << __AT__ << " state got " << rest_state << "\n"; + + + /* + std::std::cout << string << std::endl; << "---- REST test 3: true, json object "<< std::endl; + JsonBox::Value json_value; + code = rest.get_json("status", &json_value); + std::cout << "JSON " << json_value["status"] << std::endl; + */ + } FILE_LOG(logDEBUG) << __func__ << ": initialize() done"; - } @@ -187,19 +217,23 @@ int UDPRESTImplementation::startReceiver(char message[]){ 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}}"; - 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); - FILE_LOG(logDEBUG) << __FILE__ << "::" << " got: " << answer; + if(is_main_receiver){ + FILE_LOG(logDEBUG) << __FILE__ << "::" << " sending this configuration body: " << request_body; + code = rest->post_json("v1/state/configure", &answer, request_body); + code = rest->get_json("v1/state", &answer); + FILE_LOG(logDEBUG) << __FILE__ << "::" << " got: " << answer; + + rest_state = get_rest_state(rest); - //code = rest->post_json("state/open", &answer); - //code = rest->get_json("state", &answer); + code = rest->post_json("v1/state/open", &answer); + std::cout << "AAAAAA " << rest_state << std::endl; + } status = RUNNING; - + return OK; } @@ -251,6 +285,7 @@ int UDPRESTImplementation::shutDownUDPSockets(){ FILE_LOG(logDEBUG) << __AT__ << "called"; // this is just to be sure, it could be removed + /* for(int i=0;iget_json("state", &answer); - be_state = answer["state"].getString(); - - // 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; - usleep(10000); - } - - code = rest->post_json("state/close", &answer); - std::cout <post_json("state/reset", &answer); - std::cout << code << " " << answer << std::endl; - - code = rest->get_json("state", &answer); - std::cout << code << " " << answer << std::endl; + if (is_main_receiver){ + + FILE_LOG(logWARNING) << "PLEASE WAIT WHILE CHECKING AND SHUTTING DOWN ALL CONNECTIONS!"; + rest_state = get_rest_state(rest); + std::cout << rest_state << std::endl; + + while (rest_state != "OPEN"){ + rest_state = get_rest_state(rest); + std::cout << rest_state << std::endl; + usleep(10000); + } + //while (rest_state != "TRANSIENT"){ + // rest_state = get_rest_state(rest); + // usleep(10000); + //} + + code = rest->post_json("v1/state/close", &answer); + std::cout <post_json("v1/state/reset", &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 +346,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 +355,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 +366,11 @@ void UDPRESTImplementation::closeFile(int ithr){ } +uint64_t UDPRESTImplementation::getTotalFramesCaught() const{ + FILE_LOG(logDEBUG) << __AT__ << " starting"; + return (0); +} + + #endif