Main Page Class Hierarchy Compound List File List Compound Members File Members

# main.cpp

Go to the documentation of this file.

    00001 // STEP 1: Include the header file for the appropriate server object
    00002 //
    00003 #include "server_template.h"
    00004
    00005 #include <logger.h>
    00006 #include <comfile.h>
    00007 #include <globalutils.h>
    00008
    00009 #include <stdio.h>
    00010
    00011 //#ifdef _Windows
    00012 //#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
    00013 //#endif
    00014
    00015
    00016
    00017 // a Borland-specific pragma to suppress warnings
    00018 #pragma argsused
    00019 int main( int argc, char* argv[] )
    00020 {
    00021    int r_status = MSB_RESULT_GOOD;
    00022
    00023    TComFileParser * _parser = NULL;
    00024
    00025    try
    00026    {
    00027 #ifdef _DEBUG
    00028       TLogger::TruncateOnOpen();
    00029 #endif
    00030
    00031       logit_init( argv[0], 0, 1024, 1 );
    00032
    00033       // This read the lookup files specified in the environment.
    00034       TGlobalUtils _global = TGlobalUtils(argv[0]);
    00035
    00036 // STEP 2: Change "ServerTemplate" to the appropriate server object type.
    00037 //
    00038       ServerTemplate _server;
    00039
    00040
    00041       // Check command line args
    00042       //
    00043       // Generally
    00044       //
    00045       //     Mode        command line formats
    00046       //     ----        ----------------------
    00047       //     standalone  p
    00048       //                 p c.d
    00049       //
    00050       //     client      p
    00051       //                 p c.d
    00052       //
    00053       //     server      <nothing>
    00054       //                 c.d
    00055       //
    00056       //     module      <nothing>
    00057       //                 c.d
    00058       //
    00059       //       Where:  p   = one or more process parameters
    00060       //               c.d = configuration file
    00061       //
    00062       // If the configuration is not given for server or
    00063       // module modes, will build the name
    00064       // from <server>.GetDefaultConfigFileName()
    00065       // If that is also NULL, then try argv[0].d
    00066
    00067       std::string _configfilename;
    00068
    00069       std::string _param;
    00070
    00071       for ( int _p = 1 ; _p < argc ; _p++ )
    00072       {
    00073          _param = argv[_p];
    00074
    00075          if ( 2 < _param.length() )
    00076          {
    00077             if ( _param.compare( _param.length()-2, 2, ".d" ) == 0 )
    00078             {
    00079                _configfilename = argv[_p];
    00080                _p = argc;
    00081             }
    00082          }
    00083       }
    00084
    00085
    00086       if ( _configfilename.length() == 0 )
    00087       {
    00088          _configfilename = argv[0];
    00089          _configfilename.append( ".d" );
    00090       }
    00091
    00092       if ( (_parser = new TComFileParser()) == NULL )
    00093       {
    00094          throw worm_exception("Failed creating TComFileParser to parse configuration file");
    00095       }
    00096
    00097       if ( ! _parser->Open(_configfilename.c_str()) )
    00098       {
    00099          char _msg[80];
    00100          sprintf( _msg, "Failed opening configuration file %s", _configfilename.c_str() );
    00101          throw worm_exception(_msg);
    00102       }
    00103
    00104
    00105       bool _reading = true;
    00106
    00107       char * _token;
    00108
    00109       do
    00110       {
    00111           switch( _parser->ReadLine() )
    00112           {
    00113             case COMFILE_EOF:  // eof
    00114                  _reading = false;
    00115                  break;
    00116
    00117             case COMFILE_ERROR: // error
    00118                  throw worm_exception("error returned by TComFileParser::ReadLine()");
    00119
    00120             case 0:  // empty line, TComFileParser should not return this
    00121                  break;
    00122
    00123             default:
    00124
    00125                  // Get the command into _token
    00126                  _token = _parser->NextToken();
    00127
    00128                  // check if module .d commands override globals (logging level, etc.)
    00129                  //
    00130                  if ( _global.HandleConfigLine(_parser) != HANDLER_UNUSED )
    00131                  {
    00132                     continue;
    00133                  }
    00134
    00135                  if ( _server.HandleConfigLine(_parser) == HANDLER_UNUSED )
    00136                  {
    00137                     // Not handled by this, the global config, or the server.
    00138                     //
    00139                     // Don't throw an error here because the server will be queried about
    00140                     // it's readiness shortly, and it is preferable to report all error
    00141                     // in the config file.
    00142                     //
    00143                     TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR
    00144                                   , "main(): unrecognized config file parameter: %s\n"
    00145                                   , _token
    00146                                   );
    00147                     continue;
    00148                  }
    00149           }
    00150       } while( _reading );
    00151
    00152       delete( _parser );
    00153       _parser = NULL;
    00154
    00155
    00156       if ( ! _global.IsReady() )
    00157       {
    00158          throw worm_exception("Global utilities not configured properly");
    00159       }
    00160
    00161       if ( ! _server.IsReady() )
    00162       {
    00163           throw worm_exception("Server component not configured properly");
    00164       }
    00165
    00166       switch ( _server.Run( argc, argv ) )
    00167       {
    00168         case WORM_STAT_SUCCESS:
    00169              break;
    00170         case WORM_STAT_BADSTATE:
    00171              r_status = MSB_RESULT_FAIL;
    00172              break;
    00173         case WORM_STAT_FAILURE:
    00174              throw worm_exception("ServerTemplate::Run() returned error");
    00175       }
    00176
    00177    }
    00178    catch( worm_exception _we )
    00179    {
    00180       TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TOSTDERR|WORM_LOG_TIMESTAMP
    00181                    , "main(): Exiting due to\n%s\n"
    00182                    , _we.what()
    00183                    );
    00184       r_status = MSB_RESULT_ERROR;
    00185    }
    00186
    00187    if ( _parser != NULL )
    00188    {
    00189       delete( _parser ) ;
    00190    }
    00191
    00192    TLogger::Close();
    00193
    00194    return r_status;
    00195 }
    00196 //---------------------------------------------------------------------------
    00197