54 using std::ostringstream;
56 #ifdef HAVE_LIBREADLINE 57 # if defined(HAVE_READLINE_READLINE_H) 58 # include <readline/readline.h> 59 # elif defined(HAVE_READLINE_H) 60 # include <readline.h> 63 char *readline(
const char *);
71 #ifdef HAVE_READLINE_HISTORY 72 # if defined(HAVE_READLINE_HISTORY_H) 73 # include <readline/history.h> 74 # elif defined(HAVE_HISTORY_H) 78 int add_history(
const char *);
79 int write_history(
const char *);
80 int read_history(
const char *);
87 #define SIZE_COMMUNICATION_BUFFER 4096*4096 89 #include "BESXMLInterface.h" 90 #include "BESStopWatch.h" 94 #include "StandAloneClient.h" 95 #include "CmdTranslation.h" 97 StandAloneClient::~StandAloneClient()
99 if (_strmCreated && _strm) {
127 if (_strmCreated && _strm) {
135 _strmCreated = created;
151 string suppress =
"suppress";
152 if (cmd.compare(0, suppress.length(), suppress) == 0) {
157 string output =
"output to";
158 if (cmd.compare(0, output.length(), output) == 0) {
159 string subcmd = cmd.substr(output.length() + 1);
160 string screen =
"screen";
161 if (subcmd.compare(0, screen.length(), screen) == 0) {
166 string file = subcmd.substr(0, subcmd.length() - 1);
167 ofstream *fstrm =
new ofstream(file.c_str(), ios::app);
168 if (fstrm && !(*fstrm)) {
170 cerr <<
"Unable to set client output to file " << file << endl;
180 string load =
"load";
181 if (cmd.compare(0, load.length(), load) == 0) {
182 string file = cmd.substr(load.length() + 1, cmd.length() - load.length() - 2);
183 ifstream fstrm(file.c_str());
185 cerr <<
"Unable to load commands from file " << file <<
": file does not exist or failed to open file" 195 cerr <<
"Improper client command " << cmd << endl;
210 void StandAloneClient::executeCommand(
const string & cmd,
int repeat)
212 string client =
"client";
213 if (cmd.compare(0, client.length(), client) == 0) {
217 if (repeat < 1) repeat = 1;
218 for (
int i = 0; i < repeat; i++) {
219 ostringstream *show_stream = 0;
220 if (CmdTranslation::is_show()) {
221 show_stream =
new ostringstream;
224 BESDEBUG(
"standalone",
"cmd client sending " << cmd << endl );
227 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"StandAloneClient::executeCommand");
240 BESDEBUG(
"standalone",
"BESServerHandler::execute - " 241 <<
"executed successfully" << endl );
246 BESDEBUG(
"standalone",
"BESServerHandler::execute - " 247 "error occurred" << endl );
254 interface->finish_with_error( status );
258 case BES_INTERNAL_FATAL_ERROR:
260 cerr <<
"BES server " << getpid()
261 <<
": Status not OK, dispatcher returned value " 266 case BES_INTERNAL_ERROR:
267 case BES_SYNTAX_USER_ERROR:
268 case BES_FORBIDDEN_ERROR:
269 case BES_NOT_FOUND_ERROR:
279 *(_strm) << show_stream->str() << endl;
307 _isInteractive =
true;
308 if (repeat < 1) repeat = 1;
310 CmdTranslation::set_show(
false);
312 string doc = CmdTranslation::translate(cmd_list);
314 executeCommand(doc, repeat);
318 CmdTranslation::set_show(
false);
319 _isInteractive =
false;
322 CmdTranslation::set_show(
false);
323 _isInteractive =
false;
346 _isInteractive =
false;
347 if (repeat < 1) repeat = 1;
348 for (
int i = 0; i < repeat; i++) {
350 istrm.seekg(0, ios::beg);
353 while (getline(istrm, line)) {
356 this->executeCommand(cmd, 1);
377 _isInteractive =
true;
379 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' " 380 <<
"to display the help screen" << endl << endl;
385 size_t len = this->readLine(message);
386 if ( message ==
"exit" || message ==
"exit;") {
389 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
392 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
393 this->executeCommand(message, 1);
395 else if (len != 0 && message !=
"") {
396 CmdTranslation::set_show(
false);
398 string doc = CmdTranslation::translate(message);
400 this->executeCommand(doc, 1);
404 CmdTranslation::set_show(
false);
405 _isInteractive =
false;
408 CmdTranslation::set_show(
false);
411 _isInteractive =
false;
419 size_t StandAloneClient::readLine(
string & msg)
422 char *buf = (
char *) NULL;
423 buf = ::readline(
"BESClient> ");
426 #ifdef HAVE_READLINE_HISTORY 429 if (len > SIZE_COMMUNICATION_BUFFER) {
430 cerr << __FILE__ << __LINE__ <<
431 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
457 void StandAloneClient::displayHelp()
461 cout <<
"BES Command Line Client Help" << endl;
463 cout <<
"Client commands available:" << endl;
464 cout <<
" exit - exit the command line interface" << endl;
465 cout <<
" help - display this help screen" << endl;
466 cout <<
" client suppress; - suppress output from the server" << endl;
467 cout <<
" client output to screen; - display server output to the screen" << endl;
468 cout <<
" client output to <file>; - display server output to specified file" << endl;
470 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
472 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'" 486 strm << BESIndent::LMarg <<
"StandAloneClient::dump - (" << (
void *)
this <<
")" << endl;
488 strm << BESIndent::LMarg <<
"stream: " << (
void *) _strm << endl;
489 strm << BESIndent::LMarg <<
"stream created? " << _strmCreated << endl;
490 BESIndent::UnIndent();
void executeCommands(const string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
void setOutput(ostream *strm, bool created)
Set the output stream for responses from the BES server.
virtual void dump(ostream &strm) const
dumps information about this object
virtual bool start(string name)
Abstract exception class for the BES with basic string message.
void interact()
An interactive BES client that takes BES requests on the command line.
Entry point into BES using xml document requests.
virtual int execute_request(const string &from)
The entry point for command execution; called by BESServerHandler::execute()
void executeClientCommand(const string &cmd)
Executes a client side command.