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/beast/utility/instrumentation.h>
21
#include <xrpl/resource/Consumer.h>
22
#include <xrpl/resource/detail/Entry.h>
23
#include <xrpl/resource/detail/Logic.h>
24
25
namespace
ripple
{
26
namespace
Resource {
27
28
Consumer::Consumer
(
Logic
& logic,
Entry
& entry)
29
: m_logic(&logic), m_entry(&entry)
30
{
31
}
32
33
Consumer::Consumer
() : m_logic(nullptr), m_entry(nullptr)
34
{
35
}
36
37
Consumer::Consumer
(
Consumer
const
& other)
38
: m_logic(other.m_logic), m_entry(nullptr)
39
{
40
if
(
m_logic
&& other.
m_entry
)
41
{
42
m_entry
= other.
m_entry
;
43
m_logic
->
acquire
(*
m_entry
);
44
}
45
}
46
47
Consumer::~Consumer
()
48
{
49
if
(
m_logic
&&
m_entry
)
50
m_logic
->
release
(*
m_entry
);
51
}
52
53
Consumer
&
54
Consumer::operator=
(
Consumer
const
& other)
55
{
56
// remove old ref
57
if
(
m_logic
&&
m_entry
)
58
m_logic
->
release
(*
m_entry
);
59
60
m_logic
= other.
m_logic
;
61
m_entry
= other.
m_entry
;
62
63
// add new ref
64
if
(
m_logic
&&
m_entry
)
65
m_logic
->
acquire
(*
m_entry
);
66
67
return
*
this
;
68
}
69
70
std::string
71
Consumer::to_string
()
const
72
{
73
if
(
m_logic
==
nullptr
)
74
return
"(none)"
;
75
76
return
m_entry
->
to_string
();
77
}
78
79
bool
80
Consumer::isUnlimited
()
const
81
{
82
if
(
m_entry
)
83
return
m_entry
->
isUnlimited
();
84
85
return
false
;
86
}
87
88
Disposition
89
Consumer::disposition
()
const
90
{
91
Disposition
d =
ok
;
92
if
(
m_logic
&&
m_entry
)
93
d =
m_logic
->
charge
(*
m_entry
,
Charge
(0));
94
95
return
d;
96
}
97
98
Disposition
99
Consumer::charge
(
Charge
const
& what)
100
{
101
Disposition
d =
ok
;
102
103
if
(
m_logic
&&
m_entry
&& !
m_entry
->
isUnlimited
())
104
d =
m_logic
->
charge
(*
m_entry
, what);
105
106
return
d;
107
}
108
109
bool
110
Consumer::warn
()
111
{
112
XRPL_ASSERT(
m_entry
,
"ripple::Resource::Consumer::warn : non-null entry"
);
113
return
m_logic
->
warn
(*
m_entry
);
114
}
115
116
bool
117
Consumer::disconnect
(
beast::Journal
const
& j)
118
{
119
XRPL_ASSERT(
120
m_entry
,
"ripple::Resource::Consumer::disconnect : non-null entry"
);
121
bool
const
d =
m_logic
->
disconnect
(*
m_entry
);
122
if
(d)
123
{
124
JLOG(j.
debug
()) <<
"disconnecting "
<<
m_entry
->
to_string
();
125
}
126
return
d;
127
}
128
129
int
130
Consumer::balance
()
131
{
132
XRPL_ASSERT(
133
m_entry
,
"ripple::Resource::Consumer::balance : non-null entry"
);
134
return
m_logic
->
balance
(*
m_entry
);
135
}
136
137
Entry
&
138
Consumer::entry
()
139
{
140
XRPL_ASSERT(
m_entry
,
"ripple::Resource::Consumer::entry : non-null entry"
);
141
return
*
m_entry
;
142
}
143
144
std::ostream
&
145
operator<<
(
std::ostream
& os,
Consumer
const
& v)
146
{
147
os << v.
to_string
();
148
return
os;
149
}
150
151
}
// namespace Resource
152
}
// namespace ripple
std::ostream
std::string
beast::Journal
A generic endpoint for log messages.
Definition:
Journal.h:59
beast::Journal::debug
Stream debug() const
Definition:
Journal.h:317
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:110
ripple::Resource::Consumer::operator=
Consumer & operator=(Consumer const &other)
Definition:
Consumer.cpp:54
ripple::Resource::Consumer::balance
int balance()
Returns the credit balance representing consumption.
Definition:
Consumer.cpp:130
ripple::Resource::Consumer::Consumer
Consumer()
Definition:
Consumer.cpp:33
ripple::Resource::Consumer::to_string
std::string to_string() const
Return a human readable string uniquely identifying this consumer.
Definition:
Consumer.cpp:71
ripple::Resource::Consumer::disconnect
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition:
Consumer.cpp:117
ripple::Resource::Consumer::entry
Entry & entry()
Definition:
Consumer.cpp:138
ripple::Resource::Consumer::charge
Disposition charge(Charge const &fee)
Apply a load charge to the consumer.
Definition:
Consumer.cpp:99
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:80
ripple::Resource::Consumer::disposition
Disposition disposition() const
Returns the current disposition of this consumer.
Definition:
Consumer.cpp:89
ripple::Resource::Consumer::~Consumer
~Consumer()
Definition:
Consumer.cpp:47
ripple::Resource::Logic
Definition:
include/xrpl/resource/detail/Logic.h:41
ripple::Resource::Logic::acquire
void acquire(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:412
ripple::Resource::Logic::warn
bool warn(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:459
ripple::Resource::Logic::disconnect
bool disconnect(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:483
ripple::Resource::Logic::charge
Disposition charge(Entry &entry, Charge const &fee)
Definition:
include/xrpl/resource/detail/Logic.h:449
ripple::Resource::Logic::balance
int balance(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:509
ripple::Resource::Logic::release
void release(Entry &entry)
Definition:
include/xrpl/resource/detail/Logic.h:419
ripple::Resource::operator<<
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition:
Charge.cpp:52
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
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