mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-28 09:10:01 +02:00
not really sure about these changes... added timeout/exceptions
git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@800 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
parent
c44b8d34c7
commit
87407b5529
@ -5,33 +5,74 @@
|
|||||||
#include <Poco/Path.h>
|
#include <Poco/Path.h>
|
||||||
#include <Poco/URI.h>
|
#include <Poco/URI.h>
|
||||||
#include <Poco/Exception.h>
|
#include <Poco/Exception.h>
|
||||||
|
#include <Poco/Timespan.h>
|
||||||
|
|
||||||
#include "JsonBox/Value.h"
|
#include "JsonBox/Value.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
// 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::Net;
|
||||||
using namespace Poco;
|
using namespace Poco;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
class RestHelper {
|
class RestHelper {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void init(std::string hostname, int port){
|
void init(string hostname, int port){
|
||||||
/*
|
/*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
full_hostname = "http://"+hostname;
|
full_hostname = "http://"+hostname;
|
||||||
std::cout << full_hostname << std::endl;
|
cout << full_hostname << endl;
|
||||||
session = new HTTPClientSession(hostname,port );
|
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
|
//TODO: implement a timeout and max_retries
|
||||||
|
|
||||||
uri = new URI(full_hostname+"/"+request);
|
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 = "/";
|
if (path.empty()) path = "/";
|
||||||
|
|
||||||
// send request
|
// send request
|
||||||
@ -40,32 +81,12 @@ class RestHelper {
|
|||||||
|
|
||||||
// get response
|
// get response
|
||||||
HTTPResponse res;
|
HTTPResponse res;
|
||||||
//std::cout << res.getStatus() << " " << res.getReason() << std::endl;
|
//cout << res.getStatus() << " " << res.getReason() << endl;
|
||||||
std::istream &is = session->receiveResponse(res);
|
string answer;
|
||||||
StreamCopier::copyToString(is, *answer);
|
istream &is = session->receiveResponse(res);
|
||||||
|
|
||||||
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);
|
|
||||||
StreamCopier::copyToString(is, answer);
|
StreamCopier::copyToString(is, answer);
|
||||||
|
|
||||||
std::cout << answer << std::endl;
|
cout << answer << endl;
|
||||||
//returning a Json struct
|
//returning a Json struct
|
||||||
json_value->loadFromString(answer);
|
json_value->loadFromString(answer);
|
||||||
|
|
||||||
@ -75,5 +96,26 @@ class RestHelper {
|
|||||||
private:
|
private:
|
||||||
URI * uri;
|
URI * uri;
|
||||||
HTTPClientSession *session;
|
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(n<N_CONNECTION_TRIES){
|
||||||
|
try {
|
||||||
|
session->sendRequest( (*req) );
|
||||||
|
code = 0;
|
||||||
|
}
|
||||||
|
catch (exception& e){
|
||||||
|
cout << "Exception:"<< e.what() << ", sleeping " << HTTP_TIMEOUT << " seconds\n";
|
||||||
|
sleep(HTTP_TIMEOUT);
|
||||||
|
}
|
||||||
|
n+=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -73,11 +73,11 @@ public:
|
|||||||
rest.init("localhost",8080);
|
rest.init("localhost",8080);
|
||||||
std::string answer;
|
std::string answer;
|
||||||
int code = rest.get_json("status", &answer);
|
int code = rest.get_json("status", &answer);
|
||||||
std::cout << answer << std::endl;
|
std::cout << "Answer: " << answer << std::endl;
|
||||||
|
|
||||||
JsonBox::Value json_value;
|
//JsonBox::Value json_value;
|
||||||
code = rest.get_json("status", &json_value);
|
//code = rest.get_json("status", &json_value);
|
||||||
std::cout << "JSON " << json_value["status"] << std::endl;
|
//std::cout << "JSON " << json_value["status"] << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user