OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
StandAloneApp.cc
Go to the documentation of this file.
1 // StandAloneApp.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <unistd.h> // for getopt
34 #include <signal.h>
35 
36 #include <iostream>
37 #include <string>
38 #include <fstream>
39 
40 using std::cout ;
41 using std::cerr ;
42 using std::endl ;
43 using std::flush ;
44 using std::string ;
45 using std::ofstream ;
46 
47 #include "StandAloneApp.h"
48 #include "StandAloneClient.h"
49 #include "BESError.h"
50 #include "BESDebug.h"
51 #include "BESDefaultModule.h"
52 #include "BESXMLDefaultCommands.h"
53 #include "TheBESKeys.h"
54 #include "CmdTranslation.h"
55 
57  : BESModuleApp(),
58  _client( 0 ),
59  _outputStrm( 0 ),
60  _inputStrm( 0 ),
61  _createdInputStrm( false ),
62  _repeat( 0 )
63 {
64 }
65 
67 {
68  if( _client )
69  {
70  delete _client ;
71  _client = 0 ;
72  }
73 }
74 
75 void
76 StandAloneApp::showVersion()
77 {
78  cout << appName() << ": version 2.0" << endl ;
79 }
80 
81 void
82 StandAloneApp::showUsage( )
83 {
84  cout << endl ;
85  cout << appName() << ": the following flags are available:" << endl ;
86  cout << " -c <configFile> - specifies a BES configuration file to use" << endl ;
87  cout << " -x <command> - specifies a command for the server to execute" << endl ;
88  cout << " -i <inputFile> - specifies a file name for a sequence of input commands" << endl ;
89  cout << " -f <outputFile> - specifies a file name to output the results of the input" << endl ;
90  cout << " -d - sets the optional debug flag for the client session" << endl ;
91  cout << " -r <num> - repeat the command(s) num times" << endl ;
92  cout << " -? - display this list of flags" << endl ;
93  cout << endl ;
94  BESDebug::Help( cout ) ;
95 }
96 
97 int
98 StandAloneApp::initialize( int argc, char **argv )
99 {
100  CmdTranslation::initialize( argc, argv ) ;
101 
102  string outputStr = "" ;
103  string inputStr = "" ;
104  string repeatStr = "" ;
105 
106  bool badUsage = false ;
107 
108  int c ;
109 
110  while( ( c = getopt( argc, argv, "?vc:d:x:f:i:r:" ) ) != EOF )
111  {
112  switch( c )
113  {
114  case 'c':
115  TheBESKeys::ConfigFile = optarg ;
116  break ;
117  case 'd':
118  BESDebug::SetUp( optarg ) ;
119  break ;
120  case 'v':
121  {
122  showVersion() ;
123  exit( 0 ) ;
124  }
125  break ;
126  case 'x':
127  _cmd = optarg ;
128  break ;
129  case 'f':
130  outputStr = optarg ;
131  break ;
132  case 'i':
133  inputStr = optarg ;
134  break ;
135  case 'r':
136  repeatStr = optarg ;
137  break ;
138  case '?':
139  {
140  showUsage() ;
141  exit( 0 ) ;
142  }
143  break ;
144  }
145  }
146 
147  if( outputStr != "" )
148  {
149  if( _cmd == "" && inputStr == "" )
150  {
151  cerr << "When specifying an output file you must either "
152  << "specify a command or an input file"
153  << endl ;
154  badUsage = true ;
155  }
156  else if( _cmd != "" && inputStr != "" )
157  {
158  cerr << "You must specify either a command or an input file on "
159  << "the command line, not both"
160  << endl ;
161  badUsage = true ;
162  }
163  }
164 
165  if( badUsage == true )
166  {
167  showUsage( ) ;
168  return 1 ;
169  }
170 
171  if( outputStr != "" )
172  {
173  _outputStrm = new ofstream( outputStr.c_str() ) ;
174  if( !(*_outputStrm) )
175  {
176  cerr << "could not open the output file " << outputStr << endl ;
177  badUsage = true ;
178  }
179  }
180 
181  if( inputStr != "" )
182  {
183  _inputStrm = new ifstream( inputStr.c_str() ) ;
184  if( !(*_inputStrm) )
185  {
186  cerr << "could not open the input file " << inputStr << endl ;
187  badUsage = true ;
188  }
189  _createdInputStrm = true ;
190  }
191 
192  if( !repeatStr.empty() )
193  {
194  _repeat = atoi( repeatStr.c_str() ) ;
195  if( !_repeat && repeatStr != "0" )
196  {
197  cerr << "repeat number invalid: " << repeatStr << endl ;
198  badUsage = true ;
199  }
200  if( !_repeat )
201  {
202  _repeat = 1 ;
203  }
204  }
205 
206  if( badUsage == true )
207  {
208  showUsage( ) ;
209  return 1 ;
210  }
211 
212  try
213  {
214  BESDEBUG( "standalone", "ServerApp: initializing default module ... "
215  << endl ) ;
216  BESDefaultModule::initialize( argc, argv ) ;
217  BESDEBUG( "standalone", "OK" << endl ) ;
218 
219  BESDEBUG( "standalone", "ServerApp: initializing default commands ... "
220  << endl ) ;
221  BESXMLDefaultCommands::initialize( argc, argv ) ;
222  BESDEBUG( "standalone", "OK" << endl ) ;
223 
224  int retval = BESModuleApp::initialize( argc, argv ) ;
225  if( retval )
226  return retval ;
227  }
228  catch( BESError &e )
229  {
230  cerr << "Failed to initialize stand alone app" << endl ;
231  cerr << e.get_message() << endl ;
232  return 1 ;
233  }
234 
235  BESDEBUG( "standalone", "StandAloneApp: initialized settings:"
236  << endl << *this ) ;
237 
238  return 0 ;
239 }
240 
241 int
243 {
244  try
245  {
246  _client = new StandAloneClient ;
247  if( _outputStrm )
248  {
249  _client->setOutput( _outputStrm, true ) ;
250  }
251  else
252  {
253  _client->setOutput( &cout, false ) ;
254  }
255  BESDEBUG( "standalone", "OK" << endl ) ;
256  }
257  catch( BESError &e )
258  {
259  if( _client )
260  {
261  delete _client ;
262  _client = 0 ;
263  }
264  BESDEBUG( "standalone", "FAILED" << endl ) ;
265  cerr << "error starting the client" << endl ;
266  cerr << e.get_message() << endl ;
267  exit( 1 ) ;
268  }
269 
270  try
271  {
272  if( _cmd != "" )
273  {
274  _client->executeCommands( _cmd, _repeat ) ;
275  }
276  else if( _inputStrm )
277  {
278  _client->executeCommands( *_inputStrm, _repeat ) ;
279  }
280  else
281  {
282  _client->interact() ;
283  }
284  }
285  catch( BESError &e )
286  {
287  cerr << "error processing commands" << endl ;
288  cerr << e.get_message() << endl ;
289  }
290 
291  try
292  {
293  BESDEBUG( "standalone", "StandAloneApp: shutting down client ... "
294  << endl ) ;
295  if( _client )
296  {
297  delete _client ;
298  _client = 0 ;
299  }
300  BESDEBUG( "standalone", "OK" << endl ) ;
301 
302  BESDEBUG( "standalone", "StandAloneApp: closing input stream ... "
303  << endl ) ;
304  if( _createdInputStrm )
305  {
306  _inputStrm->close() ;
307  delete _inputStrm ;
308  _inputStrm = 0 ;
309  }
310  BESDEBUG( "standalone", "OK" << endl ) ;
311  }
312  catch( BESError &e )
313  {
314  BESDEBUG( "standalone", "FAILED" << endl ) ;
315  cerr << "error closing the client" << endl ;
316  cerr << e.get_message() << endl ;
317  return 1 ;
318  }
319 
320  return 0 ;
321 }
322 
328 int
330 {
331  BESDEBUG( "standalone", "ServerApp: terminating default module ... "
332  << endl ) ;
334  BESDEBUG( "standalone", "OK" << endl ) ;
335 
336  BESDEBUG( "standalone", "ServerApp: terminating default commands ... "
337  << endl ) ;
339  BESDEBUG( "standalone", "OK" << endl ) ;
340 
341  BESModuleApp::terminate( sig ) ;
342 
344 
345  return sig ;
346 }
347 
354 void
355 StandAloneApp::dump( ostream &strm ) const
356 {
357  strm << BESIndent::LMarg << "StandAloneApp::dump - ("
358  << (void *)this << ")" << endl ;
360  if( _client )
361  {
362  strm << BESIndent::LMarg << "client: " << endl ;
364  _client->dump( strm ) ;
366  }
367  else
368  {
369  strm << BESIndent::LMarg << "client: null" << endl ;
370  }
371  strm << BESIndent::LMarg << "command: " << _cmd << endl ;
372  strm << BESIndent::LMarg << "output stream: " << (void *)_outputStrm << endl ;
373  strm << BESIndent::LMarg << "input stream: " << (void *)_inputStrm << endl ;
374  strm << BESIndent::LMarg << "created input stream? " << _createdInputStrm << endl ;
375  BESBaseApp::dump( strm ) ;
377 }
378 
379 int
380 main( int argc, char **argv )
381 {
382  StandAloneApp app ;
383  return app.main( argc, argv ) ;
384 }
385 
void executeCommands(const string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
virtual int run()
the applications functionality is implemented in the run method
void setOutput(ostream *strm, bool created)
Set the output stream for responses from the BES server.
static void SetUp(const string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:68
virtual void dump(ostream &strm) const
dumps information about this object
virtual ~StandAloneApp()
static int initialize(int argc, char **argv)
virtual int main(int argC, char **argV)
main method of the BES application
Definition: BESBaseApp.cc:75
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESBaseApp.cc:169
static int terminate(void)
virtual int terminate(int sig=0)
clean up after the application
StandAloneClient is an object that handles the connection to, sending requests to, and receiving response from a specified OpenDAP server running either on this machine or another machine.
static int terminate(void)
Removes the default set of BES XML commands from the list of possible commands.
static void Indent()
Definition: BESIndent.cc:38
virtual string get_message()
get the error message for this exception
Definition: BESError.h:91
virtual void dump(ostream &strm) const
dumps information about this object
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
static int initialize(int argc, char **argv)
Loads the default set of BES XML commands.
Abstract exception class for the BES with basic string message.
Definition: BESError.h:51
void interact()
An interactive BES client that takes BES requests on the command line.
static void Help(ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:161
static int terminate(void)
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
Base application object for all BES applications.
Definition: BESModuleApp.h:59
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
Definition: BESModuleApp.cc:72
string appName(void) const
Returns the name of the application.
Definition: BESApp.h:132
virtual int terminate(int sig=0)
clean up after the application
static int initialize(int argc, char **argv)
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
static void UnIndent()
Definition: BESIndent.cc:44
static string ConfigFile
Definition: TheBESKeys.h:45