42#include "DebugFunctions.h"
44#include <libdap/ServerFunctionsList.h>
45#include <libdap/Int32.h>
46#include <libdap/Structure.h>
47#include <libdap/Str.h>
51#include <BESInternalError.h>
52#include <BESInternalFatalError.h>
53#include <BESSyntaxUserError.h>
54#include <BESForbiddenError.h>
55#include <BESNotFoundError.h>
56#include <BESTimeoutError.h>
59namespace debug_function {
61static string getFunctionNames()
64 libdap::ServerFunctionsList::TheList()->getFunctionNames(&names);
67 for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
68 if (!msg.empty()) msg +=
", ";
76void DebugFunctions::initialize(
const string &)
78 BESDEBUG(
"DebugFunctions",
"initialize() - BEGIN" << std::endl);
79 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
81 debug_function::AbortFunc *abortFunc =
new debug_function::AbortFunc();
82 libdap::ServerFunctionsList::TheList()->add_function(abortFunc);
84 debug_function::SleepFunc *sleepFunc =
new debug_function::SleepFunc();
85 libdap::ServerFunctionsList::TheList()->add_function(sleepFunc);
87 debug_function::SumUntilFunc *sumUntilFunc =
new debug_function::SumUntilFunc();
88 libdap::ServerFunctionsList::TheList()->add_function(sumUntilFunc);
90 debug_function::ErrorFunc *errorFunc =
new debug_function::ErrorFunc();
91 libdap::ServerFunctionsList::TheList()->add_function(errorFunc);
93 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
95 BESDEBUG(
"DebugFunctions",
"initialize() - END" << std::endl);
98void DebugFunctions::terminate(
const string &)
100 BESDEBUG(
"DebugFunctions",
"Removing DebugFunctions Modules (this does nothing)." << std::endl);
111 strm << BESIndent::LMarg <<
"DebugFunctions::dump - (" << (
void *)
this <<
")" << std::endl;
121string abort_usage =
"abort(##) Where ## is the number of milliseconds to sleep before calling abort.";
122AbortFunc::AbortFunc()
125 setDescriptionString((
string)
"This function calls abort() killing the beslistner process.");
126 setUsageString(abort_usage);
127 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/abort");
128 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
129 setFunction(debug_function::abort_ssf);
133void abort_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
136 std::stringstream msg;
137 libdap::Str *response =
new libdap::Str(
"info");
141 msg <<
"Missing time parameter! USAGE: " << abort_usage;
144 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
146 libdap::dods_int32 milliseconds = param1->value();
148 msg <<
"abort in " << milliseconds <<
"ms" << endl;
149 response->set_value(msg.str());
151 usleep(milliseconds * 1000);
152 msg <<
"abort now. " << endl;
157 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
163 response->set_value(msg.str());
176string sleep_usage =
"sleep(##) where ## is the number of milliseconds to sleep.";
177SleepFunc::SleepFunc()
180 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
181 setUsageString(sleep_usage);
182 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sleep");
183 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
184 setFunction(debug_function::sleep_ssf);
188void sleep_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
191 std::stringstream msg;
192 libdap::Str *sleep_info =
new libdap::Str(
"info");
201 msg <<
"Missing time parameter! USAGE: " << sleep_usage;
204 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
206 libdap::dods_int32 milliseconds = param1->value();
207 usleep(milliseconds * 1000);
208 msg <<
"Slept for " << milliseconds <<
" ms.";
211 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
217 sleep_info->set_value(msg.str());
242string sum_until_usage =
"sum_until(<val> [,0|<true>]) Compute a sum until <val> of milliseconds has elapsed; 0|<true> print the sum value.";
243SumUntilFunc::SumUntilFunc()
245 setName(
"sum_until");
246 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
247 setUsageString(sum_until_usage);
248 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sum_until");
249 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
250 setFunction(debug_function::sum_until_ssf);
254void sum_until_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
257 std::stringstream msg;
258 libdap::Str *response =
new libdap::Str(
"info");
261 if (!(argc == 1 || argc == 2)) {
262 msg <<
"Missing time parameter! USAGE: " << sum_until_usage;
264 response->set_value(msg.str());
268 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
271 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
274 response->set_value(msg.str());
278 bool print_sum_value =
true;
281 libdap::Int32 *temp =
dynamic_cast<libdap::Int32*
>(argv[1]);
282 if (temp && temp->value() == 0)
283 print_sum_value =
false;
286 libdap::dods_int32 milliseconds = param1->value();
289 gettimeofday(&tv, NULL);
290 double start_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
291 double end_time = start_time;
294 uint64_t one_past = 1;
295 uint64_t two_past = 0;
301 fib = one_past + two_past;
304 gettimeofday(&tv, NULL);
305 end_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
306 if (end_time - start_time >= milliseconds) {
311 if (!print_sum_value)
312 msg <<
"Summed for " << end_time - start_time <<
" ms.";
314 msg <<
"Summed for " << end_time - start_time <<
" ms. n: " << n;
316 response->set_value(msg.str());
328string error_usage =
"error(##) where ## is the BESError type to generate.";
329ErrorFunc::ErrorFunc()
332 setDescriptionString((
string)
"This function triggers a BES Error of the type specified");
333 setUsageString(error_usage);
334 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/error");
335 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
336 setFunction(debug_function::error_ssf);
340void error_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
342 std::stringstream msg;
343 auto *response =
new libdap::Str(
"info");
346 string location =
"error_ssf";
348 if (argc < 1 || argc>2) {
349 msg <<
"Missing error type parameter(s)! USAGE: " << error_usage;
350 response->set_value(msg.str());
354 auto param1 =
dynamic_cast<const libdap::Int32*
>(argv[0]);
356 msg <<
"This function only accepts integer values for the error type parameter(s). USAGE: "
358 response->set_value(msg.str());
363 const libdap::Int32 *param2=
nullptr;
365 param2 =
dynamic_cast<const libdap::Int32 *
>(argv[1]);
368 libdap::dods_int32 error_type = param1->value();
369 switch (error_type) {
371 case BES_INTERNAL_ERROR: {
372 msg <<
"A BESInternalError was requested.";
373 throw BESInternalError(msg.str(), location, 0);
376 case BES_INTERNAL_FATAL_ERROR: {
377 msg <<
"A BESInternalFatalError was requested.";
378 throw BESInternalFatalError(msg.str(), location, 0);
381 case BES_SYNTAX_USER_ERROR: {
382 msg <<
"A BESSyntaxUserError was requested.";
383 throw BESSyntaxUserError(msg.str(), location, 0);
386 case BES_FORBIDDEN_ERROR: {
387 msg <<
"A BESForbiddenError was requested.";
388 throw BESForbiddenError(msg.str(), location, 0);
391 case BES_NOT_FOUND_ERROR: {
392 msg <<
"A BESNotFoundError was requested.";
393 throw BESNotFoundError(msg.str(), location, 0);
396 case BES_TIMEOUT_ERROR: {
397 msg <<
"A BESTimeOutError was requested.";
398 throw BESTimeoutError(msg.str(), location, 0);
401 case BES_HTTP_ERROR: {
402 string http_err_msg(
"An HttpError was requested.");
403 CURLcode curl_code = CURLE_OK;
404 unsigned int http_status;
406 http_status = param2->value();
412 string origin_url(
"https://www.opendap.org");
413 string redirect_url(
"https://www.opendap.org/");
414 vector<string> rsp_hdrs;
415 rsp_hdrs.emplace_back(
"server: bes.error.functions");
416 rsp_hdrs.emplace_back(
"location: orbit-0");
417 string rsp_body(
"Every HTTP request includes an HTTP response. An HTTP response "
418 "is a set of metadata and a response body, where the body can "
419 "occasionally be zero bytes and thus nonexistent. An HTTP response "
420 "however always has response headers.");
422 throw http::HttpError(http_err_msg,
433 msg <<
"A Segmentation Fault has been requested.";
434 cerr << msg.str() << endl;
440 msg <<
"A PIPE signal has been requested.";
441 cerr << msg.str() << endl;
447 throw std::bad_alloc();
451 msg <<
"An unrecognized error_type parameter was received. Requested error_type: " << error_type;
452 response->set_value(msg.str());
virtual void dump(ostream &strm) const
dumps information about this object