xrpld
Loading...
Searching...
No Matches
predicates.h
1#pragma once
2
3#include <xrpld/overlay/Message.h>
4#include <xrpld/overlay/Peer.h>
5
6#include <set>
7
8namespace xrpl {
9
12{
13 using return_type = void;
14
16
18 {
19 }
20
21 void
23 {
24 peer->send(msg);
25 }
26};
27
28//------------------------------------------------------------------------------
29
31template <typename Predicate>
33{
34 using return_type = void;
35
37 Predicate const& predicate;
38
39 SendIfPred(std::shared_ptr<Message> const& m, Predicate const& p) : msg(m), predicate(p)
40 {
41 }
42
43 void
45 {
46 if (predicate(peer))
47 peer->send(msg);
48 }
49};
50
52template <typename Predicate>
53SendIfPred<Predicate>
54sendIf(std::shared_ptr<Message> const& m, Predicate const& f)
55{
56 return SendIfPred<Predicate>(m, f);
57}
58
59//------------------------------------------------------------------------------
60
62template <typename Predicate>
64{
65 using return_type = void;
66
68 Predicate const& predicate;
69
70 SendIfNotPred(std::shared_ptr<Message> const& m, Predicate const& p) : msg(m), predicate(p)
71 {
72 }
73
74 void
76 {
77 if (!predicate(peer))
78 peer->send(msg);
79 }
80};
81
83template <typename Predicate>
84SendIfNotPred<Predicate>
85sendIfNot(std::shared_ptr<Message> const& m, Predicate const& f)
86{
87 return SendIfNotPred<Predicate>(m, f);
88}
89
90//------------------------------------------------------------------------------
91
94{
96
97 MatchPeer(Peer const* match = nullptr) : matchPeer(match)
98 {
99 }
100
101 bool
103 {
104 return (matchPeer != nullptr) && (peer.get() == matchPeer);
105 }
106};
107
108//------------------------------------------------------------------------------
109
112{
114
115 PeerInCluster(Peer const* skip = nullptr) : skipPeer(skip)
116 {
117 }
118
119 bool
121 {
122 if (skipPeer(peer))
123 return false;
124
125 if (!peer->cluster())
126 return false;
127
128 return true;
129 }
130};
131
132//------------------------------------------------------------------------------
133
136{
138
140 {
141 }
142
143 bool
145 {
146 return peerSet.contains(peer->id());
147 }
148};
149
150} // namespace xrpl
Represents a peer connection in the overlay.
T get(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
SendIfPred< Predicate > sendIf(std::shared_ptr< Message > const &m, Predicate const &f)
Helper function to aid in type deduction.
Definition predicates.h:54
SendIfNotPred< Predicate > sendIfNot(std::shared_ptr< Message > const &m, Predicate const &f)
Helper function to aid in type deduction.
Definition predicates.h:85
Select the specific peer.
Definition predicates.h:94
Peer const * matchPeer
Definition predicates.h:95
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:102
MatchPeer(Peer const *match=nullptr)
Definition predicates.h:97
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:120
PeerInCluster(Peer const *skip=nullptr)
Definition predicates.h:115
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:144
PeerInSet(std::set< Peer::id_t > const &peers)
Definition predicates.h:139
std::set< Peer::id_t > const & peerSet
Definition predicates.h:137
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:22
std::shared_ptr< Message > const & msg
Definition predicates.h:15
SendAlways(std::shared_ptr< Message > const &m)
Definition predicates.h:17
Sends a message to non-matching peers.
Definition predicates.h:64
Predicate const & predicate
Definition predicates.h:68
std::shared_ptr< Message > const & msg
Definition predicates.h:67
SendIfNotPred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition predicates.h:70
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:75
Sends a message to match peers.
Definition predicates.h:33
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:44
std::shared_ptr< Message > const & msg
Definition predicates.h:36
Predicate const & predicate
Definition predicates.h:37
SendIfPred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition predicates.h:39