bes  Updated for version 3.20.10
BESResponseHandlerList.cc
1 // BESResponseHandlerList.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <mutex>
34 #include "config.h"
35 
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 
40 #include "BESResponseHandlerList.h"
41 
42 using std::endl;
43 using std::ostream;
44 using std::string;
45 
46 BESResponseHandlerList *BESResponseHandlerList::d_instance = nullptr;
47 static std::once_flag d_euc_init_once;
48 
49 BESResponseHandlerList::BESResponseHandlerList() {}
50 
51 BESResponseHandlerList::~BESResponseHandlerList() {}
52 
67 bool BESResponseHandlerList::add_handler(const string &handler_name, p_response_handler handler_method)
68 {
69  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
70 
71  BESResponseHandlerList::Handler_citer i = _handler_list.find(handler_name);
72  if (i == _handler_list.end()) {
73  _handler_list[handler_name] = handler_method;
74  return true;
75  }
76  return false;
77 }
78 
88 bool BESResponseHandlerList::remove_handler(const string &handler_name)
89 {
90  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
91 
92  BESResponseHandlerList::Handler_iter i = _handler_list.find(handler_name);
93  if (i != _handler_list.end()) {
94  _handler_list.erase(i);
95  return true;
96  }
97  return false;
98 }
99 
113 BESResponseHandlerList::find_handler(const string &handler_name)
114 {
115  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
116 
117  BESResponseHandlerList::Handler_citer i = _handler_list.find(handler_name);
118  if (i != _handler_list.end()) {
119  p_response_handler p = (*i).second;
120  if (p) {
121  return p(handler_name);
122  }
123  }
124  return 0;
125 }
126 
135 {
136  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
137 
138  string ret = "";
139  bool first_name = true;
140  BESResponseHandlerList::Handler_citer i = _handler_list.begin();
141  for (; i != _handler_list.end(); i++) {
142  if (!first_name) ret += ", ";
143  ret += (*i).first;
144  first_name = false;
145  }
146  return ret;
147 }
148 
156 void BESResponseHandlerList::dump(ostream &strm) const
157 {
158  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
159 
160  strm << BESIndent::LMarg << "BESResponseHandlerList::dump - (" << (void *) this << ")" << endl;
161  BESIndent::Indent();
162  if (_handler_list.size()) {
163  strm << BESIndent::LMarg << "registered response handlers:" << endl;
164  BESIndent::Indent();
165  BESResponseHandlerList::Handler_citer i = _handler_list.begin();
166  BESResponseHandlerList::Handler_citer ie = _handler_list.end();
167  for (; i != ie; i++) {
168  strm << BESIndent::LMarg << (*i).first << endl;
169  }
170  BESIndent::UnIndent();
171  }
172  else {
173  strm << BESIndent::LMarg << "registered response handlers: none" << endl;
174  }
175  BESIndent::UnIndent();
176 }
177 
179 BESResponseHandlerList::TheList()
180 {
181  std::call_once(d_euc_init_once,BESResponseHandlerList::initialize_instance);
182  return d_instance;
183 }
184 
185 void BESResponseHandlerList::initialize_instance() {
186  d_instance = new BESResponseHandlerList;
187 #ifdef HAVE_ATEXIT
188  atexit(delete_instance);
189 #endif
190 }
191 
192 void BESResponseHandlerList::delete_instance() {
193  delete d_instance;
194  d_instance = 0;
195 }
196 
List of all registered response handlers for this server.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual std::string get_handler_names()
returns the comma separated list of all response handlers currently registered with this server.
virtual BESResponseHandler * find_handler(const std::string &handler)
returns the response handler with the given name from the list
virtual bool remove_handler(const std::string &handler)
removes a response handler from the list
virtual bool add_handler(const std::string &handler, p_response_handler handler_method)
add a response handler to the list
handler object that knows how to create a specific response object