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

# globalutils.h

Go to the documentation of this file.

    00001 /*
    00002 ** TGlobalUtils is a class that handles program-global variables and such.
    00003 **
    00004 **
    00005 **   Specifically, it provides program-global access to:
    00006 **
    00007 **     1. System-wide defines from earthworm_global.d
    00008 **     2. Site-specific defines from earthworm.d
    00009 **     3. Program-wide globals from the <program>.d file
    00010 **     4. Program name and module id (from the Initialize method).
    00011 **
    00012 ** CONFIGURATION FILES
    00013 **
    00014 **   The two configuration files are parsed the first time any of the
    00015 **   content items are queried through the static methods.
    00016 **
    00017 **    earthworm_global.d -- world-wide [sacred] system variables
    00018 **                     Ring       - ring name to key id mapping{s)
    00019 **                     Module     - module name to id mapping(s)
    00020 **                     Message    - message type name to id mapping(s)
    00021 **    earthworm.d -- system variables
    00022 **                     LogDir     - Log directory
    00023 **                     LogLevel   - site-global logging level (1 - 9)
    00024 **                                  (over-ridden by <module>.d)
    00025 **
    00026 **    <module>.d -- module-specific parameters
    00027 **                     WriteLogFile - flag entry, if present write log for module
    00028 **                     LogLevel     - module-specific logging level (1 - 9)
    00029 **                                    (overrides magworm_site.d)
    00030 **
    00031 */
    00032 //---------------------------------------------------------------------------
    00033 #ifndef GlobalUtilsH
    00034 #define GlobalUtilsH
    00035 //---------------------------------------------------------------------------
    00036 #include <configurable.h>
    00037 //#include <worm_environ.h> -- in globalutils.cpp
    00038 //#include <logger.h> -- in globalutils.cpp
    00039 #include <process.h> // getpid
    00040
    00041 // microsoft pragma to avoid warnings from STL
    00042 #pragma warning(disable:4786)
    00043
    00044 #include <string>
    00045 #include <vector>
    00046 #include <worm_types.h>
    00047 #include <worm_defs.h>
    00048 #include <worm_statuscode.h>
    00049 #include <configurable.h>
    00050
    00051
    00052 //---------------------------------------------------------------------------
    00053 class TGlobalUtils : public TConfigurable
    00054 {
    00055 private:
    00056
    00057    /*
    00058    **  See worm_environ.h for environment variables pertaining to
    00059    **                     global and site configuration files.
    00060    */
    00061    static std::vector<std::string> ConfigFiles;
    00062    static int                      ConfigFileCount;
    00063
    00064    /*
    00065    ** GetUtil_LoadTable
    00066    **
    00067    ** Reads configuration files using TComFileParser, passes each line to ParseCommandLine().
    00068    ** Calls CheckConfig() at end.
    00069    ** Exits on some errors, many merely reported (to enable complete file parsing).
    00070    */
    00071    void LoadFiles();
    00072
    00073 protected:
    00074
    00075    // program identity
    00076    static PROGRAM_NAME ProgramName;
    00077    static WORM_MODULE_ID ThisModuleId;
    00078    static WORM_INSTALLATION_ID ThisInstallation;
    00079
    00080    static LOG_DIRECTORY HomeDirectory;
    00081    static char Version[12];
    00082
    00083    // program settings
    00084    static bool DoLogFile;
    00085    static WORM_LOGGING_LEVEL LogLevel;
    00086
    00087    static long HeartBeatInt;
    00088
    00089    // flag for all module threads to check for termination
    00090    static volatile bool TerminateFlag;
    00091
    00092    // lookup containers
    00093    static INSTALLATION_MAP InstallIds;
    00094    static RING_MAP RingIds;
    00095    static MODULE_MAP ModuleIds;
    00096    static MESSAGETYPE_MAP MessageTypeIds;
    00097
    00098    // ParseCommand() -- allows derivative classes to extend the lookup values.
    00099    //                   Called called from LoadFiles().
    00100    //
    00101    // From within any deriving class, or further derivation, ALWAYS make a call to
    00102    // <super_class>::ParseCommand() the first statement in the ParseCommand() method...
    00103    // this ensures that all classes in the heirarchy get their shot at the parameter,
    00104    // starting from the base-most class.
    00105    //
    00106    virtual bool ParseLookupLine( const char * p_filename, ConfigSource & p_parser );
    00107
    00108    // CheckConfig() -- allows derivative classes to report the status of their the lookup values.
    00109    //
    00110    // From within any deriving class, or further derivation, ALWAYS contain a call to
    00111    // <super_class>::CheckConfig() in their own CheckConfig() method...
    00112    // this ensures that all classes in the heirarchy get their chance to report status.
    00113    //
    00114    // All implementations should set ConfigStatus value to WORM_STAT_BADSTATE if there
    00115    // is a configuration problem, otherwise leave it alone.
    00116    //
    00117    virtual void CheckConfig();
    00118
    00119
    00120 public:
    00121
    00122    TGlobalUtils( char* p_programname );
    00123
    00124    /*
    00125    **  HandleConfigLine()
    00126    **
    00127    **  PARMS:
    00128    **          p_parser -- the parser being used, command string already
    00129    **                      in the current token for comparison with Its()
    00130    **
    00131    ** RETURN:
    00132    **          HANDLE_INVALID --  line invalid
    00133    **          HANDLE_UNUSED  --  line not used
    00134    **          HANDLE_USED    --  line used okay
    00135    **
    00136    **  Override for child classes to handle command lines
    00137    */
    00138    HANDLE_STATUS HandleConfigLine( ConfigSource * p_parser );
    00139
    00140
    00141    static void SetTerminateFlag() { TerminateFlag = true; }
    00142    static bool GetTerminateFlag() { return TerminateFlag; }
    00143
    00144    /*
    00145    ** GetEnvironmentValue() -- calls getenv(), can return NULL
    00146    */
    00147    static char* GetEnvironmentValue( const char* );
    00148
    00149    static char* GetHomeDirectory() { return HomeDirectory; }
    00150
    00151    static char* GetVersion() { return Version; }
    00152
    00153    static const char* GetProgramName() { return ProgramName; }
    00154
    00155    static unsigned int GetPID() { return getpid(); }
    00156
    00157    static WORM_MODULE_ID GetThisModuleId() { return ThisModuleId; }
    00158
    00159    static WORM_INSTALLATION_ID GetThisInstallationId() { return ThisInstallation; }
    00160
    00161    static void SetFileLoggingState( bool p_newstate )
    00162    {
    00163       DoLogFile = p_newstate;
    00164    }
    00165
    00166    static bool WriteLogFile() { return DoLogFile; }
    00167
    00168    static WORM_LOGGING_LEVEL GetLoggingLevel() { return LogLevel; }
    00169
    00170    static long GetHeartbeatInt() { return HeartBeatInt; }
    00171
    00172    /*
    00173    ** LookupInstallationId()
    00174    **
    00175    **  Convert installation key to id number using table defined in ConfigFile
    00176    **  RETURNS: 0 to n = installation id
    00177    **                0 = specified installation name is unknown
    00178    */
    00179    static const WORM_INSTALLATION_ID LookupInstallationId( const char* p_name );
    00180
    00181    /*
    00182    ** LookupModuleId()
    00183    **
    00184    **  Convert module name to modid number using table defined in ConfigFile
    00185    **  RETURNS: 0 to n = module id
    00186    **                0 = specified module name is unknown
    00187    */
    00188    static const WORM_MODULE_ID LookupModuleId( const char* p_name );
    00189
    00190    /*
    00191    ** LookupMessageTypeId()
    00192    **
    00193    ** Convert message-type name to number using table defined in ConfigFile
    00194    **  RETURNS: 0 to n = message type id
    00195    **                0 = specified message type name is unknown
    00196    */
    00197    static const WORM_MSGTYPE_ID LookupMessageTypeId( const char* p_name );
    00198
    00199    /*
    00200    ** LookupRingKey()
    00201    **
    00202    **  Convert ring name to key number using table defined in ConfigFile
    00203    **  RETURNS: 0 to n = key number
    00204    **                0 = specified ring name is unknown
    00205    */
    00206    static const WORM_RING_ID LookupRingKey( const char* p_name );
    00207
    00208 };
    00209
    00210 #endif
    00211