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()
73 BESCatalogUtils::delete_all_catalogs();
78 void StandAloneApp::showVersion()
80 cout << appName() <<
": version 2.0" << endl;
83 void StandAloneApp::showUsage()
86 cout << appName() <<
": the following options are available:" << endl;
87 cout <<
" -c <file>, --config=<file> - BES configuration file" << endl;
88 cout <<
" -x <command>, --execute=<command> - command for the server to execute" << endl;
89 cout <<
" -i <file>, --inputfile=<file> - file with a sequence of input commands" << endl;
90 cout <<
" -f <file>, --outputfile=<file> - write output to this file" << endl;
91 cout <<
" -d, --debug - turn on debugging for the client session" << endl;
92 cout <<
" -r <num>, --repeat=<num> - repeat the command(s) <num> times" << endl;
93 cout <<
" -v, --version - return version information" << endl;
94 cout <<
" -?, --help - display help information" << endl;
101 CmdTranslation::initialize(argc, argv);
103 string outputStr =
"";
104 string inputStr =
"";
105 string repeatStr =
"";
107 bool badUsage =
false;
111 static struct option longopts[] = { {
"config", 1, 0,
'c' }, {
"debug", 0, 0,
'd' }, {
"version", 0, 0,
'v' }, {
112 "execute", 1, 0,
'x' }, {
"outputfile", 1, 0,
'f' }, {
"inputfile", 1, 0,
'i' }, {
"repeat", 1, 0,
'r' }, {
113 "help", 0, 0,
'?' }, { 0, 0, 0, 0 } };
114 int option_index = 0;
116 while ((c = getopt_long(argc, argv,
"?vc:d:x:f:i:r:", longopts, &option_index)) != -1) {
149 if (outputStr !=
"") {
150 if (_cmd ==
"" && inputStr ==
"") {
151 cerr <<
"When specifying an output file you must either " <<
"specify a command or an input file" << endl;
154 else if (_cmd !=
"" && inputStr !=
"") {
155 cerr <<
"You must specify either a command or an input file on " <<
"the command line, not both" << endl;
160 if (badUsage ==
true) {
165 if (outputStr !=
"") {
166 _outputStrm =
new ofstream(outputStr.c_str());
167 if (!(*_outputStrm)) {
168 cerr <<
"could not open the output file " << outputStr << endl;
173 if (inputStr !=
"") {
174 _inputStrm =
new ifstream(inputStr.c_str());
175 if (!(*_inputStrm)) {
176 cerr <<
"could not open the input file " << inputStr << endl;
179 _createdInputStrm =
true;
182 if (!repeatStr.empty()) {
183 _repeat = atoi(repeatStr.c_str());
184 if (!_repeat && repeatStr !=
"0") {
185 cerr <<
"repeat number invalid: " << repeatStr << endl;
193 if (badUsage ==
true) {
199 BESDEBUG(
"standalone",
"ServerApp: initializing default module ... " << endl);
200 BESDefaultModule::initialize(argc, argv);
201 BESDEBUG(
"standalone",
"ServerApp: done initializing default module" << endl);
203 BESDEBUG(
"standalone",
"ServerApp: initializing default commands ... " << endl);
205 BESDEBUG(
"standalone",
"ServerApp: done initializing default commands" << endl);
207 BESDEBUG(
"standalone",
"ServerApp: initializing loaded modules ... " << endl);
209 BESDEBUG(
"standalone",
"ServerApp: done initializing loaded modules" << endl);
210 if (retval)
return retval;
213 cerr <<
"Failed to initialize stand alone app" << endl;
218 BESDEBUG(
"standalone",
"StandAloneApp: initialized settings:" << endl << *
this);
231 _client->setOutput(&cout,
false);
233 BESDEBUG(
"standalone",
"OK" << endl);
240 BESDEBUG(
"standalone",
"FAILED" << endl);
241 cerr <<
"error starting the client" << endl;
248 _client->executeCommands(_cmd, _repeat);
250 else if (_inputStrm) {
251 _client->executeCommands(*_inputStrm, _repeat);
258 cerr <<
"error processing commands" << endl;
263 BESDEBUG(
"standalone",
"StandAloneApp: shutting down client ... " << endl);
268 BESDEBUG(
"standalone",
"OK" << endl);
270 BESDEBUG(
"standalone",
"StandAloneApp: closing input stream ... " << endl);
271 if (_createdInputStrm && _inputStrm) {
276 BESDEBUG(
"standalone",
"OK" << endl);
279 BESDEBUG(
"standalone",
"FAILED" << endl);
280 cerr <<
"error closing the client" << endl;
295 BESDEBUG(
"standalone",
"ServerApp: terminating loaded modules ... " << endl);
297 BESDEBUG(
"standalone",
"ServerApp: done terminating loaded modules" << endl);
299 BESDEBUG(
"standalone",
"ServerApp: terminating default commands ... " << endl);
301 BESDEBUG(
"standalone",
"ServerApp: done terminating default commands" << endl);
303 BESDEBUG(
"standalone",
"ServerApp: terminating default module ... " << endl);
304 BESDefaultModule::terminate();
305 BESDEBUG(
"standalone",
"ServerApp: done terminating default module" << endl);
307 CmdTranslation::terminate();
322 strm << BESIndent::LMarg <<
"StandAloneApp::dump - (" << (
void *)
this <<
")" << endl;
325 strm << BESIndent::LMarg <<
"client: " << endl;
328 BESIndent::UnIndent();
331 strm << BESIndent::LMarg <<
"client: null" << endl;
333 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
334 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
335 strm << BESIndent::LMarg <<
"input stream: " << (
void *) _inputStrm << endl;
336 strm << BESIndent::LMarg <<
"created input stream? " << _createdInputStrm << endl;
338 BESIndent::UnIndent();
341 int main(
int argc,
char **argv)
345 return app.
main(argc, argv);
348 cerr <<
"Caught BES Error while starting the command processor: " << e.
get_message() << endl;
351 catch (std::exception &e) {
352 cerr <<
"Caught C++ error while starting the command processor: " << e.what() << endl;
356 cerr <<
"Caught unknown error while starting the command processor." << endl;
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 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()
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.
virtual int terminate(int sig=0)
clean up after the application
static std::string ConfigFile