xrpld
Loading...
Searching...
No Matches
Consumer.cpp
1#include <xrpl/resource/Consumer.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/protocol/PublicKey.h>
7#include <xrpl/resource/Charge.h>
8#include <xrpl/resource/Disposition.h>
9#include <xrpl/resource/detail/Entry.h>
10#include <xrpl/resource/detail/Logic.h>
11
12#include <ostream>
13#include <string>
14
15namespace xrpl::Resource {
16
18{
19}
20
21Consumer::Consumer() : logic_(nullptr), entry_(nullptr)
22{
23}
24
25Consumer::Consumer(Consumer const& other) : logic_(other.logic_), entry_(nullptr)
26{
27 if ((logic_ != nullptr) && (other.entry_ != nullptr))
28 {
29 entry_ = other.entry_;
30 logic_->acquire(*entry_);
31 }
32}
33
35{
36 if ((logic_ != nullptr) && (entry_ != nullptr))
37 logic_->release(*entry_);
38}
39
42{
43 if (this == &other)
44 return *this;
45
46 // remove old ref
47 if ((logic_ != nullptr) && (entry_ != nullptr))
48 logic_->release(*entry_);
49
50 logic_ = other.logic_;
51 entry_ = other.entry_;
52
53 // add new ref
54 if ((logic_ != nullptr) && (entry_ != nullptr))
55 logic_->acquire(*entry_);
56
57 return *this;
58}
59
62{
63 if (logic_ == nullptr)
64 return "(none)";
65
66 return entry_->toString();
67}
68
69bool
71{
72 if (entry_ != nullptr)
73 return entry_->isUnlimited();
74
75 return false;
76}
77
80{
82 if ((logic_ != nullptr) && (entry_ != nullptr))
83 d = logic_->charge(*entry_, Charge(0));
84
85 return d;
86}
87
89Consumer::charge(Charge const& what, std::string const& context)
90{
92
93 if ((logic_ != nullptr) && (entry_ != nullptr) && !entry_->isUnlimited())
94 d = logic_->charge(*entry_, what, context);
95
96 return d;
97}
98
99bool
101{
102 XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::warn : non-null entry");
103 return logic_->warn(*entry_);
104}
105
106bool
108{
109 XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::disconnect : non-null entry");
110 bool const d = logic_->disconnect(*entry_);
111 if (d)
112 {
113 JLOG(j.debug()) << "disconnecting " << entry_->toString();
114 }
115 return d;
116}
117
118int
120{
121 XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::balance : non-null entry");
122 return logic_->balance(*entry_);
123}
124
125Entry&
127{
128 XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::entry : non-null entry");
129 return *entry_;
130}
131
132void
134{
135 entry_->publicKey = publicKey;
136}
137
139operator<<(std::ostream& os, Consumer const& v)
140{
141 os << v.toString();
142 return os;
143}
144
145} // namespace xrpl::Resource
A generic endpoint for log messages.
Definition Journal.h:38
Stream debug() const
Definition Journal.h:297
A public key.
Definition PublicKey.h:42
A consumption charge.
Definition Charge.h:9
An endpoint that consumes resources.
Definition Consumer.h:15
Consumer & operator=(Consumer const &other)
Definition Consumer.cpp:41
void setPublicKey(PublicKey const &publicKey)
Definition Consumer.cpp:133
bool warn()
Returns true if the consumer should be warned.
Definition Consumer.cpp:100
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition Consumer.cpp:70
Consumer(Logic &logic, Entry &entry)
Definition Consumer.cpp:17
std::string toString() const
Return a human readable string uniquely identifying this consumer.
Definition Consumer.cpp:61
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition Consumer.cpp:107
Disposition disposition() const
Returns the current disposition of this consumer.
Definition Consumer.cpp:79
int balance()
Returns the credit balance representing consumption.
Definition Consumer.cpp:119
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition Consumer.cpp:89
Disposition
The disposition of a consumer after applying a load charge.
Definition Disposition.h:6
@ Ok
No action required.
Definition Disposition.h:8
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition Charge.cpp:36