38#include "DebugFunctions.h"
40#include <libdap/ServerFunctionsList.h>
41#include <libdap/Int32.h>
42#include <libdap/Structure.h>
43#include <libdap/Str.h>
47#include <BESInternalError.h>
48#include <BESInternalFatalError.h>
49#include <BESSyntaxUserError.h>
50#include <BESForbiddenError.h>
51#include <BESNotFoundError.h>
52#include <BESTimeoutError.h>
54namespace debug_function {
56static string getFunctionNames()
59 libdap::ServerFunctionsList::TheList()->getFunctionNames(&names);
62 for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
63 if (!msg.empty()) msg +=
", ";
71void DebugFunctions::initialize(
const string &)
73 BESDEBUG(
"DebugFunctions",
"initialize() - BEGIN" << std::endl);
74 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
77 libdap::ServerFunctionsList::TheList()->add_function(abortFunc);
80 libdap::ServerFunctionsList::TheList()->add_function(sleepFunc);
83 libdap::ServerFunctionsList::TheList()->add_function(sumUntilFunc);
86 libdap::ServerFunctionsList::TheList()->add_function(errorFunc);
88 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
90 BESDEBUG(
"DebugFunctions",
"initialize() - END" << std::endl);
93void DebugFunctions::terminate(
const string &)
95 BESDEBUG(
"DebugFunctions",
"Removing DebugFunctions Modules (this does nothing)." << std::endl);
106 strm << BESIndent::LMarg <<
"DebugFunctions::dump - (" << (
void *)
this <<
")" << std::endl;
116string abort_usage =
"abort(##) Where ## is the number of milliseconds to sleep before calling abort.";
117AbortFunc::AbortFunc()
120 setDescriptionString((
string)
"This function calls abort() killing the beslistner process.");
121 setUsageString(abort_usage);
122 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/abort");
123 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
124 setFunction(debug_function::abort_ssf);
128void abort_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
131 std::stringstream msg;
132 libdap::Str *response =
new libdap::Str(
"info");
136 msg <<
"Missing time parameter! USAGE: " << abort_usage;
139 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
141 libdap::dods_int32 milliseconds = param1->value();
143 msg <<
"abort in " << milliseconds <<
"ms" << endl;
144 response->set_value(msg.str());
146 usleep(milliseconds * 1000);
147 msg <<
"abort now. " << endl;
152 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
158 response->set_value(msg.str());
171string sleep_usage =
"sleep(##) where ## is the number of milliseconds to sleep.";
172SleepFunc::SleepFunc()
175 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
176 setUsageString(sleep_usage);
177 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sleep");
178 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
179 setFunction(debug_function::sleep_ssf);
183void sleep_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
186 std::stringstream msg;
187 libdap::Str *sleep_info =
new libdap::Str(
"info");
196 msg <<
"Missing time parameter! USAGE: " << sleep_usage;
199 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
201 libdap::dods_int32 milliseconds = param1->value();
202 usleep(milliseconds * 1000);
203 msg <<
"Slept for " << milliseconds <<
" ms.";
206 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
212 sleep_info->set_value(msg.str());
237string sum_until_usage =
"sum_until(<val> [,0|<true>]) Compute a sum until <val> of milliseconds has elapsed; 0|<true> print the sum value.";
238SumUntilFunc::SumUntilFunc()
240 setName(
"sum_until");
241 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
242 setUsageString(sum_until_usage);
243 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sum_until");
244 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
245 setFunction(debug_function::sum_until_ssf);
249void sum_until_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
252 std::stringstream msg;
253 libdap::Str *response =
new libdap::Str(
"info");
256 if (!(argc == 1 || argc == 2)) {
257 msg <<
"Missing time parameter! USAGE: " << sum_until_usage;
259 response->set_value(msg.str());
263 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
266 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
269 response->set_value(msg.str());
273 bool print_sum_value =
true;
276 libdap::Int32 *temp =
dynamic_cast<libdap::Int32*
>(argv[1]);
277 if (temp && temp->value() == 0)
278 print_sum_value =
false;
281 libdap::dods_int32 milliseconds = param1->value();
284 gettimeofday(&tv, NULL);
285 double start_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
286 double end_time = start_time;
296 fib = one_past + two_past;
299 gettimeofday(&tv, NULL);
300 end_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
301 if (end_time - start_time >= milliseconds) {
306 if (!print_sum_value)
307 msg <<
"Summed for " << end_time - start_time <<
" ms.";
309 msg <<
"Summed for " << end_time - start_time <<
" ms. n: " << n;
311 response->set_value(msg.str());
323string error_usage =
"error(##) where ## is the BESError type to generate.";
324ErrorFunc::ErrorFunc()
327 setDescriptionString((
string)
"This function triggers a BES Error of the type specified");
328 setUsageString(error_usage);
329 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/error");
330 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
331 setFunction(debug_function::error_ssf);
335void error_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
337 std::stringstream msg;
338 auto *response =
new libdap::Str(
"info");
341 string location =
"error_ssf";
344 msg <<
"Missing error type parameter! USAGE: " << error_usage;
347 auto param1 =
dynamic_cast<const libdap::Int32*
>(argv[0]);
349 libdap::dods_int32 error_type = param1->value();
351 switch (error_type) {
353 case BES_INTERNAL_ERROR: {
354 msg <<
"A BESInternalError was requested.";
358 case BES_INTERNAL_FATAL_ERROR: {
359 msg <<
"A BESInternalFatalError was requested.";
363 case BES_SYNTAX_USER_ERROR: {
364 msg <<
"A BESSyntaxUserError was requested.";
368 case BES_FORBIDDEN_ERROR: {
369 msg <<
"A BESForbiddenError was requested.";
373 case BES_NOT_FOUND_ERROR: {
374 msg <<
"A BESNotFoundError was requested.";
378 case BES_TIMEOUT_ERROR: {
379 msg <<
"A BESTimeOutError was requested.";
384 msg <<
"A Segmentation Fault has been requested.";
385 cerr << msg.str() << endl;
391 msg <<
"A PIPE signal has been requested.";
392 cerr << msg.str() << endl;
398 throw std::bad_alloc();
402 msg <<
"An unrecognized error_type parameter was received. Requested error_type: " << error_type;
408 msg <<
"This function only accepts integer values " <<
"for the error type parameter. USAGE: "
414 response->set_value(msg.str());
error thrown if the BES is not allowed to access the resource requested
exception thrown if internal error encountered
exception thrown if an internal error is found and is fatal to the BES
error thrown if the resource requested cannot be found
error thrown if there is a user syntax error in the request or any other user error
error thrown if there is a user syntax error in the request or any other user error
virtual void dump(ostream &strm) const
dumps information about this object