xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
resource
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
15
namespace
xrpl::resource
{
16
17
Consumer::Consumer
(
Logic
& logic,
Entry
&
entry
) :
logic_
(&logic),
entry_
(&
entry
)
18
{
19
}
20
21
Consumer::Consumer
() :
logic_
(nullptr),
entry_
(nullptr)
22
{
23
}
24
25
Consumer::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
34
Consumer::~Consumer
()
35
{
36
if
((
logic_
!=
nullptr
) && (
entry_
!=
nullptr
))
37
logic_
->release(*
entry_
);
38
}
39
40
Consumer
&
41
Consumer::operator=
(
Consumer
const
& other)
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
60
std::string
61
Consumer::toString
()
const
62
{
63
if
(
logic_
==
nullptr
)
64
return
"(none)"
;
65
66
return
entry_
->toString();
67
}
68
69
bool
70
Consumer::isUnlimited
()
const
71
{
72
if
(
entry_
!=
nullptr
)
73
return
entry_
->isUnlimited();
74
75
return
false
;
76
}
77
78
Disposition
79
Consumer::disposition
()
const
80
{
81
Disposition
d =
Disposition::Ok
;
82
if
((
logic_
!=
nullptr
) && (
entry_
!=
nullptr
))
83
d =
logic_
->charge(*
entry_
,
Charge
(0));
84
85
return
d;
86
}
87
88
Disposition
89
Consumer::charge
(
Charge
const
& what,
std::string
const
& context)
90
{
91
Disposition
d =
Disposition::Ok
;
92
93
if
((
logic_
!=
nullptr
) && (
entry_
!=
nullptr
) && !
entry_
->isUnlimited())
94
d =
logic_
->charge(*
entry_
, what, context);
95
96
return
d;
97
}
98
99
bool
100
Consumer::warn
()
101
{
102
XRPL_ASSERT(
entry_
,
"xrpl::resource::Consumer::warn : non-null entry"
);
103
return
logic_
->warn(*
entry_
);
104
}
105
106
bool
107
Consumer::disconnect
(
beast::Journal
const
& j)
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
118
int
119
Consumer::balance
()
120
{
121
XRPL_ASSERT(
entry_
,
"xrpl::resource::Consumer::balance : non-null entry"
);
122
return
logic_
->balance(*
entry_
);
123
}
124
125
Entry
&
126
Consumer::entry
()
127
{
128
XRPL_ASSERT(
entry_
,
"xrpl::resource::Consumer::entry : non-null entry"
);
129
return
*
entry_
;
130
}
131
132
void
133
Consumer::setPublicKey
(
PublicKey
const
& publicKey)
134
{
135
entry_
->publicKey = publicKey;
136
}
137
138
std::ostream
&
139
operator<<
(
std::ostream
& os,
Consumer
const
& v)
140
{
141
os << v.
toString
();
142
return
os;
143
}
144
145
}
// namespace xrpl::resource
std::ostream
std::string
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
beast::Journal::debug
Stream debug() const
Definition
Journal.h:344
xrpl::PublicKey
A public key.
Definition
PublicKey.h:53
xrpl::resource::Charge
A consumption charge.
Definition
Charge.h:13
xrpl::resource::Consumer
An endpoint that consumes resources.
Definition
Consumer.h:20
xrpl::resource::Consumer::logic_
Logic * logic_
Definition
Consumer.h:92
xrpl::resource::Consumer::Consumer
Consumer(Logic &logic, Entry &entry)
Definition
Consumer.cpp:17
xrpl::resource::Consumer::disconnect
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition
Consumer.cpp:107
xrpl::resource::Consumer::balance
int balance()
Returns the credit balance representing consumption.
Definition
Consumer.cpp:119
xrpl::resource::Consumer::toString
std::string toString() const
Return a human readable string uniquely identifying this consumer.
Definition
Consumer.cpp:61
xrpl::resource::Consumer::entry
Entry & entry()
Definition
Consumer.cpp:126
xrpl::resource::Consumer::charge
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition
Consumer.cpp:89
xrpl::resource::Consumer::disposition
Disposition disposition() const
Returns the current disposition of this consumer.
Definition
Consumer.cpp:79
xrpl::resource::Consumer::Consumer
Consumer()
Definition
Consumer.cpp:21
xrpl::resource::Consumer::~Consumer
~Consumer()
Definition
Consumer.cpp:34
xrpl::resource::Consumer::operator=
Consumer & operator=(Consumer const &other)
Definition
Consumer.cpp:41
xrpl::resource::Consumer::isUnlimited
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition
Consumer.cpp:70
xrpl::resource::Consumer::setPublicKey
void setPublicKey(PublicKey const &publicKey)
Definition
Consumer.cpp:133
xrpl::resource::Consumer::Logic
friend class Logic
Definition
Consumer.h:22
xrpl::resource::Consumer::warn
bool warn()
Returns true if the consumer should be warned.
Definition
Consumer.cpp:100
xrpl::resource::Consumer::entry_
Entry * entry_
Definition
Consumer.h:93
xrpl::resource
Definition
ServiceRegistry.h:21
xrpl::resource::Disposition
Disposition
The disposition of a consumer after applying a load charge.
Definition
Disposition.h:8
xrpl::resource::Disposition::Ok
@ Ok
No action required.
Definition
Disposition.h:12
xrpl::resource::operator<<
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition
Charge.cpp:36
ostream
string
xrpl::resource::Entry
Definition
Entry.h:22
Generated by
1.17.0