59 #define BES_CMDLN_DEFAULT_TIMEOUT 5
62 #define DEFAULT_PORT 10022
63 #define DEFAULT_HOST "localhost"
67 0), _createdInputStrm(false), _timeout(0), _repeat(0)
79 void CmdApp::showVersion()
81 cout <<
appName() <<
": version 2.0" << endl;
84 void CmdApp::showUsage()
87 cout <<
appName() <<
": the following flags are available:" << endl;
88 cout <<
" -h <host> - specifies a host for TCP/IP connection" << endl;
89 cout <<
" -p <port> - specifies a port for TCP/IP connection" << endl;
90 cout <<
" -u <unixSocket> - specifies a unix socket for connection. " << endl;
91 cout <<
" -x <command> - specifies a command for the server to execute" << endl;
92 cout <<
" -i <inputFile> - specifies a file name for a sequence of input commands" << endl;
93 cout <<
" -f <outputFile> - specifies a file name to output the results of the input" << endl;
94 cout <<
" -t <timeoutVal> - specifies an optional timeout value in seconds" << endl;
95 cout <<
" -d - sets the optional debug flag for the client session" << endl;
96 cout <<
" -r <num> - repeat the command(s) num times" << endl;
97 cout <<
" -? - display this list of flags" << endl;
104 if (sig == SIGCONT) {
110 <<
"busy with another incoming connection. exiting!\n";
129 if (sig == SIGTERM) {
139 if (sig == SIGPIPE) {
141 << endl <<
"Please check parameters and try again" << endl;
156 void CmdApp::registerSignals()
159 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGCONT ... " << endl );
162 BESDEBUG(
"cmdln",
"FAILED" << endl );
163 cerr <<
appName() <<
"Failed to register signal SIGCONT" << endl;
170 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGINT ... " << endl );
173 BESDEBUG(
"cmdln",
"FAILED" << endl );
174 cerr <<
appName() <<
"Failed to register signal SIGINT" << endl;
181 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGTERM ... " << endl );
184 BESDEBUG(
"cmdln",
"FAILED" << endl );
185 cerr <<
appName() <<
"Failed to register signal SIGTERM" << endl;
191 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGPIPE ... " << endl );
194 BESDEBUG(
"cmdln",
"FAILED" << endl );
195 cerr <<
appName() <<
"Failed to register signal SIGPIPE" << endl;
204 if (retVal != 0)
return retVal;
210 string outputStr =
"";
211 string inputStr =
"";
212 string timeoutStr =
"";
213 string repeatStr =
"";
215 bool badUsage =
false;
219 while ((c = getopt(argc, argv,
"?vd:h:p:t:u:x:f:i:r:")) != EOF) {
260 if (_hostStr ==
"" && _unixStr ==
"") {
261 cerr <<
"host/port or unix socket must be specified" << endl;
265 if (_hostStr !=
"" && _unixStr !=
"") {
266 cerr <<
"must specify either a host and port or a unix socket" << endl;
270 if (portStr !=
"" && _unixStr !=
"") {
271 cerr <<
"must specify either a host and port or a unix socket" << endl;
275 if (_hostStr !=
"") {
276 if (!_portVal && portStr ==
"") {
277 cout <<
"port must be specified when specifying a host" << endl;
280 else if (!_portVal) {
281 _portVal = atoi(portStr.c_str());
285 if (timeoutStr !=
"") {
286 _timeout = atoi(timeoutStr.c_str());
292 if (outputStr !=
"") {
293 if (_cmd ==
"" && inputStr ==
"") {
294 cerr <<
"When specifying an output file you must either " <<
"specify a command or an input file" << endl;
297 else if (_cmd !=
"" && inputStr !=
"") {
298 cerr <<
"You must specify either a command or an input file on " <<
"the command line, not both" << endl;
303 if (badUsage ==
true) {
308 if (outputStr !=
"") {
309 _outputStrm =
new ofstream(outputStr.c_str());
310 if (!(*_outputStrm)) {
311 cerr <<
"could not open the output file " << outputStr << endl;
316 if (inputStr !=
"") {
317 _inputStrm =
new ifstream(inputStr.c_str());
318 if (!(*_inputStrm)) {
319 cerr <<
"could not open the input file " << inputStr << endl;
322 _createdInputStrm =
true;
325 if (!repeatStr.empty()) {
326 _repeat = atoi(repeatStr.c_str());
327 if (!_repeat && repeatStr !=
"0") {
328 cerr <<
"repeat number invalid: " << repeatStr << endl;
336 if (badUsage ==
true) {
343 BESDEBUG(
"cmdln",
"CmdApp: initialized settings:" << endl << *
this );
352 if (_hostStr !=
"") {
353 BESDEBUG(
"cmdln",
"CmdApp: Connecting to host: " << _hostStr
354 <<
" at port: " << _portVal <<
" ... " << endl );
355 _client->
startClient( _hostStr, _portVal, _timeout );
359 BESDEBUG(
"cmdln",
"CmdApp: Connecting to unix socket: "
360 << _unixStr <<
" ... " << endl );
380 BESDEBUG(
"cmdln",
"FAILED" << endl );
381 cerr <<
"error starting the client" << endl;
386 bool do_exit =
false;
393 else if( _inputStrm )
404 cerr <<
"error processing commands" << endl;
410 BESDEBUG(
"cmdln",
"CmdApp: shutting down client ... " << endl );
421 BESDEBUG(
"cmdln",
"CmdApp: closing input stream ... " << endl );
422 if( _createdInputStrm )
432 BESDEBUG(
"cmdln",
"FAILED" << endl );
433 cerr <<
"error closing the client" << endl;
449 strm <<
BESIndent::LMarg <<
"CmdApp::dump - (" << (
void *)
this <<
")" << endl;
464 strm <<
BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
465 strm <<
BESIndent::LMarg <<
"input stream: " << (
void *) _inputStrm << endl;
466 strm <<
BESIndent::LMarg <<
"created input stream? " << _createdInputStrm << endl;
472 int main(
int argc,
char **argv)
475 return app.
main(argc, argv);
virtual int initialize(int argC, char **argV)
initialize the BES application
void shutdownClient()
Closes the connection to the OpeNDAP server and closes the output stream.
virtual int initialize(int argC, char **argV)
initialize the BES application
static void SetUp(const string &values)
Sets up debugging for the bes.
static void signalCannotConnect(int sig)
Base application object for all BES applications.
virtual int main(int argC, char **argV)
main method of the BES application
virtual void dump(ostream &strm) const
dumps information about this object
void brokenPipe()
inform the server that there has been a borken pipe
string appName(void) const
Returns the name of the application.
virtual string get_message()
get the error message for this exception
static void signalBrokenPipe(int sig)
#define BES_CMDLN_DEFAULT_TIMEOUT
Abstract exception class for the BES with basic string message.
static void Help(ostream &strm)
Writes help information for so that developers know what can be set for debugging.
static void signalTerminate(int sig)
static ostream & LMarg(ostream &strm)
bool isConnected()
return whether the client is connected to the BES
virtual int run()
the applications functionality is implemented in the run method
virtual void dump(ostream &strm) const
dumps information about this object
static void signalInterrupt(int sig)
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.
int main(int argc, char **argv)
CmdClient is an object that handles the connection to, sending requests to, and receiving response fr...
void setOutput(ostream *strm, bool created)
Set the output stream for responses from the BES server.
static int initialize(int argc, char **argv)
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
static BESApp * TheApplication(void)
Returns the BESApp application object for this application.