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:
sala 2014-03-24 16:20:41 +00:00
parent c44b8d34c7
commit 87407b5529
2 changed files with 76 additions and 34 deletions

View File

@ -5,53 +5,74 @@
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include <Poco/Timespan.h>
#include "JsonBox/Value.h"
#include <iostream>
#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;
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);
session->sendRequest(req);
//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;
//std::cout << res.getStatus() << " " << res.getReason() << std::endl;
std::istream &is = session->receiveResponse(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(std::string request, JsonBox::Value* json_value){
int get_json(string request, JsonBox::Value* json_value){
//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
@ -60,12 +81,12 @@ class RestHelper {
// 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(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;
}
};

View File

@ -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;
}