src/server/Config.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include<Config.h>
00017 #include<ErrorType.h>
00018 #include<Debug.h>
00019 
00020 Config Conf::config;
00021 
00022 int Config::readLine(FILE *fp, char * buffer)
00023 {
00024   char c =0;
00025   int count =0;
00026   while (true)
00027   {
00028       c = fgetc(fp);
00029       if (c == '\n') break;
00030       if (c == EOF) return EOF;
00031       buffer[count++] = c;
00032   }
00033   return count;
00034 }
00035 int Config::storeKeyVal(char *key, char *value)
00036 {
00037     if (strcasestr(key, "PAGE_SIZE") != NULL )
00038            { cVal.pageSize = atoi(value);  }
00039     else if (strcasestr(key, "MAX_PROCS") != NULL)
00040            { cVal.maxProcs = atoi(value);  }
00041     else if (strcasestr(key, "MAX_SYS_DB_SIZE") != NULL)
00042            { cVal.maxSysSize = atol(value);  }
00043     else if (strcasestr(key, "MAX_DB_SIZE") != NULL)
00044            { cVal.maxDbSize = atol(value);  }
00045     else if (strcasestr(key, "SYS_DB_KEY") != NULL)
00046            { cVal.sysDbKey = atoi(value);  }
00047     else if (strcasestr(key, "USER_DB_KEY") != NULL)
00048            { cVal.userDbKey = atoi(value);  }
00049     else if (strcasestr(key, "LOG_FILE") != NULL)
00050            { strcpy(cVal.logFile , value);  }
00051     else if (strcasestr(key, "DATABASE_FILE") != NULL)
00052            { strcpy(cVal.dbFile , value);  }
00053     else if (strcasestr(key, "MAP_ADDRESS") != NULL)
00054            { cVal.mapAddr = atol(value);  }
00055     else if (strcasestr(key, "MUTEX_TIMEOUT_SECS") != NULL)
00056            { cVal.mutexSecs = atoi(value);  }
00057     else if (strcasestr(key, "MUTEX_TIMEOUT_USECS") != NULL)
00058            { cVal.mutexUSecs = atoi(value);  }
00059     else if (strcasestr(key, "MUTEX_TIMEOUT_RETRIES") != NULL)
00060            { cVal.mutexRetries = atoi(value);  }
00061     else if (strcasestr(key, "LOCK_TIMEOUT_SECS") != NULL)
00062            { cVal.lockSecs = atoi(value);  }
00063     else if (strcasestr(key, "LOCK_TIMEOUT_USECS") != NULL)
00064            { cVal.lockUSecs = atoi(value);  }
00065     else if (strcasestr(key, "LOCK_TIMEOUT_RETRIES") != NULL)
00066            { cVal.lockRetries = atoi(value);  }
00067     else if (strcasestr(key, "DSN") != NULL)
00068            { strcpy(cVal.dsn , value);  }
00069     else if (strcasestr(key, "TABLE_CONFIG_FILE") != NULL)
00070            { strcpy(cVal.tableConfigFile , value);  }
00071     else if (strcasestr(key, "CACHE_TABLE") != NULL)
00072            { cVal.isCache = os::atobool(value); }
00073     else if (strcasestr(key, "REPLICATION") != NULL)
00074            { cVal.isReplication = os::atobool(value); }
00075     else if (strcasestr(key, "NETWORK_CONFIG_FILE") != NULL)
00076            { strcpy(cVal.replConfigFile , value);  }
00077     else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
00078            { cVal.logStoreSize = atol(value);  }
00079     else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
00080            { cVal.networkID = atoi(value);  }
00081     else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
00082            { cVal.cacheNetworkID = atoi(value);  }
00083     else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
00084            { cVal.nwResponseTimeout = atoi(value);  }
00085     else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
00086            { cVal.nwConnectTimeout = atoi(value);  }
00087     else if (strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
00088            { cVal.isTwoWay = os::atobool(value);  }
00089     else if (strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
00090            { cVal.cacheWaitSecs = atoi(value);  }
00091     else  return 1;
00092     return 0;
00093 }
00094 int Config::validateValues()
00095 {
00096     if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
00097     {
00098         printError(ErrBadArg,  "PAGE_SIZE should be >= 8192 and <= 10 MB");
00099         return 1;
00100     }
00101     if (cVal.pageSize  % 1024 !=0 )
00102     {
00103         printError(ErrBadArg,  "PAGE_SIZE should be multiples of 1024");
00104         return 1;
00105     }
00106     if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
00107     {
00108         printError(ErrBadArg,  "MAX_PROCS should be >= 10 and <= 8192");
00109         return 1;
00110     }
00111     if (cVal.maxSysSize < 1024 * 1024  || cVal.maxSysSize > 1024 *1024 *1024)
00112     {
00113         printError(ErrBadArg,  "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
00114         return 1;
00115     }
00116     if (cVal.maxSysSize % 8192 !=0 )
00117     {
00118         printError(ErrBadArg,  "MAX_SYS_DB_SIZE should be multiples of 8192");
00119         return 1;
00120     }
00121 
00122     if (cVal.maxDbSize < 1024 * 1024  || cVal.maxDbSize > (1024*1024*1024))
00123     {
00124         printError(ErrBadArg,  "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
00125         return 1;
00126     }
00127     if (cVal.maxDbSize % 8192 !=0)
00128     {
00129         printError(ErrBadArg,  "MAX_DB_SIZE should be multiples of 8192");
00130         return 1;
00131     }
00132 
00133     if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
00134     {
00135         printError(ErrBadArg,  "SYS_DB_KEY should be >= 10 and <= 8192");
00136         return 1;
00137     }
00138     if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
00139     {
00140         printError(ErrBadArg,  "USER_DB_KEY should be >= 10 and <= 8192");
00141         return 1;
00142     }
00143     if ( cVal.sysDbKey == cVal.userDbKey)
00144     {
00145         printError(ErrBadArg,  "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
00146         return 1;
00147     }
00148     if (0 == strcmp(cVal.logFile,""))
00149     {
00150         //TODO::check whether file exists
00151         printError(ErrBadArg,  "LOG_FILE is set to NULL");
00152         return 1;
00153     }
00154     if (0 == strcmp(cVal.dbFile,""))
00155     {
00156         printError(ErrBadArg,  "LOG_FILE is set to NULL");
00157         return 1;
00158     }
00159     if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
00160     {
00161         printError(ErrBadArg,  "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
00162         return 1;
00163     }   
00164     if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
00165     {
00166         printError(ErrBadArg,  "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
00167         return 1;
00168     }
00169     if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
00170     {
00171         printError(ErrBadArg,  "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
00172         return 1;
00173     }
00174     if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
00175     {
00176         printError(ErrBadArg,  "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
00177         return 1;
00178     }
00179     if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
00180     {
00181         printError(ErrBadArg,  "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
00182         return 1;
00183     }
00184     if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
00185     {
00186         printError(ErrBadArg,  "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
00187         return 1;
00188     }
00189     if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
00190     {
00191         printError(ErrBadArg,  "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
00192         return 1;
00193     }
00194     if (cVal.isCache && cVal.isReplication) {
00195         printError(ErrBadArg, "Either caching or replication option should be set."
00196                               " Both options are not supported together");
00197         return 1;
00198     }
00199     if (cVal.isCache) {
00200         if (0 == strcmp(cVal.dsn,""))
00201         {
00202             printError(ErrBadArg,  "DSN is set to NULL");
00203             return 1;
00204         }
00205     }
00206     if (cVal.isReplication || cVal.isCache) {
00207         if (0 == strcmp(cVal.replConfigFile,""))
00208         {
00209             //TODO::check whether file exists
00210             printError(ErrBadArg,  "NETWORK_CONFIG_FILE is set to NULL");
00211             return 1;
00212         }
00213         if (0 == strcmp(cVal.tableConfigFile,""))
00214         {
00215             //TODO::check whether file exists
00216             printError(ErrBadArg,  "TABLE_CONFIG_FILE is set to NULL");
00217             return 1;
00218         }
00219         /*FILE *fp = fopen(cVal.replConfigFile,"r");
00220         if( fp == NULL ) {
00221             printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
00222             return 1;
00223         }
00224         int count =0;
00225         int nwid, port;
00226         char hostname[IDENTIFIER_LENGTH];
00227         char nwmode;
00228  
00229         while(!feof(fp)) {
00230             fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
00231             count++;
00232         }
00233         if (count >2) {
00234             printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
00235             return 1;
00236         }*/
00237 
00238     }
00239     /*if (cVal.isCache)
00240     {
00241         
00242         if (cVal.cacheNetworkID == -1)
00243         {
00244             printError(ErrBadArg,  "CACHE_NETWORK_ID should not be -1");
00245             return 1;
00246         }else {
00247             FILE *fp;
00248             int nwid;
00249             char hostname[IDENTIFIER_LENGTH];
00250             char nwmode;
00251             int port;
00252             fp = fopen(Conf::config.getReplConfigFile(),"r");
00253             if( fp == NULL ) {
00254                 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
00255                 return 1;
00256             }
00257             bool found = false;
00258             while(!feof(fp)) {
00259                 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
00260                 if (cVal.cacheNetworkID == nwid) found = true;
00261             }
00262             if (!found) return 1;
00263         }
00264     }*/
00265     if (cVal.logStoreSize < 1024 * 1024  || cVal.logStoreSize > 1024 *1024 *1024)
00266     {
00267         printError(ErrBadArg,  "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
00268         return 1;
00269     }
00270     if (cVal.logStoreSize % 8192 !=0)
00271     {
00272         printError(ErrBadArg,  "MAX_LOG_STORE_SIZE should be multiples of 8192");
00273         return 1;
00274     }
00275     if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
00276     {
00277         printError(ErrBadArg,  "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
00278         return 1;
00279     }
00280     if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
00281     {
00282         printError(ErrBadArg,  "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
00283         return 1;
00284     }
00285     if (cVal.cacheWaitSecs <1)
00286     {
00287         printError(ErrBadArg,  "CACHE_RECEIVER_WAIT_SECS should be >1");
00288         return 1;
00289     }
00290     return 0;
00291 }
00292 
00293 int Config::readAllValues(char *fileName)
00294 {
00295     FILE *fp;
00296 
00297     fp = fopen(fileName,"r");
00298     if( fp == NULL ) {
00299         printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
00300         return 1;
00301     }
00302 
00303     int hasData = 1;
00304     char buffer[1024];
00305     char key[1024];
00306     char value[1024];
00307     while (hasData)
00308     {
00309         memset(buffer, 0, 1024);
00310         //int ret = fscanf(fp,"%s\r",buffer);
00311         int ret = readLine(fp, buffer);
00312         if (ret == EOF) break;
00313         bool isComment= false;
00314         int posEqual =0;
00315         for (int i = 0; i <1024; i++)
00316         {
00317               if (buffer[i] == '=' ) posEqual=i;
00318               else if (buffer[i] == '#' ) { isComment = true; break; }
00319               else if (buffer[i] == '\n') { break; }
00320               else if (buffer[i] == '\0') { break; }
00321         }
00322       if (isComment) continue;
00323       if (!posEqual) continue;
00324       strncpy(key, buffer, posEqual);
00325       key[posEqual] = '\0';
00326       posEqual++;
00327       strcpy(value, &buffer[posEqual]);
00328       storeKeyVal(key, value);
00329     }
00330     fclose(fp);
00331     if (validateValues())
00332     {
00333         return 1;
00334     }
00335 
00336     return 0;
00337 }
00338 void Config::print()
00339 {
00340     printf("ConfigValues\n");
00341     printf(" getPageSize %d\n", getPageSize());
00342     printf(" getMaxProcs %d\n", getMaxProcs());
00343     printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
00344     printf(" getMaxDbSize %ld\n", getMaxDbSize());
00345     printf(" getSysDbKey %d\n", getSysDbKey());
00346     printf(" getUserDbKey %d\n", getUserDbKey());
00347     printf(" getLogFile %s\n", getLogFile());
00348     printf(" getDatabaseFile %s\n", getDbFile());
00349     printf(" getMapAddress %ld\n", getMapAddress());
00350     printf(" getMutexSecs %d\n", getMutexSecs());
00351     printf(" getMutexUSecs %d\n", getMutexUSecs());
00352     printf(" getMutexRetries %d\n", getMutexRetries());
00353     printf(" getLockSecs %d\n", getLockSecs());
00354     printf(" getLockUSecs %d\n", getLockUSecs());
00355     printf(" getLockRetries %d\n", getLockRetries());
00356     printf(" useCache %d\n", useCache());
00357     printf(" getDSN %s\n", getDSN());
00358     printf(" getTableConfigFile %s\n", getTableConfigFile());
00359     printf(" isTwoWayCache %d\n", useTwoWayCache());
00360     printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
00361     //printf(" useReplication %d\n", useReplication());
00362     //printf(" getReplConfigFile %s\n", getReplConfigFile());
00363     //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
00364     //printf(" getNetworkID %d\n", getNetworkID());
00365     //printf(" getCacheNetworkID %d\n", getCacheNetworkID());
00366 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7