void RPCTestNode::sendRequest(const HttpRequest& httpReq, HttpResponse& httpResp) { TcpConnector connector(m_dispatcher, "127.0.0.1", m_rpcPort); TcpConnection connection = connector.connect(); TcpStreambuf streambuf(connection); std::iostream connectionStream(&streambuf); LOG_DEBUG("invoke rpc:" + httpReq.getMethod() + " " + httpReq.getBody()); connectionStream << httpReq; connectionStream.flush(); HttpParser parser; parser.receiveResponse(connectionStream, httpResp); }
bool RPCTestNode::submitBlock(const std::string& block) { HttpRequest httpReq; httpReq.setUrl("/json_rpc"); httpReq.addHeader("Host", "127.0.0.1:" + boost::lexical_cast<std::string>(m_rpcPort)); httpReq.addHeader("Content-Type", "application/json-rpc"); JsonValue request(cryptonote::JsonValue::OBJECT); JsonValue jsonRpc; jsonRpc = "2.0"; request.insert("jsonrpc", jsonRpc); JsonValue methodString; methodString = "submitblock"; request.insert("method", methodString); JsonValue id; id = "sync"; request.insert("id", id); JsonValue params(JsonValue::ARRAY); JsonValue blockstr; blockstr = block.c_str(); params.pushBack(blockstr); request.insert("params", params); std::stringstream jsonOutputStream; jsonOutputStream << request; httpReq.setBody(jsonOutputStream.str()); TcpConnector connector(m_dispatcher, "127.0.0.1", m_rpcPort); TcpConnection connection = connector.connect(); TcpStreambuf streambuf(connection); std::iostream connectionStream(&streambuf); LOG_DEBUG("invoke json-rpc: " + httpReq.getBody()); connectionStream << httpReq; connectionStream.flush(); HttpResponse httpResp; HttpParser parser; parser.receiveResponse(connectionStream, httpResp); connectionStream.flush(); if (httpResp.getStatus() != HttpResponse::STATUS_200) return false; epee::serialization::portable_storage ps; if (!ps.load_from_json(httpResp.getBody())) { LOG_ERROR("cannot parse response from daemon: " + httpResp.getBody()); return false; } epee::json_rpc::response<COMMAND_RPC_SUBMITBLOCK::response, epee::json_rpc::error> jsonRpcResponse; jsonRpcResponse.load(ps); if (jsonRpcResponse.error.code || jsonRpcResponse.error.message.size()) { LOG_ERROR("RPC call of submit_block returned error: " + TO_STRING(jsonRpcResponse.error.code) + ", message: " + jsonRpcResponse.error.message); return false; } if (jsonRpcResponse.result.status != CORE_RPC_STATUS_OK) return false; return true; }
void HttpClient::request(const HttpRequest &req, HttpResponse &res) { if (!m_connected) { connect(); } try { std::iostream stream(m_streamBuf.get()); HttpParser parser; stream << req; stream.flush(); parser.receiveResponse(stream, res); } catch (const std::exception &) { disconnect(); throw; } }