54 #include "CmdClient.h"
55 #include "CmdTranslation.h"
59 #define BES_CMDLN_DEFAULT_TIMEOUT 5
62 #define DEFAULT_PORT 10022
63 #define DEFAULT_HOST "localhost"
66 BESApp(), _client(0), _hostStr(DEFAULT_HOST), _unixStr(
""), _portVal(DEFAULT_PORT), _outputStrm(0), _inputStrm(
67 0), _createdInputStrm(false), _timeout(0), _repeat(0)
79 void CmdApp::showVersion()
81 cout << appName() <<
": version 2.0" << endl;
84 void CmdApp::showUsage()
87 cout << appName() <<
": the following flags are available:" << endl;
88 cout <<
" -h <host> - specifies a host for TCP/IP connection" << endl;
89 cout <<
" -p <port> - specifies a port for TCP/IP connection" << endl;
90 cout <<
" -u <unixSocket> - specifies a unix socket for connection. " << endl;
91 cout <<
" -x <command> - specifies a command for the server to execute" << endl;
92 cout <<
" -i <inputFile> - specifies a file name for a sequence of input commands" << endl;
93 cout <<
" -f <outputFile> - specifies a file name to output the results of the input" << endl;
94 cout <<
" -t <timeoutVal> - specifies an optional timeout value in seconds" << endl;
95 cout <<
" -d - sets the optional debug flag for the client session" << endl;
96 cout <<
" -r <num> - repeat the command(s) num times" << endl;
97 cout <<
" -? - display this list of flags" << endl;
102 void CmdApp::signalCannotConnect(
int sig)
104 if (sig == SIGCONT) {
110 <<
"busy with another incoming connection. exiting!\n";
117 void CmdApp::signalInterrupt(
int sig)
122 if (signal(SIGINT, CmdApp::signalInterrupt) == SIG_ERR) {
127 void CmdApp::signalTerminate(
int sig)
129 if (sig == SIGTERM) {
132 if (signal(SIGTERM, CmdApp::signalTerminate) == SIG_ERR) {
137 void CmdApp::signalBrokenPipe(
int sig)
139 if (sig == SIGPIPE) {
141 << endl <<
"Please check parameters and try again" << endl;
156 void CmdApp::registerSignals()
159 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGCONT ... " << endl);
160 if (signal( SIGCONT, signalCannotConnect) == SIG_ERR) {
161 BESDEBUG(
"cmdln",
"FAILED" << endl);
162 cerr << appName() <<
"Failed to register signal SIGCONT" << endl;
165 BESDEBUG(
"cmdln",
"OK" << endl);
169 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGINT ... " << endl);
170 if (signal( SIGINT, signalInterrupt) == SIG_ERR) {
171 BESDEBUG(
"cmdln",
"FAILED" << endl);
172 cerr << appName() <<
"Failed to register signal SIGINT" << endl;
175 BESDEBUG(
"cmdln",
"OK" << endl);
179 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGTERM ... " << endl);
180 if (signal( SIGTERM, signalTerminate) == SIG_ERR) {
181 BESDEBUG(
"cmdln",
"FAILED" << endl);
182 cerr << appName() <<
"Failed to register signal SIGTERM" << endl;
185 BESDEBUG(
"cmdln",
"OK" << endl);
188 BESDEBUG(
"cmdln",
"CmdApp: Registering signal SIGPIPE ... " << endl);
189 if (signal( SIGPIPE, CmdApp::signalBrokenPipe) == SIG_ERR) {
190 BESDEBUG(
"cmdln",
"FAILED" << endl);
191 cerr << appName() <<
"Failed to register signal SIGPIPE" << endl;
194 BESDEBUG(
"cmdln",
"OK" << endl);
200 if (retVal != 0)
return retVal;
202 CmdTranslation::initialize(argc, argv);
205 string outputStr =
"";
206 string inputStr =
"";
207 string timeoutStr =
"";
208 string repeatStr =
"";
210 bool badUsage =
false;
214 while ((c = getopt(argc, argv,
"?vd:h:p:t:u:x:f:i:r:")) != -1) {
256 if (!portStr.empty() && !_unixStr.empty()) {
257 cerr <<
"cannot use both a port number and a unix socket" << endl;
261 if (!portStr.empty()) {
262 _portVal = atoi(portStr.c_str());
265 if (!timeoutStr.empty()) {
266 _timeout = atoi(timeoutStr.c_str());
269 _timeout = BES_CMDLN_DEFAULT_TIMEOUT;
272 if (outputStr !=
"") {
273 if (_cmd ==
"" && inputStr ==
"") {
274 cerr <<
"When specifying an output file you must either " <<
"specify a command or an input file" << endl;
277 else if (_cmd !=
"" && inputStr !=
"") {
278 cerr <<
"You must specify either a command or an input file on " <<
"the command line, not both" << endl;
283 if (badUsage ==
true) {
288 if (outputStr !=
"") {
289 _outputStrm =
new ofstream(outputStr.c_str());
290 if (!(*_outputStrm)) {
291 cerr <<
"could not open the output file " << outputStr << endl;
296 if (inputStr !=
"") {
297 _inputStrm =
new ifstream(inputStr.c_str());
298 if (!(*_inputStrm)) {
299 cerr <<
"could not open the input file " << inputStr << endl;
302 _createdInputStrm =
true;
305 if (!repeatStr.empty()) {
306 _repeat = atoi(repeatStr.c_str());
307 if (!_repeat && repeatStr !=
"0") {
308 cerr <<
"repeat number invalid: " << repeatStr << endl;
316 if (badUsage ==
true) {
323 BESDEBUG(
"cmdln",
"CmdApp: initialized settings:" << endl << *
this);
334 if (!_unixStr.empty()) {
335 BESDEBUG(
"cmdln",
"CmdApp: Connecting to unix socket: " << _unixStr <<
" ... " << endl);
336 _client->startClient(_unixStr, _timeout);
340 "CmdApp: Connecting to host: " << _hostStr <<
" at port: " << _portVal <<
" ... " << endl);
341 _client->startClient(_hostStr, _portVal, _timeout);
345 _client->setOutput(_outputStrm,
true);
348 _client->setOutput(&cout,
false);
350 BESDEBUG(
"cmdln",
"OK" << endl);
354 _client->shutdownClient();
358 BESDEBUG(
"cmdln",
"FAILED" << endl);
359 cerr <<
"error starting the client" << endl;
364 bool do_exit =
false;
367 do_exit = _client->executeCommands(_cmd, _repeat);
369 else if (_inputStrm) {
370 do_exit = _client->executeCommands(*_inputStrm, _repeat);
373 do_exit = _client->interact();
377 cerr <<
"error processing commands" << endl;
382 BESDEBUG(
"cmdln",
"CmdApp: shutting down client ... " << endl);
385 if (!do_exit) _client->shutdownClient();
389 BESDEBUG(
"cmdln",
"OK" << endl);
391 BESDEBUG(
"cmdln",
"CmdApp: closing input stream ... " << endl);
392 if (_createdInputStrm && _inputStrm) {
397 BESDEBUG(
"cmdln",
"OK" << endl);
400 BESDEBUG(
"cmdln",
"FAILED" << endl);
401 cerr <<
"error closing the client" << endl;
417 strm << BESIndent::LMarg <<
"CmdApp::dump - (" << (
void *)
this <<
")" << endl;
420 strm << BESIndent::LMarg <<
"client: " << endl;
423 BESIndent::UnIndent();
426 strm << BESIndent::LMarg <<
"client: null" << endl;
428 strm << BESIndent::LMarg <<
"host: " << _hostStr << endl;
429 strm << BESIndent::LMarg <<
"unix socket: " << _unixStr << endl;
430 strm << BESIndent::LMarg <<
"port: " << _portVal << endl;
431 strm << BESIndent::LMarg <<
"command: " << _cmd << endl;
432 strm << BESIndent::LMarg <<
"output stream: " << (
void *) _outputStrm << endl;
433 strm << BESIndent::LMarg <<
"input stream: " << (
void *) _inputStrm << endl;
434 strm << BESIndent::LMarg <<
"created input stream? " << _createdInputStrm << endl;
435 strm << BESIndent::LMarg <<
"timeout: " << _timeout << endl;
437 BESIndent::UnIndent();
440 int main(
int argc,
char **argv)
443 return app.
main(argc, argv);