55 #include "StandAloneApp.h"
56 #include "StandAloneClient.h"
59 #include "BESDefaultModule.h"
60 #include "BESXMLDefaultCommands.h"
61 #include "TheBESKeys.h"
62 #include "BESCatalogUtils.h"
63 #include "CmdTranslation.h"
65 #define MODULE "standalone"
66 #define prolog string("StandAloneApp::").append(__func__).append("() - ")
68 StandAloneApp::StandAloneApp() :
73 StandAloneApp::~StandAloneApp()
78 void StandAloneApp::showVersion()
80 cout <<
appName() <<
": version 3.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 " << endl;
89 cout <<
" to execute" << endl;
90 cout <<
" -i <file>, --inputfile=<file> - file with a sequence of input " << endl;
91 cout <<
" commands, may be used multiple times." << endl;
92 cout <<
" -f <file>, --outputfile=<file> - write output to this file" << endl;
93 cout <<
" -d, --debug - turn on debugging for the client session" << endl;
94 cout <<
" -r <num>, --repeat=<num> - repeat the command(s) <num> times" << endl;
95 cout <<
" -v, --version - return version information" << endl;
96 cout <<
" -?, --help - display help information" << endl;
98 cout <<
"Note: You may provide a bes command with -x OR you may provide one " << endl;
99 cout <<
" or more BES command file names. One or the other, not neither, not both." << endl;
106 CmdTranslation::initialize(argc, argv);
108 string outputStr =
"";
110 string repeatStr =
"";
112 bool badUsage =
false;
116 static struct option longopts[] = {
117 {
"config", 1, 0,
'c' },
118 {
"debug", 0, 0,
'd' },
119 {
"version", 0, 0,
'v' },
120 {
"execute", 1, 0,
'x' },
121 {
"outputfile", 1, 0,
'f' },
122 {
"inputfile", 1, 0,
'i' },
123 {
"repeat", 1, 0,
'r' },
124 {
"help", 0, 0,
'?' },
127 int option_index = 0;
129 while ((c = getopt_long(argc, argv,
"?vc:d:x:f:i:r:", longopts, &option_index)) != -1) {
149 _command_file_names.push_back(optarg);
162 if (outputStr !=
"") {
163 if (_cmd ==
"" && _command_file_names.empty()) {
164 cerr <<
"When specifying an output file you must either specify a command or an input file" << endl;
167 else if (_cmd !=
"" && !_command_file_names.empty()) {
168 cerr <<
"You must specify either a command or an input file on the command line, not both" << endl;
173 if (badUsage ==
true) {
178 if (outputStr !=
"") {
179 _outputStrm =
new ofstream(outputStr.c_str());
180 if (!(*_outputStrm)) {
181 cerr <<
"could not open the output file " << outputStr << endl;
186 if (!repeatStr.empty()) {
187 _repeat = atoi(repeatStr.c_str());
188 if (!_repeat && repeatStr !=
"0") {
189 cerr <<
"repeat number invalid: " << repeatStr << endl;
197 if (badUsage ==
true) {
203 BESDEBUG(MODULE, prolog <<
"Initializing default module ... " << endl);
204 BESDefaultModule::initialize(argc, argv);
205 BESDEBUG(MODULE, prolog <<
"Done initializing default module" << endl);
207 BESDEBUG(MODULE, prolog <<
"Initializing default commands ... " << endl);
209 BESDEBUG(MODULE, prolog <<
"Done initializing default commands" << endl);
211 BESDEBUG(MODULE, prolog <<
"Initializing loaded modules ... " << endl);
213 BESDEBUG(MODULE, prolog <<
"Done initializing loaded modules" << endl);
214 if (retval)
return retval;
217 cerr << prolog <<
"Failed to initialize stand alone app. Message : " << e.
get_message() << endl;
221 BESDEBUG(MODULE, prolog <<
"Initialized settings:" << endl << *
this);
241 BESDEBUG(MODULE, prolog <<
"StandAloneClient instance created." << endl);
244 msg = prolog +
"FAILED to start StandAloneClient instance. Message: " + e.
get_message();
245 BESDEBUG(MODULE, msg << endl);
254 else if (!_command_file_names.empty()) {
255 BESDEBUG(MODULE, prolog <<
"Found " << _command_file_names.size() <<
" command files." << endl);
256 for(
unsigned index=0; index<_command_file_names.size(); index++){
257 string command_filename = _command_file_names[index];
258 BESDEBUG(MODULE, prolog <<
"Processing BES command file: " << command_filename<< endl);
259 if (!command_filename.empty()) {
260 ifstream cmdStrm(command_filename.c_str());
261 if (!cmdStrm.is_open()) {
262 cerr << prolog <<
"FAILED to open the input file '" << command_filename <<
"' SKIPPING." << endl;
269 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
280 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
293 BESDEBUG(MODULE,
"ServerApp: terminating loaded modules ... " << endl);
295 BESDEBUG(MODULE,
"ServerApp: done terminating loaded modules" << endl);
297 BESDEBUG(MODULE,
"ServerApp: terminating default commands ... " << endl);
299 BESDEBUG(MODULE,
"ServerApp: done terminating default commands" << endl);
301 BESDEBUG(MODULE,
"ServerApp: terminating default module ... " << endl);
302 BESDefaultModule::terminate();
303 BESDEBUG(MODULE,
"ServerApp: done terminating default module" << endl);
305 CmdTranslation::terminate();
320 strm << BESIndent::LMarg <<
"StandAloneApp::dump - (" << (
void *)
this <<
")" << endl;
323 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
324 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
325 if(_command_file_names.empty()){
326 strm << BESIndent::LMarg <<
"No command filenames were identified." << endl;
329 strm << BESIndent::LMarg <<
"Found " << _command_file_names.size() <<
" command file names." << endl;
331 for (
unsigned index = 0; index < _command_file_names.size(); index++) {
332 strm << BESIndent::LMarg <<
"command_filename["<<index<<
"]: "<< _command_file_names[index] << endl;
334 BESIndent::UnIndent();
337 BESIndent::UnIndent();
virtual void dump(std::ostream &strm) const =0
dumps information about this object
std::string appName(void) const
Returns the name of the application.
static void SetUp(const std::string &values)
Sets up debugging for the bes.
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Abstract exception class for the BES with basic string message.
virtual std::string get_message()
get the error message for this exception
Base application object for all BES applications.
virtual int terminate(int sig=0)
clean up after the application
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
static int terminate(void)
Removes the default set of BES XML commands from the list of possible commands.
static int initialize(int argc, char **argv)
Loads the default set of BES XML commands.
virtual int terminate(int sig=0)
clean up after the application
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
virtual int run()
The body of the application, implementing the primary functionality of the BES application.
void executeCommands(const std::string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
void interact()
An interactive BES client that takes BES requests on the command line.
void setOutput(std::ostream *strm, bool created)
Set the output stream for responses from the BES server.
static TheBESKeys * TheKeys()
static std::string ConfigFile