rippled
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 <ripple/overlay/Message.h>
24 #include <ripple/overlay/Peer.h>
25 
26 #include <set>
27 
28 namespace ripple {
29 
32 {
33  using return_type = void;
34 
36 
38  {
39  }
40 
41  void
42  operator()(std::shared_ptr<Peer> const& peer) const
43  {
44  peer->send(msg);
45  }
46 };
47 
48 //------------------------------------------------------------------------------
49 
51 template <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
65  operator()(std::shared_ptr<Peer> const& peer) const
66  {
67  if (predicate(peer))
68  peer->send(msg);
69  }
70 };
71 
73 template <typename Predicate>
74 send_if_pred<Predicate>
75 send_if(std::shared_ptr<Message> const& m, Predicate const& f)
76 {
77  return send_if_pred<Predicate>(m, f);
78 }
79 
80 //------------------------------------------------------------------------------
81 
83 template <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
97  operator()(std::shared_ptr<Peer> const& peer) const
98  {
99  if (!predicate(peer))
100  peer->send(msg);
101  }
102 };
103 
105 template <typename Predicate>
106 send_if_not_pred<Predicate>
107 send_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 {
117  Peer const* matchPeer;
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 
164  peer_in_set(std::set<Peer::id_t> const& peers) : peerSet(peers)
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
ripple::peer_in_cluster::skipPeer
match_peer skipPeer
Definition: predicates.h:138
ripple::peer_in_cluster::peer_in_cluster
peer_in_cluster(Peer const *skip=nullptr)
Definition: predicates.h:140
ripple::match_peer::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:124
ripple::send_if_not_pred::send_if_not_pred
send_if_not_pred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition: predicates.h:91
ripple::send_if_not_pred::predicate
Predicate const & predicate
Definition: predicates.h:89
std::shared_ptr< Message >
ripple::peer_in_set::peerSet
std::set< Peer::id_t > const & peerSet
Definition: predicates.h:162
ripple::send_if_pred::predicate
Predicate const & predicate
Definition: predicates.h:57
ripple::peer_in_set::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:169
ripple::send_if_pred::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:65
ripple::match_peer::matchPeer
Peer const * matchPeer
Definition: predicates.h:117
ripple::send_if_pred
Sends a message to match peers.
Definition: predicates.h:52
ripple::send_if_not_pred
Sends a message to non-matching peers.
Definition: predicates.h:84
std::shared_ptr::get
T get(T... args)
ripple::match_peer
Select the specific peer.
Definition: predicates.h:115
ripple::send_always::msg
std::shared_ptr< Message > const & msg
Definition: predicates.h:35
ripple::peer_in_set
Select all peers that are in the specified set.
Definition: predicates.h:160
ripple::send_if_not_pred::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:97
ripple::send_always
Sends a message to all peers.
Definition: predicates.h:31
ripple::send_if_pred::return_type
void return_type
Definition: predicates.h:54
ripple::send_if_pred::send_if_pred
send_if_pred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition: predicates.h:59
ripple::peer_in_cluster
Select all peers (except optional excluded) that are in our cluster.
Definition: predicates.h:136
ripple::send_always::return_type
void return_type
Definition: predicates.h:33
ripple::send_always::send_always
send_always(std::shared_ptr< Message > const &m)
Definition: predicates.h:37
ripple::send_if_not_pred::return_type
void return_type
Definition: predicates.h:86
ripple::send_if_not_pred::msg
std::shared_ptr< Message > const & msg
Definition: predicates.h:88
ripple::send_always::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:42
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::send_if_not
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
ripple::peer_in_set::peer_in_set
peer_in_set(std::set< Peer::id_t > const &peers)
Definition: predicates.h:164
ripple::match_peer::match_peer
match_peer(Peer const *match=nullptr)
Definition: predicates.h:119
ripple::send_if
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
ripple::peer_in_cluster::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:145
set
ripple::send_if_pred::msg
std::shared_ptr< Message > const & msg
Definition: predicates.h:56
ripple::Peer
Represents a peer connection in the overlay.
Definition: ripple/overlay/Peer.h:45