rippled
PeerSet.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_APP_PEERS_PEERSET_H_INCLUDED
21 #define RIPPLE_APP_PEERS_PEERSET_H_INCLUDED
22 
23 #include <ripple/app/main/Application.h>
24 #include <ripple/beast/clock/abstract_clock.h>
25 #include <ripple/beast/utility/Journal.h>
26 #include <ripple/overlay/Peer.h>
27 #include <boost/asio/basic_waitable_timer.hpp>
28 #include <mutex>
29 #include <set>
30 
31 namespace ripple {
32 
48 class PeerSet
49 {
50 protected:
52 
53  PeerSet(
54  Application& app,
55  uint256 const& hash,
57  beast::Journal journal);
58 
59  virtual ~PeerSet() = 0;
60 
62  void
63  addPeers(
64  std::size_t limit,
65  std::function<bool(std::shared_ptr<Peer> const&)> score);
66 
68  virtual void
70 
72  virtual void
73  onTimer(bool progress, ScopedLockType&) = 0;
74 
76  virtual void
77  queueJob() = 0;
78 
81  pmDowncast() = 0;
82 
83  bool
84  isDone() const
85  {
86  return mComplete || mFailed;
87  }
88 
90  void
91  invokeOnTimer();
92 
94  void
96  const protocol::TMGetLedger& message,
97  std::shared_ptr<Peer> const& peer);
98 
100  void
101  setTimer();
102 
103  // Used in this class for access to boost::asio::io_service and
104  // ripple::Overlay. Used in subtypes for the kitchen sink.
107 
109 
112  uint256 const mHash;
114  bool mComplete;
115  bool mFailed;
117  bool mProgress;
118 
121 
122 private:
125  // VFALCO TODO move the responsibility for the timer to a higher level
126  boost::asio::basic_waitable_timer<std::chrono::steady_clock> mTimer;
127 };
128 
129 } // namespace ripple
130 
131 #endif
ripple::Application
Definition: Application.h:101
std::shared_ptr
STL class.
ripple::PeerSet::onTimer
virtual void onTimer(bool progress, ScopedLockType &)=0
Hook called from invokeOnTimer().
ripple::PeerSet::mProgress
bool mProgress
Whether forward progress has been made.
Definition: PeerSet.h:117
ripple::PeerSet::isDone
bool isDone() const
Definition: PeerSet.h:84
ripple::PeerSet::invokeOnTimer
void invokeOnTimer()
Calls onTimer() if in the right state.
Definition: PeerSet.cpp:101
ripple::PeerSet::mTimer
boost::asio::basic_waitable_timer< std::chrono::steady_clock > mTimer
Definition: PeerSet.h:126
ripple::PeerSet::mPeers
std::set< Peer::id_t > mPeers
The identifiers of the peers we are tracking.
Definition: PeerSet.h:120
std::chrono::milliseconds
std::recursive_mutex
STL class.
ripple::PeerSet::mComplete
bool mComplete
Definition: PeerSet.h:114
std::function
ripple::PeerSet::queueJob
virtual void queueJob()=0
Queue a job to call invokeOnTimer().
ripple::PeerSet::addPeers
void addPeers(std::size_t limit, std::function< bool(std::shared_ptr< Peer > const &)> score)
Add at most limit peers to this set from the overlay.
Definition: PeerSet.cpp:50
ripple::PeerSet::mHash
const uint256 mHash
The hash of the object (in practice, always a ledger) we are trying to fetch.
Definition: PeerSet.h:112
ripple::PeerSet::sendRequest
void sendRequest(const protocol::TMGetLedger &message, std::shared_ptr< Peer > const &peer)
Send a GetLedger message to one or all peers.
Definition: PeerSet.cpp:127
ripple::base_uint< 256 >
ripple::PeerSet::~PeerSet
virtual ~PeerSet()=0
ripple::PeerSet::PeerSet
PeerSet(Application &app, uint256 const &hash, std::chrono::milliseconds interval, beast::Journal journal)
Definition: PeerSet.cpp:29
std::unique_lock< std::recursive_mutex >
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::weak_ptr
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerSet::app_
Application & app_
Definition: PeerSet.h:105
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::PeerSet::mFailed
bool mFailed
Definition: PeerSet.h:115
ripple::PeerSet::onPeerAdded
virtual void onPeerAdded(std::shared_ptr< Peer > const &)=0
Hook called from addPeers().
ripple::PeerSet::mTimerInterval
std::chrono::milliseconds mTimerInterval
The minimum time to wait between calls to execute().
Definition: PeerSet.h:124
mutex
ripple::PeerSet::m_journal
beast::Journal m_journal
Definition: PeerSet.h:106
std::size_t
ripple::PeerSet::mTimeouts
int mTimeouts
Definition: PeerSet.h:113
ripple::PeerSet::pmDowncast
virtual std::weak_ptr< PeerSet > pmDowncast()=0
Return a weak pointer to this.
ripple::PeerSet::setTimer
void setTimer()
Schedule a call to queueJob() after mTimerInterval.
Definition: PeerSet.cpp:87
set
ripple::PeerSet::mLock
std::recursive_mutex mLock
Definition: PeerSet.h:108