rippled
CollectorRef.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 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 #ifndef RIPPLE_TEST_CSF_COLLECTOREF_H_INCLUDED
20 #define RIPPLE_TEST_CSF_COLLECTOREF_H_INCLUDED
21 
22 #include <test/csf/events.h>
23 #include <test/csf/SimTime.h>
24 
25 namespace ripple {
26 namespace test {
27 namespace csf {
28 
69 {
70  using tp = SimTime;
71 
72  // Interface for type-erased collector instance
73  struct ICollector
74  {
75  virtual ~ICollector() = default;
76 
77  virtual void
78  on(PeerID node, tp when, Share<Tx> const&) = 0;
79 
80  virtual void
81  on(PeerID node, tp when, Share<TxSet> const&) = 0;
82 
83  virtual void
84  on(PeerID node, tp when, Share<Validation> const&) = 0;
85 
86  virtual void
87  on(PeerID node, tp when, Share<Ledger> const&) = 0;
88 
89  virtual void
90  on(PeerID node, tp when, Share<Proposal> const&) = 0;
91 
92  virtual void
93  on(PeerID node, tp when, Receive<Tx> const&) = 0;
94 
95  virtual void
96  on(PeerID node, tp when, Receive<TxSet> const&) = 0;
97 
98  virtual void
99  on(PeerID node, tp when, Receive<Validation> const&) = 0;
100 
101  virtual void
102  on(PeerID node, tp when, Receive<Ledger> const&) = 0;
103 
104  virtual void
105  on(PeerID node, tp when, Receive<Proposal> const&) = 0;
106 
107  virtual void
108  on(PeerID node, tp when, Relay<Tx> const&) = 0;
109 
110  virtual void
111  on(PeerID node, tp when, Relay<TxSet> const&) = 0;
112 
113  virtual void
114  on(PeerID node, tp when, Relay<Validation> const&) = 0;
115 
116  virtual void
117  on(PeerID node, tp when, Relay<Ledger> const&) = 0;
118 
119  virtual void
120  on(PeerID node, tp when, Relay<Proposal> const&) = 0;
121 
122  virtual void
123  on(PeerID node, tp when, SubmitTx const&) = 0;
124 
125  virtual void
126  on(PeerID node, tp when, StartRound const&) = 0;
127 
128  virtual void
129  on(PeerID node, tp when, CloseLedger const&) = 0;
130 
131  virtual void
132  on(PeerID node, tp when, AcceptLedger const&) = 0;
133 
134  virtual void
135  on(PeerID node, tp when, WrongPrevLedger const&) = 0;
136 
137  virtual void
138  on(PeerID node, tp when, FullyValidateLedger const&) = 0;
139  };
140 
141  // Bridge between type-ful collector T and type erased instance
142  template <class T>
143  class Any final : public ICollector
144  {
145  T & t_;
146 
147  public:
148  Any(T & t) : t_{t}
149  {
150  }
151 
152  // Can't copy
153  Any(Any const & ) = delete;
154  Any& operator=(Any const & ) = delete;
155 
156  Any(Any && ) = default;
157  Any& operator=(Any && ) = default;
158 
159  virtual void
160  on(PeerID node, tp when, Share<Tx> const& e) override
161  {
162  t_.on(node, when, e);
163  }
164 
165  virtual void
166  on(PeerID node, tp when, Share<TxSet> const& e) override
167  {
168  t_.on(node, when, e);
169  }
170 
171  virtual void
172  on(PeerID node, tp when, Share<Validation> const& e) override
173  {
174  t_.on(node, when, e);
175  }
176 
177  virtual void
178  on(PeerID node, tp when, Share<Ledger> const& e) override
179  {
180  t_.on(node, when, e);
181  }
182 
183  virtual void
184  on(PeerID node, tp when, Share<Proposal> const& e) override
185  {
186  t_.on(node, when, e);
187  }
188 
189  void
190  on(PeerID node, tp when, Receive<Tx> const& e) override
191  {
192  t_.on(node, when, e);
193  }
194 
195  virtual void
196  on(PeerID node, tp when, Receive<TxSet> const& e) override
197  {
198  t_.on(node, when, e);
199  }
200 
201  virtual void
202  on(PeerID node, tp when, Receive<Validation> const& e) override
203  {
204  t_.on(node, when, e);
205  }
206 
207  virtual void
208  on(PeerID node, tp when, Receive<Ledger> const& e) override
209  {
210  t_.on(node, when, e);
211  }
212 
213  virtual void
214  on(PeerID node, tp when, Receive<Proposal> const& e) override
215  {
216  t_.on(node, when, e);
217  }
218 
219  void
220  on(PeerID node, tp when, Relay<Tx> const& e) override
221  {
222  t_.on(node, when, e);
223  }
224 
225  virtual void
226  on(PeerID node, tp when, Relay<TxSet> const& e) override
227  {
228  t_.on(node, when, e);
229  }
230 
231  virtual void
232  on(PeerID node, tp when, Relay<Validation> const& e) override
233  {
234  t_.on(node, when, e);
235  }
236 
237  virtual void
238  on(PeerID node, tp when, Relay<Ledger> const& e) override
239  {
240  t_.on(node, when, e);
241  }
242 
243  virtual void
244  on(PeerID node, tp when, Relay<Proposal> const& e) override
245  {
246  t_.on(node, when, e);
247  }
248 
249  virtual void
250  on(PeerID node, tp when, SubmitTx const& e) override
251  {
252  t_.on(node, when, e);
253  }
254 
255  virtual void
256  on(PeerID node, tp when, StartRound const& e) override
257  {
258  t_.on(node, when, e);
259  }
260 
261  virtual void
262  on(PeerID node, tp when, CloseLedger const& e) override
263  {
264  t_.on(node, when, e);
265  }
266 
267  virtual void
268  on(PeerID node, tp when, AcceptLedger const& e) override
269  {
270  t_.on(node, when, e);
271  }
272 
273  virtual void
274  on(PeerID node, tp when, WrongPrevLedger const& e) override
275  {
276  t_.on(node, when, e);
277  }
278 
279  virtual void
280  on(PeerID node, tp when, FullyValidateLedger const& e) override
281  {
282  t_.on(node, when, e);
283  }
284  };
285 
287 
288 public:
289  template <class T>
290  CollectorRef(T& t) : impl_{new Any<T>(t)}
291  {
292  }
293 
294  // Non-copyable
295  CollectorRef(CollectorRef const& c) = delete;
296  CollectorRef& operator=(CollectorRef& c) = delete;
297 
298  CollectorRef(CollectorRef&&) = default;
299  CollectorRef& operator=(CollectorRef&&) = default;
300 
301  template <class E>
302  void
303  on(PeerID node, tp when, E const& e)
304  {
305  impl_->on(node, when, e);
306  }
307 };
308 
320 {
322 public:
323 
324  template <class Collector>
325  void add(Collector & collector)
326  {
327  collectors_.emplace_back(collector);
328  }
329 
330  template <class E>
331  void
332  on(PeerID node, SimTime when, E const& e)
333  {
334  for (auto & c : collectors_)
335  {
336  c.on(node, when, e);
337  }
338  }
339 
340 };
341 
342 } // namespace csf
343 } // namespace test
344 } // namespace ripple
345 
346 #endif
ripple::test::csf::SimTime
typename SimClock::time_point SimTime
Definition: SimTime.h:36
ripple::test::csf::CollectorRef::ICollector
Definition: CollectorRef.h:73
std::vector
STL class.
ripple::test::csf::CollectorRef::Any::on
void on(PeerID node, tp when, Receive< Tx > const &e) override
Definition: CollectorRef.h:190
ripple::test::csf::FullyValidateLedger
Peer fully validated a new ledger.
Definition: events.h:140
ripple::test::csf::CollectorRef::operator=
CollectorRef & operator=(CollectorRef &c)=delete
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Share< TxSet > const &e) override
Definition: CollectorRef.h:166
ripple::test::csf::CollectorRef::Any::operator=
Any & operator=(Any const &)=delete
ripple::test::csf::CollectorRef::ICollector::on
virtual void on(PeerID node, tp when, Share< Tx > const &)=0
ripple::test::csf::CollectorRef::CollectorRef
CollectorRef(T &t)
Definition: CollectorRef.h:290
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, StartRound const &e) override
Definition: CollectorRef.h:256
ripple::test::csf::AcceptLedger
Peer accepted consensus results.
Definition: events.h:121
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Share< Ledger > const &e) override
Definition: CollectorRef.h:178
ripple::test::csf::CollectorRef::Any
Definition: CollectorRef.h:143
ripple::test::csf::CollectorRefs::add
void add(Collector &collector)
Definition: CollectorRef.h:325
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Receive< Validation > const &e) override
Definition: CollectorRef.h:202
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, FullyValidateLedger const &e) override
Definition: CollectorRef.h:280
ripple::test::csf::CollectorRefs::collectors_
std::vector< CollectorRef > collectors_
Definition: CollectorRef.h:321
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, SubmitTx const &e) override
Definition: CollectorRef.h:250
ripple::test::csf::CloseLedger
Peer closed the open ledger.
Definition: events.h:111
ripple::test::csf::CollectorRef::impl_
std::unique_ptr< ICollector > impl_
Definition: CollectorRef.h:286
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Share< Tx > const &e) override
Definition: CollectorRef.h:160
ripple::test::csf::CollectorRef::ICollector::~ICollector
virtual ~ICollector()=default
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Receive< Proposal > const &e) override
Definition: CollectorRef.h:214
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Share< Validation > const &e) override
Definition: CollectorRef.h:172
ripple::test::csf::Relay
A value relayed to another peer as part of flooding.
Definition: events.h:70
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Relay< TxSet > const &e) override
Definition: CollectorRef.h:226
ripple::test::csf::CollectorRef
Holds a type-erased reference to an arbitray collector.
Definition: CollectorRef.h:68
ripple::test::csf::Receive
A value received from another peer as part of flooding.
Definition: events.h:82
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Relay< Ledger > const &e) override
Definition: CollectorRef.h:238
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Receive< TxSet > const &e) override
Definition: CollectorRef.h:196
ripple::test::csf::WrongPrevLedger
Peer detected a wrong prior ledger during consensus.
Definition: events.h:131
ripple::test::csf::CollectorRef::Any::Any
Any(T &t)
Definition: CollectorRef.h:148
ripple::test::csf::CollectorRef::Any::t_
T & t_
Definition: CollectorRef.h:145
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Receive< Ledger > const &e) override
Definition: CollectorRef.h:208
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Relay< Proposal > const &e) override
Definition: CollectorRef.h:244
ripple::test::csf::CollectorRefs
A container of CollectorRefs.
Definition: CollectorRef.h:319
ripple::test::csf::CollectorRef::tp
SimTime tp
Definition: CollectorRef.h:70
ripple::test::csf::Share
A value to be flooded to all other peers starting from this peer.
Definition: events.h:61
ripple::test::csf::StartRound
Peer starts a new consensus round.
Definition: events.h:100
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, CloseLedger const &e) override
Definition: CollectorRef.h:262
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, AcceptLedger const &e) override
Definition: CollectorRef.h:268
ripple::tagged_integer< std::uint32_t, PeerIDTag >
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, WrongPrevLedger const &e) override
Definition: CollectorRef.h:274
ripple::test::csf::CollectorRefs::on
void on(PeerID node, SimTime when, E const &e)
Definition: CollectorRef.h:332
std::unique_ptr
STL class.
ripple::test::csf::CollectorRef::on
void on(PeerID node, tp when, E const &e)
Definition: CollectorRef.h:303
ripple::test::csf::SubmitTx
A transaction submitted to a peer.
Definition: events.h:92
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Share< Proposal > const &e) override
Definition: CollectorRef.h:184
ripple::test::csf::CollectorRef::Any::on
void on(PeerID node, tp when, Relay< Tx > const &e) override
Definition: CollectorRef.h:220
ripple::test::csf::CollectorRef::Any::on
virtual void on(PeerID node, tp when, Relay< Validation > const &e) override
Definition: CollectorRef.h:232