00001
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
00012
00013
00014
00015
00016
00017
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
00034 TGlobalUtils _global = TGlobalUtils(argv[0]);
00035
00036
00037
00038 ServerTemplate _server;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
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:
00114 _reading = false;
00115 break;
00116
00117 case COMFILE_ERROR:
00118 throw worm_exception("error returned by TComFileParser::ReadLine()");
00119
00120 case 0:
00121 break;
00122
00123 default:
00124
00125
00126 _token = _parser->NextToken();
00127
00128
00129
00130 if ( _global.HandleConfigLine(_parser) != HANDLER_UNUSED )
00131 {
00132 continue;
00133 }
00134
00135 if ( _server.HandleConfigLine(_parser) == HANDLER_UNUSED )
00136 {
00137
00138
00139
00140
00141
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