40#include "BESContextManager.h"
49static std::once_flag d_euc_init_once;
51#define MODULE "context"
52#define prolog std::string("BESContextManager::").append(__func__).append("() - ")
54BESContextManager::BESContextManager() {}
56BESContextManager::~BESContextManager() {}
65 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
67 BESDEBUG(MODULE, prolog <<
"name=\"" << name <<
"\", value=\"" << value <<
"\"" << endl);
68 _context_list[name] = value;
78 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
80 BESDEBUG(MODULE, prolog <<
"name=\"" << name <<
"\"" << endl);
81 _context_list.erase(name);
95 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
101 auto const i = _context_list.find(name);
102 if (i != _context_list.end()) {
107 BESDEBUG(MODULE, prolog <<
"name=\"" << name <<
"\", found=\"" << found <<
"\" value:\"" << ret <<
"\"" << endl);
123 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
125 string value = BESContextManager::TheManager()->get_context(name, found);
127 if (!found || value.empty())
return 0;
131 int val = strtol(value.c_str(), &endptr, 10);
132 if (val == 0 && errno > 0) {
133 throw BESInternalError(
string(
"Error reading an integer value for the context '") + name +
"': " + strerror(errno),
136 BESDEBUG(MODULE, prolog <<
"name=\"" << name <<
"\", found=\"" << found <<
"\" value: \"" << val <<
"\"" << endl);
141uint64_t BESContextManager::get_context_uint64(
const string &name,
bool &found)
143 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
145 string value = BESContextManager::TheManager()->get_context(name, found);
147 if (!found || value.empty())
return 0;
151 uint64_t val = strtoull(value.c_str(), &endptr, 10);
152 if (val == 0 && errno > 0) {
153 throw BESInternalError(
string(
"Error reading an integer value for the context '") + name +
"': " + strerror(errno),
156 BESDEBUG(MODULE, prolog <<
"name=\"" << name <<
"\", found=\"" << found <<
"\" value: \"" << val <<
"\"" << endl);
168 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
172 std::map<string, string, std::less<>> props;
173 auto i = _context_list.begin();
174 auto e = _context_list.end();
175 for (; i != e; i++) {
179 props[
"name"] = name;
180 info.add_tag(
"context", value, &props);
193 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
195 strm << BESIndent::LMarg << prolog <<
"(this: " << (
void *)
this <<
")" << endl;
197 if (_context_list.size()) {
198 strm << BESIndent::LMarg <<
"current context:" << endl;
200 auto i = _context_list.begin();
201 auto ie = _context_list.end();
202 for (; i != ie; i++) {
203 strm << BESIndent::LMarg << (*i).first <<
": " << (*i).second << endl;
205 BESIndent::UnIndent();
208 strm << BESIndent::LMarg <<
"no context" << endl;
210 BESIndent::UnIndent();
214BESContextManager::TheManager()
216 std::call_once(d_euc_init_once,BESContextManager::initialize_instance);
220void BESContextManager::initialize_instance() {
221 d_instance =
new BESContextManager;
223 atexit(delete_instance);
227void BESContextManager::delete_instance() {
maintains the list of registered request handlers for this server
virtual void list_context(BESInfo &info)
Adds all context and their values to the given informational object.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual int get_context_int(const std::string &name, bool &found)
Get the value of the given context and return it as an integer.
virtual void set_context(const std::string &name, const std::string &value)
set context in the BES
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
virtual void unset_context(const std::string &name)
set context in the BES
informational response object
exception thrown if internal error encountered