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",
"StandAloneClient::executeCommand sending: " << cmd << endl );
227 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"StandAloneClient::executeCommand");
244 status = interface->finish(status);
247 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - executed successfully" << endl);
251 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - error occurred" << endl);
253 case BES_INTERNAL_FATAL_ERROR: {
254 cerr <<
"Status not OK, dispatcher returned value " << status << endl;
258 case BES_INTERNAL_ERROR:
259 case BES_SYNTAX_USER_ERROR:
260 case BES_FORBIDDEN_ERROR:
261 case BES_NOT_FOUND_ERROR:
272 *(_strm) << show_stream->str() << endl;
300 _isInteractive =
true;
301 if (repeat < 1) repeat = 1;
303 CmdTranslation::set_show(
false);
305 string doc = CmdTranslation::translate(cmd_list);
307 executeCommand(doc, repeat);
311 CmdTranslation::set_show(
false);
312 _isInteractive =
false;
315 CmdTranslation::set_show(
false);
316 _isInteractive =
false;
339 _isInteractive =
false;
340 if (repeat < 1) repeat = 1;
341 for (
int i = 0; i < repeat; i++) {
343 istrm.seekg(0, ios::beg);
346 while (getline(istrm, line)) {
349 this->executeCommand(cmd, 1);
370 _isInteractive =
true;
372 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' " 373 <<
"to display the help screen" << endl << endl;
378 size_t len = this->readLine(message);
379 if ( message ==
"exit" || message ==
"exit;") {
382 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
385 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
386 this->executeCommand(message, 1);
388 else if (len != 0 && message !=
"") {
389 CmdTranslation::set_show(
false);
391 string doc = CmdTranslation::translate(message);
393 this->executeCommand(doc, 1);
397 CmdTranslation::set_show(
false);
398 _isInteractive =
false;
401 CmdTranslation::set_show(
false);
404 _isInteractive =
false;
412 size_t StandAloneClient::readLine(
string & msg)
415 char *buf = (
char *) NULL;
416 buf = ::readline(
"BESClient> ");
419 #ifdef HAVE_READLINE_HISTORY 422 if (len > SIZE_COMMUNICATION_BUFFER) {
423 cerr << __FILE__ << __LINE__ <<
424 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
450 void StandAloneClient::displayHelp()
454 cout <<
"BES Command Line Client Help" << endl;
456 cout <<
"Client commands available:" << endl;
457 cout <<
" exit - exit the command line interface" << endl;
458 cout <<
" help - display this help screen" << endl;
459 cout <<
" client suppress; - suppress output from the server" << endl;
460 cout <<
" client output to screen; - display server output to the screen" << endl;
461 cout <<
" client output to <file>; - display server output to specified file" << endl;
463 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
465 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'" 479 strm << BESIndent::LMarg <<
"StandAloneClient::dump - (" << (
void *)
this <<
")" << endl;
481 strm << BESIndent::LMarg <<
"stream: " << (
void *) _strm << endl;
482 strm << BESIndent::LMarg <<
"stream created? " << _strmCreated << endl;
483 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.
void executeClientCommand(const string &cmd)
Executes a client side command.
virtual int execute_request(const std::string &from)
The entry point for command execution; called by BESServerHandler::execute()