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 * ) ;
60 char *cmdline = NULL ;
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 )
127 _client =
new PPTClient( host, portVal, timeout ) ;
143 _client =
new PPTClient( unixStr, timeout ) ;
181 if( _strmCreated && _strm )
191 _strmCreated = created ;
208 bool do_exit = false ;
209 string suppress =
"suppress" ;
210 if( cmd.compare( 0, suppress.length(), suppress ) == 0 )
216 string output =
"output to" ;
217 if( cmd.compare( 0, output.length(), output ) == 0 )
219 string subcmd = cmd.substr( output.length() + 1 ) ;
220 string screen =
"screen" ;
221 if( subcmd.compare( 0, screen.length(), screen ) == 0 )
228 string file = subcmd.substr( 0, subcmd.length() - 1 ) ;
229 ofstream *fstrm =
new ofstream( file.c_str(), ios::app ) ;
230 if( fstrm && !(*fstrm) )
233 cerr <<
"Unable to set client output to file " << file
245 string load =
"load" ;
246 if( cmd.compare( 0, load.length(), load ) == 0 )
248 string file = cmd.substr( load.length() + 1,
249 cmd.length() - load.length() - 2 ) ;
250 ifstream fstrm( file.c_str() ) ;
253 cerr <<
"Unable to load commands from file " << file
254 <<
": file does not exist or failed to open file" << endl ;
264 cerr <<
"Improper client command " << cmd << endl ;
282 CmdClient::executeCommand(
const string &cmd,
int repeat )
284 bool do_exit = false ;
285 string client =
"client" ;
286 if( cmd.compare( 0, client.length(), client ) == 0 )
292 if( repeat < 1 ) repeat = 1 ;
293 for(
int i = 0; i < repeat && !do_exit; i++ )
295 BESDEBUG(
"cmdln",
"cmdclient sending " << cmd << endl ) ;
303 map<string,string> extensions ;
304 _client->
send( cmd, extensions ) ;
306 BESDEBUG(
"cmdln",
"cmdclient receiving " << endl ) ;
309 ostringstream *show_stream = 0 ;
316 show_stream =
new ostringstream ;
321 done = _client->
receive( extensions, show_stream ) ;
325 done = _client->
receive( extensions, _strm ) ;
327 if( extensions[
"status"] ==
"error" )
340 if( extensions[
"exit"] ==
"true" )
347 *(_strm) << show_stream->str() << endl ;
353 BESDEBUG(
"cmdln",
"extensions:" << endl ) ;
354 map<string,string>::const_iterator i = extensions.begin() ;
355 map<string,string>::const_iterator e = extensions.end() ;
358 BESDEBUG(
"cmdln",
" " << (*i).first <<
" = " 359 << (*i).second << endl ) ;
361 BESDEBUG(
"cmdln",
"cmdclient done receiving " << endl ) ;
365 if( sw && sw->
stop() )
367 BESDEBUG(
"timing",
"cmdclient - executed \"" 368 << cmd <<
"\" in " << sw->
seconds()
370 <<
" microseconds" << endl ) ;
374 BESDEBUG(
"timing",
"cmdclient - executed \"" << cmd
375 <<
"\" - no timing available" 408 bool do_exit = false ;
409 _isInteractive = true ;
410 if( repeat < 1 ) repeat = 1 ;
418 do_exit = this->executeCommand( doc, repeat ) ;
424 _isInteractive = false ;
428 _isInteractive = false ;
453 bool do_exit = false ;
454 _isInteractive = false ;
455 if( repeat < 1 ) repeat = 1 ;
456 for(
int i = 0; i < repeat; i++ )
459 istrm.seekg( 0, ios::beg ) ;
461 while( !istrm.eof() )
465 istrm.getline( line, 4096,
'\n' ) ;
468 do_exit = this->executeCommand( cmd, 1 ) ;
495 bool do_exit = false ;
496 _isInteractive = true ;
499 <<
"Type 'exit' to exit the command line client and 'help' or '?' " 500 <<
"to display the help screen" << endl << endl ;
503 while( !done && !do_exit )
505 string message =
"" ;
506 size_t len = this->readLine( message ) ;
507 if( len == -1 || message ==
"exit" || message ==
"exit;" )
511 else if( message ==
"help" || message ==
"help;" || message ==
"?" )
513 this->displayHelp() ;
515 else if( message.length() > 6 && message.substr( 0, 6 ) ==
"client" )
517 do_exit = this->executeCommand( message, 1 ) ;
519 else if( len != 0 && message !=
"" )
527 do_exit = this->executeCommand( doc, 1 ) ;
533 _isInteractive = false ;
539 _isInteractive = false ;
550 CmdClient::readLine(
string &msg )
553 char *buf = (
char *) NULL ;
554 buf =::readline(
"BESClient> " ) ;
557 len = strlen( buf ) ;
558 #ifdef HAVE_READLINE_HISTORY 563 cerr << __FILE__ << __LINE__
565 ": incoming data buffer exceeds maximum capacity with lenght " 595 CmdClient::displayHelp()
599 cout <<
"BES Command Line Client Help" << endl ;
601 cout <<
"Client commands available:" << endl ;
603 " exit - exit the command line interface" <<
605 cout <<
" help - display this help screen" <<
608 " client suppress; - suppress output from the server" <<
611 " client output to screen; - display server output to the screen" 614 " client output to <file>; - display server output to specified file" 617 " client load <file>; - load xml document from file" 621 "Any commands beginning with 'client' must end with a semicolon" <<
624 cout <<
"To display the list of commands available from the server " 625 <<
"please type the command 'show help;'" << endl ;
661 << (
void *)
this <<
")" << endl ;
667 _client->
dump( strm ) ;
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
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.
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