rippled
Loading...
Searching...
No Matches
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
25namespace ripple {
26namespace Resource {
27
29 : m_logic(&logic), m_entry(&entry)
30{
31}
32
33Consumer::Consumer() : m_logic(nullptr), m_entry(nullptr)
34{
35}
36
38 : m_logic(other.m_logic), m_entry(nullptr)
39{
40 if (m_logic && other.m_entry)
41 {
42 m_entry = other.m_entry;
44 }
45}
46
48{
49 if (m_logic && m_entry)
51}
52
55{
56 // remove old ref
57 if (m_logic && 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)
66
67 return *this;
68}
69
72{
73 if (m_logic == nullptr)
74 return "(none)";
75
76 return m_entry->to_string();
77}
78
79bool
81{
82 if (m_entry)
83 return m_entry->isUnlimited();
84
85 return false;
86}
87
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
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
109bool
111{
112 XRPL_ASSERT(m_entry, "ripple::Resource::Consumer::warn : non-null entry");
113 return m_logic->warn(*m_entry);
114}
115
116bool
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
129int
131{
132 XRPL_ASSERT(
133 m_entry, "ripple::Resource::Consumer::balance : non-null entry");
134 return m_logic->balance(*m_entry);
135}
136
137Entry&
139{
140 XRPL_ASSERT(m_entry, "ripple::Resource::Consumer::entry : non-null entry");
141 return *m_entry;
142}
143
145operator<<(std::ostream& os, Consumer const& v)
146{
147 os << v.to_string();
148 return os;
149}
150
151} // namespace Resource
152} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream debug() const
Definition: Journal.h:317
A consumption charge.
Definition: Charge.h:31
An endpoint that consumes resources.
Definition: Consumer.h:35
bool warn()
Returns true if the consumer should be warned.
Definition: Consumer.cpp:110
Consumer & operator=(Consumer const &other)
Definition: Consumer.cpp:54
int balance()
Returns the credit balance representing consumption.
Definition: Consumer.cpp:130
std::string to_string() const
Return a human readable string uniquely identifying this consumer.
Definition: Consumer.cpp:71
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition: Consumer.cpp:117
Disposition charge(Charge const &fee)
Apply a load charge to the consumer.
Definition: Consumer.cpp:99
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition: Consumer.cpp:80
Disposition disposition() const
Returns the current disposition of this consumer.
Definition: Consumer.cpp:89
Disposition charge(Entry &entry, Charge const &fee)
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition: Charge.cpp:52
Disposition
The disposition of a consumer after applying a load charge.
Definition: Disposition.h:27
@ ok
No action required.
Definition: Disposition.h:29
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string to_string() const
Definition: Entry.h:54
bool isUnlimited() const
Returns true if this connection should have no resource limits applied–it is still possible for certa...
Definition: Entry.h:65