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/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
32namespace ripple {
33namespace Resource {
34
36 : m_logic(&logic), m_entry(&entry)
37{
38}
39
40Consumer::Consumer() : m_logic(nullptr), m_entry(nullptr)
41{
42}
43
45 : m_logic(other.m_logic), m_entry(nullptr)
46{
47 if (m_logic && other.m_entry)
48 {
49 m_entry = other.m_entry;
51 }
52}
53
55{
56 if (m_logic && m_entry)
58}
59
62{
63 // remove old ref
64 if (m_logic && 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)
73
74 return *this;
75}
76
79{
80 if (m_logic == nullptr)
81 return "(none)";
82
83 return m_entry->to_string();
84}
85
86bool
88{
89 if (m_entry)
90 return m_entry->isUnlimited();
91
92 return false;
93}
94
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
106Consumer::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
116bool
118{
119 XRPL_ASSERT(m_entry, "ripple::Resource::Consumer::warn : non-null entry");
120 return m_logic->warn(*m_entry);
121}
122
123bool
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
136int
138{
139 XRPL_ASSERT(
140 m_entry, "ripple::Resource::Consumer::balance : non-null entry");
141 return m_logic->balance(*m_entry);
142}
143
144Entry&
146{
147 XRPL_ASSERT(m_entry, "ripple::Resource::Consumer::entry : non-null entry");
148 return *m_entry;
149}
150
152operator<<(std::ostream& os, Consumer const& v)
153{
154 os << v.to_string();
155 return os;
156}
157
158} // namespace Resource
159} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream debug() const
Definition: Journal.h:328
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:117
Consumer & operator=(Consumer const &other)
Definition: Consumer.cpp:61
int balance()
Returns the credit balance representing consumption.
Definition: Consumer.cpp:137
std::string to_string() const
Return a human readable string uniquely identifying this consumer.
Definition: Consumer.cpp:78
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition: Consumer.cpp:124
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition: Consumer.cpp:106
bool isUnlimited() const
Returns true if this is a privileged endpoint.
Definition: Consumer.cpp:87
Disposition disposition() const
Returns the current disposition of this consumer.
Definition: Consumer.cpp:96
Disposition charge(Entry &entry, Charge const &fee, std::string context={})
std::ostream & operator<<(std::ostream &os, Charge const &v)
Definition: Charge.cpp:56
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