first implementation of REST and JSON interfaces for eigerReceiver

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@799 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
sala
2014-03-24 14:03:44 +00:00
parent 4d797fb3b1
commit c44b8d34c7
4 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,79 @@
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include "JsonBox/Value.h"
#include <iostream>
#include <string>
using namespace Poco::Net;
using namespace Poco;
class RestHelper {
public:
void init(std::string hostname, int port){
/*
*/
full_hostname = "http://"+hostname;
std::cout << full_hostname << std::endl;
session = new HTTPClientSession(hostname,port );
};
int get_json(std::string request, std::string* answer){
//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::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);
StreamCopier::copyToString(is, answer);
std::cout << answer << std::endl;
//returning a Json struct
json_value->loadFromString(answer);
return res.getStatus();
};
private:
URI * uri;
HTTPClientSession *session;
std::string full_hostname;
};