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