48 #include "StandAloneApp.h" 49 #include "StandAloneClient.h" 52 #include "BESDefaultModule.h" 53 #include "BESXMLDefaultCommands.h" 54 #include "TheBESKeys.h" 55 #include "BESCatalogUtils.h" 56 #include "CmdTranslation.h" 58 StandAloneApp::StandAloneApp() :
59 BESModuleApp(), _client(0), _outputStrm(0), _inputStrm(0), _createdInputStrm( false), _repeat(0)
63 StandAloneApp::~StandAloneApp()
72 BESCatalogUtils::delete_all_catalogs();
75 void StandAloneApp::showVersion()
77 cout <<
appName() <<
": version 2.0" << endl;
80 void StandAloneApp::showUsage()
83 cout <<
appName() <<
": the following options are available:" << endl;
84 cout <<
" -c <file>, --config=<file> - BES configuration file" << endl;
85 cout <<
" -x <command>, --execute=<command> - command for the server to execute" << endl;
86 cout <<
" -i <file>, --inputfile=<file> - file with a sequence of input commands" << endl;
87 cout <<
" -f <file>, --outputfile=<file> - write output to this file" << endl;
88 cout <<
" -d, --debug - turn on debugging for the client session" << endl;
89 cout <<
" -r <num>, --repeat=<num> - repeat the command(s) <num> times" << endl;
90 cout <<
" -v, --version - return version information" << endl;
91 cout <<
" -?, --help - display help information" << endl;
98 CmdTranslation::initialize(argc, argv);
100 string outputStr =
"";
101 string inputStr =
"";
102 string repeatStr =
"";
104 bool badUsage =
false;
108 static struct option longopts[] = { {
"config", 1, 0,
'c' }, {
"debug", 0, 0,
'd' }, {
"version", 0, 0,
'v' }, {
109 "execute", 1, 0,
'x' }, {
"outputfile", 1, 0,
'f' }, {
"inputfile", 1, 0,
'i' }, {
"repeat", 1, 0,
'r' }, {
110 "help", 0, 0,
'?' }, { 0, 0, 0, 0 } };
111 int option_index = 0;
113 while ((c = getopt_long(argc, argv,
"?vc:d:x:f:i:r:", longopts, &option_index)) != -1) {
146 if (outputStr !=
"") {
147 if (_cmd ==
"" && inputStr ==
"") {
148 cerr <<
"When specifying an output file you must either " <<
"specify a command or an input file" << endl;
151 else if (_cmd !=
"" && inputStr !=
"") {
152 cerr <<
"You must specify either a command or an input file on " <<
"the command line, not both" << endl;
157 if (badUsage ==
true) {
162 if (outputStr !=
"") {
163 _outputStrm =
new ofstream(outputStr.c_str());
164 if (!(*_outputStrm)) {
165 cerr <<
"could not open the output file " << outputStr << endl;
170 if (inputStr !=
"") {
171 _inputStrm =
new ifstream(inputStr.c_str());
172 if (!(*_inputStrm)) {
173 cerr <<
"could not open the input file " << inputStr << endl;
176 _createdInputStrm =
true;
179 if (!repeatStr.empty()) {
180 _repeat = atoi(repeatStr.c_str());
181 if (!_repeat && repeatStr !=
"0") {
182 cerr <<
"repeat number invalid: " << repeatStr << endl;
190 if (badUsage ==
true) {
196 BESDEBUG(
"standalone",
"ServerApp: initializing default module ... " << endl);
197 BESDefaultModule::initialize(argc, argv);
198 BESDEBUG(
"standalone",
"ServerApp: done initializing default module" << endl);
200 BESDEBUG(
"standalone",
"ServerApp: initializing default commands ... " << endl);
202 BESDEBUG(
"standalone",
"ServerApp: done initializing default commands" << endl);
204 BESDEBUG(
"standalone",
"ServerApp: initializing loaded modules ... " << endl);
206 BESDEBUG(
"standalone",
"ServerApp: done initializing loaded modules" << endl);
207 if (retval)
return retval;
210 cerr <<
"Failed to initialize stand alone app" << endl;
215 BESDEBUG(
"standalone",
"StandAloneApp: initialized settings:" << endl << *
this);
230 BESDEBUG(
"standalone",
"OK" << endl);
237 BESDEBUG(
"standalone",
"FAILED" << endl);
238 cerr <<
"error starting the client" << endl;
247 else if (_inputStrm) {
255 cerr <<
"error processing commands" << endl;
260 BESDEBUG(
"standalone",
"StandAloneApp: shutting down client ... " << endl);
265 BESDEBUG(
"standalone",
"OK" << endl);
267 BESDEBUG(
"standalone",
"StandAloneApp: closing input stream ... " << endl);
268 if (_createdInputStrm && _inputStrm) {
273 BESDEBUG(
"standalone",
"OK" << endl);
276 BESDEBUG(
"standalone",
"FAILED" << endl);
277 cerr <<
"error closing the client" << endl;
292 BESDEBUG(
"standalone",
"ServerApp: terminating loaded modules ... " << endl);
294 BESDEBUG(
"standalone",
"ServerApp: done terminating loaded modules" << endl);
296 BESDEBUG(
"standalone",
"ServerApp: terminating default commands ... " << endl);
298 BESDEBUG(
"standalone",
"ServerApp: done terminating default commands" << endl);
300 BESDEBUG(
"standalone",
"ServerApp: terminating default module ... " << endl);
301 BESDefaultModule::terminate();
302 BESDEBUG(
"standalone",
"ServerApp: done terminating default module" << endl);
304 CmdTranslation::terminate();
319 strm << BESIndent::LMarg <<
"StandAloneApp::dump - (" << (
void *)
this <<
")" << endl;
322 strm << BESIndent::LMarg <<
"client: " << endl;
325 BESIndent::UnIndent();
328 strm << BESIndent::LMarg <<
"client: null" << endl;
330 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
331 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
332 strm << BESIndent::LMarg <<
"input stream: " << (
void *) _inputStrm << endl;
333 strm << BESIndent::LMarg <<
"created input stream? " << _createdInputStrm << endl;
335 BESIndent::UnIndent();
338 int main(
int argc,
char **argv)
342 return app.
main(argc, argv);
345 cerr <<
"Caught BES Error while starting the command processor: " << e.
get_message() << endl;
348 catch (std::exception &e) {
349 cerr <<
"Caught C++ error while starting the command processor: " << e.what() << endl;
353 cerr <<
"Caught unknown error while starting the command processor." << endl;
void executeCommands(const string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
virtual int run()
The body of the application, implementing the primary functionality of the BES application.
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 std::string get_message()
get the error message for this exception
virtual void dump(std::ostream &strm) const =0
dumps information about this object
static void SetUp(const std::string &values)
Sets up debugging for the bes.
virtual int terminate(int sig=0)
clean up after the application
static int terminate(void)
Removes the default set of BES XML commands from the list of possible commands.
virtual void dump(ostream &strm) const
dumps information about this object
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
static int initialize(int argc, char **argv)
Loads the default set of BES XML commands.
Abstract exception class for the BES with basic string message.
static TheBESKeys * TheKeys()
void interact()
An interactive BES client that takes BES requests on the command line.
virtual int main(int argC, char **argV)
main routine, the main entry point for any BES applications.
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Base application object for all BES applications.
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
string appName(void) const
Returns the name of the application.
virtual int terminate(int sig=0)
clean up after the application