42#include <libxml/parser.h>
44#include "StandAloneApp.h"
45#include "StandAloneClient.h"
48#include "BESDefaultModule.h"
49#include "BESXMLDefaultCommands.h"
50#include "TheBESKeys.h"
51#include "CmdTranslation.h"
53#define MODULE "standalone"
54#define prolog string("StandAloneApp::").append(__func__).append("() - ")
58void StandAloneApp::showVersion()
60 cout <<
appName() <<
": version 3.0" << endl;
63void StandAloneApp::showUsage()
66 cout <<
appName() <<
": the following options are available:" << endl;
67 cout <<
" -c <file>, --config=<file> - BES configuration file" << endl;
68 cout <<
" -x <command>, --execute=<command> - command for the server " << endl;
69 cout <<
" to execute" << endl;
70 cout <<
" -i <file>, --inputfile=<file> - file with a sequence of input " << endl;
71 cout <<
" commands, may be used multiple times." << endl;
72 cout <<
" -f <file>, --outputfile=<file> - write output to this file" << endl;
73 cout <<
" -d, --debug - turn on debugging for the client session" << endl;
74 cout <<
" -r <num>, --repeat=<num> - repeat the command(s) <num> times" << endl;
75 cout <<
" -v, --version - return version information" << endl;
76 cout <<
" -?, --help - display help information" << endl;
78 cout <<
"Note: You may provide a bes command with -x OR you may provide one " << endl;
79 cout <<
" or more BES command file names. One or the other, not neither, not both." << endl;
86 CmdTranslation::initialize(argc, argv);
88 static struct option longopts[] = {
89 {
"config", 1, 0,
'c' },
90 {
"debug", 0, 0,
'd' },
91 {
"version", 0, 0,
'v' },
92 {
"execute", 1, 0,
'x' },
93 {
"outputfile", 1, 0,
'f' },
94 {
"inputfile", 1, 0,
'i' },
95 {
"repeat", 1, 0,
'r' },
96 {
"help", 0, 0,
'?' },
102 int option_index = 0;
104 while ((c = getopt_long(argc, argv,
"?vc:d:x:f:i:r:", longopts, &option_index)) != -1) {
119 _command_file_names.emplace_back(optarg);
134 bool badUsage =
false;
135 if (!outputStr.empty()) {
136 if (_cmd.empty() && _command_file_names.empty()) {
137 cerr <<
"When specifying an output file you must either specify a command or an input file" << endl;
140 else if (!_cmd.empty() && !_command_file_names.empty()) {
141 cerr <<
"You must specify either a command or an input file on the command line, not both" << endl;
151 if (!outputStr.empty()) {
152 _outputStrm =
new ofstream(outputStr.c_str());
153 if (!(*_outputStrm)) {
154 cerr <<
"could not open the output file " << outputStr << endl;
159 if (!repeatStr.empty()) {
160 _repeat = atoi(repeatStr.c_str());
161 if (!_repeat && repeatStr !=
"0") {
162 cerr <<
"repeat number invalid: " << repeatStr << endl;
176 BESDEBUG(MODULE, prolog <<
"Initializing default module ... " << endl);
177 BESDefaultModule::initialize(argc, argv);
178 BESDEBUG(MODULE, prolog <<
"Done initializing default module" << endl);
180 BESDEBUG(MODULE, prolog <<
"Initializing default commands ... " << endl);
182 BESDEBUG(MODULE, prolog <<
"Done initializing default commands" << endl);
184 BESDEBUG(MODULE, prolog <<
"Initializing loaded modules ... " << endl);
186 BESDEBUG(MODULE, prolog <<
"Done initializing loaded modules" << endl);
187 if (retval)
return retval;
190 cerr << prolog <<
"Failed to initialize stand alone app. Message : " << e.
get_message() << endl;
194 BESDEBUG(MODULE, prolog <<
"Initialized settings:" << endl << *
this);
209 sac.setOutput(_outputStrm,
true);
212 sac.setOutput(&cout,
false);
214 BESDEBUG(MODULE, prolog <<
"StandAloneClient instance created." << endl);
217 msg = prolog +
"FAILED to start StandAloneClient instance. Message: " + e.
get_message();
218 BESDEBUG(MODULE, msg << endl);
225 sac.executeCommands(_cmd, _repeat);
227 else if (!_command_file_names.empty()) {
228 BESDEBUG(MODULE, prolog <<
"Found " << _command_file_names.size() <<
" command files." << endl);
229 unsigned int commands = 0;
230 for (
auto &command_filename: _command_file_names) {
232 BESDEBUG(MODULE, prolog <<
"Processing BES command file: " << command_filename<< endl);
233 if (!command_filename.empty()) {
234 ifstream cmdStrm(command_filename);
235 if (!cmdStrm.is_open()) {
236 cerr << prolog <<
"FAILED to open the input file '" << command_filename <<
"' SKIPPING." << endl;
240 sac.executeCommands(cmdStrm, _repeat);
241 if (commands < _command_file_names.size()) {
242 sac.getOutput() <<
"\nNext-Response:\n" << flush;
246 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
257 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
270 BESDEBUG(MODULE,
"ServerApp: terminating loaded modules ... " << endl);
272 BESDEBUG(MODULE,
"ServerApp: done terminating loaded modules" << endl);
274 BESDEBUG(MODULE,
"ServerApp: terminating default commands ... " << endl);
276 BESDEBUG(MODULE,
"ServerApp: done terminating default commands" << endl);
278 BESDEBUG(MODULE,
"ServerApp: terminating default module ... " << endl);
279 BESDefaultModule::terminate();
280 BESDEBUG(MODULE,
"ServerApp: done terminating default module" << endl);
282 CmdTranslation::terminate();
297 strm << BESIndent::LMarg <<
"StandAloneApp::dump - (" << (
void *)
this <<
")" << endl;
300 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
301 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
302 if(_command_file_names.empty()){
303 strm << BESIndent::LMarg <<
"No command filenames were identified." << endl;
306 strm << BESIndent::LMarg <<
"Found " << _command_file_names.size() <<
" command file names." << endl;
308 for (
unsigned index = 0; index < _command_file_names.size(); index++) {
309 strm << BESIndent::LMarg <<
"command_filename["<<index<<
"]: "<< _command_file_names[index] << endl;
311 BESIndent::UnIndent();
314 BESIndent::UnIndent();
std::string appName() const
Returns the name of the application.
void dump(std::ostream &strm) const override=0
dumps information about this object
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.
Base exception class for the BES with basic string message.
std::string get_message() const
get the error message for this exception
int terminate(int sig=0) override
clean up after the application
int initialize(int argC, char **argV) override
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.
int terminate(int sig=0) override
clean up after the application
int initialize(int argC, char **argV) override
Load and initialize any BES modules.
void dump(std::ostream &strm) const override
dumps information about this object
int run() override
The body of the application, implementing the primary functionality of the BES application.
static std::string ConfigFile