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