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", "ServerApp: done initializing default module"
218  << endl ) ;
219 
220  BESDEBUG( "standalone", "ServerApp: initializing default commands ... "
221  << endl ) ;
222  BESXMLDefaultCommands::initialize( argc, argv ) ;
223  BESDEBUG( "standalone", "ServerApp: done initializing default commands"
224  << endl ) ;
225 
226  BESDEBUG( "standalone", "ServerApp: initializing loaded modules ... "
227  << endl ) ;
228  int retval = BESModuleApp::initialize( argc, argv ) ;
229  BESDEBUG( "standalone", "ServerApp: done initializing loaded modules"
230  << endl ) ;
231  if( retval )
232  return retval ;
233  }
234  catch( BESError &e )
235  {
236  cerr << "Failed to initialize stand alone app" << endl ;
237  cerr << e.get_message() << endl ;
238  return 1 ;
239  }
240 
241  BESDEBUG( "standalone", "StandAloneApp: initialized settings:"
242  << endl << *this ) ;
243 
244  return 0 ;
245 }
246 
247 int
249 {
250  try
251  {
252  _client = new StandAloneClient ;
253  if( _outputStrm )
254  {
255  _client->setOutput( _outputStrm, true ) ;
256  }
257  else
258  {
259  _client->setOutput( &cout, false ) ;
260  }
261  BESDEBUG( "standalone", "OK" << endl ) ;
262  }
263  catch( BESError &e )
264  {
265  if( _client )
266  {
267  delete _client ;
268  _client = 0 ;
269  }
270  BESDEBUG( "standalone", "FAILED" << endl ) ;
271  cerr << "error starting the client" << endl ;
272  cerr << e.get_message() << endl ;
273  exit( 1 ) ;
274  }
275 
276  try
277  {
278  if( _cmd != "" )
279  {
280  _client->executeCommands( _cmd, _repeat ) ;
281  }
282  else if( _inputStrm )
283  {
284  _client->executeCommands( *_inputStrm, _repeat ) ;
285  }
286  else
287  {
288  _client->interact() ;
289  }
290  }
291  catch( BESError &e )
292  {
293  cerr << "error processing commands" << endl ;
294  cerr << e.get_message() << endl ;
295  }
296 
297  try
298  {
299  BESDEBUG( "standalone", "StandAloneApp: shutting down client ... "
300  << endl ) ;
301  if( _client )
302  {
303  delete _client ;
304  _client = 0 ;
305  }
306  BESDEBUG( "standalone", "OK" << endl ) ;
307 
308  BESDEBUG( "standalone", "StandAloneApp: closing input stream ... "
309  << endl ) ;
310  if( _createdInputStrm )
311  {
312  _inputStrm->close() ;
313  delete _inputStrm ;
314  _inputStrm = 0 ;
315  }
316  BESDEBUG( "standalone", "OK" << endl ) ;
317  }
318  catch( BESError &e )
319  {
320  BESDEBUG( "standalone", "FAILED" << endl ) ;
321  cerr << "error closing the client" << endl ;
322  cerr << e.get_message() << endl ;
323  return 1 ;
324  }
325 
326  return 0 ;
327 }
328 
334 int
336 {
337  BESDEBUG( "standalone", "ServerApp: terminating loaded modules ... "
338  << endl ) ;
339  BESModuleApp::terminate( sig ) ;
340  BESDEBUG( "standalone", "ServerApp: done terminating loaded modules"
341  << endl ) ;
342 
343  BESDEBUG( "standalone", "ServerApp: terminating default commands ... "
344  << endl ) ;
346  BESDEBUG( "standalone", "ServerApp: done terminating default commands"
347  << endl ) ;
348 
349  BESDEBUG( "standalone", "ServerApp: terminating default module ... "
350  << endl ) ;
352  BESDEBUG( "standalone", "ServerApp: done terminating default module"
353  << endl ) ;
354 
356 
357  return sig ;
358 }
359 
366 void
367 StandAloneApp::dump( ostream &strm ) const
368 {
369  strm << BESIndent::LMarg << "StandAloneApp::dump - ("
370  << (void *)this << ")" << endl ;
372  if( _client )
373  {
374  strm << BESIndent::LMarg << "client: " << endl ;
376  _client->dump( strm ) ;
378  }
379  else
380  {
381  strm << BESIndent::LMarg << "client: null" << endl ;
382  }
383  strm << BESIndent::LMarg << "command: " << _cmd << endl ;
384  strm << BESIndent::LMarg << "output stream: " << (void *)_outputStrm << endl ;
385  strm << BESIndent::LMarg << "input stream: " << (void *)_inputStrm << endl ;
386  strm << BESIndent::LMarg << "created input stream? " << _createdInputStrm << endl ;
387  BESBaseApp::dump( strm ) ;
389 }
390 
391 int
392 main( int argc, char **argv )
393 {
394  StandAloneApp app ;
395  return app.main( argc, argv ) ;
396 }
397 
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 ~StandAloneApp()
static int initialize(int argc, char **argv)
virtual void dump(ostream &strm) const
dumps information about this object
virtual int main(int argC, char **argV)
main method of the BES application
Definition: BESBaseApp.cc:74
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
string appName(void) const
Returns the name of the application.
Definition: BESApp.h:132
virtual string get_message()
get the error message for this exception
Definition: BESError.h:91
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:163
static int terminate(void)
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESBaseApp.cc:140
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
virtual int terminate(int sig=0)
clean up after the application
static int initialize(int argc, char **argv)
virtual void dump(ostream &strm) const
dumps information about this object
int main(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:50