50 #include "StandAloneApp.h"
51 #include "StandAloneClient.h"
54 #include "BESDefaultModule.h"
55 #include "BESXMLDefaultCommands.h"
56 #include "TheBESKeys.h"
57 #include "BESCatalogUtils.h"
58 #include "CmdTranslation.h"
60 StandAloneApp::StandAloneApp() :
61 BESModuleApp(), _client(0), _outputStrm(0), _inputStrm(0), _createdInputStrm( false), _repeat(0)
65 StandAloneApp::~StandAloneApp()
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);
228 _client->setOutput(&cout,
false);
230 BESDEBUG(
"standalone",
"OK" << endl);
237 BESDEBUG(
"standalone",
"FAILED" << endl);
238 cerr <<
"error starting the client" << endl;
245 _client->executeCommands(_cmd, _repeat);
247 else if (_inputStrm) {
248 _client->executeCommands(*_inputStrm, _repeat);
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;