Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Backend.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "cluster/ClioNode.hpp"
23#include "cluster/impl/RepeatedTask.hpp"
24#include "data/BackendInterface.hpp"
25#include "etl/WriterState.hpp"
26#include "util/log/Logger.hpp"
27
28#include <boost/asio/any_io_executor.hpp>
29#include <boost/asio/cancellation_signal.hpp>
30#include <boost/asio/execution_context.hpp>
31#include <boost/asio/executor.hpp>
32#include <boost/asio/spawn.hpp>
33#include <boost/asio/strand.hpp>
34#include <boost/asio/thread_pool.hpp>
35#include <boost/signals2/connection.hpp>
36#include <boost/signals2/signal.hpp>
37#include <boost/signals2/variadic_signal.hpp>
38#include <boost/uuid/uuid.hpp>
39
40#include <chrono>
41#include <concepts>
42#include <memory>
43#include <string>
44#include <vector>
45
46namespace cluster {
47
55class Backend {
56public:
58 using ClusterData = std::expected<std::vector<ClioNode>, std::string>;
59
60private:
61 util::Logger log_{"ClusterCommunication"};
62
63 std::shared_ptr<data::BackendInterface> backend_;
64 std::unique_ptr<etl::WriterStateInterface const> writerState_;
65
68
69 ClioNode::Uuid selfUuid_;
70
71 boost::signals2::signal<void(ClioNode::CUuid, std::shared_ptr<ClusterData const>)> onNewState_;
72
73public:
83 Backend(
84 boost::asio::thread_pool& ctx,
85 std::shared_ptr<data::BackendInterface> backend,
86 std::unique_ptr<etl::WriterStateInterface const> writerState,
87 std::chrono::steady_clock::duration readInterval,
88 std::chrono::steady_clock::duration writeInterval
89 );
90
91 ~Backend();
92
93 Backend(Backend&&) = delete;
94 Backend&
95 operator=(Backend&&) = delete;
96 Backend(Backend const&) = delete;
97 Backend&
98 operator=(Backend const&) = delete;
99
105 void
106 run();
107
113 void
114 stop();
115
123 template <typename S>
124 requires std::invocable<S, ClioNode::CUuid, std::shared_ptr<ClusterData const>>
125 boost::signals2::connection
127 {
128 return onNewState_.connect(s);
129 }
130
136 ClioNode::CUuid
137 selfId() const;
138
139private:
141 doRead(boost::asio::yield_context yield);
142
143 void
144 doWrite();
145};
146
147} // namespace cluster
Backend communication handler for cluster state synchronization.
Definition Backend.hpp:55
void run()
Start the backend read and write tasks.
Definition Backend.cpp:60
void stop()
Stop the backend read and write tasks.
Definition Backend.cpp:76
ClioNode::CUuid selfId() const
Get the UUID of this node in the cluster.
Definition Backend.cpp:83
std::expected< std::vector< ClioNode >, std::string > ClusterData
Type representing cluster data result - either a vector of nodes or an error message.
Definition Backend.hpp:58
boost::signals2::connection subscribeToNewState(S &&s)
Subscribe to new cluster state notifications.
Definition Backend.hpp:126
Backend(boost::asio::thread_pool &ctx, std::shared_ptr< data::BackendInterface > backend, std::unique_ptr< etl::WriterStateInterface const > writerState, std::chrono::steady_clock::duration readInterval, std::chrono::steady_clock::duration writeInterval)
Construct a Backend communication handler.
Definition Backend.cpp:44
Definition RepeatedTask.hpp:44
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95