10 changed files with 180 additions and 13 deletions
@ -0,0 +1,48 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
#include "CURLRequest.h" |
|||
|
|||
using namespace std; |
|||
|
|||
static size_t write_data(void *buffer, size_t elementSize, size_t numberOfElements, void *userp) |
|||
{ |
|||
static_cast<stringstream *>(userp)->write((const char *)buffer, elementSize * numberOfElements); |
|||
return elementSize * numberOfElements; |
|||
} |
|||
|
|||
void CURLRequest::commonCURLPreparation() |
|||
{ |
|||
m_resultBuffer.str(""); |
|||
curl_easy_setopt(m_curl, CURLOPT_URL, (m_url + "?").c_str()); |
|||
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L); |
|||
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, write_data); |
|||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &m_resultBuffer); |
|||
} |
|||
|
|||
std::tuple<long, string> CURLRequest::commonCURLPerform() |
|||
{ |
|||
CURLcode res = curl_easy_perform(m_curl); |
|||
if (res != CURLE_OK) { |
|||
throw runtime_error(curl_easy_strerror(res)); |
|||
} |
|||
long httpCode = 0; |
|||
curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &httpCode); |
|||
return make_tuple(httpCode, m_resultBuffer.str()); |
|||
} |
|||
|
|||
std::tuple<long, string> CURLRequest::post() |
|||
{ |
|||
commonCURLPreparation(); |
|||
curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, m_body.c_str()); |
|||
|
|||
struct curl_slist *headerList = NULL; |
|||
headerList = curl_slist_append(headerList, "Content-Type: application/json"); |
|||
curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, headerList); |
|||
|
|||
auto result = commonCURLPerform(); |
|||
|
|||
curl_slist_free_all(headerList); |
|||
return result; |
|||
} |
@ -0,0 +1,38 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
// based on http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c/27026683#27026683
|
|||
|
|||
#include <stdio.h> |
|||
#include <sstream> |
|||
#include <unordered_map> |
|||
#include <curl/curl.h> |
|||
|
|||
class CURLRequest |
|||
{ |
|||
public: |
|||
CURLRequest(): m_curl(curl_easy_init()) {}; |
|||
~CURLRequest() |
|||
{ |
|||
if (m_curl) |
|||
curl_easy_cleanup(m_curl); |
|||
} |
|||
|
|||
void setUrl(std::string _url) { m_url = _url; } |
|||
void setBody(std::string _body) { m_body = _body; } |
|||
|
|||
std::tuple<long, std::string> post(); |
|||
|
|||
private: |
|||
std::string m_url; |
|||
std::string m_body; |
|||
|
|||
CURL* m_curl; |
|||
std::stringstream m_resultBuffer; |
|||
|
|||
void commonCURLPreparation(); |
|||
std::tuple<long, std::string> commonCURLPerform(); |
|||
}; |
|||
|
|||
|
@ -0,0 +1,5 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
#include "JSRemoteConsole.h" |
@ -0,0 +1,22 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
#include <libjsengine/JSV8Printer.h> |
|||
#include "JSConsole.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSRemoteConsole: public JSConsole<JSV8Engine, JSV8Printer> |
|||
{ |
|||
|
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
#include "JSV8RemoteConnector.h" |
|||
#include "CURLRequest.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
void JSV8RemoteConnector::onSend(char const* _payload) |
|||
{ |
|||
CURLRequest request; |
|||
request.setUrl(m_url); |
|||
request.setBody(_payload); |
|||
long code; |
|||
string response; |
|||
tie(code, response) = request.post(); |
|||
(void)code; |
|||
m_lastResponse = response.c_str(); |
|||
} |
@ -0,0 +1,30 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 15/06/15.
|
|||
//
|
|||
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <libjsengine/JSV8RPC.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8RemoteConnector : public JSV8RPC |
|||
{ |
|||
|
|||
public: |
|||
JSV8RemoteConnector(JSV8Engine const& _engine, std::string _url): JSV8RPC(_engine), m_url(_url) {} |
|||
virtual ~JSV8RemoteConnector(); |
|||
|
|||
// implement JSV8RPC interface
|
|||
void onSend(char const* _payload); |
|||
|
|||
private: |
|||
std::string m_url; |
|||
}; |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue