rippled
ripple
resource
impl
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 <ripple/resource/Consumer.h>
21
#include <ripple/resource/impl/Entry.h>
22
#include <ripple/resource/impl/Logic.h>
23
#include <
cassert
>
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
assert(
m_entry
!=
nullptr
);
113
return
m_logic
->
warn
(*
m_entry
);
114
}
115
116
bool
117
Consumer::disconnect
(
beast::Journal
const
& j)
118
{
119
assert(
m_entry
!=
nullptr
);
120
bool
const
d =
m_logic
->
disconnect
(*
m_entry
);
121
if
(d)
122
{
123
JLOG(j.
debug
()) <<
"disconnecting "
<<
m_entry
->
to_string
();
124
}
125
return
d;
126
}
127
128
int
129
Consumer::balance
()
130
{
131
assert(
m_entry
!=
nullptr
);
132
return
m_logic
->
balance
(*
m_entry
);
133
}
134
135
Entry
&
136
Consumer::entry
()
137
{
138
assert(
m_entry
!=
nullptr
);
139
return
*
m_entry
;
140
}
141
142
std::ostream
&
143
operator<<
(
std::ostream
& os,
Consumer
const
& v)
144
{
145
os << v.
to_string
();
146
return
os;
147
}
148
149
}
// namespace Resource
150
}
// namespace ripple
ripple::Resource::Consumer::warn
bool warn()
Returns true if the consumer should be warned.
Definition:
Consumer.cpp:110
std::string
STL class.
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:136
ripple::Resource::Entry::to_string
std::string to_string() const
Definition:
Entry.h:54
ripple::Resource::Logic::disconnect
bool disconnect(Entry &entry)
Definition:
resource/impl/Logic.h:479
ripple::Resource::Logic::balance
int balance(Entry &entry)
Definition:
resource/impl/Logic.h:505
ripple::Resource::Logic::acquire
void acquire(Entry &entry)
Definition:
resource/impl/Logic.h:410
ripple::Resource::Logic
Definition:
resource/impl/Logic.h:40
ripple::Resource::Consumer::disposition
Disposition disposition() const
Returns the current disposition of this consumer.
Definition:
Consumer.cpp:89
ripple::Resource::operator<<
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition:
Charge.cpp:52
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::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
ripple::Resource::Logic::warn
bool warn(Entry &entry)
Definition:
resource/impl/Logic.h:455
std::ostream
STL class.
ripple::Resource::Consumer::balance
int balance()
Returns the credit balance representing consumption.
Definition:
Consumer.cpp:129
ripple::Resource::Logic::release
void release(Entry &entry)
Definition:
resource/impl/Logic.h:417
beast::Journal
A generic endpoint for log messages.
Definition:
Journal.h:58
ripple::Resource::Consumer::m_entry
Entry * m_entry
Definition:
Consumer.h:92
ripple::Resource::Entry
Definition:
Entry.h:37
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
cassert
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition:
Consumer.h:34
ripple::Resource::Charge
A consumption charge.
Definition:
Charge.h:30
ripple::Resource::Consumer::isUnlimited
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition:
Consumer.cpp:80
ripple::Resource::Consumer::operator=
Consumer & operator=(Consumer const &other)
Definition:
Consumer.cpp:54
ripple::Resource::Disposition
Disposition
The disposition of a consumer after applying a load charge.
Definition:
Disposition.h:27
beast::Journal::debug
Stream debug() const
Definition:
Journal.h:315
ripple::Resource::Consumer::charge
Disposition charge(Charge const &fee)
Apply a load charge to the consumer.
Definition:
Consumer.cpp:99
ripple::Resource::Logic::charge
Disposition charge(Entry &entry, Charge const &fee)
Definition:
resource/impl/Logic.h:445
ripple::Resource::Consumer::m_logic
Logic * m_logic
Definition:
Consumer.h:91
ripple::Resource::Consumer::Consumer
Consumer()
Definition:
Consumer.cpp:33
ripple::Resource::Consumer::~Consumer
~Consumer()
Definition:
Consumer.cpp:47
ripple::Resource::ok
@ ok
No action required.
Definition:
Disposition.h:29
Generated by
1.8.17