StandAloneApp.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <unistd.h>
00034 #include <signal.h>
00035
00036 #include <iostream>
00037 #include <string>
00038 #include <fstream>
00039
00040 using std::cout ;
00041 using std::cerr ;
00042 using std::endl ;
00043 using std::flush ;
00044 using std::string ;
00045 using std::ofstream ;
00046
00047 #include "StandAloneApp.h"
00048 #include "StandAloneClient.h"
00049 #include "BESError.h"
00050 #include "BESDebug.h"
00051 #include "BESDefaultModule.h"
00052 #include "BESXMLDefaultCommands.h"
00053 #include "TheBESKeys.h"
00054 #include "CmdTranslation.h"
00055
00056 StandAloneApp::StandAloneApp()
00057 : BESModuleApp(),
00058 _client( 0 ),
00059 _outputStrm( 0 ),
00060 _inputStrm( 0 ),
00061 _createdInputStrm( false ),
00062 _repeat( 0 )
00063 {
00064 }
00065
00066 StandAloneApp::~StandAloneApp()
00067 {
00068 if( _client )
00069 {
00070 delete _client ;
00071 _client = 0 ;
00072 }
00073 }
00074
00075 void
00076 StandAloneApp::showVersion()
00077 {
00078 cout << appName() << ": version 2.0" << endl ;
00079 }
00080
00081 void
00082 StandAloneApp::showUsage( )
00083 {
00084 cout << endl ;
00085 cout << appName() << ": the following flags are available:" << endl ;
00086 cout << " -c <configFile> - specifies a BES configuration file to use" << endl ;
00087 cout << " -x <command> - specifies a command for the server to execute" << endl ;
00088 cout << " -i <inputFile> - specifies a file name for a sequence of input commands" << endl ;
00089 cout << " -f <outputFile> - specifies a file name to output the results of the input" << endl ;
00090 cout << " -d - sets the optional debug flag for the client session" << endl ;
00091 cout << " -r <num> - repeat the command(s) num times" << endl ;
00092 cout << " -? - display this list of flags" << endl ;
00093 cout << endl ;
00094 BESDebug::Help( cout ) ;
00095 }
00096
00097 int
00098 StandAloneApp::initialize( int argc, char **argv )
00099 {
00100 CmdTranslation::initialize( argc, argv ) ;
00101
00102 string outputStr = "" ;
00103 string inputStr = "" ;
00104 string repeatStr = "" ;
00105
00106 bool badUsage = false ;
00107
00108 int c ;
00109
00110 while( ( c = getopt( argc, argv, "?vc:d:x:f:i:r:" ) ) != EOF )
00111 {
00112 switch( c )
00113 {
00114 case 'c':
00115 TheBESKeys::ConfigFile = optarg ;
00116 break ;
00117 case 'd':
00118 BESDebug::SetUp( optarg ) ;
00119 break ;
00120 case 'v':
00121 {
00122 showVersion() ;
00123 exit( 0 ) ;
00124 }
00125 break ;
00126 case 'x':
00127 _cmd = optarg ;
00128 break ;
00129 case 'f':
00130 outputStr = optarg ;
00131 break ;
00132 case 'i':
00133 inputStr = optarg ;
00134 break ;
00135 case 'r':
00136 repeatStr = optarg ;
00137 break ;
00138 case '?':
00139 {
00140 showUsage() ;
00141 exit( 0 ) ;
00142 }
00143 break ;
00144 }
00145 }
00146
00147 if( outputStr != "" )
00148 {
00149 if( _cmd == "" && inputStr == "" )
00150 {
00151 cerr << "When specifying an output file you must either "
00152 << "specify a command or an input file"
00153 << endl ;
00154 badUsage = true ;
00155 }
00156 else if( _cmd != "" && inputStr != "" )
00157 {
00158 cerr << "You must specify either a command or an input file on "
00159 << "the command line, not both"
00160 << endl ;
00161 badUsage = true ;
00162 }
00163 }
00164
00165 if( badUsage == true )
00166 {
00167 showUsage( ) ;
00168 return 1 ;
00169 }
00170
00171 if( outputStr != "" )
00172 {
00173 _outputStrm = new ofstream( outputStr.c_str() ) ;
00174 if( !(*_outputStrm) )
00175 {
00176 cerr << "could not open the output file " << outputStr << endl ;
00177 badUsage = true ;
00178 }
00179 }
00180
00181 if( inputStr != "" )
00182 {
00183 _inputStrm = new ifstream( inputStr.c_str() ) ;
00184 if( !(*_inputStrm) )
00185 {
00186 cerr << "could not open the input file " << inputStr << endl ;
00187 badUsage = true ;
00188 }
00189 _createdInputStrm = true ;
00190 }
00191
00192 if( !repeatStr.empty() )
00193 {
00194 _repeat = atoi( repeatStr.c_str() ) ;
00195 if( !_repeat && repeatStr != "0" )
00196 {
00197 cerr << "repeat number invalid: " << repeatStr << endl ;
00198 badUsage = true ;
00199 }
00200 if( !_repeat )
00201 {
00202 _repeat = 1 ;
00203 }
00204 }
00205
00206 if( badUsage == true )
00207 {
00208 showUsage( ) ;
00209 return 1 ;
00210 }
00211
00212 try
00213 {
00214 BESDEBUG( "standalone", "ServerApp: initializing default module ... " << endl )
00215 BESDefaultModule::initialize( argc, argv ) ;
00216 BESDEBUG( "standalone", "OK" << endl ) ;
00217
00218 BESDEBUG( "standalone", "ServerApp: initializing default commands ... " << endl )
00219 BESXMLDefaultCommands::initialize( argc, argv ) ;
00220 BESDEBUG( "standalone", "OK" << endl ) ;
00221
00222 int retval = BESModuleApp::initialize( argc, argv ) ;
00223 if( retval )
00224 return retval ;
00225 }
00226 catch( BESError &e )
00227 {
00228 cerr << "Failed to initialize stand alone app" << endl ;
00229 cerr << e.get_message() << endl ;
00230 return 1 ;
00231 }
00232
00233 BESDEBUG( "standalone", "StandAloneApp: initialized settings:" << endl << *this ) ;
00234
00235 return 0 ;
00236 }
00237
00238 int
00239 StandAloneApp::run()
00240 {
00241 try
00242 {
00243 _client = new StandAloneClient ;
00244 if( _outputStrm )
00245 {
00246 _client->setOutput( _outputStrm, true ) ;
00247 }
00248 else
00249 {
00250 _client->setOutput( &cout, false ) ;
00251 }
00252 BESDEBUG( "cmdln", "OK" << endl ) ;
00253 }
00254 catch( BESError &e )
00255 {
00256 if( _client )
00257 {
00258 delete _client ;
00259 _client = 0 ;
00260 }
00261 BESDEBUG( "cmdln", "FAILED" << endl ) ;
00262 cerr << "error starting the client" << endl ;
00263 cerr << e.get_message() << endl ;
00264 exit( 1 ) ;
00265 }
00266
00267 try
00268 {
00269 if( _cmd != "" )
00270 {
00271 _client->executeCommands( _cmd, _repeat ) ;
00272 }
00273 else if( _inputStrm )
00274 {
00275 _client->executeCommands( *_inputStrm, _repeat ) ;
00276 }
00277 else
00278 {
00279 _client->interact() ;
00280 }
00281 }
00282 catch( BESError &e )
00283 {
00284 cerr << "error processing commands" << endl ;
00285 cerr << e.get_message() << endl ;
00286 }
00287
00288 try
00289 {
00290 BESDEBUG( "cmdln", "StandAloneApp: shutting down client ... " << endl )
00291 if( _client )
00292 {
00293 delete _client ;
00294 _client = 0 ;
00295 }
00296 BESDEBUG( "cmdln", "OK" << endl ) ;
00297
00298 BESDEBUG( "cmdln", "StandAloneApp: closing input stream ... " << endl )
00299 if( _createdInputStrm )
00300 {
00301 _inputStrm->close() ;
00302 delete _inputStrm ;
00303 _inputStrm = 0 ;
00304 }
00305 BESDEBUG( "cmdln", "OK" << endl )
00306 }
00307 catch( BESError &e )
00308 {
00309 BESDEBUG( "cmdln", "FAILED" << endl )
00310 cerr << "error closing the client" << endl ;
00311 cerr << e.get_message() << endl ;
00312 return 1 ;
00313 }
00314
00315 return 0 ;
00316 }
00317
00323 int
00324 StandAloneApp::terminate( int sig )
00325 {
00326 BESDEBUG( "server", "ServerApp: terminating default module ... " << endl )
00327 BESDefaultModule::terminate( ) ;
00328 BESDEBUG( "server", "OK" << endl ) ;
00329
00330 BESDEBUG( "server", "ServerApp: terminating default commands ... " << endl )
00331 BESXMLDefaultCommands::terminate( ) ;
00332 BESDEBUG( "server", "OK" << endl ) ;
00333
00334 BESModuleApp::terminate( sig ) ;
00335
00336 CmdTranslation::terminate( ) ;
00337
00338 return sig ;
00339 }
00340
00347 void
00348 StandAloneApp::dump( ostream &strm ) const
00349 {
00350 strm << BESIndent::LMarg << "StandAloneApp::dump - ("
00351 << (void *)this << ")" << endl ;
00352 BESIndent::Indent() ;
00353 if( _client )
00354 {
00355 strm << BESIndent::LMarg << "client: " << endl ;
00356 BESIndent::Indent() ;
00357 _client->dump( strm ) ;
00358 BESIndent::UnIndent() ;
00359 }
00360 else
00361 {
00362 strm << BESIndent::LMarg << "client: null" << endl ;
00363 }
00364 strm << BESIndent::LMarg << "command: " << _cmd << endl ;
00365 strm << BESIndent::LMarg << "output stream: " << (void *)_outputStrm << endl ;
00366 strm << BESIndent::LMarg << "input stream: " << (void *)_inputStrm << endl ;
00367 strm << BESIndent::LMarg << "created input stream? " << _createdInputStrm << endl ;
00368 BESBaseApp::dump( strm ) ;
00369 BESIndent::UnIndent() ;
00370 }
00371
00372 int
00373 main( int argc, char **argv )
00374 {
00375 StandAloneApp app ;
00376 return app.main( argc, argv ) ;
00377 }
00378