00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
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
00210 printError(ErrBadArg, "NETWORK_CONFIG_FILE is set to NULL");
00211 return 1;
00212 }
00213 if (0 == strcmp(cVal.tableConfigFile,""))
00214 {
00215
00216 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
00217 return 1;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
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
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
00362
00363
00364
00365
00366 }