rippled
Loading...
Searching...
No Matches
predicates.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or 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#ifndef RIPPLE_OVERLAY_PREDICATES_H_INCLUDED
21#define RIPPLE_OVERLAY_PREDICATES_H_INCLUDED
22
23#include <xrpld/overlay/Message.h>
24#include <xrpld/overlay/Peer.h>
25
26#include <set>
27
28namespace ripple {
29
32{
33 using return_type = void;
34
36
38 {
39 }
40
41 void
43 {
44 peer->send(msg);
45 }
46};
47
48//------------------------------------------------------------------------------
49
51template <typename Predicate>
53{
54 using return_type = void;
55
57 Predicate const& predicate;
58
59 send_if_pred(std::shared_ptr<Message> const& m, Predicate const& p)
60 : msg(m), predicate(p)
61 {
62 }
63
64 void
66 {
67 if (predicate(peer))
68 peer->send(msg);
69 }
70};
71
73template <typename Predicate>
74send_if_pred<Predicate>
75send_if(std::shared_ptr<Message> const& m, Predicate const& f)
76{
77 return send_if_pred<Predicate>(m, f);
78}
79
80//------------------------------------------------------------------------------
81
83template <typename Predicate>
85{
86 using return_type = void;
87
89 Predicate const& predicate;
90
91 send_if_not_pred(std::shared_ptr<Message> const& m, Predicate const& p)
92 : msg(m), predicate(p)
93 {
94 }
95
96 void
98 {
99 if (!predicate(peer))
100 peer->send(msg);
101 }
102};
103
105template <typename Predicate>
106send_if_not_pred<Predicate>
107send_if_not(std::shared_ptr<Message> const& m, Predicate const& f)
108{
109 return send_if_not_pred<Predicate>(m, f);
110}
111
112//------------------------------------------------------------------------------
113
116{
118
119 match_peer(Peer const* match = nullptr) : matchPeer(match)
120 {
121 }
122
123 bool
125 {
126 if (matchPeer && (peer.get() == matchPeer))
127 return true;
128
129 return false;
130 }
131};
132
133//------------------------------------------------------------------------------
134
137{
139
140 peer_in_cluster(Peer const* skip = nullptr) : skipPeer(skip)
141 {
142 }
143
144 bool
146 {
147 if (skipPeer(peer))
148 return false;
149
150 if (!peer->cluster())
151 return false;
152
153 return true;
154 }
155};
156
157//------------------------------------------------------------------------------
158
161{
163
165 {
166 }
167
168 bool
170 {
171 if (peerSet.count(peer->id()) == 0)
172 return false;
173
174 return true;
175 }
176};
177
178} // namespace ripple
179
180#endif
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:25
send_if_not_pred< Predicate > send_if_not(std::shared_ptr< Message > const &m, Predicate const &f)
Helper function to aid in type deduction.
Definition predicates.h:107
send_if_pred< Predicate > send_if(std::shared_ptr< Message > const &m, Predicate const &f)
Helper function to aid in type deduction.
Definition predicates.h:75
Select the specific peer.
Definition predicates.h:116
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:124
match_peer(Peer const *match=nullptr)
Definition predicates.h:119
Peer const * matchPeer
Definition predicates.h:117
Select all peers (except optional excluded) that are in our cluster.
Definition predicates.h:137
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:145
peer_in_cluster(Peer const *skip=nullptr)
Definition predicates.h:140
Select all peers that are in the specified set.
Definition predicates.h:161
peer_in_set(std::set< Peer::id_t > const &peers)
Definition predicates.h:164
std::set< Peer::id_t > const & peerSet
Definition predicates.h:162
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:169
Sends a message to all peers.
Definition predicates.h:32
std::shared_ptr< Message > const & msg
Definition predicates.h:35
send_always(std::shared_ptr< Message > const &m)
Definition predicates.h:37
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:42
Sends a message to non-matching peers.
Definition predicates.h:85
Predicate const & predicate
Definition predicates.h:89
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:97
std::shared_ptr< Message > const & msg
Definition predicates.h:88
send_if_not_pred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition predicates.h:91
Sends a message to match peers.
Definition predicates.h:53
std::shared_ptr< Message > const & msg
Definition predicates.h:56
void operator()(std::shared_ptr< Peer > const &peer) const
Definition predicates.h:65
Predicate const & predicate
Definition predicates.h:57
send_if_pred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition predicates.h:59