35 #include "DebugFunctions.h"
37 #include <libdap/ServerFunctionsList.h>
39 #include <libdap/Int32.h>
40 #include <libdap/Structure.h>
41 #include <libdap/Str.h>
43 #include <BESInternalError.h>
44 #include <BESInternalFatalError.h>
45 #include <BESSyntaxUserError.h>
46 #include <BESForbiddenError.h>
47 #include <BESNotFoundError.h>
48 #include <BESTimeoutError.h>
50 namespace debug_function {
52 static string getFunctionNames()
55 libdap::ServerFunctionsList::TheList()->getFunctionNames(&names);
58 for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
59 if (!msg.empty()) msg +=
", ";
67 void DebugFunctions::initialize(
const string &)
69 BESDEBUG(
"DebugFunctions",
"initialize() - BEGIN" << std::endl);
70 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
73 libdap::ServerFunctionsList::TheList()->add_function(abortFunc);
76 libdap::ServerFunctionsList::TheList()->add_function(sleepFunc);
79 libdap::ServerFunctionsList::TheList()->add_function(sumUntilFunc);
82 libdap::ServerFunctionsList::TheList()->add_function(errorFunc);
84 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
86 BESDEBUG(
"DebugFunctions",
"initialize() - END" << std::endl);
89 void DebugFunctions::terminate(
const string &)
91 BESDEBUG(
"DebugFunctions",
"Removing DebugFunctions Modules (this does nothing)." << std::endl);
102 strm << BESIndent::LMarg <<
"DebugFunctions::dump - (" << (
void *)
this <<
")" << std::endl;
112 string abort_usage =
"abort(##) Where ## is the number of milliseconds to sleep before calling abort.";
113 AbortFunc::AbortFunc()
116 setDescriptionString((
string)
"This function calls abort() killing the beslistner process.");
117 setUsageString(abort_usage);
118 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/abort");
119 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
120 setFunction(debug_function::abort_ssf);
124 void abort_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
127 std::stringstream msg;
128 libdap::Str *response =
new libdap::Str(
"info");
132 msg <<
"Missing time parameter! USAGE: " << abort_usage;
135 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
137 libdap::dods_int32 milliseconds = param1->value();
139 msg <<
"abort in " << milliseconds <<
"ms" << endl;
140 response->set_value(msg.str());
142 usleep(milliseconds * 1000);
143 msg <<
"abort now. " << endl;
148 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
154 response->set_value(msg.str());
167 string sleep_usage =
"sleep(##) where ## is the number of milliseconds to sleep.";
168 SleepFunc::SleepFunc()
171 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
172 setUsageString(sleep_usage);
173 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sleep");
174 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
175 setFunction(debug_function::sleep_ssf);
179 void sleep_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
182 std::stringstream msg;
183 libdap::Str *sleep_info =
new libdap::Str(
"info");
192 msg <<
"Missing time parameter! USAGE: " << sleep_usage;
195 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
197 libdap::dods_int32 milliseconds = param1->value();
198 usleep(milliseconds * 1000);
199 msg <<
"Slept for " << milliseconds <<
" ms.";
202 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
208 sleep_info->set_value(msg.str());
233 string sum_until_usage =
"sum_until(<val> [,0|<true>]) Compute a sum until <val> of milliseconds has elapsed; 0|<true> print the sum value.";
234 SumUntilFunc::SumUntilFunc()
236 setName(
"sum_until");
237 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
238 setUsageString(sum_until_usage);
239 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sum_until");
240 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
241 setFunction(debug_function::sum_until_ssf);
245 void sum_until_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
248 std::stringstream msg;
249 libdap::Str *response =
new libdap::Str(
"info");
252 if (!(argc == 1 || argc == 2)) {
253 msg <<
"Missing time parameter! USAGE: " << sum_until_usage;
255 response->set_value(msg.str());
259 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
262 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: "
265 response->set_value(msg.str());
269 bool print_sum_value =
true;
272 libdap::Int32 *temp =
dynamic_cast<libdap::Int32*
>(argv[1]);
273 if (temp && temp->value() == 0)
274 print_sum_value =
false;
277 libdap::dods_int32 milliseconds = param1->value();
280 gettimeofday(&tv, NULL);
281 double start_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
282 double end_time = start_time;
292 fib = one_past + two_past;
295 gettimeofday(&tv, NULL);
296 end_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
297 if (end_time - start_time >= milliseconds) {
302 if (!print_sum_value)
303 msg <<
"Summed for " << end_time - start_time <<
" ms.";
305 msg <<
"Summed for " << end_time - start_time <<
" ms. n: " << n;
307 response->set_value(msg.str());
319 string error_usage =
"error(##) where ## is the BESError type to generate.";
320 ErrorFunc::ErrorFunc()
323 setDescriptionString((
string)
"This function triggers a BES Error of the type specified");
324 setUsageString(error_usage);
325 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/error");
326 setDocUrl(
"https://docs.opendap.org/index.php/Debug_Functions");
327 setFunction(debug_function::error_ssf);
331 void error_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
334 std::stringstream msg;
335 libdap::Str *response =
new libdap::Str(
"info");
338 string location =
"error_ssf";
341 msg <<
"Missing error type parameter! USAGE: " << error_usage;
344 libdap::Int32 *param1 =
dynamic_cast<libdap::Int32*
>(argv[0]);
346 libdap::dods_int32 error_type = param1->value();
348 switch (error_type) {
350 case BES_INTERNAL_ERROR: {
351 msg <<
"A BESInternalError was requested.";
357 case BES_INTERNAL_FATAL_ERROR: {
358 msg <<
"A BESInternalFatalError was requested.";
364 case BES_SYNTAX_USER_ERROR: {
365 msg <<
"A BESSyntaxUserError was requested.";
371 case BES_FORBIDDEN_ERROR: {
372 msg <<
"A BESForbiddenError was requested.";
378 case BES_NOT_FOUND_ERROR: {
379 msg <<
"A BESNotFoundError was requested.";
385 case BES_TIMEOUT_ERROR: {
386 msg <<
"A BESTimeOutError was requested.";
393 msg <<
"A Segmentation Fault has been requested.";
394 cerr << msg.str() << endl;
400 msg <<
"An unrecognized error_type parameter was received. Requested error_type: " << error_type;
406 msg <<
"This function only accepts integer values " <<
"for the error type parameter. USAGE: "
412 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