45 using std::ostringstream;
49 #ifdef HAVE_LIBREADLINE
50 # if defined(HAVE_READLINE_READLINE_H)
51 # include <readline/readline.h>
52 # elif defined(HAVE_READLINE_H)
53 # include <readline.h>
57 char *readline(
const char * );
65 #ifdef HAVE_READLINE_HISTORY
66 # if defined(HAVE_READLINE_HISTORY_H)
67 # include <readline/history.h>
68 # elif defined(HAVE_HISTORY_H)
73 int add_history(
const char * );
74 int write_history(
const char * );
75 int read_history(
const char * );
81 #include <libxml/encoding.h>
83 #define SIZE_COMMUNICATION_BUFFER 4096*4096
93 if (_strmCreated && _strm) {
123 _client =
new PPTClient(host, portVal, timeout);
138 _client =
new PPTClient(unixStr, timeout);
173 if (_strmCreated && _strm) {
181 _strmCreated = created;
197 bool do_exit =
false;
198 string suppress =
"suppress";
199 if (cmd.compare(0, suppress.length(), suppress) == 0) {
204 string output =
"output to";
205 if (cmd.compare(0, output.length(), output) == 0) {
206 string subcmd = cmd.substr(output.length() + 1);
207 string screen =
"screen";
208 if (subcmd.compare(0, screen.length(), screen) == 0) {
213 string file = subcmd.substr(0, subcmd.length() - 1);
214 ofstream *fstrm =
new ofstream(file.c_str(), ios::app);
215 if (fstrm && !(*fstrm)) {
217 cerr <<
"Unable to set client output to file " << file << endl;
227 string load =
"load";
228 if (cmd.compare(0, load.length(), load) == 0) {
229 string file = cmd.substr(load.length() + 1, cmd.length() - load.length() - 2);
230 ifstream fstrm(file.c_str());
232 cerr <<
"Unable to load commands from file " << file <<
": file does not exist or failed to open file"
242 cerr <<
"Improper client command " << cmd << endl;
259 bool CmdClient::executeCommand(
const string &cmd,
int repeat)
261 bool do_exit =
false;
262 const string client =
"client";
263 if (cmd.compare(0, client.length(), client) == 0) {
267 if (repeat < 1) repeat = 1;
268 for (
int i = 0; i < repeat && !do_exit; i++) {
269 BESDEBUG(
"cmdln",
"cmdclient sending " << cmd << endl );
277 map<string,string> extensions;
278 _client->
send( cmd, extensions );
280 BESDEBUG(
"cmdln",
"cmdclient receiving " << endl );
283 ostringstream *show_stream = 0;
290 show_stream =
new ostringstream;
295 done = _client->
receive( extensions, show_stream );
299 done = _client->
receive( extensions, _strm );
301 if( extensions[
"status"] ==
"error" )
314 if( extensions[
"exit"] ==
"true" )
321 *(_strm) << show_stream->str() << endl;
327 BESDEBUG(
"cmdln",
"extensions:" << endl );
328 map<string,string>::const_iterator i = extensions.begin();
329 map<string,string>::const_iterator e = extensions.end();
332 BESDEBUG(
"cmdln",
" " << (*i).first <<
" = "
333 << (*i).second << endl );
335 BESDEBUG(
"cmdln",
"cmdclient done receiving " << endl );
339 if( sw && sw->
stop() )
341 BESDEBUG(
"timing",
"cmdclient - executed \""
342 << cmd <<
"\" in " << sw->
seconds()
344 <<
" microseconds" << endl );
348 BESDEBUG(
"timing",
"cmdclient - executed \"" << cmd
349 <<
"\" - no timing available"
381 bool do_exit =
false;
382 _isInteractive =
true;
383 if (repeat < 1) repeat = 1;
389 do_exit = this->executeCommand(doc, repeat);
394 _isInteractive =
false;
398 _isInteractive =
false;
422 bool do_exit =
false;
423 _isInteractive =
false;
424 if (repeat < 1) repeat = 1;
425 for (
int i = 0; i < repeat; i++) {
427 istrm.seekg(0, ios::beg);
429 while (!istrm.eof()) {
432 istrm.getline(line, 4096,
'\n');
435 do_exit = this->executeCommand(cmd, 1);
461 bool do_exit =
false;
462 _isInteractive =
true;
464 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' "
465 <<
"to display the help screen" << endl << endl;
468 while (!done && !do_exit) {
470 size_t len = this->readLine(message);
472 if ( message ==
"exit" || message ==
"exit;") {
475 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
478 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
479 do_exit = this->executeCommand(message, 1);
481 else if (len != 0 && message !=
"") {
486 do_exit = this->executeCommand(doc, 1);
491 _isInteractive =
false;
497 _isInteractive =
false;
509 size_t CmdClient::readLine(
string &msg)
512 char *buf = (
char *) NULL;
513 buf = ::readline(
"BESClient> ");
516 #ifdef HAVE_READLINE_HISTORY
520 cerr << __FILE__ << __LINE__ <<
521 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
548 void CmdClient::displayHelp()
552 cout <<
"BES Command Line Client Help" << endl;
554 cout <<
"Client commands available:" << endl;
555 cout <<
" exit - exit the command line interface" << endl;
556 cout <<
" help - display this help screen" << endl;
557 cout <<
" client suppress; - suppress output from the server" << endl;
558 cout <<
" client output to screen; - display server output to the screen" << endl;
559 cout <<
" client output to <file>; - display server output to specified file" << endl;
560 cout <<
" client load <file>; - load xml document from file" << endl;
562 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
564 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'"
595 strm <<
BESIndent::LMarg <<
"CmdClient::dump - (" << (
void *)
this <<
")" << endl;
void shutdownClient()
Closes the connection to the OpeNDAP server and closes the output stream.
#define BESISDEBUG(x)
macro used to determine if the specified debug context is set
virtual bool receive(map< string, string > &extensions, ostream *strm=0)
receive a chunk of either extensions into the specified map or data into the specified stream ...
bool executeClientCommand(const string &cmd)
Executes a client side command.
virtual int microseconds()
virtual void closeConnection()
virtual bool isConnected()
void brokenPipe()
inform the server that there has been a borken pipe
static void set_show(bool val)
static string translate(const string &commands)
virtual void initConnection()
Abstract exception class for the BES with basic string message.
virtual void brokenPipe()
static ostream & LMarg(ostream &strm)
bool isConnected()
return whether the client is connected to the BES
static bool IsSet(const string &flagName)
see if the debug context flagName is set to true
virtual void dump(ostream &strm) const
dumps information about this object
void startClient(const string &host, int portVal, int timeout)
Connect the BES client to the BES server.
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
bool executeCommands(const string &cmd, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
bool interact()
An interactive BES client that takes BES requests on the command line.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
virtual void send(const string &buffer)
sends the buffer to the socket
#define SIZE_COMMUNICATION_BUFFER