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 public:
52 
54  uint256 const& getHash () const
55  {
56  return mHash;
57  }
58 
60  bool isComplete () const
61  {
62  return mComplete;
63  }
64 
66  bool isFailed () const
67  {
68  return mFailed;
69  }
70 
72  int getTimeouts () const
73  {
74  return mTimeouts;
75  }
76 
77  bool isActive ();
78 
80  void progress ()
81  {
82  mProgress = true;
83  }
84 
85  void touch ()
86  {
88  }
89 
91  {
92  return mLastAction;
93  }
94 
99  bool insert (std::shared_ptr<Peer> const&);
100 
101  virtual bool isDone () const
102  {
103  return mComplete || mFailed;
104  }
105 
106  Application&
107  app()
108  {
109  return app_;
110  }
111 
112 protected:
114 
115  PeerSet (Application& app, uint256 const& hash, std::chrono::milliseconds interval,
116  clock_type& clock, beast::Journal journal);
117 
118  virtual ~PeerSet() = 0;
119 
120  virtual void newPeer (std::shared_ptr<Peer> const&) = 0;
121 
122  virtual void onTimer (bool progress, ScopedLockType&) = 0;
123 
124  virtual void execute () = 0;
125 
126  virtual std::weak_ptr<PeerSet> pmDowncast () = 0;
127 
128  bool isProgress ()
129  {
130  return mProgress;
131  }
132 
133  void setComplete ()
134  {
135  mComplete = true;
136  }
137  void setFailed ()
138  {
139  mFailed = true;
140  }
141 
142  void invokeOnTimer ();
143 
144  void sendRequest (const protocol::TMGetLedger& message);
145 
146  void sendRequest (const protocol::TMGetLedger& message, std::shared_ptr<Peer> const& peer);
147 
148  void setTimer ();
149 
150  std::size_t getPeerCount () const;
151 
152 protected:
156 
158 
162  bool mComplete;
163  bool mFailed;
165  bool mProgress;
166 
167  // VFALCO TODO move the responsibility for the timer to a higher level
168  boost::asio::basic_waitable_timer<std::chrono::steady_clock> mTimer;
169 
170  // The identifiers of the peers we are tracking.
172 };
173 
174 } // ripple
175 
176 #endif
ripple::Application
Definition: Application.h:85
ripple::PeerSet::setFailed
void setFailed()
Definition: PeerSet.h:137
std::shared_ptr
STL class.
ripple::PeerSet::onTimer
virtual void onTimer(bool progress, ScopedLockType &)=0
ripple::PeerSet::mProgress
bool mProgress
Definition: PeerSet.h:165
ripple::PeerSet::getPeerCount
std::size_t getPeerCount() const
Definition: PeerSet.cpp:137
ripple::PeerSet::getLastAction
clock_type::time_point getLastAction() const
Definition: PeerSet.h:90
ripple::PeerSet::invokeOnTimer
void invokeOnTimer()
Definition: PeerSet.cpp:83
ripple::PeerSet::mTimer
boost::asio::basic_waitable_timer< std::chrono::steady_clock > mTimer
Definition: PeerSet.h:168
ripple::PeerSet::mPeers
std::set< Peer::id_t > mPeers
Definition: PeerSet.h:171
ripple::PeerSet::mHash
uint256 mHash
Definition: PeerSet.h:159
std::chrono::milliseconds
std::recursive_mutex
STL class.
ripple::PeerSet::mComplete
bool mComplete
Definition: PeerSet.h:162
ripple::PeerSet::isDone
virtual bool isDone() const
Definition: PeerSet.h:101
ripple::PeerSet::newPeer
virtual void newPeer(std::shared_ptr< Peer > const &)=0
ripple::PeerSet::insert
bool insert(std::shared_ptr< Peer > const &)
Insert a peer to the managed set.
Definition: PeerSet.cpp:58
ripple::PeerSet::touch
void touch()
Definition: PeerSet.h:85
ripple::PeerSet::m_clock
clock_type & m_clock
Definition: PeerSet.h:155
beast::abstract_clock::now
virtual time_point now() const =0
Returns the current time.
ripple::base_uint< 256 >
ripple::PeerSet::~PeerSet
virtual ~PeerSet()=0
std::unique_lock
STL class.
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::PeerSet::app
Application & app()
Definition: PeerSet.h:107
ripple::PeerSet::PeerSet
PeerSet(Application &app, uint256 const &hash, std::chrono::milliseconds interval, clock_type &clock, beast::Journal journal)
Definition: PeerSet.cpp:38
beast::abstract_clock< std::chrono::steady_clock >
ripple::PeerSet::isActive
bool isActive()
Definition: PeerSet.cpp:107
ripple::PeerSet::sendRequest
void sendRequest(const protocol::TMGetLedger &message)
Definition: PeerSet.cpp:121
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::getTimeouts
int getTimeouts() const
Returns the number of times we timed out.
Definition: PeerSet.h:72
ripple::PeerSet::app_
Application & app_
Definition: PeerSet.h:153
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::PeerSet::mFailed
bool mFailed
Definition: PeerSet.h:163
ripple::PeerSet::mTimerInterval
std::chrono::milliseconds mTimerInterval
Definition: PeerSet.h:160
ripple::PeerSet::mLastAction
clock_type::time_point mLastAction
Definition: PeerSet.h:164
ripple::PeerSet::execute
virtual void execute()=0
mutex
ripple::PeerSet::m_journal
beast::Journal m_journal
Definition: PeerSet.h:154
std::size_t
ripple::PeerSet::getHash
uint256 const & getHash() const
Returns the hash of the data we want.
Definition: PeerSet.h:54
ripple::PeerSet::isComplete
bool isComplete() const
Returns true if we got all the data.
Definition: PeerSet.h:60
ripple::PeerSet::isFailed
bool isFailed() const
Returns false if we failed to get the data.
Definition: PeerSet.h:66
ripple::PeerSet::mTimeouts
int mTimeouts
Definition: PeerSet.h:161
ripple::PeerSet::setComplete
void setComplete()
Definition: PeerSet.h:133
ripple::PeerSet::pmDowncast
virtual std::weak_ptr< PeerSet > pmDowncast()=0
ripple::PeerSet::isProgress
bool isProgress()
Definition: PeerSet.h:128
beast::abstract_clock< std::chrono::steady_clock >::time_point
typename std::chrono::steady_clock ::time_point time_point
Definition: abstract_clock.h:63
ripple::PeerSet::setTimer
void setTimer()
Definition: PeerSet.cpp:69
set
ripple::PeerSet::progress
void progress()
Called to indicate that forward progress has been made.
Definition: PeerSet.h:80
ripple::PeerSet::mLock
std::recursive_mutex mLock
Definition: PeerSet.h:157