rippled
Loading...
Searching...
No Matches
libxrpl
resource
Consumer.cpp
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
#include <xrpl/basics/Log.h>
21
#include <xrpl/beast/utility/Journal.h>
22
#include <xrpl/beast/utility/instrumentation.h>
23
#include <xrpl/resource/Charge.h>
24
#include <xrpl/resource/Consumer.h>
25
#include <xrpl/resource/Disposition.h>
26
#include <xrpl/resource/detail/Entry.h>
27
#include <xrpl/resource/detail/Logic.h>
28
29
#include <
ostream
>
30
#include <
string
>
31
32
namespace
ripple
{
33
namespace
Resource {
34
35
Consumer::Consumer
(
Logic
& logic,
Entry
& entry)
36
: m_logic(&logic), m_entry(&entry)
37
{
38
}
39
40
Consumer::Consumer
() : m_logic(nullptr), m_entry(nullptr)
41
{
42
}
43
44
Consumer::Consumer
(
Consumer
const
& other)
45
: m_logic(other.m_logic), m_entry(nullptr)
46
{
47
if
(
m_logic
&& other.
m_entry
)
48
{
49
m_entry
= other.
m_entry
;
50
m_logic
->
acquire
(*
m_entry
);
51
}
52
}
53
54
Consumer::~Consumer
()
55
{
56
if
(
m_logic
&&
m_entry
)
57
m_logic
->
release
(*
m_entry
);
58
}
59
60
Consumer
&
61
Consumer::operator=
(
Consumer
const
& other)
62
{
63
// remove old ref
64
if
(
m_logic
&&
m_entry
)
65
m_logic
->
release
(*
m_entry
);
66
67
m_logic
= other.
m_logic
;
68
m_entry
= other.
m_entry
;
69
70
// add new ref
71
if
(
m_logic
&&
m_entry
)
72
m_logic
->
acquire
(*
m_entry
);
73
74
return
*
this
;
75
}
76
77
std::string
78
Consumer::to_string
()
const
79
{
80
if
(
m_logic
==
nullptr
)
81
return
"(none)"
;
82
83
return
m_entry
->
to_string
();
84
}
85
86
bool
87
Consumer::isUnlimited
()
const
88
{
89
if
(
m_entry
)
90
return
m_entry
->
isUnlimited
();
91
92
return
false
;
93
}
94
95
Disposition
96
Consumer::disposition
()
const
97
{
98
Disposition
d =
ok
;
99
if
(
m_logic
&&
m_entry
)
100
d =
m_logic
->
charge
(*
m_entry
,
Charge
(0));
101
102
return
d;
103
}
104
105
Disposition
106
Consumer::charge
(
Charge
const
& what,
std::string
const
& context)
107
{
108
Disposition
d =
ok
;
109
110
if
(
m_logic
&&
m_entry
&& !
m_entry
->
isUnlimited
())
111
d =
m_logic
->
charge
(*
m_entry
, what, context);
112
113
return
d;
114
}
115
116
bool
117
Consumer::warn
()
118
{
119
XRPL_ASSERT(
m_entry
,
"ripple::Resource::Consumer::warn : non-null entry"
);
120
return
m_logic
->
warn
(*
m_entry
);
121
}
122
123
bool
124
Consumer::disconnect
(
beast::Journal
const
& j)
125
{
126
XRPL_ASSERT(
127
m_entry
,
"ripple::Resource::Consumer::disconnect : non-null entry"
);
128
bool
const
d =
m_logic
->
disconnect
(*
m_entry
);
129
if
(d)
130
{
131
JLOG(j.
debug
()) <<
"disconnecting "
<<
m_entry
->
to_string
();
132
}
133
return
d;
134
}
135
136
int
137
Consumer::balance
()
138
{
139
XRPL_ASSERT(
140
m_entry
,
"ripple::Resource::Consumer::balance : non-null entry"
);
141
return
m_logic
->
balance
(*
m_entry
);
142
}
143
144
Entry
&
145
Consumer::entry
()
146
{
147
XRPL_ASSERT(
m_entry
,
"ripple::Resource::Consumer::entry : non-null entry"
);
148
return
*
m_entry
;
149
}
150
151
std::ostream
&
152
operator<<
(
std::ostream
& os,
Consumer
const
& v)
153
{
154
os << v.
to_string
();
155
return
os;
156
}
157
158
}
// namespace Resource
159
}
// namespace ripple
std::ostream
std::string
beast::Journal
A generic endpoint for log messages.
Definition:
Journal.h:60
beast::Journal::debug
Stream debug() const
Definition:
Journal.h:328
ripple::Resource::Charge
A consumption charge.
Definition:
Charge.h:31
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition:
Consumer.h:35
ripple::Resource::Consumer::m_entry
Entry * m_entry
Definition:
Consumer.h:92
ripple::Resource::Consumer::warn
bool warn()
Returns true if the consumer should be warned.
Definition:
Consumer.cpp:117
ripple::Resource::Consumer::operator=
Consumer & operator=(Consumer const &other)
Definition:
Consumer.cpp:61
ripple::Resource::Consumer::balance
int balance()
Returns the credit balance representing consumption.
Definition:
Consumer.cpp:137
ripple::Resource::Consumer::Consumer
Consumer()
Definition:
Consumer.cpp:40
ripple::Resource::Consumer::to_string
std::string to_string() const
Return a human readable string uniquely identifying this consumer.
Definition:
Consumer.cpp:78
ripple::Resource::Consumer::disconnect
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition:
Consumer.cpp:124
ripple::Resource::Consumer::entry
Entry & entry()
Definition:
Consumer.cpp:145
ripple::Resource::Consumer::charge
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition:
Consumer.cpp:106
ripple::Resource::Consumer::m_logic
Logic * m_logic
Definition:
Consumer.h:91
ripple::Resource::Consumer::isUnlimited
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition:
Consumer.cpp:87
ripple::Resource::Consumer::disposition
Disposition disposition() const
Returns the current disposition of this consumer.
Definition:
Consumer.cpp:96
ripple::Resource::Consumer::~Consumer
~Consumer()
Definition:
Consumer.cpp:54
ripple::Resource::Logic
Definition:
include/xrpl/resource/detail/Logic.h:42
ripple::Resource::Logic::acquire
void acquire(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:413
ripple::Resource::Logic::warn
bool warn(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:482
ripple::Resource::Logic::disconnect
bool disconnect(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:506
ripple::Resource::Logic::charge
Disposition charge(Entry &entry, Charge const &fee, std::string context={})
Definition:
include/xrpl/resource/detail/Logic.h:450
ripple::Resource::Logic::balance
int balance(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:532
ripple::Resource::Logic::release
void release(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:420
ripple::Resource::operator<<
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition:
Charge.cpp:56
ripple::Resource::Disposition
Disposition
The disposition of a consumer after applying a load charge.
Definition:
Disposition.h:27
ripple::Resource::ok
@ ok
No action required.
Definition:
Disposition.h:29
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
algorithm.h:26
ostream
string
ripple::Resource::Entry
Definition:
Entry.h:38
ripple::Resource::Entry::to_string
std::string to_string() const
Definition:
Entry.h:54
ripple::Resource::Entry::isUnlimited
bool isUnlimited() const
Returns true if this connection should have no resource limits applied–it is still possible for certa...
Definition:
Entry.h:65
Generated by
1.9.5