Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
rpc_client.h
Go to the documentation of this file.
1
// Copyrights(c) 2017-2021, The Electroneum Project
2
// Copyrights(c) 2014-2019, The Monero Project
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without modification, are
7
// permitted provided that the following conditions are met:
8
//
9
// 1. Redistributions of source code must retain the above copyright notice, this list of
10
// conditions and the following disclaimer.
11
//
12
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13
// of conditions and the following disclaimer in the documentation and/or other
14
// materials provided with the distribution.
15
//
16
// 3. Neither the name of the copyright holder nor the names of its contributors may be
17
// used to endorse or promote products derived from this software without specific
18
// prior written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
#pragma once
31
32
#include <boost/optional/optional.hpp>
33
34
#include "
common/http_connection.h
"
35
#include "
common/scoped_message_writer.h
"
36
#include "
rpc/core_rpc_server_commands_defs.h
"
37
#include "
storages/http_abstract_invoke.h
"
38
#include "
net/http_auth.h
"
39
#include "
net/http_client.h
"
40
#include "
net/net_ssl.h
"
41
#include "
string_tools.h
"
42
43
namespace
tools
44
{
45
class
t_rpc_client
final
46
{
47
private
:
48
epee::net_utils::http::http_simple_client
m_http_client;
49
public
:
50
t_rpc_client
(
51
uint32_t
ip
52
,
uint16_t
port
53
, boost::optional<epee::net_utils::http::login> user
54
,
epee::net_utils::ssl_options_t
ssl_options
55
)
56
: m_http_client{}
57
{
58
m_http_client.set_server(
59
epee::string_tools::get_ip_string_from_int32
(ip), std::to_string(port), std::move(user), std::move(ssl_options)
60
);
61
}
62
63
template
<
typename
T_req,
typename
T_res>
64
bool
basic_json_rpc_request
(
65
T_req & req
66
, T_res &
res
67
, std::string
const
& method_name
68
)
69
{
70
t_http_connection
connection
(&m_http_client);
71
72
bool
ok =
connection
.is_open();
73
if
(!ok)
74
{
75
fail_msg_writer
() <<
"Couldn't connect to daemon: "
<< m_http_client.get_host() <<
":"
<< m_http_client.get_port();
76
return
false
;
77
}
78
ok =
epee::net_utils::invoke_http_json_rpc
(
"/json_rpc"
, method_name, req,
res
, m_http_client,
t_http_connection::TIMEOUT
());
79
if
(!ok)
80
{
81
fail_msg_writer
() <<
"basic_json_rpc_request: Daemon request failed"
;
82
return
false
;
83
}
84
else
85
{
86
return
true
;
87
}
88
}
89
90
template
<
typename
T_req,
typename
T_res>
91
bool
json_rpc_request
(
92
T_req & req
93
, T_res &
res
94
, std::string
const
& method_name
95
, std::string
const
& fail_msg
96
)
97
{
98
t_http_connection
connection
(&m_http_client);
99
100
bool
ok =
connection
.is_open();
101
if
(!ok)
102
{
103
fail_msg_writer
() <<
"Couldn't connect to daemon: "
<< m_http_client.get_host() <<
":"
<< m_http_client.get_port();
104
return
false
;
105
}
106
ok =
epee::net_utils::invoke_http_json_rpc
(
"/json_rpc"
, method_name, req,
res
, m_http_client,
t_http_connection::TIMEOUT
());
107
if
(!ok ||
res
.status !=
CORE_RPC_STATUS_OK
)
// TODO - handle CORE_RPC_STATUS_BUSY ?
108
{
109
fail_msg_writer
() << fail_msg <<
" -- json_rpc_request: "
<<
res
.status;
110
return
false
;
111
}
112
else
113
{
114
return
true
;
115
}
116
}
117
118
template
<
typename
T_req,
typename
T_res>
119
bool
rpc_request
(
120
T_req & req
121
, T_res &
res
122
, std::string
const
& relative_url
123
, std::string
const
& fail_msg
124
)
125
{
126
t_http_connection
connection
(&m_http_client);
127
128
bool
ok =
connection
.is_open();
129
if
(!ok)
130
{
131
fail_msg_writer
() <<
"Couldn't connect to daemon: "
<< m_http_client.get_host() <<
":"
<< m_http_client.get_port();
132
return
false
;
133
}
134
ok =
epee::net_utils::invoke_http_json
(relative_url, req,
res
, m_http_client,
t_http_connection::TIMEOUT
());
135
if
(!ok ||
res
.status !=
CORE_RPC_STATUS_OK
)
// TODO - handle CORE_RPC_STATUS_BUSY ?
136
{
137
fail_msg_writer
() << fail_msg <<
"-- rpc_request: "
<<
res
.status;
138
return
false
;
139
}
140
else
141
{
142
return
true
;
143
}
144
}
145
146
bool
check_connection
()
147
{
148
t_http_connection
connection
(&m_http_client);
149
return
connection
.is_open();
150
}
151
};
152
}
connection
connection(typename TProtocol::config_type &ref_config)
Definition
abstract_tcp_server_cp.h:188
epee::net_utils::ssl_options_t
Definition
net_ssl.h:74
tools::t_http_connection
Definition
http_connection.h:38
tools::t_http_connection::TIMEOUT
static constexpr std::chrono::seconds TIMEOUT()
Definition
http_connection.h:43
tools::t_rpc_client::check_connection
bool check_connection()
Definition
rpc_client.h:146
tools::t_rpc_client::t_rpc_client
t_rpc_client(uint32_t ip, uint16_t port, boost::optional< epee::net_utils::http::login > user, epee::net_utils::ssl_options_t ssl_options)
Definition
rpc_client.h:50
tools::t_rpc_client::basic_json_rpc_request
bool basic_json_rpc_request(T_req &req, T_res &res, std::string const &method_name)
Definition
rpc_client.h:64
tools::t_rpc_client::json_rpc_request
bool json_rpc_request(T_req &req, T_res &res, std::string const &method_name, std::string const &fail_msg)
Definition
rpc_client.h:91
tools::t_rpc_client::rpc_request
bool rpc_request(T_req &req, T_res &res, std::string const &relative_url, std::string const &fail_msg)
Definition
rpc_client.h:119
core_rpc_server_commands_defs.h
CORE_RPC_STATUS_OK
#define CORE_RPC_STATUS_OK
Definition
core_rpc_server_commands_defs.h:72
res
const char * res
Definition
hmac_keccak.cpp:41
http_abstract_invoke.h
http_auth.h
http_client.h
http_connection.h
epee::net_utils::http::http_simple_client
http_simple_client_template< blocked_mode_client > http_simple_client
Definition
http_client.h:1018
epee::net_utils::invoke_http_json
bool invoke_http_json(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct, t_transport &transport, std::chrono::milliseconds timeout=std::chrono::seconds(15), const boost::string_ref method="GET")
Definition
http_abstract_invoke.h:41
epee::net_utils::invoke_http_json_rpc
bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request &out_struct, t_response &result_struct, t_transport &transport, std::chrono::milliseconds timeout=std::chrono::seconds(15), const boost::string_ref http_method="GET", const std::string &req_id="0")
Definition
http_abstract_invoke.h:138
epee::string_tools::get_ip_string_from_int32
std::string get_ip_string_from_int32(uint32_t ip)
Definition
string_tools.cpp:42
tools
Various Tools.
Definition
tools.cpp:31
tools::fail_msg_writer
scoped_message_writer fail_msg_writer()
Definition
scoped_message_writer.h:131
net_ssl.h
scoped_message_writer.h
uint16_t
unsigned short uint16_t
Definition
stdint.h:125
uint32_t
unsigned int uint32_t
Definition
stdint.h:126
string_tools.h
src
common
rpc_client.h
Generated on
for Electroneum by
1.17.0