53 using std::ostringstream;
55 #ifdef HAVE_LIBREADLINE 56 # if defined(HAVE_READLINE_READLINE_H) 57 # include <readline/readline.h> 58 # elif defined(HAVE_READLINE_H) 59 # include <readline.h> 62 char *readline(
const char *);
70 #ifdef HAVE_READLINE_HISTORY 71 # if defined(HAVE_READLINE_HISTORY_H) 72 # include <readline/history.h> 73 # elif defined(HAVE_HISTORY_H) 77 int add_history(
const char *);
78 int write_history(
const char *);
79 int read_history(
const char *);
84 #define SIZE_COMMUNICATION_BUFFER 4096*4096 85 #include "StandAloneClient.h" 87 #include "BESXMLInterface.h" 88 #include "CmdTranslation.h" 90 StandAloneClient::~StandAloneClient()
92 if (_strmCreated && _strm) {
120 if (_strmCreated && _strm) {
128 _strmCreated = created;
144 string suppress =
"suppress";
145 if (cmd.compare(0, suppress.length(), suppress) == 0) {
150 string output =
"output to";
151 if (cmd.compare(0, output.length(), output) == 0) {
152 string subcmd = cmd.substr(output.length() + 1);
153 string screen =
"screen";
154 if (subcmd.compare(0, screen.length(), screen) == 0) {
159 string file = subcmd.substr(0, subcmd.length() - 1);
160 ofstream *fstrm =
new ofstream(file.c_str(), ios::app);
161 if (fstrm && !(*fstrm)) {
163 cerr <<
"Unable to set client output to file " << file << endl;
173 string load =
"load";
174 if (cmd.compare(0, load.length(), load) == 0) {
175 string file = cmd.substr(load.length() + 1, cmd.length() - load.length() - 2);
176 ifstream fstrm(file.c_str());
178 cerr <<
"Unable to load commands from file " << file <<
": file does not exist or failed to open file" 188 cerr <<
"Improper client command " << cmd << endl;
203 void StandAloneClient::executeCommand(
const string & cmd,
int repeat)
205 string client =
"client";
206 if (cmd.compare(0, client.length(), client) == 0) {
210 if (repeat < 1) repeat = 1;
211 for (
int i = 0; i < repeat; i++) {
212 ostringstream *show_stream = 0;
213 if (CmdTranslation::is_show()) {
214 show_stream =
new ostringstream;
216 BESDEBUG(
"standalone",
"cmdclient sending " << cmd << endl );
227 BESDEBUG(
"standalone",
"BESServerHandler::execute - " 228 <<
"executed successfully" << endl );
233 BESDEBUG(
"standalone",
"BESServerHandler::execute - " 234 "error occurred" << endl );
241 interface->finish_with_error( status );
245 case BES_INTERNAL_FATAL_ERROR:
247 cerr <<
"BES server " << getpid()
248 <<
": Status not OK, dispatcher returned value " 253 case BES_INTERNAL_ERROR:
254 case BES_SYNTAX_USER_ERROR:
255 case BES_FORBIDDEN_ERROR:
256 case BES_NOT_FOUND_ERROR:
266 *(_strm) << show_stream->str() << endl;
294 _isInteractive =
true;
295 if (repeat < 1) repeat = 1;
297 CmdTranslation::set_show(
false);
299 string doc = CmdTranslation::translate(cmd_list);
301 executeCommand(doc, repeat);
305 CmdTranslation::set_show(
false);
306 _isInteractive =
false;
309 CmdTranslation::set_show(
false);
310 _isInteractive =
false;
333 _isInteractive =
false;
334 if (repeat < 1) repeat = 1;
335 for (
int i = 0; i < repeat; i++) {
337 istrm.seekg(0, ios::beg);
340 while (getline(istrm, line)) {
343 this->executeCommand(cmd, 1);
364 _isInteractive =
true;
366 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' " 367 <<
"to display the help screen" << endl << endl;
372 size_t len = this->readLine(message);
373 if ( message ==
"exit" || message ==
"exit;") {
376 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
379 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
380 this->executeCommand(message, 1);
382 else if (len != 0 && message !=
"") {
383 CmdTranslation::set_show(
false);
385 string doc = CmdTranslation::translate(message);
387 this->executeCommand(doc, 1);
391 CmdTranslation::set_show(
false);
392 _isInteractive =
false;
395 CmdTranslation::set_show(
false);
398 _isInteractive =
false;
406 size_t StandAloneClient::readLine(
string & msg)
409 char *buf = (
char *) NULL;
410 buf = ::readline(
"BESClient> ");
413 #ifdef HAVE_READLINE_HISTORY 416 if (len > SIZE_COMMUNICATION_BUFFER) {
417 cerr << __FILE__ << __LINE__ <<
418 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
444 void StandAloneClient::displayHelp()
448 cout <<
"BES Command Line Client Help" << endl;
450 cout <<
"Client commands available:" << endl;
451 cout <<
" exit - exit the command line interface" << endl;
452 cout <<
" help - display this help screen" << endl;
453 cout <<
" client suppress; - suppress output from the server" << endl;
454 cout <<
" client output to screen; - display server output to the screen" << endl;
455 cout <<
" client output to <file>; - display server output to specified file" << endl;
457 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
459 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'" 473 strm << BESIndent::LMarg <<
"StandAloneClient::dump - (" << (
void *)
this <<
")" << endl;
475 strm << BESIndent::LMarg <<
"stream: " << (
void *) _strm << endl;
476 strm << BESIndent::LMarg <<
"stream created? " << _strmCreated << endl;
477 BESIndent::UnIndent();
void executeCommands(const string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
virtual int execute_request(const string &from)
Override execute_request in order to register memory pool.
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
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.