OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
uhs/atomizer/watchtower/client.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "client.hpp"
7
9#include "watchtower.hpp"
10
11#include <utility>
12
13namespace cbdc::watchtower {
16
18 m_network.close();
19 m_client_thread.join();
20 }
21
22 auto blocking_client::init() -> bool {
23 // TODO: Add error handling.
24 auto ct = m_network.start_cluster_handler(
25 {m_ep},
27 -> std::optional<cbdc::buffer> {
28 auto deser = cbdc::buffer_serializer(*pkt.m_pkt);
29 auto res = response(deser);
30 m_res_q.push(
31 std::make_shared<cbdc::watchtower::response>(res));
32 return std::nullopt;
33 });
34
35 if(!ct.has_value()) {
36 return false;
37 }
38
39 m_client_thread = std::move(ct.value());
40
41 return true;
42 }
43
45 -> std::shared_ptr<best_block_height_response> {
47 auto pkt = make_shared_buffer(data);
48 m_network.broadcast(pkt);
49
50 std::shared_ptr<response> res;
51 if(!m_res_q.pop(res)) {
52 return nullptr;
53 }
54 return std::make_shared<best_block_height_response>(
55 std::get<best_block_height_response>(res->payload()));
56 }
57
58 auto
60 -> std::shared_ptr<status_request_check_success> {
61 auto data = request{req};
62 auto pkt = make_shared_buffer(data);
63 m_network.broadcast(pkt);
64
65 std::shared_ptr<response> res;
66 if(!m_res_q.pop(res)) {
67 return nullptr;
68 }
69 return std::make_shared<status_request_check_success>(
70 std::get<status_request_check_success>(res->payload()));
71 }
72
74
76 m_handler_running = false;
77 m_network.close();
78
79 if(m_handler_thread.joinable()) {
80 m_handler_thread.join();
81 }
82 m_client_thread.join();
83 }
84
85 auto async_client::init() -> bool {
86 // TODO: Add error handling.
87 auto ct = m_network.start_cluster_handler(
88 {m_ep},
90 -> std::optional<cbdc::buffer> {
91 auto deser = cbdc::buffer_serializer(*pkt.m_pkt);
92 auto res = response(deser);
93 m_res_q.push(
94 std::make_shared<cbdc::watchtower::response>(res));
95 return std::nullopt;
96 });
97
98 if(!ct.has_value()) {
99 return false;
100 }
101
102 m_client_thread = std::move(ct.value());
103
104 m_handler_running = true;
105 m_handler_thread = std::thread{[this]() {
106 while(m_handler_running) {
107 std::shared_ptr<response> res;
108 if(!m_res_q.pop(res)) {
109 break;
110 }
111
112 std::visit(
114 [&](const status_request_check_success& s) {
115 m_su_handler(
116 std::make_shared<status_request_check_success>(
117 s));
118 },
119 [&](const best_block_height_response& s) {
120 m_bbh_handler(
121 std::make_shared<best_block_height_response>(
122 s));
123 }},
124 res->payload());
125 }
126 }};
127 return true;
128 }
129
132 auto pkt = make_shared_buffer(data);
133 m_network.broadcast(pkt);
134 }
135
136 void
138 auto data = request{req};
139 auto pkt = make_shared_buffer(data);
140 m_network.broadcast(pkt);
141 }
142
145 m_su_handler = handler;
146 }
147
150 m_bbh_handler = handler;
151 }
152}
Serializer implementation for buffer.
void close()
Shuts down the network listener and all existing peer connections.
void broadcast(const std::shared_ptr< buffer > &data)
Sends the provided data to all added peers.
auto init() -> bool
Attempts to connect to the watchtower.
void set_block_height_handler(const best_block_height_handler_t &handler)
Sets or replaces the handler for asynchronously delivered StatusUpdateResponses.
void request_status_update(const status_update_request &req)
Sends a StatusUpdateRequest to the Watchtower.
void set_status_update_handler(const status_update_response_handler_t &handler)
Sets or replaces the handler for asynchronously delivered StatusUpdateResponses.
void request_best_block_height()
Sends a request_best_block_height to the Watchtower.
std::function< void( std::shared_ptr< best_block_height_response > &&)> best_block_height_handler_t
std::function< void( std::shared_ptr< status_request_check_success > &&)> status_update_response_handler_t
Contains the watchtower's known best block height.
auto init() -> bool
Attempts to connect to the watchtower.
auto request_status_update(const status_update_request &req) -> std::shared_ptr< status_request_check_success >
Sends a StatusUpdateRequest to the Watchtower.
auto request_best_block_height() -> std::shared_ptr< best_block_height_response >
Sends a request_best_block_height to the Watchtower.
RPC request message to the watchtower external endpoint.
RPC response message from the watchtower external endpoint.
Indicates a successful check request, sent with a StatusUpdateResponse.
Network request to interact with the Watchtower's status update service.
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
Definition socket.hpp:19
auto make_shared_buffer(const T &obj) -> std::shared_ptr< cbdc::buffer >
Serialize object into std::shared_ptr<cbdc::buffer> using a cbdc::buffer_serializer.
Received message type.
Variant handler template.
Request the watchtower's known best block height.
Client helpers for interfacing with a watchtower server.
Watchtower core functionality.