32 #include "DebugFunctions.h" 34 #include "ServerFunctionsList.h" 37 #include <Structure.h> 40 #include <BESInternalError.h> 41 #include <BESInternalFatalError.h> 42 #include <BESSyntaxUserError.h> 43 #include <BESForbiddenError.h> 44 #include <BESNotFoundError.h> 45 #include <BESTimeoutError.h> 47 namespace debug_function {
49 static string getFunctionNames()
52 libdap::ServerFunctionsList::TheList()->getFunctionNames(&names);
55 for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
56 if (!msg.empty()) msg +=
", ";
64 void DebugFunctions::initialize(
const string &)
66 BESDEBUG(
"DebugFunctions",
"initialize() - BEGIN" << std::endl);
67 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
70 libdap::ServerFunctionsList::TheList()->add_function(abortFunc);
73 libdap::ServerFunctionsList::TheList()->add_function(sleepFunc);
76 libdap::ServerFunctionsList::TheList()->add_function(sumUntilFunc);
79 libdap::ServerFunctionsList::TheList()->add_function(errorFunc);
81 BESDEBUG(
"DebugFunctions",
"initialize() - function names: " << getFunctionNames() << std::endl);
83 BESDEBUG(
"DebugFunctions",
"initialize() - END" << std::endl);
86 void DebugFunctions::terminate(
const string &)
88 BESDEBUG(
"DebugFunctions",
"Removing DebugFunctions Modules (this does nothing)." << std::endl);
99 strm << BESIndent::LMarg <<
"DebugFunctions::dump - (" << (
void *)
this <<
")" << std::endl;
109 string abort_usage =
"abort(##) Where ## is the number of milliseconds to sleep before calling abort.";
110 AbortFunc::AbortFunc()
113 setDescriptionString((
string)
"This function calls abort() killing the beslistner process.");
114 setUsageString(abort_usage);
115 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/abort");
116 setDocUrl(
"http://docs.opendap.org/index.php/Debug_Functions");
117 setFunction(debug_function::abort_ssf);
121 void abort_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
124 std::stringstream msg;
125 libdap::Str *response =
new libdap::Str(
"info");
129 msg <<
"Missing time parameter! USAGE: " << abort_usage;
132 libdap::Int32 *param1 = dynamic_cast<libdap::Int32*>(argv[0]);
134 libdap::dods_int32 milliseconds = param1->value();
136 msg <<
"abort in " << milliseconds <<
"ms" << endl;
137 response->set_value(msg.str());
139 usleep(milliseconds * 1000);
140 msg <<
"abort now. " << endl;
145 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: " 151 response->set_value(msg.str());
165 string sleep_usage =
"sleep(##) where ## is the number of milliseconds to sleep.";
166 SleepFunc::SleepFunc()
169 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
170 setUsageString(sleep_usage);
171 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sleep");
172 setDocUrl(
"http://docs.opendap.org/index.php/Debug_Functions");
173 setFunction(debug_function::sleep_ssf);
177 void sleep_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
180 std::stringstream msg;
181 libdap::Str *sleep_info =
new libdap::Str(
"info");
190 msg <<
"Missing time parameter! USAGE: " << sleep_usage;
193 libdap::Int32 *param1 = dynamic_cast<libdap::Int32*>(argv[0]);
195 libdap::dods_int32 milliseconds = param1->value();
196 usleep(milliseconds * 1000);
197 msg <<
"Slept for " << milliseconds <<
" ms.";
200 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: " 206 sleep_info->set_value(msg.str());
232 string sum_until_usage =
"sum_until(<val> [,0|<true>]) Compute a sum until <val> of milliseconds has elapsed; 0|<true> print the sum value.";
233 SumUntilFunc::SumUntilFunc()
235 setName(
"sum_until");
236 setDescriptionString((
string)
"This function calls sleep() for the specified number of millisecs.");
237 setUsageString(sum_until_usage);
238 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/sum_until");
239 setDocUrl(
"http://docs.opendap.org/index.php/Debug_Functions");
240 setFunction(debug_function::sum_until_ssf);
244 void sum_until_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
247 std::stringstream msg;
248 libdap::Str *response =
new libdap::Str(
"info");
251 if (!(argc == 1 || argc == 2)) {
252 msg <<
"Missing time parameter! USAGE: " << sum_until_usage;
254 response->set_value(msg.str());
258 libdap::Int32 *param1 = dynamic_cast<libdap::Int32*>(argv[0]);
261 msg <<
"This function only accepts integer values " <<
"for the time (in milliseconds) parameter. USAGE: " 264 response->set_value(msg.str());
268 bool print_sum_value =
true;
271 libdap::Int32 *temp = dynamic_cast<libdap::Int32*>(argv[1]);
272 if (temp && temp->value() == 0)
273 print_sum_value =
false;
276 libdap::dods_int32 milliseconds = param1->value();
279 gettimeofday(&tv, NULL);
280 double start_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
281 double end_time = start_time;
291 fib = one_past + two_past;
294 gettimeofday(&tv, NULL);
295 end_time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
296 if (end_time - start_time >= milliseconds) {
301 if (!print_sum_value)
302 msg <<
"Summed for " << end_time - start_time <<
" ms.";
304 msg <<
"Summed for " << end_time - start_time <<
" ms. n: " << n;
306 response->set_value(msg.str());
318 string error_usage =
"error(##) where ## is the BESError type to generate.";
319 ErrorFunc::ErrorFunc()
322 setDescriptionString((
string)
"This function triggers a BES Error of the type specified");
323 setUsageString(error_usage);
324 setRole(
"http://services.opendap.org/dap4/server-side-function/debug/error");
325 setDocUrl(
"http://docs.opendap.org/index.php/Debug_Functions");
326 setFunction(debug_function::error_ssf);
330 void error_ssf(
int argc, libdap::BaseType * argv[], libdap::DDS &, libdap::BaseType **btpp)
333 std::stringstream msg;
334 libdap::Str *response =
new libdap::Str(
"info");
337 string location =
"error_ssf";
340 msg <<
"Missing error type parameter! USAGE: " << error_usage;
343 libdap::Int32 *param1 = dynamic_cast<libdap::Int32*>(argv[0]);
345 libdap::dods_int32 error_type = param1->value();
347 switch (error_type) {
349 case BES_INTERNAL_ERROR: {
350 msg <<
"A BESInternalError was requested.";
356 case BES_INTERNAL_FATAL_ERROR: {
357 msg <<
"A BESInternalFatalError was requested.";
363 case BES_SYNTAX_USER_ERROR: {
364 msg <<
"A BESSyntaxUserError was requested.";
370 case BES_FORBIDDEN_ERROR: {
371 msg <<
"A BESForbiddenError was requested.";
377 case BES_NOT_FOUND_ERROR: {
378 msg <<
"A BESNotFoundError was requested.";
384 case BES_TIMEOUT_ERROR: {
385 msg <<
"A BESTimeOutError was requested.";
392 msg <<
"An unrecognized error_type parameter was received. Requested error_type: " << error_type;
398 msg <<
"This function only accepts integer values " <<
"for the error type parameter. USAGE: " 404 response->set_value(msg.str());
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
exception thrown if an internal error is found and is fatal to the BES
exception thrown if inernal error encountered
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
error thrown if the BES is not allowed to access the resource requested