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:
41 using ClusterData = std::expected<std::vector<ClioNode>, std::string>;
42
43private:
44 util::Logger log_{"ClusterCommunication"};
45
46 std::shared_ptr<data::BackendInterface> backend_;
47 std::unique_ptr<etl::WriterStateInterface const> writerState_;
48 std::unique_ptr<data::LedgerCacheLoadingStateInterface const> cacheLoadingState_;
49
52
53 ClioNode::Uuid selfUuid_;
54
55 boost::signals2::signal<void(ClioNode::CUuid, std::shared_ptr<ClusterData const>)> onNewState_;
56
57public:
68 Backend(
69 boost::asio::thread_pool& ctx,
70 std::shared_ptr<data::BackendInterface> backend,
71 std::unique_ptr<etl::WriterStateInterface const> writerState,
72 std::unique_ptr<data::LedgerCacheLoadingStateInterface const> cacheLoadingState,
73 std::chrono::steady_clock::duration readInterval,
74 std::chrono::steady_clock::duration writeInterval
75 );
76
77 ~Backend();
78
79 Backend(Backend&&) = delete;
80 Backend&
81 operator=(Backend&&) = delete;
82 Backend(Backend const&) = delete;
83 Backend&
84 operator=(Backend const&) = delete;
85
91 void
92 run();
93
99 void
100 stop();
101
109 template <typename S>
110 requires std::invocable<S, ClioNode::CUuid, std::shared_ptr<ClusterData const>>
111 boost::signals2::connection
113 {
114 return onNewState_.connect(s);
115 }
116
122 ClioNode::CUuid
123 selfId() const;
124
125private:
127 doRead(boost::asio::yield_context yield);
128
129 void
130 doWrite();
131};
132
133} // 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:41
boost::signals2::connection subscribeToNewState(S &&s)
Subscribe to new cluster state notifications.
Definition Backend.hpp:112
Definition RepeatedTask.hpp:24
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77