wip for Eiger9M

This commit is contained in:
sala 2016-12-01 07:43:46 +00:00
parent b9ac75330c
commit f61d819599
4 changed files with 162 additions and 113 deletions

View File

@ -167,8 +167,8 @@ class RestHelper {
string answer; string answer;
int code = send_request(session, req, &answer); int code = send_request(session, req, &answer);
if(code == 0 ) { if(code == 0 ) {
FILE_LOG(logDEBUG) << __AT__ << " REQUEST: " << " ANSWER: " << answer; //FILE_LOG(logDEBUG) << __AT__ << " REQUEST: " << " ANSWER: " << answer;
json_value->loadFromString(answer); json_value->loadFromString(answer);
} }
delete uri; delete uri;
return code; return code;

View File

@ -42,8 +42,8 @@ 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*/);
/************************************************************************* /*************************************************************************
* Setters *************************************************************** * Setters ***************************************************************
@ -138,12 +138,15 @@ public:
*/ */
void closeFile(int i = -1); void closeFile(int i = -1);
uint64_t getTotalFramesCaught() const;
private: 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

@ -29,10 +29,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 +43,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 +67,20 @@ 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);
int code = rest->get_json("v1/state", &answer);
if ( code != -1 ){ 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__ << " called";
FILE_LOG(logDEBUG) << __AT__ << " REST status is initialized: " << isInitialized; 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()) { 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() ;
FILE_LOG(logDEBUG) << __AT__ << "already initialized, can't initialize several times"; rest->init(rest_hostname, rest_port);
} rest->set_connection_params(1, 3);
else {
FILE_LOG(logDEBUG) << __AT__ << "with receiverHostName=" << rest_hostname << ":" << rest_port; int code;
rest = new RestHelper() ; if (!is_main_receiver){
std::string answer; isInitialized = true;
int code; status = slsReceiverDefs::IDLE;
try{ return;
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 (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"; FILE_LOG(logDEBUG) << __func__ << ": initialize() done";
} }
@ -187,19 +217,23 @@ int UDPRESTImplementation::startReceiver(char message[]){
cout << "Starting Receiver" << endl; cout << "Starting Receiver" << endl;
string rest_state = "";
std::string request_body = "{\"settings\": {\"bit_depth\": " + str_dr + ", \"nimages\": " + str_n + "}}"; 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}}"; //std::string request_body = "{\"settings\": {\"nimages\":1, \"scanid\":999, \"bit_depth\":16}}";
FILE_LOG(logDEBUG) << __FILE__ << "::" << " sending this configuration body: " << request_body; if(is_main_receiver){
code = rest->post_json("state/configure", &answer, request_body); FILE_LOG(logDEBUG) << __FILE__ << "::" << " sending this configuration body: " << request_body;
code = rest->get_json("state", &answer); code = rest->post_json("v1/state/configure", &answer, request_body);
FILE_LOG(logDEBUG) << __FILE__ << "::" << " got: " << answer; 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->post_json("v1/state/open", &answer);
//code = rest->get_json("state", &answer); std::cout << "AAAAAA " << rest_state << std::endl;
}
status = RUNNING; status = RUNNING;
return OK; return OK;
} }
@ -251,6 +285,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 +294,46 @@ 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(); FILE_LOG(logWARNING) << "PLEASE WAIT WHILE CHECKING AND SHUTTING DOWN ALL CONNECTIONS!";
rest_state = get_rest_state(rest);
// LEO: this is probably wrong std::cout << rest_state << std::endl;
if (be_state == "OPEN"){
while (be_state != "TRANSIENT"){ while (rest_state != "OPEN"){
code = rest->get_json("state", &answer); rest_state = get_rest_state(rest);
be_state = answer["state"].getString(); std::cout << rest_state << std::endl;
cout << "be_State: " << be_state << endl; usleep(10000);
usleep(10000); }
} //while (rest_state != "TRANSIENT"){
// rest_state = get_rest_state(rest);
code = rest->post_json("state/close", &answer); // usleep(10000);
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);
std::cout <<code << " " << answer << std::endl;
code = rest->get_json("state", &answer); code = rest->post_json("v1/state/reset", &answer);
std::cout << code << " " << answer << std::endl; std::cout << code << " " << answer << std::endl;
rest_state = get_rest_state(rest);
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 +346,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 +355,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 +366,11 @@ void UDPRESTImplementation::closeFile(int ithr){
} }
uint64_t UDPRESTImplementation::getTotalFramesCaught() const{
FILE_LOG(logDEBUG) << __AT__ << " starting";
return (0);
}
#endif #endif