| 1 | #pragma once |
| 2 | |
| 3 | #include <functional> |
| 4 | #include <map> |
| 5 | #include <utility> |
| 6 | |
| 7 | #include <boost/optional/optional.hpp> |
| 8 | #include <boost/thread/mutex.hpp> |
| 9 | #include <boost/utility/string_ref.hpp> |
| 10 | |
| 11 | #include "net/http.h" |
| 12 | #include "storages/http_abstract_invoke.h" |
| 13 | |
| 14 | #include "bootstrap_node_selector.h" |
| 15 | |
| 16 | namespace cryptonote |
| 17 | { |
| 18 | |
| 19 | class bootstrap_daemon |
| 20 | { |
| 21 | public: |
| 22 | bootstrap_daemon( |
| 23 | std::function<std::map<std::string, bool>()> get_public_nodes, |
| 24 | bool rpc_payment_enabled, |
| 25 | const std::string &proxy); |
| 26 | bootstrap_daemon( |
| 27 | const std::string &address, |
| 28 | boost::optional<epee::net_utils::http::login> credentials, |
| 29 | bool rpc_payment_enabled, |
| 30 | const std::string &proxy); |
| 31 | |
| 32 | std::string address() const noexcept; |
| 33 | boost::optional<std::pair<uint64_t, uint64_t>> get_height(); |
| 34 | bool handle_result(bool success, const std::string &status); |
| 35 | |
| 36 | template <class t_request, class t_response> |
| 37 | bool invoke_http_json(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct) |
| 38 | { |
| 39 | if (!switch_server_if_needed()) |
| 40 | { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | const bool result = epee::net_utils::invoke_http_json(uri, out_struct, result_struct, m_http_client); |
| 45 | return handle_result(result, result_struct.status); |
| 46 | } |
| 47 | |
| 48 | template <class t_request, class t_response> |
| 49 | bool invoke_http_bin(const boost::string_ref uri, const t_request &out_struct, t_response &result_struct) |
| 50 | { |
| 51 | if (!switch_server_if_needed()) |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | const bool result = epee::net_utils::invoke_http_bin(uri, out_struct, result_struct, m_http_client); |
| 57 | return handle_result(result, result_struct.status); |
| 58 | } |
| 59 | |
| 60 | template <class t_request, class t_response> |
| 61 | bool invoke_http_json_rpc(const boost::string_ref command_name, const t_request &out_struct, t_response &result_struct) |
| 62 | { |
| 63 | if (!switch_server_if_needed()) |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | const bool result = epee::net_utils::invoke_http_json_rpc( |
| 69 | "/json_rpc", |
| 70 | std::string(command_name.begin(), command_name.end()), |
| 71 | out_struct, |
| 72 | result_struct, |
| 73 | m_http_client); |
| 74 | return handle_result(result, result_struct.status); |
| 75 | } |
| 76 | |
| 77 | void set_proxy(const std::string &address); |
| 78 | |
| 79 | private: |
| 80 | bool set_server(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials = boost::none); |
| 81 | bool switch_server_if_needed(); |
| 82 | |
| 83 | private: |
| 84 | net::http::client m_http_client; |
| 85 | const bool m_rpc_payment_enabled; |
| 86 | const std::unique_ptr<bootstrap_node::selector> m_selector; |
| 87 | boost::mutex m_selector_mutex; |
| 88 | }; |
| 89 | |
| 90 | } |