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  : msg(m)
39  { }
40 
41  void operator()(std::shared_ptr<Peer> const& peer) const
42  {
43  peer->send (msg);
44  }
45 };
46 
47 //------------------------------------------------------------------------------
48 
50 template <typename Predicate>
52 {
53  using return_type = void;
54 
56  Predicate const& predicate;
57 
58  send_if_pred(std::shared_ptr<Message> const& m, Predicate const& p)
59  : msg(m), predicate(p)
60  { }
61 
62  void operator()(std::shared_ptr<Peer> const& peer) const
63  {
64  if (predicate (peer))
65  peer->send (msg);
66  }
67 };
68 
70 template <typename Predicate>
72  std::shared_ptr<Message> const& m,
73  Predicate const &f)
74 {
75  return send_if_pred<Predicate>(m, f);
76 }
77 
78 //------------------------------------------------------------------------------
79 
81 template <typename Predicate>
83 {
84  using return_type = void;
85 
87  Predicate const& predicate;
88 
89  send_if_not_pred(std::shared_ptr<Message> const& m, Predicate const& p)
90  : msg(m), predicate(p)
91  { }
92 
93  void operator()(std::shared_ptr<Peer> const& peer) const
94  {
95  if (!predicate (peer))
96  peer->send (msg);
97  }
98 };
99 
101 template <typename Predicate>
103  std::shared_ptr<Message> const& m,
104  Predicate const &f)
105 {
106  return send_if_not_pred<Predicate>(m, f);
107 }
108 
109 //------------------------------------------------------------------------------
110 
113 {
114  Peer const* matchPeer;
115 
116  match_peer (Peer const* match = nullptr)
117  : matchPeer (match)
118  { }
119 
120  bool operator() (std::shared_ptr<Peer> const& peer) const
121  {
122  if(matchPeer && (peer.get () == matchPeer))
123  return true;
124 
125  return false;
126  }
127 };
128 
129 //------------------------------------------------------------------------------
130 
133 {
135 
136  peer_in_cluster (Peer const* skip = nullptr)
137  : skipPeer (skip)
138  { }
139 
140  bool operator() (std::shared_ptr<Peer> const& peer) const
141  {
142  if (skipPeer (peer))
143  return false;
144 
145  if (! peer->cluster())
146  return false;
147 
148  return true;
149  }
150 };
151 
152 //------------------------------------------------------------------------------
153 
156 {
158 
160  : peerSet (peers)
161  { }
162 
163  bool operator() (std::shared_ptr<Peer> const& peer) const
164  {
165  if (peerSet.count (peer->id ()) == 0)
166  return false;
167 
168  return true;
169  }
170 };
171 
172 }
173 
174 #endif
ripple::peer_in_cluster::skipPeer
match_peer skipPeer
Definition: predicates.h:134
ripple::peer_in_cluster::peer_in_cluster
peer_in_cluster(Peer const *skip=nullptr)
Definition: predicates.h:136
ripple::match_peer::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:120
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:89
ripple::send_if_not_pred::predicate
Predicate const & predicate
Definition: predicates.h:87
std::shared_ptr
STL class.
ripple::peer_in_set::peerSet
std::set< Peer::id_t > const & peerSet
Definition: predicates.h:157
ripple::send_if_pred::predicate
Predicate const & predicate
Definition: predicates.h:56
ripple::peer_in_set::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:163
ripple::send_if_pred::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:62
ripple::match_peer::matchPeer
Peer const * matchPeer
Definition: predicates.h:114
ripple::send_if_pred
Sends a message to match peers.
Definition: predicates.h:51
ripple::send_if_not_pred
Sends a message to non-matching peers.
Definition: predicates.h:82
std::shared_ptr::get
T get(T... args)
ripple::match_peer
Select the specific peer.
Definition: predicates.h:112
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:155
ripple::send_if_not_pred::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:93
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:53
ripple::send_if_pred::send_if_pred
send_if_pred(std::shared_ptr< Message > const &m, Predicate const &p)
Definition: predicates.h:58
ripple::peer_in_cluster
Select all peers (except optional excluded) that are in our cluster.
Definition: predicates.h:132
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:84
ripple::send_if_not_pred::msg
std::shared_ptr< Message > const & msg
Definition: predicates.h:86
ripple::send_always::operator()
void operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:41
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:102
ripple::peer_in_set::peer_in_set
peer_in_set(std::set< Peer::id_t > const &peers)
Definition: predicates.h:159
ripple::match_peer::match_peer
match_peer(Peer const *match=nullptr)
Definition: predicates.h:116
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:71
ripple::peer_in_cluster::operator()
bool operator()(std::shared_ptr< Peer > const &peer) const
Definition: predicates.h:140
set
ripple::send_if_pred::msg
std::shared_ptr< Message > const & msg
Definition: predicates.h:55
ripple::Peer
Represents a peer connection in the overlay.
Definition: ripple/overlay/Peer.h:43