Electroneum
rpc_client.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, 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"
37 #include "storages/http_abstract_invoke.h"
38 #include "net/http_auth.h"
39 #include "net/http_client.h"
40 #include "string_tools.h"
41 
42 namespace tools
43 {
44  class t_rpc_client final
45  {
46  private:
47  epee::net_utils::http::http_simple_client m_http_client;
48  public:
50  uint32_t ip
51  , uint16_t port
52  , boost::optional<epee::net_utils::http::login> user
53  )
54  : m_http_client{}
55  {
56  m_http_client.set_server(
57  epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port), std::move(user)
58  );
59  }
60 
61  template <typename T_req, typename T_res>
63  T_req & req
64  , T_res & res
65  , std::string const & method_name
66  )
67  {
68  t_http_connection connection(&m_http_client);
69 
70  bool ok = connection.is_open();
71  if (!ok)
72  {
73  fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
74  return false;
75  }
76  ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
77  if (!ok)
78  {
79  fail_msg_writer() << "Daemon request failed";
80  return false;
81  }
82  else
83  {
84  return true;
85  }
86  }
87 
88  template <typename T_req, typename T_res>
90  T_req & req
91  , T_res & res
92  , std::string const & method_name
93  , std::string const & fail_msg
94  )
95  {
96  t_http_connection connection(&m_http_client);
97 
98  bool ok = connection.is_open();
99  ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
100  if (!ok)
101  {
102  fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
103  return false;
104  }
105  else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
106  {
107  fail_msg_writer() << fail_msg << " -- " << res.status;
108  return false;
109  }
110  else
111  {
112  return true;
113  }
114  }
115 
116  template <typename T_req, typename T_res>
118  T_req & req
119  , T_res & res
120  , std::string const & relative_url
121  , std::string const & fail_msg
122  )
123  {
124  t_http_connection connection(&m_http_client);
125 
126  bool ok = connection.is_open();
127  ok = ok && epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT());
128  if (!ok)
129  {
130  fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
131  return false;
132  }
133  else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
134  {
135  fail_msg_writer() << fail_msg << " -- " << res.status;
136  return false;
137  }
138  else
139  {
140  return true;
141  }
142  }
143 
145  {
146  t_http_connection connection(&m_http_client);
147  return connection.is_open();
148  }
149  };
150 }
bool basic_json_rpc_request(T_req &req, T_res &res, std::string const &method_name)
Definition: rpc_client.h:62
static constexpr std::chrono::seconds TIMEOUT()
Definition: http_connection.h:43
#define CORE_RPC_STATUS_OK
Definition: core_rpc_server_commands_defs.h:41
bool json_rpc_request(T_req &req, T_res &res, std::string const &method_name, std::string const &fail_msg)
Definition: rpc_client.h:89
bool rpc_request(T_req &req, T_res &res, std::string const &relative_url, std::string const &fail_msg)
Definition: rpc_client.h:117
Various Tools.
Definition: base58.cpp:43
bool check_connection()
Definition: rpc_client.h:144
Definition: rpc_client.h:44
Definition: http_connection.h:38
scoped_message_writer fail_msg_writer()
Definition: scoped_message_writer.h:131
std::string to_string(t_connection_type type)
Definition: connection_basic.cpp:96
t_rpc_client(uint32_t ip, uint16_t port, boost::optional< epee::net_utils::http::login > user)
Definition: rpc_client.h:49
std::string method_name(HTTPMethod method)
Definition: common.h:34
bool is_open() const
Definition: http_connection.h:63
epee::net_utils::http::http_simple_client m_http_client
Definition: rpc_client.h:47