OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
|
00001 // StandAloneApp.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <unistd.h> // for getopt 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 ... " 00215 << endl ) ; 00216 BESDefaultModule::initialize( argc, argv ) ; 00217 BESDEBUG( "standalone", "OK" << endl ) ; 00218 00219 BESDEBUG( "standalone", "ServerApp: initializing default commands ... " 00220 << endl ) ; 00221 BESXMLDefaultCommands::initialize( argc, argv ) ; 00222 BESDEBUG( "standalone", "OK" << endl ) ; 00223 00224 int retval = BESModuleApp::initialize( argc, argv ) ; 00225 if( retval ) 00226 return retval ; 00227 } 00228 catch( BESError &e ) 00229 { 00230 cerr << "Failed to initialize stand alone app" << endl ; 00231 cerr << e.get_message() << endl ; 00232 return 1 ; 00233 } 00234 00235 BESDEBUG( "standalone", "StandAloneApp: initialized settings:" 00236 << endl << *this ) ; 00237 00238 return 0 ; 00239 } 00240 00241 int 00242 StandAloneApp::run() 00243 { 00244 try 00245 { 00246 _client = new StandAloneClient ; 00247 if( _outputStrm ) 00248 { 00249 _client->setOutput( _outputStrm, true ) ; 00250 } 00251 else 00252 { 00253 _client->setOutput( &cout, false ) ; 00254 } 00255 BESDEBUG( "standalone", "OK" << endl ) ; 00256 } 00257 catch( BESError &e ) 00258 { 00259 if( _client ) 00260 { 00261 delete _client ; 00262 _client = 0 ; 00263 } 00264 BESDEBUG( "standalone", "FAILED" << endl ) ; 00265 cerr << "error starting the client" << endl ; 00266 cerr << e.get_message() << endl ; 00267 exit( 1 ) ; 00268 } 00269 00270 try 00271 { 00272 if( _cmd != "" ) 00273 { 00274 _client->executeCommands( _cmd, _repeat ) ; 00275 } 00276 else if( _inputStrm ) 00277 { 00278 _client->executeCommands( *_inputStrm, _repeat ) ; 00279 } 00280 else 00281 { 00282 _client->interact() ; 00283 } 00284 } 00285 catch( BESError &e ) 00286 { 00287 cerr << "error processing commands" << endl ; 00288 cerr << e.get_message() << endl ; 00289 } 00290 00291 try 00292 { 00293 BESDEBUG( "standalone", "StandAloneApp: shutting down client ... " 00294 << endl ) ; 00295 if( _client ) 00296 { 00297 delete _client ; 00298 _client = 0 ; 00299 } 00300 BESDEBUG( "standalone", "OK" << endl ) ; 00301 00302 BESDEBUG( "standalone", "StandAloneApp: closing input stream ... " 00303 << endl ) ; 00304 if( _createdInputStrm ) 00305 { 00306 _inputStrm->close() ; 00307 delete _inputStrm ; 00308 _inputStrm = 0 ; 00309 } 00310 BESDEBUG( "standalone", "OK" << endl ) ; 00311 } 00312 catch( BESError &e ) 00313 { 00314 BESDEBUG( "standalone", "FAILED" << endl ) ; 00315 cerr << "error closing the client" << endl ; 00316 cerr << e.get_message() << endl ; 00317 return 1 ; 00318 } 00319 00320 return 0 ; 00321 } 00322 00328 int 00329 StandAloneApp::terminate( int sig ) 00330 { 00331 BESDEBUG( "standalone", "ServerApp: terminating default module ... " 00332 << endl ) ; 00333 BESDefaultModule::terminate( ) ; 00334 BESDEBUG( "standalone", "OK" << endl ) ; 00335 00336 BESDEBUG( "standalone", "ServerApp: terminating default commands ... " 00337 << endl ) ; 00338 BESXMLDefaultCommands::terminate( ) ; 00339 BESDEBUG( "standalone", "OK" << endl ) ; 00340 00341 BESModuleApp::terminate( sig ) ; 00342 00343 CmdTranslation::terminate( ) ; 00344 00345 return sig ; 00346 } 00347 00354 void 00355 StandAloneApp::dump( ostream &strm ) const 00356 { 00357 strm << BESIndent::LMarg << "StandAloneApp::dump - (" 00358 << (void *)this << ")" << endl ; 00359 BESIndent::Indent() ; 00360 if( _client ) 00361 { 00362 strm << BESIndent::LMarg << "client: " << endl ; 00363 BESIndent::Indent() ; 00364 _client->dump( strm ) ; 00365 BESIndent::UnIndent() ; 00366 } 00367 else 00368 { 00369 strm << BESIndent::LMarg << "client: null" << endl ; 00370 } 00371 strm << BESIndent::LMarg << "command: " << _cmd << endl ; 00372 strm << BESIndent::LMarg << "output stream: " << (void *)_outputStrm << endl ; 00373 strm << BESIndent::LMarg << "input stream: " << (void *)_inputStrm << endl ; 00374 strm << BESIndent::LMarg << "created input stream? " << _createdInputStrm << endl ; 00375 BESBaseApp::dump( strm ) ; 00376 BESIndent::UnIndent() ; 00377 } 00378 00379 int 00380 main( int argc, char **argv ) 00381 { 00382 StandAloneApp app ; 00383 return app.main( argc, argv ) ; 00384 } 00385