Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Backend.hpp
1#pragma once
2
3#include "cluster/ClioNode.hpp"
4#include "cluster/impl/RepeatedTask.hpp"
5#include "data/BackendInterface.hpp"
6#include "data/LedgerCacheLoadingState.hpp"
7#include "etl/WriterState.hpp"
8#include "util/log/Logger.hpp"
9
10#include <boost/asio/any_io_executor.hpp>
11#include <boost/asio/cancellation_signal.hpp>
12#include <boost/asio/execution_context.hpp>
13#include <boost/asio/executor.hpp>
14#include <boost/asio/spawn.hpp>
15#include <boost/asio/strand.hpp>
16#include <boost/asio/thread_pool.hpp>
17#include <boost/signals2/connection.hpp>
18#include <boost/signals2/signal.hpp>
19#include <boost/signals2/variadic_signal.hpp>
20#include <boost/uuid/uuid.hpp>
21
22#include <chrono>
23#include <concepts>
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace cluster {
29
37class Backend {
38public:
42 using ClusterData = std::expected<std::vector<ClioNode>, std::string>;
43
44private:
45 util::Logger log_{"ClusterCommunication"};
46
47 std::shared_ptr<data::BackendInterface> backend_;
48 std::unique_ptr<etl::WriterStateInterface const> writerState_;
49 std::unique_ptr<data::LedgerCacheLoadingStateInterface const> cacheLoadingState_;
50
53
54 ClioNode::Uuid selfUuid_;
55
56 boost::signals2::signal<void(ClioNode::CUuid, std::shared_ptr<ClusterData const>)> onNewState_;
57
58public:
69 Backend(
70 boost::asio::thread_pool& ctx,
71 std::shared_ptr<data::BackendInterface> backend,
72 std::unique_ptr<etl::WriterStateInterface const> writerState,
73 std::unique_ptr<data::LedgerCacheLoadingStateInterface const> cacheLoadingState,
74 std::chrono::steady_clock::duration readInterval,
75 std::chrono::steady_clock::duration writeInterval
76 );
77
78 ~Backend();
79
80 Backend(Backend&&) = delete;
81 Backend&
82 operator=(Backend&&) = delete;
83 Backend(Backend const&) = delete;
84 Backend&
85 operator=(Backend const&) = delete;
86
92 void
93 run();
94
100 void
101 stop();
102
110 template <typename S>
111 requires std::invocable<S, ClioNode::CUuid, std::shared_ptr<ClusterData const>>
112 boost::signals2::connection
114 {
115 return onNewState_.connect(s);
116 }
117
123 [[nodiscard]] ClioNode::CUuid
124 selfId() const;
125
126private:
128 doRead(boost::asio::yield_context yield);
129
130 void
131 doWrite();
132};
133
134} // namespace cluster
Backend communication handler for cluster state synchronization.
Definition Backend.hpp:37
Backend(boost::asio::thread_pool &ctx, std::shared_ptr< data::BackendInterface > backend, std::unique_ptr< etl::WriterStateInterface const > writerState, std::unique_ptr< data::LedgerCacheLoadingStateInterface const > cacheLoadingState, std::chrono::steady_clock::duration readInterval, std::chrono::steady_clock::duration writeInterval)
Construct a Backend communication handler.
Definition Backend.cpp:26
void run()
Start the backend read and write tasks.
Definition Backend.cpp:44
void stop()
Stop the backend read and write tasks.
Definition Backend.cpp:60
ClioNode::CUuid selfId() const
Get the UUID of this node in the cluster.
Definition Backend.cpp:67
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:42
boost::signals2::connection subscribeToNewState(S &&s)
Subscribe to new cluster state notifications.
Definition Backend.hpp:113
Definition RepeatedTask.hpp:24
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:78