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 && other.m_entry)
27 {
28 m_entry = other.m_entry;
30 }
31}
32
38
41{
42 // remove old ref
43 if (m_logic && m_entry)
45
46 m_logic = other.m_logic;
47 m_entry = other.m_entry;
48
49 // add new ref
50 if (m_logic && m_entry)
52
53 return *this;
54}
55
58{
59 if (m_logic == nullptr)
60 return "(none)";
61
62 return m_entry->to_string();
63}
64
65bool
67{
68 if (m_entry)
69 return m_entry->isUnlimited();
70
71 return false;
72}
73
76{
77 Disposition d = ok;
78 if (m_logic && m_entry)
79 d = m_logic->charge(*m_entry, Charge(0));
80
81 return d;
82}
83
85Consumer::charge(Charge const& what, std::string const& context)
86{
87 Disposition d = ok;
88
89 if (m_logic && m_entry && !m_entry->isUnlimited())
90 d = m_logic->charge(*m_entry, what, context);
91
92 return d;
93}
94
95bool
97{
98 XRPL_ASSERT(m_entry, "xrpl::Resource::Consumer::warn : non-null entry");
99 return m_logic->warn(*m_entry);
100}
101
102bool
104{
105 XRPL_ASSERT(m_entry, "xrpl::Resource::Consumer::disconnect : non-null entry");
106 bool const d = m_logic->disconnect(*m_entry);
107 if (d)
108 {
109 JLOG(j.debug()) << "disconnecting " << m_entry->to_string();
110 }
111 return d;
112}
113
114int
116{
117 XRPL_ASSERT(m_entry, "xrpl::Resource::Consumer::balance : non-null entry");
118 return m_logic->balance(*m_entry);
119}
120
121Entry&
123{
124 XRPL_ASSERT(m_entry, "xrpl::Resource::Consumer::entry : non-null entry");
125 return *m_entry;
126}
127
128void
130{
131 m_entry->publicKey = publicKey;
132}
133
135operator<<(std::ostream& os, Consumer const& v)
136{
137 os << v.to_string();
138 return os;
139}
140
141} // namespace Resource
142} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:301
A public key.
Definition PublicKey.h:43
A consumption charge.
Definition Charge.h:11
An endpoint that consumes resources.
Definition Consumer.h:17
Consumer & operator=(Consumer const &other)
Definition Consumer.cpp:40
void setPublicKey(PublicKey const &publicKey)
Definition Consumer.cpp:129
bool warn()
Returns true if the consumer should be warned.
Definition Consumer.cpp:96
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition Consumer.cpp:66
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition Consumer.cpp:103
Disposition disposition() const
Returns the current disposition of this consumer.
Definition Consumer.cpp:75
std::string to_string() const
Return a human readable string uniquely identifying this consumer.
Definition Consumer.cpp:57
int balance()
Returns the credit balance representing consumption.
Definition Consumer.cpp:115
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition Consumer.cpp:85
Disposition charge(Entry &entry, Charge const &fee, std::string context={})
Disposition
The disposition of a consumer after applying a load charge.
Definition Disposition.h:8
@ ok
No action required.
Definition Disposition.h:10
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:6
std::string to_string() const
Definition Entry.h:31
bool isUnlimited() const
Returns true if this connection should have no resource limits applied–it is still possible for certa...
Definition Entry.h:42
std::optional< PublicKey > publicKey
Definition Entry.h:63