diff --git a/slsDetectorSoftware/slsReceiver/RestHelper.h b/slsDetectorSoftware/slsReceiver/RestHelper.h index 248ec792f..2df557cb7 100644 --- a/slsDetectorSoftware/slsReceiver/RestHelper.h +++ b/slsDetectorSoftware/slsReceiver/RestHelper.h @@ -5,33 +5,74 @@ #include #include #include +#include #include "JsonBox/Value.h" #include #include +#include + +// HTTP timeout in seconds, default is 8 +#define HTTP_TIMEOUT 10 +// Number of connection tries +#define N_CONNECTION_TRIES 0 using namespace Poco::Net; using namespace Poco; - +using namespace std; class RestHelper { public: - void init(std::string hostname, int port){ + void init(string hostname, int port){ /* */ full_hostname = "http://"+hostname; - std::cout << full_hostname << std::endl; + cout << full_hostname << endl; session = new HTTPClientSession(hostname,port ); + session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) ); }; - int get_json(std::string request, std::string* answer){ + int get_json(string request, string* answer){ //TODO: implement a timeout and max_retries uri = new URI(full_hostname+"/"+request); - std::string path(uri->getPathAndQuery()); + string path(uri->getPathAndQuery()); + if (path.empty()) path = "/"; + + // send request + //HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); + req = new HTTPRequest(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); + + try { + session->sendRequest(*req); + } + catch (exception& e){ + cout << "Exception:"<< e.what() << '\n'; + } + + int code = send_request(session, req); + + // get response + //if (code!=0){ + HTTPResponse res; + cout << res.getStatus() << " " << res.getReason() << endl; + istream &is = session->receiveResponse(res); + StreamCopier::copyToString(is, *answer); + + return res.getStatus(); + //} + //else + //return code; + }; + + int get_json(string request, JsonBox::Value* json_value){ + //TODO: implement a timeout and max_retries + + uri = new URI(full_hostname+"/"+request); + string path(uri->getPathAndQuery()); if (path.empty()) path = "/"; // send request @@ -40,32 +81,12 @@ class RestHelper { // get response HTTPResponse res; - //std::cout << res.getStatus() << " " << res.getReason() << std::endl; - std::istream &is = session->receiveResponse(res); - StreamCopier::copyToString(is, *answer); - - return res.getStatus(); - }; - - int get_json(std::string request, JsonBox::Value* json_value){ - //TODO: implement a timeout and max_retries - - uri = new URI(full_hostname+"/"+request); - std::string path(uri->getPathAndQuery()); - if (path.empty()) path = "/"; - - // send request - HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); - session->sendRequest(req); - - // get response - HTTPResponse res; - //std::cout << res.getStatus() << " " << res.getReason() << std::endl; - std::string answer; - std::istream &is = session->receiveResponse(res); + //cout << res.getStatus() << " " << res.getReason() << endl; + string answer; + istream &is = session->receiveResponse(res); StreamCopier::copyToString(is, answer); - std::cout << answer << std::endl; + cout << answer << endl; //returning a Json struct json_value->loadFromString(answer); @@ -75,5 +96,26 @@ class RestHelper { private: URI * uri; HTTPClientSession *session; - std::string full_hostname; + HTTPRequest *req; + + string full_hostname; + + int send_request(HTTPClientSession *session, HTTPRequest *req){ + int n=0; + int code = -1; + while(nsendRequest( (*req) ); + code = 0; + } + catch (exception& e){ + cout << "Exception:"<< e.what() << ", sleeping " << HTTP_TIMEOUT << " seconds\n"; + sleep(HTTP_TIMEOUT); + } + n+=1; + } + + return code; + } + }; diff --git a/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp b/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp index 6b679db5b..9bc25ae25 100644 --- a/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp +++ b/slsDetectorSoftware/slsReceiver/eigerReceiver.cpp @@ -73,11 +73,11 @@ public: rest.init("localhost",8080); std::string answer; int code = rest.get_json("status", &answer); - std::cout << answer << std::endl; + std::cout << "Answer: " << answer << std::endl; - JsonBox::Value json_value; - code = rest.get_json("status", &json_value); - std::cout << "JSON " << json_value["status"] << std::endl; + //JsonBox::Value json_value; + //code = rest.get_json("status", &json_value); + //std::cout << "JSON " << json_value["status"] << std::endl; }