54 using std::ostringstream;
59 #ifdef HAVE_LIBREADLINE
60 # if defined(HAVE_READLINE_READLINE_H)
61 # include <readline/readline.h>
62 # elif defined(HAVE_READLINE_H)
63 # include <readline.h>
66 char *readline(
const char *);
74 #ifdef HAVE_READLINE_HISTORY
75 # if defined(HAVE_READLINE_HISTORY_H)
76 # include <readline/history.h>
77 # elif defined(HAVE_HISTORY_H)
81 int add_history(
const char *);
82 int write_history(
const char *);
83 int read_history(
const char *);
90 #define SIZE_COMMUNICATION_BUFFER 4096*4096
92 #include "BESXMLInterface.h"
93 #include "BESStopWatch.h"
97 #include "StandAloneClient.h"
98 #include "CmdTranslation.h"
100 StandAloneClient::~StandAloneClient()
102 if (_strmCreated && _strm) {
130 if (_strmCreated && _strm) {
138 _strmCreated = created;
154 string suppress =
"suppress";
155 if (cmd.compare(0, suppress.length(), suppress) == 0) {
160 string output =
"output to";
161 if (cmd.compare(0, output.length(), output) == 0) {
162 string subcmd = cmd.substr(output.length() + 1);
163 string screen =
"screen";
164 if (subcmd.compare(0, screen.length(), screen) == 0) {
169 string file = subcmd.substr(0, subcmd.length() - 1);
170 ofstream *fstrm =
new ofstream(file.c_str(), ios::app);
171 if (fstrm && !(*fstrm)) {
173 cerr <<
"Unable to set client output to file " << file << endl;
183 string load =
"load";
184 if (cmd.compare(0, load.length(), load) == 0) {
185 string file = cmd.substr(load.length() + 1, cmd.length() - load.length() - 2);
186 ifstream fstrm(file.c_str());
188 cerr <<
"Unable to load commands from file " << file <<
": file does not exist or failed to open file"
198 cerr <<
"Improper client command " << cmd << endl;
213 void StandAloneClient::executeCommand(
const string & cmd,
int repeat)
215 string client =
"client";
216 if (cmd.compare(0, client.length(), client) == 0) {
220 if (repeat < 1) repeat = 1;
221 for (
int i = 0; i < repeat; i++) {
222 ostringstream *show_stream = 0;
223 if (CmdTranslation::is_show()) {
224 show_stream =
new ostringstream;
227 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand sending: " << cmd << endl );
230 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"StandAloneClient::executeCommand");
247 status = interface->finish(status);
250 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - executed successfully" << endl);
254 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - error occurred" << endl);
256 case BES_INTERNAL_FATAL_ERROR: {
257 cerr <<
"Status not OK, dispatcher returned value " << status << endl;
261 case BES_INTERNAL_ERROR:
262 case BES_SYNTAX_USER_ERROR:
263 case BES_FORBIDDEN_ERROR:
264 case BES_NOT_FOUND_ERROR:
275 *(_strm) << show_stream->str() << endl;
303 _isInteractive =
true;
304 if (repeat < 1) repeat = 1;
306 CmdTranslation::set_show(
false);
308 string doc = CmdTranslation::translate(cmd_list);
310 executeCommand(doc, repeat);
314 CmdTranslation::set_show(
false);
315 _isInteractive =
false;
318 CmdTranslation::set_show(
false);
319 _isInteractive =
false;
342 _isInteractive =
false;
343 if (repeat < 1) repeat = 1;
344 for (
int i = 0; i < repeat; i++) {
346 istrm.seekg(0, ios::beg);
349 while (getline(istrm, line)) {
352 this->executeCommand(cmd, 1);
373 _isInteractive =
true;
375 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' "
376 <<
"to display the help screen" << endl << endl;
381 size_t len = this->readLine(message);
382 if ( message ==
"exit" || message ==
"exit;") {
385 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
388 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
389 this->executeCommand(message, 1);
391 else if (len != 0 && message !=
"") {
392 CmdTranslation::set_show(
false);
394 string doc = CmdTranslation::translate(message);
396 this->executeCommand(doc, 1);
400 CmdTranslation::set_show(
false);
401 _isInteractive =
false;
404 CmdTranslation::set_show(
false);
407 _isInteractive =
false;
415 size_t StandAloneClient::readLine(
string & msg)
418 char *buf = (
char *) NULL;
419 buf = ::readline(
"BESClient> ");
422 #ifdef HAVE_READLINE_HISTORY
425 if (len > SIZE_COMMUNICATION_BUFFER) {
426 cerr << __FILE__ << __LINE__ <<
427 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
453 void StandAloneClient::displayHelp()
457 cout <<
"BES Command Line Client Help" << endl;
459 cout <<
"Client commands available:" << endl;
460 cout <<
" exit - exit the command line interface" << endl;
461 cout <<
" help - display this help screen" << endl;
462 cout <<
" client suppress; - suppress output from the server" << endl;
463 cout <<
" client output to screen; - display server output to the screen" << endl;
464 cout <<
" client output to <file>; - display server output to specified file" << endl;
466 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
468 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'"
482 strm << BESIndent::LMarg <<
"StandAloneClient::dump - (" << (
void *)
this <<
")" << endl;
484 strm << BESIndent::LMarg <<
"stream: " << (
void *) _strm << endl;
485 strm << BESIndent::LMarg <<
"stream created? " << _strmCreated << endl;
486 BESIndent::UnIndent();
Abstract exception class for the BES with basic string message.
virtual int execute_request(const std::string &from)
The entry point for command execution; called by BESServerHandler::execute()
virtual bool start(std::string name)
Entry point into BES using xml document requests.
void executeCommands(const std::string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
void interact()
An interactive BES client that takes BES requests on the command line.
void executeClientCommand(const std::string &cmd)
Executes a client side command.
virtual void dump(std::ostream &strm) const
dumps information about this object
void setOutput(std::ostream *strm, bool created)
Set the output stream for responses from the BES server.