42#include "StandAloneApp.h"
43#include "StandAloneClient.h"
46#include "BESDefaultModule.h"
47#include "BESXMLDefaultCommands.h"
48#include "TheBESKeys.h"
49#include "CmdTranslation.h"
51#define MODULE "standalone"
52#define prolog string("StandAloneApp::").append(__func__).append("() - ")
56StandAloneApp::~StandAloneApp()
61void StandAloneApp::showVersion()
63 cout <<
appName() <<
": version 3.0" << endl;
66void StandAloneApp::showUsage()
69 cout <<
appName() <<
": the following options are available:" << endl;
70 cout <<
" -c <file>, --config=<file> - BES configuration file" << endl;
71 cout <<
" -x <command>, --execute=<command> - command for the server " << endl;
72 cout <<
" to execute" << endl;
73 cout <<
" -i <file>, --inputfile=<file> - file with a sequence of input " << endl;
74 cout <<
" commands, may be used multiple times." << endl;
75 cout <<
" -f <file>, --outputfile=<file> - write output to this file" << endl;
76 cout <<
" -d, --debug - turn on debugging for the client session" << endl;
77 cout <<
" -r <num>, --repeat=<num> - repeat the command(s) <num> times" << endl;
78 cout <<
" -v, --version - return version information" << endl;
79 cout <<
" -?, --help - display help information" << endl;
81 cout <<
"Note: You may provide a bes command with -x OR you may provide one " << endl;
82 cout <<
" or more BES command file names. One or the other, not neither, not both." << endl;
89 CmdTranslation::initialize(argc, argv);
91 static struct option longopts[] = {
92 {
"config", 1, 0,
'c' },
93 {
"debug", 0, 0,
'd' },
94 {
"version", 0, 0,
'v' },
95 {
"execute", 1, 0,
'x' },
96 {
"outputfile", 1, 0,
'f' },
97 {
"inputfile", 1, 0,
'i' },
98 {
"repeat", 1, 0,
'r' },
99 {
"help", 0, 0,
'?' },
105 int option_index = 0;
107 while ((c = getopt_long(argc, argv,
"?vc:d:x:f:i:r:", longopts, &option_index)) != -1) {
122 _command_file_names.emplace_back(optarg);
137 bool badUsage =
false;
138 if (!outputStr.empty()) {
139 if (_cmd.empty() && _command_file_names.empty()) {
140 cerr <<
"When specifying an output file you must either specify a command or an input file" << endl;
143 else if (!_cmd.empty() && !_command_file_names.empty()) {
144 cerr <<
"You must specify either a command or an input file on the command line, not both" << endl;
154 if (!outputStr.empty()) {
155 _outputStrm =
new ofstream(outputStr.c_str());
156 if (!(*_outputStrm)) {
157 cerr <<
"could not open the output file " << outputStr << endl;
162 if (!repeatStr.empty()) {
163 _repeat = atoi(repeatStr.c_str());
164 if (!_repeat && repeatStr !=
"0") {
165 cerr <<
"repeat number invalid: " << repeatStr << endl;
179 BESDEBUG(MODULE, prolog <<
"Initializing default module ... " << endl);
180 BESDefaultModule::initialize(argc, argv);
181 BESDEBUG(MODULE, prolog <<
"Done initializing default module" << endl);
183 BESDEBUG(MODULE, prolog <<
"Initializing default commands ... " << endl);
185 BESDEBUG(MODULE, prolog <<
"Done initializing default commands" << endl);
187 BESDEBUG(MODULE, prolog <<
"Initializing loaded modules ... " << endl);
189 BESDEBUG(MODULE, prolog <<
"Done initializing loaded modules" << endl);
190 if (retval)
return retval;
193 cerr << prolog <<
"Failed to initialize stand alone app. Message : " << e.
get_message() << endl;
197 BESDEBUG(MODULE, prolog <<
"Initialized settings:" << endl << *
this);
217 BESDEBUG(MODULE, prolog <<
"StandAloneClient instance created." << endl);
220 msg = prolog +
"FAILED to start StandAloneClient instance. Message: " + e.
get_message();
221 BESDEBUG(MODULE, msg << endl);
230 else if (!_command_file_names.empty()) {
231 BESDEBUG(MODULE, prolog <<
"Found " << _command_file_names.size() <<
" command files." << endl);
232 for (
auto &command_filename: _command_file_names) {
233 BESDEBUG(MODULE, prolog <<
"Processing BES command file: " << command_filename<< endl);
234 if (!command_filename.empty()) {
235 ifstream cmdStrm(command_filename);
236 if (!cmdStrm.is_open()) {
237 cerr << prolog <<
"FAILED to open the input file '" << command_filename <<
"' SKIPPING." << endl;
244 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
255 cerr << prolog <<
"Error processing commands. Message: " << e.
get_message() << endl;
268 BESDEBUG(MODULE,
"ServerApp: terminating loaded modules ... " << endl);
270 BESDEBUG(MODULE,
"ServerApp: done terminating loaded modules" << endl);
272 BESDEBUG(MODULE,
"ServerApp: terminating default commands ... " << endl);
274 BESDEBUG(MODULE,
"ServerApp: done terminating default commands" << endl);
276 BESDEBUG(MODULE,
"ServerApp: terminating default module ... " << endl);
277 BESDefaultModule::terminate();
278 BESDEBUG(MODULE,
"ServerApp: done terminating default module" << endl);
280 CmdTranslation::terminate();
295 strm << BESIndent::LMarg <<
"StandAloneApp::dump - (" << (
void *)
this <<
")" << endl;
298 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
299 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
300 if(_command_file_names.empty()){
301 strm << BESIndent::LMarg <<
"No command filenames were identified." << endl;
304 strm << BESIndent::LMarg <<
"Found " << _command_file_names.size() <<
" command file names." << endl;
306 for (
unsigned index = 0; index < _command_file_names.size(); index++) {
307 strm << BESIndent::LMarg <<
"command_filename["<<index<<
"]: "<< _command_file_names[index] << endl;
309 BESIndent::UnIndent();
312 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.
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